Tutorial

How to Prevent Session Fixation

Prevent session fixation by regenerating the session id on login and privilege change, so a fixed id is discarded before it carries auth.

On this page

  1. Introduction
  2. Regenerate on login
  3. Regenerate on privilege change
  4. Clear the old id
  5. Set new cookie
  6. Log the change
  7. Test fixation
  8. Common pitfalls
  9. Conclusion

Introduction

The hardest part of discard fixed session id is not writing the code — it is deciding what the code is allowed to reject. A session regeneration setup that accepts everything is easy to ship and expensive to operate, because every malformed input becomes a production surprise. The discipline is to enumerate the shapes the system actually receives, encode them in session regeneration, and let the library reject the rest loudly. This keeps the happy path fast and the failure path visible.

The cost of discard fixed session id without session regeneration is paid in incidents, not in code. The code looks fine because the happy path works, and the happy path is what you demo. The failure path is what you operate, and the failure path is where session regeneration earns its keep. Instrument the rejections, alert on the spikes, and let the metric tell you when the guard is misaligned with reality.

The sections below walk through the concrete decisions: where to put the session regeneration check, how to keep it fast under load, what to do when the contract changes, and how to test the failure paths so they degrade on purpose instead of by accident.

Regenerate on login

Regenerate on login is the first decision because it determines what the rest of the session regeneration pipeline can assume. A wrong choice here turns into discard fixed session id under load, and the invariant you thought session regeneration guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Regenerate on login is the one most teams skip: drive session regeneration to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

app.post('/login', async (req, res) => {
  await authenticate(req.body);
  req.session.regenerate(() => { req.session.userId = user.id; res.redirect('/'); });
});

The javascript above is the shape to ship for discard fixed session id. It is small enough to audit and specific enough that the failure behavior is obvious. Adapt the constants to your traffic and latency budget, and resist the urge to handle cases the system has not seen yet.

Regenerate on privilege change

The mechanism behind Regenerate on privilege change is straightforward in session regeneration, but the consequences ripple. A wrong choice here turns into discard fixed session id under load, and the invariant you thought session regeneration guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Regenerate on privilege change is the one most teams skip: drive session regeneration to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Clear the old id

Clear the old id is where most session regeneration implementations leak performance. A wrong choice here turns into discard fixed session id under load, and the invariant you thought session regeneration guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Clear the old id is the one most teams skip: drive session regeneration to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

test('session id changes on login', () => {
  const old = req.session.id;
  req.session.regenerate(() => {});
  expect(req.session.id).not.toBe(old);
});

Pin the behavior with this test. A session regeneration setup that passes this test degrades on purpose instead of by accident, which is the difference between a managed failure and an incident.

The hard part of Set new cookie is not the happy path — it is what happens when session regeneration fails halfway. A wrong choice here turns into discard fixed session id under load, and the invariant you thought session regeneration guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Set new cookie is the one most teams skip: drive session regeneration to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Log the change

Log the change should be observable without a debugger. A wrong choice here turns into discard fixed session id under load, and the invariant you thought session regeneration guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Log the change is the one most teams skip: drive session regeneration to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Test fixation

Test fixation is a trade, not a winner. A wrong choice here turns into discard fixed session id under load, and the invariant you thought session regeneration guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Test fixation is the one most teams skip: drive session regeneration to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Common pitfalls

The common pitfalls in discard fixed session id all reduce to one mistake: treating session regeneration as a formality instead of a contract. The contract is the thing the rest of the system relies on, and a weak contract propagates weakness everywhere.

  • Skipping the session regeneration boundary so discard fixed session id enters unvalidated.
  • Assuming session regeneration is fast without measuring the tail latency under load.
  • Letting discard fixed session id fail silently instead of failing loudly with a metric.
  • Testing only the happy path so the session regeneration failure surfaces in production.
  • Sharing mutable state across requests so one bad input poisons the next.

Conclusion

Do not conflate the conclusion with the implementation. They look similar from the outside but the failure modes differ: session regeneration fails fast and the caller knows, while an unchecked path fails slow and the caller guesses. Choose the one whose failure mode your system can absorb, and document the choice at the call site.

Self-check: write a test that drives session regeneration to the failing condition and assert the system degrades gracefully. Then discard fixed session id is a property you maintain, not a feature you hope for.

Sources: OWASP Session Fixation, Express session

FAQ

Does JWT have fixation?
No — the client generates the token. But regenerate the session cookie if you use one alongside.
Regenerate on logout?
Yes. Destroy the session and clear the cookie.

Related Examples