120 lines
11 KiB
JavaScript
120 lines
11 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import http from 'node:http';
|
|
import assert from 'node:assert/strict';
|
|
import { spawn } from 'node:child_process';
|
|
import { randomUUID, createHash } from 'node:crypto';
|
|
import { chromium, expect } from 'playwright/test';
|
|
import { fixture, until, compatibleFixture, completion, workspace } from '../tests/helpers.mjs';
|
|
import { gitEnvironment } from '../src/core/git-process.mjs';
|
|
import { upgradeRunning } from '../scripts/upgrade-running.mjs';
|
|
|
|
const directory = path.resolve('.qa/milestone-security-ui-' + randomUUID()); fs.mkdirSync(directory);
|
|
const original = JSON.parse(fs.readFileSync('docs/model-center-release-identity.json', 'utf8'));
|
|
const oldRoot = original.release.portableRoot;
|
|
const disposers = [], t = { after: action => disposers.push(action) }, f = fixture(t); f.store.close();
|
|
const assertions = [], pageErrors = [], expectedPolicyMessages = [], unexpectedRequests = [];
|
|
const record = value => { assertions.push(value); console.log('PASS ' + value); };
|
|
const fakeModel = await compatibleFixture(t, () => completion([], 'owned model probe response'));
|
|
const child = spawn(process.execPath, [path.join(oldRoot, 'dist-server/server.mjs')], { cwd: oldRoot, env: gitEnvironment({
|
|
AGENT_WORKBENCH_APP_DIR: oldRoot, AGENT_WORKBENCH_DATA_DIR: f.data, AGENT_WORKBENCH_PORT: '0' }), windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
let url = '', stdout = '', stderr = '', browser, external;
|
|
child.stdout.on('data', bytes => { stdout += bytes.toString(); for (const line of stdout.split('\n')) { try { const item = JSON.parse(line); if (item.ready) url = item.url; } catch {} } });
|
|
child.stderr.on('data', bytes => { stderr += bytes.toString(); });
|
|
let bootstrap;
|
|
async function api(route, input) {
|
|
const response = await fetch(url + '/api' + route, { method: input === undefined ? 'GET' : 'POST', headers: {
|
|
'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token, Connection: 'close' }, ...(input === undefined ? {} : { body: JSON.stringify(input) }) });
|
|
const value = await response.json(); if (!response.ok) throw Object.assign(new Error(value.error), { status: response.status }); return value;
|
|
}
|
|
try {
|
|
await until(() => url || child.exitCode !== null); assert.ok(url, stderr); assert.notEqual(new URL(url).port, '4318');
|
|
bootstrap = await (await fetch(url + '/api/bootstrap')).json(); assert.equal(bootstrap.version, '1.12.0'); assert.equal(bootstrap.config.hasKey, false);
|
|
const oldHeaders = Object.fromEntries((await fetch(url)).headers); assert.equal(oldHeaders['x-frame-options'], undefined);
|
|
external = http.createServer((request, response) => {
|
|
if (request.url === '/forbidden.js') { unexpectedRequests.push('external script fetched'); response.writeHead(200, { 'Content-Type': 'text/javascript' }); response.end('window.ownedExternalScriptExecuted=true;'); }
|
|
else { response.writeHead(200, { 'Content-Type': 'text/html' }); response.end('<!doctype html><title>Owned embedding fixture</title><h1>External fixture</h1><iframe id="workbench" src="' + url + '" style="width:1100px;height:760px"></iframe>'); }
|
|
});
|
|
await new Promise(resolve => external.listen(0, '127.0.0.1', resolve)); const externalUrl = 'http://127.0.0.1:' + external.address().port;
|
|
browser = await chromium.launch({ channel: 'msedge', headless: true });
|
|
const page = await browser.newPage({ viewport: { width: 1280, height: 1000 } });
|
|
page.on('pageerror', error => pageErrors.push(error.message));
|
|
page.on('console', message => { if (message.type() === 'error' && /frame|Content Security Policy|script-src/i.test(message.text())) expectedPolicyMessages.push(message.text()); });
|
|
await page.goto(externalUrl); await page.waitForLoadState('networkidle');
|
|
await expect(page.frameLocator('#workbench').getByRole('button', { name: '打开模型中心', exact: true })).toBeVisible();
|
|
fs.writeFileSync(path.join(directory, 'old-frame.html'), await page.content()); await page.screenshot({ path: path.join(directory, 'old-embedded.png') });
|
|
record('real original 1.12.0 renders the workbench inside an independently hosted cross-origin iframe');
|
|
|
|
await api('/config', { mode: 'compatible', model: 'owned-legacy', baseUrl: fakeModel.baseUrl, apiKey: 'owned-legacy-upgrade-marker' });
|
|
const connection = await api('/model-center/connections', { name: 'Retained owned connection', baseUrl: fakeModel.baseUrl, model: 'owned-reserve', apiKey: 'owned-connection-upgrade-marker' });
|
|
const importFile = path.join(directory, 'owned-import.json');
|
|
fs.writeFileSync(importFile, JSON.stringify({ schema: 1, connections: [
|
|
{ name: 'First valid imported entry', baseUrl: fakeModel.baseUrl, model: 'owned-first', apiKey: 'owned-import-marker' },
|
|
{ name: 'Second invalid imported entry', baseUrl: fakeModel.baseUrl, model: 'owned-invalid', apiKey: 'x'.repeat(16001) },
|
|
] }));
|
|
const preview = await api('/model-center/import-preview', { source: 'workbench-json', path: importFile });
|
|
const beforeCenter = await api('/model-center'), beforeSession = await api('/sessions/' + f.session.id), beforeConfig = (await api('/bootstrap')).config;
|
|
const upgraded = await upgradeRunning({ url, pid: child.pid, currentRoot: oldRoot, newRoot: workspace });
|
|
assert.equal(upgraded.verified, true); assert.equal(upgraded.version, '1.12.1'); assert.equal(upgraded.pid, child.pid); assert.equal(upgraded.hasKey, true);
|
|
bootstrap = await api('/bootstrap'); assert.deepEqual(bootstrap.config, beforeConfig);
|
|
assert.deepEqual(await api('/model-center'), beforeCenter); assert.deepEqual(await api('/sessions/' + f.session.id), beforeSession);
|
|
record('an actual original 1.12.0 process upgrades in place with initialized ModelHub/Importer, unchanged PID, connection metadata and original session');
|
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
await assert.rejects(api('/model-center/import-apply', { previewId: preview.id }), error => error.status === 400 && !/预览已过期/.test(error.message));
|
|
assert.deepEqual(await api('/model-center'), beforeCenter);
|
|
}
|
|
record('retained importer now rolls back an actual partially invalid batch and keeps the original preview usable after failure');
|
|
assert.equal((await api('/model-center/probe', { id: connection.id })).ok, true);
|
|
assert.equal(fakeModel.requests.at(-1).headers.authorization, 'Bearer owned-connection-upgrade-marker');
|
|
await api('/config/models', {}); assert.equal(fakeModel.requests.at(-1).headers.authorization, 'Bearer owned-legacy-upgrade-marker');
|
|
assert.ok(!fs.readFileSync(path.join(f.data, 'config.json'), 'utf8').includes('owned-legacy-upgrade-marker'));
|
|
record('both volatile fixture credentials still route correctly after the real process upgrade and are absent from persisted configuration');
|
|
|
|
const newHeaders = Object.fromEntries((await fetch(url)).headers); assert.equal(newHeaders['x-frame-options'], 'DENY');
|
|
assert.match(newHeaders['content-security-policy'], /frame-ancestors 'none'/); assert.equal(newHeaders['referrer-policy'], 'no-referrer');
|
|
await page.goto(externalUrl); await page.waitForLoadState('networkidle');
|
|
assert.equal(await page.frameLocator('#workbench').locator('#root').count(), 0);
|
|
assert.ok(expectedPolicyMessages.some(value => /frame-ancestors|frame/i.test(value)));
|
|
await page.screenshot({ path: path.join(directory, 'new-blocked-iframe.png') });
|
|
record('real Edge refuses the cross-origin iframe after the patch instead of exposing the approval interface');
|
|
|
|
await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id });
|
|
for (const width of [1280, 390]) {
|
|
await page.setViewportSize({ width, height: 1000 }); await page.goto(url); await page.waitForLoadState('networkidle');
|
|
fs.writeFileSync(path.join(directory, 'buttons-' + width + '.json'), JSON.stringify(await page.locator('button').evaluateAll(nodes => nodes.map(node => ({ text: node.textContent, label: node.getAttribute('aria-label'), title: node.title }))), null, 2));
|
|
await page.getByRole('button', { name: '打开模型中心', exact: true }).click();
|
|
const dialog = page.getByRole('dialog', { name: '模型中心', exact: true }); await expect(dialog).toBeVisible();
|
|
await expect(dialog).toContainText('Retained owned connection');
|
|
const bounds = await dialog.boundingBox(); assert.ok(bounds.x >= 0 && bounds.x + bounds.width <= width + 1);
|
|
await page.screenshot({ path: path.join(directory, 'model-center-' + width + '.png') }); await page.keyboard.press('Escape');
|
|
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true);
|
|
record(width + 'px workbench and model center remain usable under the new response policy');
|
|
}
|
|
const blobImage = await page.evaluate(async () => {
|
|
const bytes = Uint8Array.from(atob('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/lWQAAAAASUVORK5CYII='), value => value.charCodeAt(0));
|
|
const url = URL.createObjectURL(new Blob([bytes], { type: 'image/png' })), image = document.createElement('img');
|
|
try { const loaded = await new Promise(resolve => { image.onload = () => resolve(image.naturalWidth); image.onerror = () => resolve(0); image.src = url; document.body.append(image); }); return loaded; }
|
|
finally { image.remove(); URL.revokeObjectURL(url); }
|
|
});
|
|
assert.equal(blobImage, 1);
|
|
await assert.rejects(page.addScriptTag({ url: externalUrl + '/forbidden.js' }));
|
|
assert.equal(await page.evaluate(() => window.ownedExternalScriptExecuted === true), false); assert.deepEqual(unexpectedRequests, []);
|
|
record('policy permits PNG Blob image loading and blocks fetching/executing an independently hosted script');
|
|
assert.deepEqual(pageErrors, []); await browser.close(); browser = null;
|
|
await api('/shutdown', {}); await until(() => child.exitCode !== null); assert.equal(child.exitCode, 0);
|
|
await assert.rejects(fetch('http://127.0.0.1:9229/json/list', { signal: AbortSignal.timeout(500) }));
|
|
const hash = filename => createHash('sha256').update(fs.readFileSync(filename)).digest('hex');
|
|
const files = fs.readdirSync(directory).map(name => ({ path: path.join(directory, name), sha256: hash(path.join(directory, name)) }));
|
|
const result = { success: true, version: '1.12.1', at: new Date().toISOString(), directory, oldReleaseId: original.releaseId,
|
|
oldHeaders, newHeaders, assertions, pageErrors, expectedPolicyMessages, unexpectedRequests, pid: child.pid, processExited: true,
|
|
syntheticModelRequests: fakeModel.requests.length, realModelRequests: 0, files, scope: 'Owned projects, credentials, independent ports and original immutable 1.12.0 process; no production service affected.' };
|
|
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2) + '\n');
|
|
fs.writeFileSync('.qa/milestone-security-ui-result.json', JSON.stringify(result, null, 2) + '\n'); console.log('EVIDENCE ' + directory);
|
|
} catch (error) {
|
|
fs.writeFileSync(path.join(directory, 'failure.json'), JSON.stringify({ success: false, directory, error: error.stack, assertions, pageErrors, expectedPolicyMessages, stderr }, null, 2)); throw error;
|
|
} finally {
|
|
await browser?.close(); if (external) await new Promise(resolve => external.close(resolve));
|
|
if (child.exitCode === null && child.signalCode === null) { try { await api('/shutdown', {}); await until(() => child.exitCode !== null, 5000); } catch { child.kill(); } }
|
|
for (const dispose of disposers.reverse()) await dispose();
|
|
}
|