Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

feat(ok): send a Content-Security-Policy on the web app shell (#6538)

Dimitri POSTOLOVecfb5be61589
diff --git a/.changeset/web-shell-csp.md b/.changeset/web-shell-csp.md
new file mode 100644
index 000000000..1da0ee2d4
--- /dev/null
+++ b/.changeset/web-shell-csp.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+The browser app that `ok start` serves now sends a Content-Security-Policy, which stops scripts injected into the page from running.
diff --git a/packages/app/tests/stress/app-shell-csp.e2e.ts b/packages/app/tests/stress/app-shell-csp.e2e.ts
new file mode 100644
index 000000000..71208e773
--- /dev/null
+++ b/packages/app/tests/stress/app-shell-csp.e2e.ts
@@ -0,0 +1,113 @@
+import { randomUUID } from 'node:crypto';
+import type { Page } from '@playwright/test';
+import { type ApiHelpers, expect, test } from './_helpers';
+
+test.use({ workerServerEnv: { OK_TEST_PRODUCTION_SHELL: '1' } });
+
+type PolicyViolationLog = Window & { shellPolicyViolations?: string[] };
+
+async function openSeededDoc(page: Page, api: ApiHelpers, heading: string, body = '') {
+ const docName = `shell-csp-${randomUUID().slice(0, 8)}`;
+ await api.createPage(`${docName}.md`);
+ await api.replaceDoc(docName, `# ${heading}\n\n${body}`);
+ await page.goto(`/#/${docName}`);
+ await expect(page.getByRole('heading', { name: heading })).toBeVisible();
+}
+
+test('every shell document sirv serves carries the policy of that document', async ({ page }) => {
+ const policyOf = async (path: string) => {
+ const response = await page.request.get(path);
+ expect(response.status(), path).toBe(200);
+ return response.headers()['content-security-policy'] ?? '';
+ };
+
+ const shellPolicy = await policyOf('/');
+ expect(shellPolicy).toContain("script-src 'self'");
+ expect(shellPolicy).toContain("object-src 'none'");
+ expect(shellPolicy).not.toContain("'unsafe-inline'");
+ for (const fallbackPath of ['/@x.y', '/notes.v2/', '/[email protected]']) {
+ expect(await policyOf(fallbackPath), fallbackPath).toBe(shellPolicy);
+ }
+
+ const uninstallPolicy = await policyOf('/uninstall.html');
+ expect(uninstallPolicy).toContain("script-src 'self'");
+ expect(uninstallPolicy).not.toBe(shellPolicy);
+ expect(await policyOf('/uninstall')).toBe(uninstallPolicy);
+
+ const framePolicy = "sandbox allow-scripts; frame-ancestors 'self'";
+ expect(await policyOf('/assets/preview-frame.html')).toBe(framePolicy);
+ expect(await policyOf('/assets/preview-frame')).toBe(framePolicy);
+});
+
+test('the web shell refuses an injected inline event handler', async ({ page, api }) => {
+ await openSeededDoc(page, api, 'Shell policy');
+
+ const outcome = await page.evaluate(
+ () =>
+ new Promise<{ ran: boolean; directive: string }>((resolve) => {
+ const probe = window as Window & { injectedHandlerRan?: boolean };
+ const timer = setTimeout(
+ () => resolve({ ran: Boolean(probe.injectedHandlerRan), directive: '' }),
+ 5_000,
+ );
+ document.addEventListener(
+ 'securitypolicyviolation',
+ (event) => {
+ clearTimeout(timer);
+ resolve({
+ ran: Boolean(probe.injectedHandlerRan),
+ directive: event.effectiveDirective,
+ });
+ },
+ { once: true },
+ );
+ const host = document.createElement('div');
+ host.innerHTML = '<img src="data:," alt="" onerror="window.injectedHandlerRan = true">';
+ document.body.append(host);
+ }),
+ );
+
+ expect(outcome.ran).toBe(false);
+ expect(outcome.directive).toMatch(/^script-src/);
+});
+
+test('the editor, Mermaid and html previews keep working under the shell policy', async ({
+ page,
+ api,
+}) => {
+ await page.addInitScript(() => {
+ const violations: string[] = [];
+ (window as PolicyViolationLog).shellPolicyViolations = violations;
+ document.addEventListener('securitypolicyviolation', (event) => {
+ violations.push(`${event.effectiveDirective} ${event.blockedURI}`);
+ });
+ });
+
+ await openSeededDoc(
+ page,
+ api,
+ 'Under policy',
+ [
+ '```mermaid',
+ 'flowchart TD',
+ ' A[Start] --> B[Finish]',
+ '```',
+ '',
+ '```html preview',
+ '<div id="probe">before</div>',
+ "<script>document.getElementById('probe').textContent = 'ran';</script>",
+ '```',
+ '',
+ ].join('\n'),
+ );
+
+ await expect(
+ page.locator('[data-component-type="mermaid"].mermaid-ready .nodeLabel', { hasText: 'Start' }),
+ ).toBeVisible();
+ await expect(page.frameLocator('iframe[title="HTML preview"]').locator('#probe')).toHaveText(
+ 'ran',
+ );
+ expect(
+ await page.evaluate(() => (window as PolicyViolationLog).shellPolicyViolations ?? []),
+ ).toEqual([]);
+});
diff --git a/packages/desktop/src/renderer/index.html b/packages/desktop/src/renderer/index.html
deleted file mode 100644
index 9126775a4..000000000
--- a/packages/desktop/src/renderer/index.html
+++ /dev/null
@@ -1,14 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' https://cdn.agentclientprotocol.com; connect-src 'self' ws: http: https:;" />
- <meta name="viewport" content="width=device-width,initial-scale=1" />
- <title>OpenKnowledge</title>
- </head>
- <body>
- <div id="root">
- OpenKnowledge — renderer shell (real React bundle loaded via main-process extraResources in packaged builds).
- </div>
- </body>
-</html>
diff --git a/packages/desktop/tests/unit/renderer-csp.test.ts b/packages/desktop/tests/unit/renderer-csp.test.ts
deleted file mode 100644
index 411137662..000000000
--- a/packages/desktop/tests/unit/renderer-csp.test.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { readFileSync } from 'node:fs';
-import { join } from 'node:path';
-import { describe, expect, test } from 'vitest';
-
-const rendererHtml = readFileSync(join(import.meta.dir, '../../src/renderer/index.html'), 'utf8');
-
-describe('renderer content security policy', () => {
- test('allows agent icons from the ACP registry CDN', () => {
- const csp = rendererHtml.match(/Content-Security-Policy" content="([^"]+)"/)?.[1];
-
- expect(csp).toBeDefined();
- expect(csp).toContain("img-src 'self' https://cdn.agentclientprotocol.com");
- });
-});
diff --git a/packages/server/src/boot.ts b/packages/server/src/boot.ts
index a1ee85473..9c5bfda62 100644
--- a/packages/server/src/boot.ts
+++ b/packages/server/src/boot.ts
@@ -41,6 +41,7 @@ import {
GitTooOldError,
} from './git-preflight.ts';
import { emitPreflightFailureSpan } from './git-preflight-telemetry.ts';
+import { createShellHeaderSetter } from './http/shell-csp.ts';
import {
attachCollabClientCounter,
attachIdleShutdown,
@@ -468,6 +469,7 @@ async function bootServerInner(opts: BootServerOptions): Promise<BootedServer> {
gzip: true,
etag: true,
dev: true,
+ setHeaders: createShellHeaderSetter(opts.reactShellDistDir),
})
: undefined;
diff --git a/packages/server/src/content-assets-composition.test.ts b/packages/server/src/content-assets-composition.test.ts
index 71f999361..b8d6b6ada 100644
--- a/packages/server/src/content-assets-composition.test.ts
+++ b/packages/server/src/content-assets-composition.test.ts
@@ -135,12 +135,14 @@ describe('native serving over the real booted listener', () => {
const response = await wire(path);
expect(response.status, path).toBe(200);
expect(response.bytes.toString(), path).toBe(shell);
- expect(response.headers['content-security-policy'], path).toBeUndefined();
+ expect(response.headers['content-security-policy'], path).toContain("script-src 'self'");
+ expect(response.headers['content-security-policy'], path).not.toContain('sandbox');
expect(response.headers['content-disposition'], path).toBeUndefined();
}
const html = await wire('/docs/missing.html');
expect(html.bytes.toString()).toBe('<h1>shell HTML fallback</h1>');
- expect(html.headers['content-security-policy']).toBeUndefined();
+ expect(html.headers['content-security-policy']).toContain("script-src 'self'");
+ expect(html.headers['content-security-policy']).not.toContain('sandbox');
expect(html.headers['content-disposition']).toBeUndefined();
expect(html.headers['x-content-type-options']).toBeUndefined();
expect(html.headers['cache-control']).not.toBe('no-store');
diff --git a/packages/server/src/http/shell-csp.test.ts b/packages/server/src/http/shell-csp.test.ts
new file mode 100644
index 000000000..631b2e9e8
--- /dev/null
+++ b/packages/server/src/http/shell-csp.test.ts
@@ -0,0 +1,121 @@
+import { createHash } from 'node:crypto';
+import {
+ linkSync,
+ mkdirSync,
+ mkdtempSync,
+ readdirSync,
+ readFileSync,
+ rmSync,
+ statSync,
+ utimesSync,
+ writeFileSync,
+} from 'node:fs';
+import { IncomingMessage, ServerResponse } from 'node:http';
+import { Socket } from 'node:net';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import { createShellHeaderSetter } from './shell-csp.ts';
+
+vi.mock('node:fs', async (importOriginal) => {
+ const fs = await importOriginal<typeof import('node:fs')>();
+ return { ...fs, readFileSync: vi.fn(fs.readFileSync), readdirSync: vi.fn(fs.readdirSync) };
+});
+
+function scriptHash(source: string): string {
+ return `'sha256-${createHash('sha256').update(source).digest('base64')}'`;
+}
+
+function denied(): never {
+ throw Object.assign(new Error('permission denied'), { code: 'EACCES' });
+}
+
+const SHELL_SCRIPT = 'window.shellBooted = true;';
+
+describe('createShellHeaderSetter', () => {
+ let dist: string;
+
+ beforeEach(() => {
+ dist = mkdtempSync(join(tmpdir(), 'ok-shell-csp-'));
+ mkdirSync(join(dist, 'assets'));
+ writeFileSync(
+ join(dist, 'index.html'),
+ `<!doctype html><script>${SHELL_SCRIPT}</script><script type="module" src="./assets/app.js"></script>`,
+ );
+ writeFileSync(join(dist, 'assets', 'app.js'), 'export {};');
+ writeFileSync(
+ join(dist, 'assets', 'preview-frame.html'),
+ "<!doctype html><script>addEventListener('message', () => {});</script>",
+ );
+ });
+
+ afterEach(() => {
+ vi.mocked(readFileSync).mockReset();
+ vi.mocked(readdirSync).mockReset();
+ rmSync(dist, { recursive: true, force: true });
+ });
+
+ function serve(setHeaders: ReturnType<typeof createShellHeaderSetter>, file: string) {
+ const res = new ServerResponse(new IncomingMessage(new Socket()));
+ setHeaders(res, '/the-request-path-is-not-used', statSync(join(dist, file)));
+ return res.getHeader('content-security-policy');
+ }
+
+ test('each served document gets its own policy, and other files get none', () => {
+ const setHeaders = createShellHeaderSetter(dist);
+ expect(serve(setHeaders, 'index.html')).toContain(scriptHash(SHELL_SCRIPT));
+ expect(serve(setHeaders, 'assets/preview-frame.html')).toBe(
+ "sandbox allow-scripts; frame-ancestors 'self'",
+ );
+ expect(serve(setHeaders, 'assets/app.js')).toBeUndefined();
+ });
+
+ function setFolderTimes(time: Date) {
+ for (const folder of [dist, join(dist, 'assets')]) utimesSync(folder, time, time);
+ }
+
+ test('a document added to a subfolder after the first scan gets its policy', () => {
+ setFolderTimes(new Date('2020-01-01T00:00:00Z'));
+ const setHeaders = createShellHeaderSetter(dist);
+ serve(setHeaders, 'index.html');
+ writeFileSync(join(dist, 'assets', 'late.html'), '<script>window.late = true;</script>');
+ const assetsTouched = new Date('2021-01-01T00:00:00Z');
+ utimesSync(join(dist, 'assets'), assetsTouched, assetsTouched);
+ expect(serve(setHeaders, 'assets/late.html')).toContain(scriptHash('window.late = true;'));
+ });
+
+ test('a listing of recently changed folders is taken again even if no mtime moves', () => {
+ const recent = new Date(Date.now() - 500);
+ setFolderTimes(recent);
+ const setHeaders = createShellHeaderSetter(dist);
+ serve(setHeaders, 'index.html');
+ writeFileSync(join(dist, 'assets', 'late.html'), '<script>window.late = true;</script>');
+ setFolderTimes(recent);
+ expect(serve(setHeaders, 'assets/late.html')).toContain(scriptHash('window.late = true;'));
+ });
+
+ test('a served file that matches two documents blocks inline scripts', () => {
+ linkSync(join(dist, 'index.html'), join(dist, 'assets', 'index-copy.html'));
+ const policy = serve(createShellHeaderSetter(dist), 'index.html');
+ expect(policy).toContain("script-src 'self'");
+ expect(policy).not.toContain('sha256-');
+ });
+
+ test('a failed document read blocks inline scripts once and is not remembered', () => {
+ const setHeaders = createShellHeaderSetter(dist);
+ vi.mocked(readFileSync).mockImplementationOnce(denied);
+ const failed = serve(setHeaders, 'index.html');
+ expect(failed).toContain("script-src 'self'");
+ expect(failed).not.toContain('sha256-');
+ expect(serve(setHeaders, 'index.html')).toContain(scriptHash(SHELL_SCRIPT));
+ });
+
+ test('a failed scan blocks inline scripts once and is not remembered', () => {
+ const setHeaders = createShellHeaderSetter(dist);
+ vi.mocked(readdirSync).mockImplementationOnce(denied);
+ const failed = serve(setHeaders, 'index.html');
+ expect(failed).toContain("script-src 'self'");
+ expect(failed).not.toContain('sha256-');
+ expect(serve(setHeaders, 'index.html')).toContain(scriptHash(SHELL_SCRIPT));
+ });
+});
diff --git a/packages/server/src/http/shell-csp.ts b/packages/server/src/http/shell-csp.ts
new file mode 100644
index 000000000..cdff40f02
--- /dev/null
+++ b/packages/server/src/http/shell-csp.ts
@@ -0,0 +1,144 @@
+import { createHash } from 'node:crypto';
+import { readdirSync, readFileSync, type Stats, statSync } from 'node:fs';
+import type { ServerResponse } from 'node:http';
+import { extname, join, resolve } from 'node:path';
+import { getLogger } from '../logger.ts';
+
+// WARN: packages/app ships this page as public/assets/preview-frame.html and loads it from preview-iframe-header.ts; keep the paths in step.
+const PREVIEW_FRAME_FILE = join('assets', 'preview-frame.html');
+
+const PREVIEW_FRAME_CSP = "sandbox allow-scripts; frame-ancestors 'self'";
+
+const INLINE_SCRIPT = /<script(?![^>]*\ssrc\s*=)[^>]*>([\s\S]*?)<\/script>/gi;
+
+function inlineScriptHashes(html: string): string[] {
+ return [...html.matchAll(INLINE_SCRIPT)].map(
+ (match) =>
+ `'sha256-${createHash('sha256')
+ .update(match[1] ?? '')
+ .digest('base64')}'`,
+ );
+}
+
+function shellContentSecurityPolicy(scriptHashes: readonly string[]): string {
+ return [
+ ["script-src 'self' 'unsafe-eval' 'wasm-unsafe-eval'", ...scriptHashes].join(' '),
+ "worker-src 'self' blob:",
+ "object-src 'none'",
+ "base-uri 'self'",
+ ].join('; ');
+}
+
+const NO_INLINE_SCRIPT_POLICY = shellContentSecurityPolicy([]);
+
+const HTML_EXTENSIONS = new Set(['.html', '.htm']);
+
+const RECENT_CHANGE_MS = 2_000;
+
+interface DistListing {
+ documents: string[];
+ folders: Map<string, number>;
+ settled: boolean;
+}
+
+function listDist(root: string): DistListing {
+ const listedAt = Date.now();
+ const entries = readdirSync(root, { recursive: true, withFileTypes: true });
+ const folders = [
+ root,
+ ...entries
+ .filter((entry) => entry.isDirectory())
+ .map((entry) => join(entry.parentPath, entry.name)),
+ ];
+ const folderMtimes = new Map(folders.map((folder) => [folder, statSync(folder).mtimeMs]));
+ const recentFrom = listedAt - RECENT_CHANGE_MS;
+ const recentUntil = Date.now() + RECENT_CHANGE_MS;
+ return {
+ documents: entries
+ .filter((entry) => entry.isFile() && HTML_EXTENSIONS.has(extname(entry.name).toLowerCase()))
+ .map((entry) => join(entry.parentPath, entry.name)),
+ folders: folderMtimes,
+ settled: ![...folderMtimes.values()].some(
+ (mtimeMs) => mtimeMs >= recentFrom && mtimeMs <= recentUntil,
+ ),
+ };
+}
+
+function folderChanged([folder, mtimeMs]: [string, number]): boolean {
+ try {
+ return statSync(folder).mtimeMs !== mtimeMs;
+ } catch {
+ return true;
+ }
+}
+
+function fileIdentity(stats: Stats): string {
+ return [stats.dev, stats.ino, stats.size, stats.mtimeMs].join(':');
+}
+
+function currentIdentity(file: string): string | undefined {
+ try {
+ return fileIdentity(statSync(file));
+ } catch (err) {
+ if ((err as NodeJS.ErrnoException).code === 'ENOENT') return undefined;
+ throw err;
+ }
+}
+
+export function createShellHeaderSetter(distDir: string) {
+ const root = resolve(distDir);
+ const previewFrame = resolve(root, PREVIEW_FRAME_FILE);
+ let listing: DistListing | undefined;
+ const policies = new Map<string, { mtimeMs: number; policy: string }>();
+
+ function currentListing(): DistListing {
+ if (listing === undefined || !listing.settled || [...listing.folders].some(folderChanged)) {
+ listing = listDist(root);
+ }
+ return listing;
+ }
+
+ function servedDocument(stats: Stats): string | undefined {
+ const served = fileIdentity(stats);
+ const matches = currentListing().documents.filter((file) => currentIdentity(file) === served);
+ if (matches.length > 1) {
+ throw new Error(`the served file matches more than one document: ${matches.join(', ')}`);
+ }
+ return matches[0];
+ }
+
+ function documentPolicy(file: string, mtimeMs: number): string {
+ const cached = policies.get(file);
+ if (cached?.mtimeMs === mtimeMs) return cached.policy;
+ try {
+ const policy = shellContentSecurityPolicy(inlineScriptHashes(readFileSync(file, 'utf8')));
+ policies.set(file, { mtimeMs, policy });
+ return policy;
+ } catch (err) {
+ getLogger('shell-csp').warn(
+ { err, file },
+ 'could not read an app shell document; serving it with a policy that allows no inline script',
+ );
+ return NO_INLINE_SCRIPT_POLICY;
+ }
+ }
+
+ return (res: ServerResponse, _pathname: string, stats: Stats): void => {
+ let file: string | undefined;
+ try {
+ file = servedDocument(stats);
+ } catch (err) {
+ getLogger('shell-csp').warn(
+ { err, root },
+ 'could not identify the app shell file being served; sending a policy that allows no inline script',
+ );
+ res.setHeader('Content-Security-Policy', NO_INLINE_SCRIPT_POLICY);
+ return;
+ }
+ if (file === undefined) return;
+ res.setHeader(
+ 'Content-Security-Policy',
+ file === previewFrame ? PREVIEW_FRAME_CSP : documentPolicy(file, stats.mtimeMs),
+ );
+ };
+}