fix: normalize referer before redirect#1908
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a security vulnerability related to the "Trailing Double-Slash" attack vector in the back() method. Previously, the code had a special case that directly redirected relative paths without proper validation, which could be exploited using URLs like //evil.com that appear relative but are actually protocol-relative URLs.
Key changes:
- Removed the early return for relative path referrers, forcing all referrers through URL parsing and same-origin validation
- Added test coverage for the security fix to prevent regression
- Updated existing tests to include proper context with host information
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lib/response.js | Removed unsafe early return for relative path referrers, now all referrers are validated through URL parsing |
| tests/response/back.test.js | Added security test case for double-slash attack and updated existing tests with proper host context |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1908 +/- ##
==========================================
- Coverage 99.90% 99.90% -0.01%
==========================================
Files 9 9
Lines 2066 2060 -6
==========================================
- Hits 2064 2058 -6
Misses 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
pick from koajs/koa#1908 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Redirect "back" now only honors same-origin absolute referrers and falls back safely for protocol-relative or unsafe referrers. * **Tests** * Expanded redirect-back test coverage (origin validation, referer variations, host-context cases). * Marked flaky cluster tests to skip on Windows. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
No description provided.