-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[App] Add CSRF token-based mitigation (#187)
* [*.test] Refactor logInAgent to a common place * [Deps] npm i --save-dev @types/lusca * [App] Default CSRF protection on Express app * [CSRF] Echo back the CSRF token when logging in * [CSRF] Use the _csrf value that Lusca places in res.locals * [CSRF] Echo back CSRF token on all forms served through EJS * [tRPC] Echo the CSRF token through the x-csrf-token header * [ES] Support NODE_ENV=test (at least on Posix) * [CSRF] Disable in testing environment
- Loading branch information
Showing
17 changed files
with
130 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expect } from "chai"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import request from "supertest"; | ||
|
||
import { | ||
authenticateUser, | ||
AuthenticateUserParam, | ||
} from "../models/LogInUtilities"; | ||
import { HOME, LOGIN } from "../paths"; | ||
import { app } from "../server"; | ||
import { dummyAccountDetails } from "./DummyAccountUtils"; | ||
|
||
/** | ||
* Log in a user using `supertest` and return the agent. By default, logs in the | ||
* dummy account. | ||
*/ | ||
export async function logInAgent(authDetails: AuthenticateUserParam = { | ||
username_or_email: dummyAccountDetails.email, | ||
password: dummyAccountDetails.password, | ||
}) { | ||
const agent = request.agent(app); | ||
|
||
// Check that the user does exist. | ||
const user = await authenticateUser(authDetails); | ||
expect(user).to.not.be.null; | ||
|
||
// Login, and follow redirect to `HOME`. | ||
const result = await agent | ||
.post(LOGIN) | ||
.send(authDetails) | ||
.redirects(1); | ||
|
||
expect(result.status).to.equal(StatusCodes.OK); | ||
expect(result.type).to.equal("text/html"); | ||
|
||
const finalURL = new URL(result.request.url); | ||
expect(finalURL.pathname).to.equal(HOME); | ||
|
||
return Promise.resolve(agent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
<form method="post" action="<%= LOGIN %>"> | ||
<label for="username"> Username or Email Address: </label> | ||
<input class="w3-input" type="text" name="username_or_email" required/> | ||
<input type="hidden" name="_csrf" value="<%= _csrf %>"> | ||
<label for="username"> Username or Email Address: </label> | ||
<input class="w3-input" type="text" name="username_or_email" required /> | ||
|
||
<label for="password"> Password: </label> | ||
<input class="w3-input" type="password" name="password" minlength="8" required/> | ||
<label for="password"> Password: </label> | ||
<input class="w3-input" type="password" name="password" minlength="8" required /> | ||
|
||
<button class="w3-button w3-center w3-green" type="submit">Log In</button> | ||
<button class="w3-button w3-center w3-green" type="submit">Log In</button> | ||
|
||
<p> | ||
Do not have an account? <a href="<%= REGISTER_USER %>">Sign up</a> | ||
</p> | ||
<p> | ||
Do not have an account? <a href="<%= REGISTER_USER %>">Sign up</a> | ||
</p> | ||
|
||
<p> | ||
Forgot password? <a href="<%= RESET_PASSWORD %>">Reset password</a> | ||
</p> | ||
<p> | ||
Forgot password? <a href="<%= RESET_PASSWORD %>">Reset password</a> | ||
</p> | ||
|
||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
<form method="post"> | ||
<label for="email"> Type your new password: </label> | ||
<input class="w3-input" type="password" name="password_1" minlength="8" required/> | ||
<input type="hidden" name="_csrf" value="<%= _csrf %>"> | ||
<label for="email"> Type your new password: </label> | ||
<input class="w3-input" type="password" name="password_1" minlength="8" required /> | ||
|
||
<label for="username"> Re-type your new password: </label> | ||
<input class="w3-input" type="password" name="password_2" minlength="8" required/> | ||
<label for="username"> Re-type your new password: </label> | ||
<input class="w3-input" type="password" name="password_2" minlength="8" required /> | ||
|
||
<button class="w3-button w3-center w3-green" type="submit">Reset Password</button> | ||
<button class="w3-button w3-center w3-green" type="submit">Reset Password</button> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
<form method="POST"> | ||
<label for="email"> Type the email address associated with your account: </label> | ||
<input class="w3-input" type="email" name="email" required/> | ||
<input type="hidden" name="_csrf" value="<%= _csrf %>"> | ||
<label for="email"> Type the email address associated with your account: </label> | ||
<input class="w3-input" type="email" name="email" required /> | ||
|
||
<button class="w3-button w3-center w3-green" type="submit"> | ||
Request Password Reset | ||
</button> | ||
<button class="w3-button w3-center w3-green" type="submit"> | ||
Request Password Reset | ||
</button> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<form method="post"> | ||
<input type="hidden" name="_csrf" value="<%= _csrf %>"> | ||
<h5>Request a validation URL for your <%= APP_NAME %> Account</h5> | ||
|
||
<h5>Request a validation URL for your <%= APP_NAME %> Account</h5> | ||
<label for="email"> Type your email address below: </label> | ||
<input class="w3-input" type="email" name="email" required /> | ||
|
||
<label for="email"> Type your email address below: </label> | ||
<input class="w3-input" type="email" name="email" required/> | ||
|
||
<button class="w3-button w3-center w3-green" type="submit">Send Validation URL</button> | ||
<button class="w3-button w3-center w3-green" type="submit">Send Validation URL</button> | ||
|
||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
<form method="post"> | ||
<input type="hidden" name="_csrf" value="<%= _csrf %>"> | ||
<label for="email">Email Address: </label> | ||
<input class="w3-input" type="email" name="email" required /> | ||
|
||
<label for="email">Email Address: </label> | ||
<input class="w3-input" type="email" name="email" | ||
required/> | ||
<label for="username"> Choose an alphanumeric username: </label> | ||
<input class="w3-input" type="text" name="username" pattern="[_\-A-Za-z0-9]+" required /> | ||
|
||
<label for="username"> Choose an alphanumeric username: </label> | ||
<input class="w3-input" type="text" name="username" | ||
pattern="[_\-A-Za-z0-9]+" required/> | ||
<label for="password"> Choose a password: </label> | ||
<input class="w3-input" type="password" name="password" id="signup_password" minlength="8" required /> | ||
|
||
<label for="password"> Choose a password: </label> | ||
<input class="w3-input" type="password" name="password" | ||
id="signup_password" minlength="8" required/> | ||
<button class="w3-button w3-center w3-green" type="submit">Sign Up</button> | ||
|
||
<button class="w3-button w3-center w3-green" type="submit">Sign Up</button> | ||
|
||
<p> | ||
Already have an account? <a href="<%= LOGIN %>">Log In</a> | ||
</p> | ||
<p> | ||
Already have an account? <a href="<%= LOGIN %>">Log In</a> | ||
</p> | ||
|
||
</form> |
Oops, something went wrong.