-
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: csrf support check origin header with referer type #69
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #69 +/- ##
=======================================
Coverage 95.89% 95.89%
=======================================
Files 32 32
Lines 560 560
=======================================
Hits 537 537
Misses 23 23 ☔ View full report in Codecov by Sentry. |
Warning Rate limit exceeded@fengmk2 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 19 minutes and 29 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughThe pull request introduces enhancements to the CSRF (Cross-Site Request Forgery) protection mechanism in the application. The changes focus on expanding the validation process to check both Changes
Sequence DiagramsequenceDiagram
participant Client
participant Server
participant CSRFValidator
Client->>Server: Request with headers
Server->>CSRFValidator: Validate CSRF token
CSRFValidator->>CSRFValidator: Check referer header
CSRFValidator->>CSRFValidator: Check origin header
alt Valid headers
CSRFValidator-->>Server: Allow request
else Invalid headers
CSRFValidator-->>Server: Reject request (403 Forbidden)
end
Server-->>Client: Response
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
app/extend/context.js (1)
206-207
: Consider prioritizing Origin header when present.While the current implementation correctly accepts either header, the Origin header is more reliable for CSRF protection. Consider prioritizing it when available:
- const referer = (this.headers.referer || this.headers.origin || '').toLowerCase(); + // Origin header is more reliable for CSRF protection + const referer = (this.headers.origin || this.headers.referer || '').toLowerCase();
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/extend/context.js
(1 hunks)test/csrf.test.js
(14 hunks)
🔇 Additional comments (4)
app/extend/context.js (2)
206-207
: LGTM! Enhanced CSRF protection with Origin header check.The addition of the Origin header check alongside Referer strengthens CSRF protection. The Origin header is more reliable for CSRF prevention as it:
- Cannot be spoofed by attackers
- Is automatically stripped of path information
- Is mandatorily sent by browsers in cross-origin requests
210-212
: LGTM! Clear and informative error messages.Error messages have been updated to accurately reflect that either Referer or Origin header can be used for CSRF validation.
Also applies to: 218-220
test/csrf.test.js (2)
Line range hint
472-486
: LGTM! Comprehensive test coverage for Origin header support.The test suite has been thoroughly updated to verify CSRF protection with both Referer and Origin headers. The tests cover:
- Success cases with valid headers
- Failure cases with invalid headers
- Edge cases and different domain scenarios
Also applies to: 489-504, 524-535, 549-554, 567-571, 584-588, 602-607, 621-626
619-626
: LGTM! Consistent error message testing.The test assertions properly verify the updated error messages for both Referer and Origin header validation scenarios.
Also applies to: 693-694
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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
test/csrf.test.js (2)
Line range hint
472-708
: Consider reorganizing test cases for better maintainability.The test file has multiple test cases testing similar scenarios with different variations. Consider grouping related tests using
describe
blocks to improve organization and readability:
- Group Referer/Origin validation tests
- Group token validation tests
- Group configuration type tests (all, any, referer)
Example structure:
describe('CSRF Protection', () => { describe('Header Validation', () => { describe('Referer/Origin Checks', () => { // Tests for correct, incorrect, evil, illegal headers }); }); describe('Configuration Types', () => { describe('type: all', () => { // Tests for all type }); describe('type: any', () => { // Tests for any type }); // etc. }); });
Line range hint
472-708
: Consider adding edge case tests for Origin header.While the current test coverage is good, consider adding tests for the following edge cases:
- Origin header with different casing (e.g., HTTPS://nodejs.org)
- Origin header with port numbers
- Origin header with IP addresses (IPv4 and IPv6)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/extend/context.js
(1 hunks)test/csrf.test.js
(14 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- app/extend/context.js
🧰 Additional context used
🪛 Biome (1.9.4)
test/csrf.test.js
[error] 550-550: yield
is only allowed within generator functions.
(parse)
🔇 Additional comments (2)
test/csrf.test.js (2)
Line range hint
472-487
: LGTM! Good test coverage for both Referer and Origin headers.The test case properly validates that requests with either a correct Referer or Origin header from whitelisted domains are accepted.
Line range hint
640-668
: LGTM! Comprehensive coverage of CSRF type configurations.The test cases thoroughly validate the behavior of different CSRF configuration types:
all
: Requires both token and header validationany
: Requires either token or header validation
test/csrf.test.js
Outdated
yield this.app.httpRequest() | ||
.post('/update') | ||
.set('accept', 'text/html') | ||
.set('origin', 'https://wwwnodejs.org/en/') | ||
.set('host', 'nodejs.org') | ||
.expect(403); |
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.
Fix syntax error in test case.
The test case contains a syntax error using yield
instead of await
.
Apply this diff to fix the syntax error:
- yield this.app.httpRequest()
+ await app.httpRequest()
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
yield this.app.httpRequest() | |
.post('/update') | |
.set('accept', 'text/html') | |
.set('origin', 'https://wwwnodejs.org/en/') | |
.set('host', 'nodejs.org') | |
.expect(403); | |
await this.app.httpRequest() | |
.post('/update') | |
.set('accept', 'text/html') | |
.set('origin', 'https://wwwnodejs.org/en/') | |
.set('host', 'nodejs.org') | |
.expect(403); |
🧰 Tools
🪛 Biome (1.9.4)
[error] 550-550: yield
is only allowed within generator functions.
(parse)
commit: |
[skip ci] ## [3.7.0](v3.6.0...v3.7.0) (2025-01-13) ### Features * csrf support check origin header with referer type ([#69](#69)) ([2c950d3](2c950d3))
csrf 防范可以通过检查 Origin 头来验证来源
Identifying Source Origin (via Origin/Referer header)
Summary by CodeRabbit
Security Improvements
referer
andorigin
headers.Testing
referer
andorigin
.origin
header.