fix(security): complete full security remediation - httpOnly cookies, CsrfGuard, nonce-based CSP, and strict JWT production validation
This commit is contained in:
1 parent
8a3adacd38
commit
abc957ba75
8 files changed
+352
-34
No files matched your search
+31
-14
@@ -10,6 +10,7 @@ import { DecimalInterceptor } from './common/interceptors/decimal.interceptor';
|
||||
import { validateEnv } from './common/env.validation';
|
||||
import helmet from 'helmet';
|
||||
import compression from 'compression';
|
||||
import * as crypto from 'crypto';
|
||||
|
||||
// Ensure BigInt can be serialized to JSON across all NestJS / Express responses
|
||||
(BigInt.prototype as any).toJSON = function () {
|
||||
@@ -44,22 +45,38 @@ async function bootstrap() {
|
||||
|
||||
app.use(compression());
|
||||
|
||||
// SEC-013: Dynamic cryptographically-secure nonce per request for CSP
|
||||
app.use((req: any, res: any, next: any) => {
|
||||
res.locals.cspNonce = crypto.randomBytes(16).toString('base64');
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(
|
||||
helmet({
|
||||
contentSecurityPolicy: process.env.NODE_ENV === 'production' ? {
|
||||
directives: {
|
||||
defaultSrc: ["'self'"],
|
||||
scriptSrc: ["'self'"],
|
||||
styleSrc: ["'self'", "'unsafe-inline'"], // Needed for swagger and UI styling if applicable
|
||||
imgSrc: ["'self'", 'data:', 'blob:', 'https:'],
|
||||
connectSrc: ["'self'", 'https:'],
|
||||
fontSrc: ["'self'", 'data:'],
|
||||
objectSrc: ["'none'"],
|
||||
baseUri: ["'self'"],
|
||||
frameAncestors: ["'none'"],
|
||||
upgradeInsecureRequests: [],
|
||||
},
|
||||
} : false,
|
||||
contentSecurityPolicy:
|
||||
process.env.NODE_ENV === 'production'
|
||||
? {
|
||||
directives: {
|
||||
defaultSrc: ["'self'"],
|
||||
scriptSrc: [
|
||||
"'self'",
|
||||
(req: any, res: any) => `'nonce-${res.locals.cspNonce}'`,
|
||||
],
|
||||
styleSrc: [
|
||||
"'self'",
|
||||
"'unsafe-inline'", // Needed for inline styles across dashboards/UI
|
||||
(req: any, res: any) => `'nonce-${res.locals.cspNonce}'`,
|
||||
],
|
||||
imgSrc: ["'self'", 'data:', 'blob:', 'https:'],
|
||||
connectSrc: ["'self'", 'https:'],
|
||||
fontSrc: ["'self'", 'data:'],
|
||||
objectSrc: ["'none'"],
|
||||
baseUri: ["'self'"],
|
||||
frameAncestors: ["'none'"],
|
||||
upgradeInsecureRequests: [],
|
||||
},
|
||||
}
|
||||
: false,
|
||||
crossOriginEmbedderPolicy: false,
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user