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
059dd43b72
commit
2856c5ef65
1 changed files with 4 additions and 1 deletions
|
|
@ -75,9 +75,12 @@ function setCsrfToken(req, res, next) {
|
|||
if (!req.cookies['csrf-token']) {
|
||||
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, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
secure: isSecure && process.env.NODE_ENV === 'production',
|
||||
sameSite: 'strict',
|
||||
maxAge: 24 * 60 * 60 * 1000 // 24 hours
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue