-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: YL-36-BackSecuring-CSRFToken #19
Merged
Merged
Conversation
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
- Middleware CSRF, Rate-Limit, and Cookie-Parser were added with creation of the security module and controller. - For enforcing security against cross-site request forgery attacks, brute force attacks and abuse. Reviewed by: @C-Alexis4414
- Using swagger ui and insomnia, tested paths with decorator @ApiHeader to pass the CSRF token in the header of these requests. And Creating a custom exception filter in case of 403 error (invalid CSRF token) - Ensure the proper functioning of the protection implemented Reviewed by: @C-Alexis4414 Github issue: #18
github-actions
bot
added
size/XL
ci/cd
Continuous integration and delivery
tables
crud
tests
gitWorkflow
and removed
size/XL
labels
Sep 30, 2024
C-Alexis4414
changed the title
YL-36-BackSecuring-CSRFToken
feat: YL-36-BackSecuring-CSRFToken
Sep 30, 2024
C-Alexis4414
added
enhancement
New feature or request
Security
Securing against attacks and abuses
and removed
ci/cd
Continuous integration and delivery
tables
tests
gitWorkflow
labels
Sep 30, 2024
Implementation of user sessions to complement good security practices. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approve
accardigianni
approved these changes
Oct 4, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approve
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WHAT
Middleware CSRF, Rate-Limit, and Cookie-Parser were added with creation of the security module and controller.
WHY
For enforcing security against cross-site request forgery attacks, brute force attacks and abuse.
HOW
Nature of CSRF Token
A CSRF token is a temporary token generated for each user or request session, and it is stored in a cookie or sent in the request header. The server simply checks whether the token received corresponds to the one generated for the current user session.
CSRF Configuration in the application
Recommended middleware order :
a) RateLimitMiddleware -> To limit abusive requests first .
b) cookie-parser -> To analyze cookies and make them available in the
req
object.c) csurf -> To protect against CSRF attacks, using already parsed cookies.
csrf error handling
Custom exception filter in case of 403 error (invalid CSRF token) created in
csrf-exception.filter.ts
file.This filter has added globally in
main.ts
file.Creating a route to get the CSRF token
Added a route to return the CSRF token that can be used by the frontend to protect its requests.
Creation of the security module
Added a
security.module.ts
file.Create the rate limiter instance (rateLimiter) before the method configure to ensure it is properly initialized at application start.
Then, in the configure method, we apply the rateLimiter instance as middleware first, followed by cookie-parser and csurf.
Importation of the security module in the
app.module.ts
file.For the tests
To test with Swagger UI, change OpenAPI* file in
main.ts
.(*) Ability to export the OpenAPI file via http://localhost:3000/api-json or http://localhost:3000/api-yaml, to import the query collection into Insomnia.
Link to Jira ticket
BackSecuring-CSRFTokens
Checklist
Blocked
N/A
WIP
N/A
Resources
Medium article :
Best Security implementation Practices In NestJS. A Comprehensive Guide
Dev.to article :
Best Security implementation Practices In NestJS. A Comprehensive Guide
┆Issue is synchronized with this Jira Task by Unito