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(/