mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
Move Desktop startup to a module and read fonts and workers from its manifest. Allow the HTML preview wheel bridge by hash on both hosts. Check the hash against the source and verify that Chromium returns wheel input to the canvas.
377 lines
12 KiB
JavaScript
377 lines
12 KiB
JavaScript
#!/usr/bin/env node
|
|
// @ts-check
|
|
|
|
/**
|
|
* Zero-dependency static gate for `packages/frontend/hosts/web/`.
|
|
* Verifies zero third-party runtime JS imports, zero package manifests under
|
|
* `packages/frontend/hosts/`, strict `index.html` CSP and DOM allowlist, and
|
|
* strict iframe sandbox rules.
|
|
*/
|
|
|
|
import { createHash } from 'node:crypto';
|
|
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
import { join, relative, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const DEFAULT_WEB_ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)));
|
|
const DEFAULT_HOSTS_ROOT = resolve(DEFAULT_WEB_ROOT, '..');
|
|
const PREVIEW_BRIDGE = new URL(
|
|
'../../../../../blocksuite/crates/playground/web/preview-workers.js',
|
|
import.meta.url
|
|
);
|
|
const TURNSTILE_ALLOWLIST_URL =
|
|
'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit';
|
|
|
|
/**
|
|
* Recursively walks a directory tree and returns all file paths and matching
|
|
* forbidden directory paths (`node_modules`).
|
|
*
|
|
* @param {string} rootDir
|
|
* @returns {{ files: string[], nodeModulesDirs: string[] }}
|
|
*/
|
|
function walkTree(rootDir) {
|
|
/** @type {string[]} */
|
|
const files = [];
|
|
/** @type {string[]} */
|
|
const nodeModulesDirs = [];
|
|
if (!existsSync(rootDir)) {
|
|
return { files, nodeModulesDirs };
|
|
}
|
|
/** @param {string} dir */
|
|
function visit(dir) {
|
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
const full = join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
if (entry.name === 'node_modules') {
|
|
nodeModulesDirs.push(full);
|
|
continue;
|
|
}
|
|
visit(full);
|
|
} else if (entry.isFile()) {
|
|
files.push(full);
|
|
}
|
|
}
|
|
}
|
|
visit(rootDir);
|
|
return { files, nodeModulesDirs };
|
|
}
|
|
|
|
/**
|
|
* Strips block and line comments from JS source for static URL/import scanning.
|
|
* Preserves URLs inside string literals by only removing full-line comments
|
|
* and block comments.
|
|
*
|
|
* @param {string} source
|
|
* @returns {string}
|
|
*/
|
|
function stripComments(source) {
|
|
return source.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, '');
|
|
}
|
|
|
|
/**
|
|
* Verifies zero third-party runtime JS and static allowlist invariants across
|
|
* `packages/frontend/hosts/web/` and an optional staged output directory.
|
|
*
|
|
* @param {Object} [options]
|
|
* @param {string} [options.webRoot]
|
|
* @param {string} [options.hostsRoot]
|
|
* @param {string | null} [options.stageDir]
|
|
* @returns {{ ok: boolean, violations: string[], checkedFiles: number }}
|
|
*/
|
|
export function verifyZeroThirdPartyJs(options = {}) {
|
|
const webRoot = resolve(options.webRoot || DEFAULT_WEB_ROOT);
|
|
const hostsRoot = resolve(options.hostsRoot || DEFAULT_HOSTS_ROOT);
|
|
const stageDir = options.stageDir ? resolve(options.stageDir) : null;
|
|
|
|
/** @type {string[]} */
|
|
const violations = [];
|
|
let checkedFiles = 0;
|
|
|
|
// 1. Zero package.json, yarn.lock, or node_modules under hosts/ and stageDir.
|
|
const scanRoots = [hostsRoot];
|
|
if (stageDir && existsSync(stageDir)) {
|
|
scanRoots.push(stageDir);
|
|
}
|
|
for (const root of scanRoots) {
|
|
const { files, nodeModulesDirs } = walkTree(root);
|
|
for (const dir of nodeModulesDirs) {
|
|
violations.push(`Forbidden node_modules directory: ${dir}`);
|
|
}
|
|
for (const file of files) {
|
|
if (file.endsWith('/package.json') || file.endsWith('/yarn.lock')) {
|
|
violations.push(`Forbidden package manifest: ${file}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 2. Scan source modules and staged JS for imports and external scripts.
|
|
const srcDir = join(webRoot, 'src');
|
|
const jsFiles = walkTree(srcDir).files.filter(f => f.endsWith('.js'));
|
|
const desktopBootPath = join(webRoot, 'desktop/boot.js');
|
|
if (existsSync(desktopBootPath)) jsFiles.push(desktopBootPath);
|
|
if (stageDir && existsSync(stageDir)) {
|
|
for (const file of walkTree(stageDir).files) {
|
|
if (file.endsWith('.js') || file.endsWith('.mjs')) {
|
|
jsFiles.push(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
const staticImportRe =
|
|
/(?:^|[;\n])\s*(?:import|export)\s+(?:[^'";]*?\s+from\s+)?['"]([^'"]+)['"]/g;
|
|
const dynamicImportRe = /\bimport\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
const createScriptRe = /createElement\(\s*['"]script['"]\s*\)/g;
|
|
const externalUrlRe = /https?:\/\/[^\s'"`<>)]+/g;
|
|
|
|
for (const file of jsFiles) {
|
|
checkedFiles += 1;
|
|
const raw = readFileSync(file, 'utf8');
|
|
const code = stripComments(raw);
|
|
const rel = relative(webRoot, file);
|
|
const stageRel = stageDir ? relative(stageDir, file) : '';
|
|
const isSignIn = rel === 'src/sign-in.js' || stageRel === 'src/sign-in.js';
|
|
|
|
for (const match of code.matchAll(staticImportRe)) {
|
|
const specifier = match[1];
|
|
if (!specifier.startsWith('./') && !specifier.startsWith('../')) {
|
|
violations.push(
|
|
`${rel}: bare or external static import "${specifier}" is forbidden`
|
|
);
|
|
}
|
|
}
|
|
|
|
for (const match of code.matchAll(dynamicImportRe)) {
|
|
const specifier = match[1];
|
|
const isDesktopWasm =
|
|
((rel === 'desktop/boot.js' || stageRel === 'desktop/boot.js') &&
|
|
specifier === '/pkg/affine_desktop.js') ||
|
|
(/^desktop\/boot\.[a-f0-9]{12}\.js$/.test(stageRel) &&
|
|
/^\/pkg\/desktop\/affine_desktop\.[a-f0-9]{12}\.js$/.test(specifier));
|
|
if (
|
|
!isDesktopWasm &&
|
|
!specifier.startsWith('./') &&
|
|
!specifier.startsWith('../')
|
|
) {
|
|
violations.push(
|
|
`${rel}: bare or external dynamic import "${specifier}" is forbidden`
|
|
);
|
|
}
|
|
}
|
|
|
|
const createsScript = createScriptRe.test(code);
|
|
createScriptRe.lastIndex = 0;
|
|
if (createsScript && !isSignIn) {
|
|
violations.push(
|
|
`${rel}: dynamic <script> creation is only allowed in src/sign-in.js`
|
|
);
|
|
}
|
|
|
|
if (isSignIn) {
|
|
const urls = [...code.matchAll(externalUrlRe)].map(m => m[0]);
|
|
for (const url of urls) {
|
|
if (
|
|
url !== TURNSTILE_ALLOWLIST_URL &&
|
|
!url.startsWith('https://app.affine.pro')
|
|
) {
|
|
violations.push(
|
|
`${rel}: unexpected external URL "${url}" outside Turnstile allowlist`
|
|
);
|
|
}
|
|
}
|
|
if (
|
|
!code.includes("'/api/auth/captcha'") &&
|
|
!code.includes('"/api/auth/captcha"')
|
|
) {
|
|
violations.push(
|
|
`${rel}: Turnstile must be gated behind /api/auth/captcha`
|
|
);
|
|
}
|
|
} else if (/\bsrc\s*=\s*['"]https?:\/\//.test(code)) {
|
|
violations.push(
|
|
`${rel}: external http(s) script/resource assignment is forbidden`
|
|
);
|
|
}
|
|
}
|
|
|
|
// 3. Scan HTML files for external <script src="http..."> tags.
|
|
const htmlRoots = [webRoot];
|
|
if (stageDir && existsSync(stageDir)) {
|
|
htmlRoots.push(stageDir);
|
|
}
|
|
const externalScriptTagRe =
|
|
/<script\b[^>]*\bsrc\s*=\s*['"]https?:\/\/[^'"]+['"][^>]*>/gi;
|
|
for (const root of htmlRoots.flatMap(root => [root, join(root, 'desktop')])) {
|
|
if (!existsSync(root)) continue;
|
|
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
if (!entry.isFile() || !entry.name.endsWith('.html')) {
|
|
continue;
|
|
}
|
|
const full = join(root, entry.name);
|
|
checkedFiles += 1;
|
|
const html = readFileSync(full, 'utf8');
|
|
for (const match of html.matchAll(externalScriptTagRe)) {
|
|
violations.push(
|
|
`${entry.name}: static external script tag is forbidden (${match[0]})`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. Verify script policies and the mobile visual DOM allowlist.
|
|
const allowedScriptTokens = new Set(["'self'", "'wasm-unsafe-eval'"]);
|
|
// The blob preview inherits the host CSP. Hash the literal script body.
|
|
const bridge = readFileSync(PREVIEW_BRIDGE, 'utf8').match(
|
|
/^\s*const EMBED_WHEEL_BRIDGE =\s*"<script>([^"\\\r\n]+)<\/script>";$/m
|
|
);
|
|
let wheelHash;
|
|
if (bridge) {
|
|
wheelHash = `'sha256-${createHash('sha256').update(bridge[1], 'utf8').digest('base64')}'`;
|
|
allowedScriptTokens.add(wheelHash);
|
|
} else {
|
|
violations.push(
|
|
'preview-workers.js: missing the literal EMBED_WHEEL_BRIDGE script'
|
|
);
|
|
}
|
|
for (const page of ['index.html', 'desktop/index.html']) {
|
|
const file = join(webRoot, page);
|
|
if (!existsSync(file)) {
|
|
if (page === 'index.html' || webRoot === DEFAULT_WEB_ROOT) {
|
|
violations.push(`Missing ${page}`);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
const html = readFileSync(file, 'utf8').replace(/<!--[\s\S]*?-->/g, '');
|
|
const cspMatch = html.match(
|
|
/http-equiv="Content-Security-Policy"\s+content="([^"]+)"/i
|
|
);
|
|
if (!cspMatch) {
|
|
violations.push(`${page}: missing Content-Security-Policy meta tag`);
|
|
} else {
|
|
const scriptSrcMatch = cspMatch[1].match(
|
|
/(?:^|;)\s*script-src\s+([^;]+)/i
|
|
);
|
|
if (!scriptSrcMatch) {
|
|
violations.push(`${page}: CSP missing script-src directive`);
|
|
} else {
|
|
const tokens = scriptSrcMatch[1].trim().split(/\s+/);
|
|
if (wheelHash && !tokens.includes(wheelHash)) {
|
|
violations.push(
|
|
`${page}: CSP script-src must include ${wheelHash} for EMBED_WHEEL_BRIDGE`
|
|
);
|
|
}
|
|
for (const token of tokens) {
|
|
if (!allowedScriptTokens.has(token)) {
|
|
violations.push(
|
|
`${page}: forbidden CSP script-src token "${token}"`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
if (/(?:^|;)\s*script-src-(?:elem|attr)(?:\s|;|$)/i.test(cspMatch[1])) {
|
|
violations.push(`${page}: CSP must use script-src for all scripts`);
|
|
}
|
|
}
|
|
|
|
for (const [, attrs, content] of html.matchAll(
|
|
/<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi
|
|
)) {
|
|
const isJson =
|
|
/\stype\s*=\s*(?:"application\/json"|'application\/json'|application\/json)(?=\s|$)/i.test(
|
|
attrs
|
|
);
|
|
const hasSource = /\ssrc\s*=\s*(?:"[^"]+"|'[^']+'|[^\s"'=<>`]+)/i.test(
|
|
attrs
|
|
);
|
|
if (!isJson && (!hasSource || content.trim())) {
|
|
violations.push(`${page}: inline executable script is forbidden`);
|
|
}
|
|
}
|
|
if (page === 'desktop/index.html') continue;
|
|
|
|
const bodyMatch = html.match(/<body>([\s\S]*?)<\/body>/i);
|
|
if (!bodyMatch) {
|
|
violations.push('index.html: missing <body>');
|
|
} else {
|
|
const body = bodyMatch[1];
|
|
const tagIds = [
|
|
...body.matchAll(/<([a-z0-9-]+)\b[^>]*\bid="([^"]+)"/gi),
|
|
].map(m => `${m[1].toLowerCase()}#${m[2]}`);
|
|
const allowedBodyIds = new Set([
|
|
'canvas#app-canvas',
|
|
'textarea#ime',
|
|
'div#safe-area-probe',
|
|
'div#embed-layer',
|
|
'div#a11y-tree',
|
|
'div#boot',
|
|
'p#boot-message',
|
|
'script#affine-build-manifest',
|
|
]);
|
|
for (const tagId of tagIds) {
|
|
if (!allowedBodyIds.has(tagId)) {
|
|
violations.push(
|
|
`index.html: unexpected DOM element "${tagId}" outside allowlist`
|
|
);
|
|
}
|
|
}
|
|
if (!tagIds.includes('canvas#app-canvas')) {
|
|
violations.push('index.html: missing <canvas id="app-canvas">');
|
|
}
|
|
if (!tagIds.includes('textarea#ime')) {
|
|
violations.push('index.html: missing <textarea id="ime">');
|
|
}
|
|
if (!body.includes('<div id="embed-layer"></div>')) {
|
|
violations.push('index.html: #embed-layer must be empty at rest');
|
|
}
|
|
const probeMatches = body.match(/id="safe-area-probe"/g) || [];
|
|
if (probeMatches.length > 1) {
|
|
violations.push(
|
|
'index.html: must contain at most one #safe-area-probe element'
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 5. Verify host.js iframe sandbox allowlist strips allow-same-origin.
|
|
const hostJsPath = join(webRoot, 'src/host.js');
|
|
if (existsSync(hostJsPath)) {
|
|
const hostCode = stripComments(readFileSync(hostJsPath, 'utf8'));
|
|
const sandboxSetMatch = hostCode.match(
|
|
/ALLOWED_SANDBOX_TOKENS\s*=\s*new Set\(\[([\s\S]*?)\]\)/
|
|
);
|
|
if (!sandboxSetMatch) {
|
|
violations.push('src/host.js: missing ALLOWED_SANDBOX_TOKENS allowlist');
|
|
} else if (sandboxSetMatch[1].includes('allow-same-origin')) {
|
|
violations.push(
|
|
'src/host.js: ALLOWED_SANDBOX_TOKENS must never include allow-same-origin'
|
|
);
|
|
}
|
|
}
|
|
|
|
return {
|
|
ok: violations.length === 0,
|
|
violations,
|
|
checkedFiles,
|
|
};
|
|
}
|
|
|
|
if (
|
|
process.argv[1] &&
|
|
resolve(process.argv[1]) === fileURLToPath(import.meta.url)
|
|
) {
|
|
const stageDir = process.argv[2] ? resolve(process.argv[2]) : null;
|
|
const report = verifyZeroThirdPartyJs({ stageDir });
|
|
if (!report.ok) {
|
|
console.error(
|
|
`Zero-third-party-JS gate failed (${report.violations.length} violations):`
|
|
);
|
|
for (const v of report.violations) {
|
|
console.error(` - ${v}`);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
console.log(
|
|
`Zero-third-party-JS gate passed (${report.checkedFiles} files verified, 0 violations)`
|
|
);
|
|
}
|