Skip to content

Commit f3b22ae

Browse files
committed
feat(session): add tutorial documentation
1 parent 15d5b74 commit f3b22ae

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

app/routes/session.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,19 @@ 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-
// @FIXME fix this problem which doesnt regenerate a session id
87-
// upon user login by wrapping the below code within a function
88-
// callback for `req.session.regenerate(function() {})`
84+
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() {})`
8997
req.session.userId = user._id;
9098
if (user.isAdmin) {
9199
return res.redirect("/benefits");

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)