Skip to content

Commit 4963b5b

Browse files
committed
feat: update CSP headers
1 parent 4441149 commit 4963b5b

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

config/csp/index.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ export const contentSecurityPolicy: ContentSecurityPolicyOption = {
2323
imgSrc: [
2424
"'self'",
2525
'data:',
26+
'blob:',
2627
'https://*.walletconnect.org',
2728
'https://*.walletconnect.com',
2829
],
2930
scriptSrc: [
3031
"'self'",
31-
"'unsafe-inline'",
32+
// light/dark theme on pageload apply script
33+
// if script changes, new hash must be checked against CSP error and updated
34+
"'sha256-wTvVT3oJ2rMAqNUILvSYccTn53N47S3NIZbPE0ql0No='",
3235
...(config.developmentMode ? ["'unsafe-eval'"] : []), // for HMR
3336
...trustedHosts,
3437
],
@@ -40,27 +43,51 @@ export const contentSecurityPolicy: ContentSecurityPolicyOption = {
4043
'wss:',
4144
...(config.developmentMode ? ['ws:'] : []), // for HMR
4245
],
43-
46+
// These directives are ignored when delivered via a <meta> element (IPFS mode).
4447
...(!config.ipfsMode && {
45-
// CSP directive 'frame-ancestors' is ignored when delivered via a <meta> element.
46-
// CSP directive 'report-uri' is ignored when delivered via a <meta> element.
48+
// Widget can be integrated into may wallets as iframe e.g Ledger Live, Safe Wallet
4749
frameAncestors: ['*'],
50+
// Modern way - References the group declared in the Reporting-Endpoints response header
51+
// Unavailable due to next.js and docker environment limitations
52+
// ...(secretConfig.cspReportUri && { reportTo: 'csp-endpoint' }),
53+
// Legacy way
4854
reportURI: secretConfig.cspReportUri,
4955
}),
56+
// frame-src takes precedence over child-src for iframes in modern browsers
57+
frameSrc: [
58+
"'self'",
59+
'https://*.walletconnect.org',
60+
'https://*.walletconnect.com',
61+
],
62+
// child-src kept as fallback for older browsers
5063
childSrc: [
5164
"'self'",
5265
'https://*.walletconnect.org',
5366
'https://*.walletconnect.com',
5467
],
5568
workerSrc: ["'none'"],
69+
objectSrc: ["'none'"], // Block plugins (Flash etc.)
70+
mediaSrc: ["'none'"], // No audio/video sources needed
71+
manifestSrc: ["'self'"],
72+
formAction: ["'self'"], // Prevent form hijacking via XSS
73+
// Block inline event handlers (onclick="...", onerror="..." etc.)
74+
'script-src-attr': ["'none'"],
5675
'base-uri': config.ipfsMode ? undefined : ["'none'"],
5776
},
5877
reportOnly: secretConfig.cspReportOnly,
5978
};
6079

80+
// TODO: remove this HOC for better control over CSP headers when next/runtime-envs situation is fixed
6181
export const withCsp = (app: FC<AppProps>): FC =>
6282
withSecureHeaders({
6383
contentSecurityPolicy,
84+
// applied manually in next.config.mjs for better control
6485
frameGuard: false,
65-
referrerPolicy: 'same-origin',
86+
forceHTTPSRedirect: false,
87+
noopen: false,
88+
expectCT: false,
89+
nosniff: false,
90+
// there is no way to avoid setting it, so align with next.config.mjs value
91+
xssProtection: 'block-rendering',
92+
referrerPolicy: false,
6693
})(app);

0 commit comments

Comments
 (0)