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
@@ -52,15 +52,28 @@ export function validateEnv(config: Record<string, unknown>) {
|
||||
throw new Error(`Environment Validation Error: ${messages}`);
|
||||
}
|
||||
|
||||
const isProduction = validatedConfig.NODE_ENV === 'production';
|
||||
|
||||
if (!validatedConfig.JWT_ACCESS_SECRET) {
|
||||
if (isProduction && !process.env.JWT_SECRET) {
|
||||
throw new Error(
|
||||
'Environment Validation Error: JWT_ACCESS_SECRET is strictly required in production mode.',
|
||||
);
|
||||
}
|
||||
process.env.JWT_ACCESS_SECRET =
|
||||
process.env.JWT_SECRET ||
|
||||
'canina_secure_access_token_jwt_secret_production_2026';
|
||||
'canina_dev_access_token_jwt_secret_development_only';
|
||||
validatedConfig.JWT_ACCESS_SECRET = process.env.JWT_ACCESS_SECRET;
|
||||
}
|
||||
|
||||
if (!validatedConfig.JWT_REFRESH_SECRET) {
|
||||
if (isProduction) {
|
||||
throw new Error(
|
||||
'Environment Validation Error: JWT_REFRESH_SECRET is strictly required in production mode.',
|
||||
);
|
||||
}
|
||||
process.env.JWT_REFRESH_SECRET =
|
||||
'canina_secure_refresh_token_jwt_secret_production_2026';
|
||||
'canina_dev_refresh_token_jwt_secret_development_only';
|
||||
validatedConfig.JWT_REFRESH_SECRET = process.env.JWT_REFRESH_SECRET;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { Request } from 'express';
|
||||
|
||||
@Injectable()
|
||||
export class CsrfGuard implements CanActivate {
|
||||
private readonly stateChangingMethods = new Set([
|
||||
'POST',
|
||||
'PUT',
|
||||
'PATCH',
|
||||
'DELETE',
|
||||
]);
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const req = context.switchToHttp().getRequest<Request>();
|
||||
const method = (req.method || '').toUpperCase();
|
||||
|
||||
// Safe / Read-only HTTP methods are not subject to CSRF
|
||||
if (!this.stateChangingMethods.has(method)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const path = req.originalUrl || req.url || '';
|
||||
|
||||
// Webhooks & Payment Callbacks (e.g. Zibal gateway callback, MeliPayamak incoming webhooks)
|
||||
if (
|
||||
path.includes('/payment/callback') ||
|
||||
path.includes('/payment/verify') ||
|
||||
path.includes('/webhooks')
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 1. Authorization Bearer Token requests are immune to standard cross-site browser CSRF
|
||||
const authHeader = req.headers['authorization'];
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 2. Custom header verification (X-Requested-With or X-CSRF-Token)
|
||||
const customHeader =
|
||||
req.headers['x-requested-with'] ||
|
||||
req.headers['x-csrf-token'] ||
|
||||
req.headers['x-api-key'];
|
||||
|
||||
if (customHeader) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 3. Same-origin Sec-Fetch-Site check (Modern Browsers Protection)
|
||||
const secFetchSite = req.headers['sec-fetch-site'];
|
||||
if (secFetchSite === 'same-origin' || secFetchSite === 'same-site') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If request has cookie credentials but lacks any anti-CSRF signature, block it
|
||||
if (req.headers.cookie) {
|
||||
throw new ForbiddenException(
|
||||
'درخواست مشکوک به حمله CSRF است. هدر اعتبارسنجی یافت نشد.',
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user