diff --git a/server/middlewares/csp.ts b/server/middlewares/csp.ts index e32fada556..477793da6d 100644 --- a/server/middlewares/csp.ts +++ b/server/middlewares/csp.ts @@ -33,10 +33,18 @@ const getBucketOrigin = () => { } }; +interface CSPOptions { + /** Additional origins to allow as script sources. */ + extraScriptSrc?: string[]; +} + /** * Create a Content Security Policy middleware for the application. + * + * @param options Optional configuration for the CSP middleware. + * @returns A Koa middleware function that applies the CSP headers. */ -export default function createCSPMiddleware() { +export default function createCSPMiddleware(options?: CSPOptions) { // Construct scripts CSP based on options in use const defaultSrc: string[] = ["'self'"]; const scriptSrc: string[] = []; @@ -83,6 +91,7 @@ export default function createCSPMiddleware() { styleSrc, scriptSrc: [ ...uniq(scriptSrc), + ...(options?.extraScriptSrc ?? []), env.DEVELOPMENT_UNSAFE_INLINE_CSP ? "'unsafe-inline'" : `'nonce-${ctx.state.cspNonce}'`, diff --git a/server/models/helpers/DocumentHelper.test.ts b/server/models/helpers/DocumentHelper.test.ts index 2f4e8964b1..e82d956b3b 100644 --- a/server/models/helpers/DocumentHelper.test.ts +++ b/server/models/helpers/DocumentHelper.test.ts @@ -100,6 +100,32 @@ describe("DocumentHelper", () => { expect(result).toContain('

This is a test paragraph

'); }); + it("should apply the cspNonce to the injected mermaid script", async () => { + const document = await buildDocument({ + text: "```mermaid\ngraph TD;\nA-->B;\n```", + }); + const result = await DocumentHelper.toHTML(document, { + includeTitle: false, + includeStyles: false, + includeMermaid: true, + cspNonce: "test-nonce-123", + }); + expect(result).toMatch(/]*nonce="test-nonce-123"/); + expect(result).toContain('window.status = "ready"'); + }); + + it("should not set a nonce attribute when cspNonce is not provided", async () => { + const document = await buildDocument({ + text: "```mermaid\ngraph TD;\nA-->B;\n```", + }); + const result = await DocumentHelper.toHTML(document, { + includeTitle: false, + includeStyles: false, + includeMermaid: true, + }); + expect(result).not.toMatch(/]*nonce="/); + }); + it("should render diff classes when changes provided", async () => { const doc1 = await buildDocument({ text: "Hello world" }); const doc2 = await buildDocument({ text: "Hello modified world" }); diff --git a/server/models/helpers/DocumentHelper.tsx b/server/models/helpers/DocumentHelper.tsx index 4ee42ee067..b0cee05ae0 100644 --- a/server/models/helpers/DocumentHelper.tsx +++ b/server/models/helpers/DocumentHelper.tsx @@ -68,6 +68,8 @@ type HTMLOptions = { baseUrl?: string; /** Changes to highlight in the document */ changes?: readonly ExtendedChange[]; + /** CSP nonce to apply to injected inline scripts */ + cspNonce?: string; }; @trace() @@ -257,12 +259,12 @@ export class DocumentHelper { centered: options?.centered, baseUrl: options?.baseUrl, changes: options?.changes, + cspNonce: options?.cspNonce, }); addTags({ collectionId: model instanceof Collection ? model.id : undefined, documentId: !(model instanceof Collection) ? model.id : undefined, - options, }); if (options?.signedUrls) { diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index f067dc7c5f..e6085830fd 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -50,6 +50,8 @@ export type HTMLOptions = { baseUrl?: string; /** Changes to highlight in the document */ changes?: readonly ExtendedChange[]; + /** CSP nonce to apply to injected inline scripts */ + cspNonce?: string; }; export type MentionAttrs = { @@ -558,6 +560,10 @@ export class ProsemirrorHelper extends SharedProsemirrorHelper { const element = dom.window.document.createElement("script"); element.setAttribute("type", "module"); + if (options?.cspNonce) { + element.setAttribute("nonce", options.cspNonce); + } + // Inject Mermaid script if (mermaidElements.length) { element.innerHTML = `