Description
User data stored in a module global or singleton remains available to other requests in the process, even when requests run one at a time.
Potential impact
One user’s identity or permissions may be disclosed or used to authorize another user.
Remediation
Keep request-only values in local variables or on the request object. For values that must persist between requests, use a store keyed to the user’s session.
Examples
Before
javascript
let currentUser;
app.get("/profile", (req, res) => {
currentUser = req.session.user;
res.json(currentUser);
});
After
javascript
app.get("/profile", (req, res) => {
const currentUser = req.session.user;
res.json(currentUser);
});
The first synchronous handler returns the value it just assigned, but currentUser stays in shared state afterward. The revised variable is local to that call. Both examples assume session middleware and authentication.