Skip to content

fix(koa): sanitize protocol-relative redirects#5951

Closed
killagu wants to merge 2 commits into
nextfrom
agent/egg-dev/262092a7
Closed

fix(koa): sanitize protocol-relative redirects#5951
killagu wants to merge 2 commits into
nextfrom
agent/egg-dev/262092a7

Conversation

@killagu
Copy link
Copy Markdown
Contributor

@killagu killagu commented May 20, 2026

Summary

  • sanitize ambiguous ctx.redirect() targets that browsers may treat as protocol-relative URLs
  • keep existing absolute http(s) formatting behavior
  • add regression coverage for //host, /\host, and mixed slash/backslash variants

Test

  • pnpm exec oxfmt --check packages/koa/src/response.ts packages/koa/test/response/redirect.test.ts
  • pnpm exec oxlint --type-aware packages/koa/src/response.ts packages/koa/test/response/redirect.test.ts
  • pnpm --filter @eggjs/koa run typecheck
  • pnpm exec vitest run packages/koa/test/response/redirect.test.ts --bail 1 --retry 0 --testTimeout 20000 --hookTimeout 20000

Summary by CodeRabbit

  • Bug Fixes

    • Improved redirect normalization to sanitize and consistently handle a wider range of URL patterns (protocol-relative //, backslash-prefixed, mixed slash/backslash, double-slash), preventing unsafe redirects and improving stability.
  • Tests

    • Added tests covering normalization of malformed and edge-case redirect inputs and ensuring redirects set expected status without throwing.

Review Change Stack

Copilot AI review requested due to automatic review settings May 20, 2026 12:01
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3bb0a08a-3701-429e-8c9e-842042f2f1a3

📥 Commits

Reviewing files that changed from the base of the PR and between fb57885 and f5c17f1.

📒 Files selected for processing (2)
  • packages/koa/src/response.ts
  • packages/koa/test/response/redirect.test.ts

📝 Walkthrough

Walkthrough

Adds formatRedirectUrl() to normalize/sanitize redirect targets (protocol-relative, leading backslashes, double slashes) and calls it from Response.redirect(); expands tests to cover these unsafe patterns and a malformed absolute URL, ensuring redirects set Location and status 302 without throwing.

Changes

Redirect URL sanitization

Layer / File(s) Summary
Redirect URL normalization helper and integration
packages/koa/src/response.ts
Adds formatRedirectUrl() to normalize absolute http(s) URLs and rewrite leading backslashes and double-leading slashes; Response.redirect() now calls this helper before encoding and setting the Location header.
Redirect safety test coverage
packages/koa/test/response/redirect.test.ts
Adds test cases for protocol-relative (//evil.com), backslash-prefixed, mixed slash/backslash, double-backslash inputs, and a malformed absolute URL (http://[invalid) to verify normalized location header values and that redirects respond with HTTP 302 without throwing.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit trims each crooked trail,
Rewrites the slashes, mends the frail,
No sneaky host can find a door,
Redirects now hop safe once more,
Hooray — a tidy header tale! 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: sanitizing protocol-relative redirects in the koa package's redirect functionality.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/egg-dev/262092a7

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 20, 2026

Deploying egg with  Cloudflare Pages  Cloudflare Pages

Latest commit: f5c17f1
Status: ✅  Deploy successful!
Preview URL: https://f14f31a4.egg-cci.pages.dev
Branch Preview URL: https://agent-egg-dev-262092a7.egg-cci.pages.dev

View logs

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.21%. Comparing base (0dec2c9) to head (f5c17f1).

Files with missing lines Patch % Lines
packages/koa/src/response.ts 93.33% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             next    #5951   +/-   ##
=======================================
  Coverage   85.21%   85.21%           
=======================================
  Files         669      669           
  Lines       19304    19317   +13     
  Branches     3787     3790    +3     
=======================================
+ Hits        16449    16461   +12     
- Misses       2463     2464    +1     
  Partials      392      392           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 20, 2026

Deploying egg-v3 with  Cloudflare Pages  Cloudflare Pages

Latest commit: f5c17f1
Status: ✅  Deploy successful!
Preview URL: https://11ba90f5.egg-v3.pages.dev
Branch Preview URL: https://agent-egg-dev-262092a7.egg-v3.pages.dev

View logs

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a 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 (1)
packages/koa/test/response/redirect.test.ts (1)

38-43: 💤 Low value

Consider adding test coverage for double backslash and malformed URL edge cases.

The current tests cover the primary security vectors. For completeness, consider adding:

  • \\evil.com (UNC-style double backslash) → should become /%2Fevil.com
  • Malformed URL like http://[invalid → should not throw (once the fix is applied)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/koa/test/response/redirect.test.ts` around lines 38 - 43, Add two
tests to redirect.test.ts: one that calls ctx.redirect(String.raw`\\evil.com`)
and asserts ctx.response.header.location === '/%2Fevil.com' and ctx.status ===
302, and another that calls ctx.redirect('http://[invalid') and asserts it does
not throw (wrap call in a try/catch or let the test runner surface exceptions)
and that ctx.response.header.location and ctx.status are set (e.g.,
header.location equals the provided input and status === 302) to ensure
malformed URLs don't crash the redirect logic; locate and modify the existing
test block that uses context() and ctx.redirect to add these cases.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/koa/src/response.ts`:
- Around line 22-25: The URL normalization branch that currently does "new
URL(url).toString()" must be made robust: wrap the new URL(url) call in a
try/catch inside the same conditional that checks url.startsWith('https://') ||
url.startsWith('http://') (the block in response.ts handling URL formatting)
and, on catch, return the original input "url" (or a safe fallback) instead of
letting the TypeError bubble; keep the existing comment and only change the
block that constructs the URL so malformed inputs no longer throw.

---

Nitpick comments:
In `@packages/koa/test/response/redirect.test.ts`:
- Around line 38-43: Add two tests to redirect.test.ts: one that calls
ctx.redirect(String.raw`\\evil.com`) and asserts ctx.response.header.location
=== '/%2Fevil.com' and ctx.status === 302, and another that calls
ctx.redirect('http://[invalid') and asserts it does not throw (wrap call in a
try/catch or let the test runner surface exceptions) and that
ctx.response.header.location and ctx.status are set (e.g., header.location
equals the provided input and status === 302) to ensure malformed URLs don't
crash the redirect logic; locate and modify the existing test block that uses
context() and ctx.redirect to add these cases.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9cced03f-1100-4a0e-9abc-2e1dbedff97d

📥 Commits

Reviewing files that changed from the base of the PR and between 0dec2c9 and fb57885.

📒 Files selected for processing (2)
  • packages/koa/src/response.ts
  • packages/koa/test/response/redirect.test.ts

Comment thread packages/koa/src/response.ts
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens ctx.redirect() in @eggjs/koa by sanitizing redirect targets that could be interpreted by user agents as protocol-relative (cross-origin) URLs, while preserving the existing behavior for absolute http(s):// URLs.

Changes:

  • Introduce a formatRedirectUrl() helper to normalize/sanitize redirect targets before setting the Location header.
  • Block protocol-relative and backslash-based redirect variants by rewriting them into same-origin absolute-path redirects with percent-encoded leading characters.
  • Add regression tests covering //host, /\host, and mixed slash/backslash prefixes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/koa/src/response.ts Adds redirect URL normalization/sanitization and applies it in Response.redirect() before Location is encoded.
packages/koa/test/response/redirect.test.ts Adds regression tests ensuring protocol-relative and backslash-prefixed redirect inputs are rewritten to safe, same-origin paths.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a formatRedirectUrl utility to sanitize redirect URLs, preventing potential open redirect vulnerabilities by handling protocol-relative and backslash-prefixed paths. New tests were added to verify these security fixes. A review comment suggests refactoring the URL prefix validation logic to use explicit startsWith checks instead of a regular expression to improve clarity and maintainability.

Comment thread packages/koa/src/response.ts
@killagu killagu closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants