Commit 893e135
committed
fix(auth): harden session cookie, strip password from req.user, change logout to POST
Fixes #554 - Add httpOnly, Secure, and SameSite=strict to the session cookie
so the token is inaccessible to JavaScript and cannot be sent in cross-origin
requests. The Secure flag is conditioned on NODE_ENV=production to keep
local development functional over HTTP.
Fixes #555 - Pass .select('-password -__v').lean() to deserializeUser so the
bcrypt hash is never attached to req.user. Using .lean() also returns a plain
object instead of a full Mongoose document, preventing accidental exposure of
model methods on the request context.
Fixes #556 - Change the logout endpoint from GET to POST. GET is a safe,
idempotent method that browsers trigger automatically (img src, link prefetch,
CSS url). Any third-party page could embed a zero-pixel image pointing at the
logout URL to silently sign out authenticated users. POST requires an explicit
fetch or form submission, which CORS and SameSite=strict will block from
cross-origin callers. The handler now also calls req.session.destroy() and
clears the cookie to ensure the session is fully invalidated server-side.1 parent 4ae0ef6 commit 893e135
3 files changed
Lines changed: 23 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
38 | 41 | | |
39 | 42 | | |
40 | | - | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
45 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
46 | 54 | | |
| 55 | + | |
47 | 56 | | |
48 | 57 | | |
49 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
35 | 43 | | |
36 | 44 | | |
37 | 45 | | |
| |||
0 commit comments