Skip to content

Commit 08d1b8d

Browse files
committed
fix(security): tighten production CSP and validate CSRF allowedOrigins
- CSP: use 'self' for defaultSrc/scriptSrc instead of blanket 'https:' to prevent XSS payload injection from arbitrary HTTPS domains (aligned with OWASP and Helmet defaults) - CSRF: validate allowedOrigins entries with new URL() and warn on malformed entries instead of silently adding them Signed-off-by: Pawel Kosiec <pawel.kosiec@databricks.com>
1 parent 1a99c12 commit 08d1b8d

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

packages/appkit/src/plugins/server/security/csrf.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ function buildTrustedOrigins(config?: CsrfConfig): Set<string> {
3939
}
4040

4141
for (const o of config?.allowedOrigins ?? []) {
42-
origins.add(o.toLowerCase().replace(/\/$/, ""));
42+
try {
43+
origins.add(new URL(o).origin.toLowerCase());
44+
} catch {
45+
logger.warn(
46+
"CSRF allowedOrigins entry is not a valid URL: %s — skipping",
47+
o,
48+
);
49+
}
4350
}
4451

4552
for (const o of parseEnvOrigins(process.env.APPKIT_CSRF_ALLOWED_ORIGINS)) {

packages/appkit/src/plugins/server/security/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ function getDefaultHelmetOptions(isDev: boolean) {
3434
return {
3535
contentSecurityPolicy: {
3636
directives: {
37-
defaultSrc: ["https:", "wss:"],
38-
scriptSrc: ["https:"],
39-
styleSrc: ["'self'", "https:", "'unsafe-inline'"],
40-
imgSrc: ["https:", "data:"],
41-
fontSrc: ["https:", "data:"],
37+
defaultSrc: ["'self'"],
38+
scriptSrc: ["'self'"],
39+
styleSrc: ["'self'", "'unsafe-inline'"],
40+
imgSrc: ["'self'", "https:", "data:"],
41+
fontSrc: ["'self'", "https:", "data:"],
4242
objectSrc: ["'none'"],
4343
baseUri: ["'self'"],
44-
connectSrc: ["https:", "wss:"],
44+
connectSrc: ["'self'", "https:", "wss:"],
4545
frameAncestors: ["'none'"],
4646
},
4747
},

0 commit comments

Comments
 (0)