feat(core): Allow specifying Content-Security-Policy-Report-Only (#15805)

This commit is contained in:
Mike Arvela
2025-05-29 09:26:24 +03:00
committed by GitHub
parent e860dd6d2e
commit c12784600f
3 changed files with 9 additions and 0 deletions

View File

@@ -32,4 +32,10 @@ export class SecurityConfig {
// TODO: create a new type that parses and validates this string into a strongly-typed object // TODO: create a new type that parses and validates this string into a strongly-typed object
@Env('N8N_CONTENT_SECURITY_POLICY') @Env('N8N_CONTENT_SECURITY_POLICY')
contentSecurityPolicy: string = '{}'; contentSecurityPolicy: string = '{}';
/**
* Whether to set the `Content-Security-Policy-Report-Only` header instead of `Content-Security-Policy`.
*/
@Env('N8N_CONTENT_SECURITY_POLICY_REPORT_ONLY')
contentSecurityPolicyReportOnly: boolean = false;
} }

View File

@@ -272,6 +272,7 @@ describe('GlobalConfig', () => {
blockFileAccessToN8nFiles: true, blockFileAccessToN8nFiles: true,
daysAbandonedWorkflow: 90, daysAbandonedWorkflow: 90,
contentSecurityPolicy: '{}', contentSecurityPolicy: '{}',
contentSecurityPolicyReportOnly: false,
}, },
executions: { executions: {
pruneData: true, pruneData: true,

View File

@@ -354,11 +354,13 @@ export class Server extends AbstractServer {
errorMessage: 'The contentSecurityPolicy is not valid JSON.', errorMessage: 'The contentSecurityPolicy is not valid JSON.',
}, },
); );
const cspReportOnly = Container.get(SecurityConfig).contentSecurityPolicyReportOnly;
const securityHeadersMiddleware = helmet({ const securityHeadersMiddleware = helmet({
contentSecurityPolicy: isEmpty(cspDirectives) contentSecurityPolicy: isEmpty(cspDirectives)
? false ? false
: { : {
useDefaults: false, useDefaults: false,
reportOnly: cspReportOnly,
directives: { directives: {
...cspDirectives, ...cspDirectives,
}, },