Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
refactor(html-artifact): always-wrap CSP injection
Akarshan Biswasa6a584832ba9
diff --git a/web-app/src/components/HtmlArtifact.tsx b/web-app/src/components/HtmlArtifact.tsxindex 9292fe8bd..7adc327b3 100644--- a/web-app/src/components/HtmlArtifact.tsx+++ b/web-app/src/components/HtmlArtifact.tsx@@ -8,32 +8,19 @@ import type { BundledLanguage } from 'shiki'interface HtmlArtifactProps {code: stringclassName?: string- /**- * When true the live preview cannot be opened yet (content still streaming).- * The toggle stays on the Code view and the Preview tab is disabled.- */+ // While streaming, Preview is disabled and the toggle stays on Code.isStreaming?: boolean- /**- * Relax the iframe CSP to permit https: scripts/styles/fonts/images and- * outbound connections. Off by default: model-generated HTML runs with no- * network access so it cannot phone home or pull remote payloads.- */+ // Off by default so model HTML has no network access; on relaxes CSP to https:.allowNetwork?: boolean- /**- * Static mode for SVG: no scripts at all (CSP script-src 'none' and the- * iframe drops allow-scripts). Code view highlights as the given language.- */+ // Off (SVG static mode) forbids scripts via CSP and drops iframe allow-scripts.allowScripts?: booleanlanguage?: string}type View = 'code' | 'preview'-/**- * Strict CSP keeps model HTML inert on the network. The iframe already runs in- * an opaque origin (sandbox without allow-same-origin), so this only governs- * what the document itself may reach out to.- */+// Strict CSP governs what model HTML can reach out to; the iframe is already+// sandboxed to an opaque origin (no allow-same-origin).function buildCsp(allowNetwork: boolean, allowScripts: boolean): string {if (!allowScripts) {return [@@ -71,12 +58,8 @@ function buildSrcDoc(): string {const csp = buildCsp(allowNetwork, allowScripts)const meta = `<meta http-equiv="Content-Security-Policy" content="${csp}">`- if (/<head[\s>]/i.test(code)) {- return code.replace(/<head([^>]*)>/i, `<head$1>${meta}`)- }- if (/<html[\s>]/i.test(code)) {- return code.replace(/<html([^>]*)>/i, `<html$1><head>${meta}</head>`)- }+ // Always wrap so the CSP meta precedes all model markup — a meta CSP is only+ // honored before resource-fetching content, and a later one can't loosen it.return `<!doctype html><html><head>${meta}</head><body>${code}</body></html>`}diff --git a/web-app/src/components/__tests__/HtmlArtifact.test.tsx b/web-app/src/components/__tests__/HtmlArtifact.test.tsxindex 221a3d50b..097513607 100644--- a/web-app/src/components/__tests__/HtmlArtifact.test.tsx+++ b/web-app/src/components/__tests__/HtmlArtifact.test.tsx@@ -97,7 +97,7 @@ describe('HtmlArtifact', () => {expect(screen.getByTestId('code-block')).toBeInTheDocument()})- it('preserves an existing <head> when injecting CSP', async () => {+ it('wraps a full document so the CSP meta precedes the model markup', async () => {const user = userEvent.setup()const fullDoc = '<html><head><title>t</title></head><body>x</body></html>'render(<HtmlArtifact code={fullDoc} />)@@ -106,6 +106,8 @@ describe('HtmlArtifact', () => {const iframe = screen.getByTestId('html-artifact-iframe') as HTMLIFrameElementconst doc = iframe.getAttribute('srcdoc') ?? ''expect(doc).toContain('<title>t</title>')- expect((doc.match(/<head/gi) ?? []).length).toBe(1)+ expect(doc.indexOf('Content-Security-Policy')).toBeLessThan(+ doc.indexOf('<title>t</title>')+ )})})