perf(csrf,redirect): share scheme/host matching and skip url.Parse on the hot path - #4449
Conversation
… the hot path schemeAndHostMatch + normalizeSchemeHost were duplicated verbatim in the CSRF middleware (Origin/Referer validation) and the core redirect logic (Back's open-redirect prevention), each calling url.Parse twice per request only to split host and port. Extract them into a shared internal/schemehost package and add a fast path that handles clean "<host>" / "<host>:<port>" values directly, avoiding url.Parse. Anything unusual (userinfo, path, percent-encoding, bracketed IPv6, control chars, empty/invalid port) falls back to the unchanged url.Parse path. The security-critical top-level url.Parse(origin)/url.Parse(referer) is untouched. Equivalence is guarded by a differential test against a copy of the previous implementation and a fuzz target (FuzzNormalizeSchemeHost, 6M+ execs, zero divergence). normalizeOrigin/subdomain (which have diverged between csrf and cors) are intentionally left in place. Benchmarks (benchstat, count=6, Apple M2 Pro): schemehost.Match 6 -> 2 allocs, 368 -> 32 B, -78% ns normalizeSchemeHost/noport 3 -> 1 allocs, 184 -> 16 B, -80% ns normalizeSchemeHost/port 2 -> 0 allocs, 168 -> 0 B, -88% ns Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
💤 Files with no reviewable changes (2)
WalkthroughA new ChangesShared schemehost package and migration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" 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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4449 +/- ##
==========================================
+ Coverage 91.58% 91.59% +0.01%
==========================================
Files 134 134
Lines 13548 13494 -54
==========================================
- Hits 12408 12360 -48
+ Misses 725 723 -2
+ Partials 415 411 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
schemeAndHostMatch+normalizeSchemeHostwere duplicated verbatim in twosecurity-relevant spots:
Redirect.Back's open-redirect prevention).Each called
url.Parsetwice per request only to split host and port. This PRextracts the shared logic into a new
internal/schemehostpackage and adds afast path that handles clean
<host>/<host>:<port>values directly,avoiding the
url.Parseallocation. Anything unusual (userinfo, path,percent-encoding, bracketed IPv6, control characters, empty/invalid port) falls
back to the unchanged
url.Parsepath.The security-critical top-level
url.Parse(origin)/url.Parse(referer)(input validation ->
ErrOriginInvalid) is untouched.Equivalence / safety
Test_normalizeSchemeHost_matchesReference): the fastpath is compared against a copy of the previous
url.Parse-basedimplementation across a broad, adversarial corpus.
FuzzNormalizeSchemeHost): 6M+ executions, zero divergence.Test_normalizeSchemeHost) andTest_Matchcoverthe documented behavior. Full CSRF suite and root redirect tests pass.
Benchmarks
benchstat, count=6, Apple M2 Pro:
schemehost.MatchnormalizeSchemeHost/noportnormalizeSchemeHost/portThe optimization now benefits both the CSRF unsafe-request path and
Redirect.Back.Notes
normalizeOrigin/subdomainare also duplicated (between CSRF and CORS) buthave diverged in their validation rules, so they are intentionally left in
place; unifying them is a separate change.
🤖 Generated with Claude Code