Skip to content

Commit fa5b1f9

Browse files
authored
Merge pull request #107 from lirantal/feat/session-fixation
feat(session): improper session management
2 parents b4b8355 + f3b22ae commit fa5b1f9

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

app/routes/session.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,25 @@ function SessionHandler(db) {
8181
return next(err);
8282
}
8383
}
84-
// Regenerating in each login
85-
// TODO: Add another vulnerability related with not to do it
86-
req.session.regenerate(function() {
87-
req.session.userId = user._id;
8884

89-
if (user.isAdmin) {
90-
return res.redirect("/benefits");
91-
} else {
92-
return res.redirect("/dashboard");
93-
}
94-
});
85+
// A2-Broken Authentication and Session Management
86+
// Upon login, a security best practice with regards to cookies session management
87+
// would be to regenerate the session id so that if an id was already created for
88+
// a user on an insecure medium (i.e: non-HTTPS website or otherwise), or if an
89+
// attacker was able to get their hands on the cookie id before the user logged-in,
90+
// then the old session id will render useless as the logged-in user with new privileges
91+
// holds a new session id now.
92+
93+
// Fix the problem by regenerating a session in each login
94+
// by wrapping the below code as a function callback for the method req.session.regenerate()
95+
// i.e:
96+
// `req.session.regenerate(function() {})`
97+
req.session.userId = user._id;
98+
if (user.isAdmin) {
99+
return res.redirect("/benefits");
100+
} else {
101+
return res.redirect("/dashboard");
102+
}
95103
});
96104
};
97105

app/views/tutorial/a2.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,35 @@ <h3>2. Session timeout and protecting cookies in transit</h3>
166166
});
167167
</pre> Note: The example code uses
168168
<code>MemoryStore</code>to manage session data, which is not designed for production environment, as it will leak memory, and will not scale past a single process. Use database based storage MongoStore or RedisStore for production. Alternatively, sessions can be managed using popular passport module.
169+
<br/>
170+
<br/>
171+
<h3>3. Session hijacking</h3>
172+
173+
<p>The insecure demo application does not regenerate a new session id upon user's login, therefore rendering a vulnerability of session hijacking if an attacker is able to somehow steal the cookie with the session id and use it.
174+
175+
<p>Upon login, a security best practice with regards to cookies session management would be to regenerate the session id so that if an id was already created for a user on an insecure medium (i.e: non-HTTPS website or otherwise), or if an attacker was able to get their hands on the cookie id before the user logged-in, then the old session id will render useless as the logged-in user with new privileges holds a new session id now.
176+
</p>
177+
178+
<p>To secure the application:</p>
179+
<p>1. Re-generate a new session id upon login (and best practice is to keep regenerating them
180+
upon requests or at least upon sensitive actions like a user's password reset.
181+
182+
Re-generate a session id as follows:
183+
By wrapping the below code as a function callback for the method req.session.regenerate()
184+
<pre>
185+
req.session.regenerate(function() {
186+
187+
req.session.userId = user._id;
188+
189+
if (user.isAdmin) {
190+
return res.redirect("/benefits");
191+
} else {
192+
return res.redirect("/dashboard");
193+
}
194+
195+
})
196+
</pre>
197+
</p>
169198
</div>
170199
</div>
171200
<div class="panel panel-default">

0 commit comments

Comments
 (0)