fix: CSRF cookie secure flag for reverse proxy environments
Check X-Forwarded-Proto header to determine if request is HTTPS This ensures CSRF cookies work correctly when nginx terminates SSL
This commit is contained in:
parent
9d0dd0d83f
commit
0debe26af0
1 changed files with 4 additions and 1 deletions
|
|
@ -75,9 +75,12 @@ function setCsrfToken(req, res, next) {
|
||||||
if (!req.cookies['csrf-token']) {
|
if (!req.cookies['csrf-token']) {
|
||||||
const token = generateCsrfToken();
|
const token = generateCsrfToken();
|
||||||
|
|
||||||
|
//Check if we're behind a proxy (X-Forwarded-Proto header)
|
||||||
|
const isSecure = req.secure || req.headers['x-forwarded-proto'] === 'https';
|
||||||
|
|
||||||
res.cookie('csrf-token', token, {
|
res.cookie('csrf-token', token, {
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: process.env.NODE_ENV === 'production',
|
secure: isSecure && process.env.NODE_ENV === 'production',
|
||||||
sameSite: 'strict',
|
sameSite: 'strict',
|
||||||
maxAge: 24 * 60 * 60 * 1000 // 24 hours
|
maxAge: 24 * 60 * 60 * 1000 // 24 hours
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue