Merge pull request #69 from saswatds/helmet-fix
fix: resolve static asset SSL errors from helmet's upgrade-insecure-requests
This commit is contained in:
@@ -44,6 +44,8 @@ if (allowedOrigins) {
|
|||||||
corsOrigin = true;
|
corsOrigin = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shouldForceHttps = process.env.FORCE_HTTPS === 'true';
|
||||||
|
|
||||||
app.use(cors({
|
app.use(cors({
|
||||||
origin: corsOrigin,
|
origin: corsOrigin,
|
||||||
credentials: true
|
credentials: true
|
||||||
@@ -60,13 +62,15 @@ app.use(helmet({
|
|||||||
objectSrc: ["'self'"],
|
objectSrc: ["'self'"],
|
||||||
frameSrc: ["'self'"],
|
frameSrc: ["'self'"],
|
||||||
frameAncestors: ["'self'"],
|
frameAncestors: ["'self'"],
|
||||||
|
upgradeInsecureRequests: shouldForceHttps ? [] : null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
crossOriginEmbedderPolicy: false,
|
crossOriginEmbedderPolicy: false,
|
||||||
hsts: process.env.FORCE_HTTPS === 'true' ? { maxAge: 31536000, includeSubDomains: false } : false,
|
hsts: shouldForceHttps ? { maxAge: 31536000, includeSubDomains: false } : false,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Redirect HTTP to HTTPS (opt-in via FORCE_HTTPS=true)
|
// Redirect HTTP to HTTPS (opt-in via FORCE_HTTPS=true)
|
||||||
if (process.env.FORCE_HTTPS === 'true') {
|
if (shouldForceHttps) {
|
||||||
app.use((req: Request, res: Response, next: NextFunction) => {
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
||||||
if (req.secure || req.headers['x-forwarded-proto'] === 'https') return next();
|
if (req.secure || req.headers['x-forwarded-proto'] === 'https') return next();
|
||||||
res.redirect(301, 'https://' + req.headers.host + req.url);
|
res.redirect(301, 'https://' + req.headers.host + req.url);
|
||||||
|
|||||||
Reference in New Issue
Block a user