🐛 bug: reject empty normalized host before dynamic matching - #4291
Conversation
|
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 (2)
WalkthroughThe host-authorization middleware now performs early validation of the normalized request hostname. When the normalized hostname is empty, the middleware immediately rejects the request with ChangesEmpty Hostname Validation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
There was a problem hiding this comment.
Pull request overview
Restores stricter Host header handling in the hostauthorization middleware by rejecting requests whose normalized hostname is empty before any dynamic allowlist callback is evaluated, preventing potential bypasses/panics from malformed effective hosts.
Changes:
- Reintroduced an explicit
host == ""guard inmiddleware/hostauthorization.New()to immediately reject empty normalized hosts via the configuredErrorHandler. - Added a regression test ensuring
Host: :443is rejected with403and thatAllowedHostsFuncis not invoked.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| middleware/hostauthorization/hostauthorization.go | Adds an early empty-normalized-host rejection prior to matchHost / AllowedHostsFunc. |
| middleware/hostauthorization/hostauthorization_test.go | Adds a regression test covering empty-host rejection before dynamic matching callbacks. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4291 +/- ##
=======================================
Coverage 91.32% 91.32%
=======================================
Files 132 132
Lines 12931 12933 +2
=======================================
+ Hits 11809 11811 +2
Misses 708 708
Partials 414 414
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request introduces a check to reject empty hostnames in the host authorization middleware. If the normalized host is empty, the request is immediately rejected with a forbidden error. A new test case has been added to verify that empty hosts are rejected and that the AllowedHostsFunc callback is not executed in such cases. I have no feedback to provide as there were no review comments.
Motivation
Host: :443, missing Host header, empty trustedX-Forwarded-Hostfirst element) to reachAllowedHostsFunc, which can cause authorization bypasses or panics.Description
middleware/hostauthorization.Newthat callscfg.ErrorHandler(c, ErrForbiddenHost)whennormalizeHost(c.Hostname())is empty.Test_HostAuthorization_AllowedHostsFunc_EmptyHostRejectedBeforeCallbackthat assertsHost: :443is rejected with403and thatAllowedHostsFuncis not invoked.middleware/hostauthorization/hostauthorization.goandmiddleware/hostauthorization/hostauthorization_test.go.