-
Notifications
You must be signed in to change notification settings - Fork 43
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: add rotateWhenInvalid option for CSRF token #98
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -283,6 +283,17 @@ describe('test/csrf.test.js', () => { | |||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
it('token should be rotated when enable rotateWhenInvalid', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
mm(app.config.security.csrf, 'rotateWhenInvalid', true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
await app.httpRequest() | ||||||||||||||||||||||||||||||||||||||||||||||||||
.post('/update') | ||||||||||||||||||||||||||||||||||||||||||||||||||
.set('x-csrf-token', '2') | ||||||||||||||||||||||||||||||||||||||||||||||||||
.set('cookie', 'csrfToken=1') | ||||||||||||||||||||||||||||||||||||||||||||||||||
.send({ title: 'invalid token' }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.expect(403) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.expect(res => assert(!!res.header['set-cookie'])); | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+286
to
+295
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comments for clarity. The test function is well-structured and correctly tests the new functionality. Adding comments will improve readability and maintainability. + // Test to check if the CSRF token is rotated when `rotateWhenInvalid` is enabled
it('token should be rotated when enable rotateWhenInvalid', async () => {
+ // Mock the `rotateWhenInvalid` configuration to true
mm(app.config.security.csrf, 'rotateWhenInvalid', true);
+ // Perform an HTTP request with an invalid CSRF token
await app.httpRequest()
.post('/update')
.set('x-csrf-token', '2')
.set('cookie', 'csrfToken=1')
.send({ title: 'invalid token' })
.expect(403)
+ // Assert that the CSRF token is rotated by checking the `set-cookie` header
.expect(res => assert(!!res.header['set-cookie']));
}); Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
it('should show deprecate message if ignoreJSON = true', async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const app = mm.app({ baseDir: 'apps/csrf-ignorejson' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
await app.ready(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
Add documentation for the new
rotateWhenInvalid
option.Ensure that the new
rotateWhenInvalid
option is documented within the comments for thecsrf
configuration.Committable suggestion