-
Notifications
You must be signed in to change notification settings - Fork 993
Fix protocol port fallback for cases with port in input #2336
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
Conversation
- When falling back from HTTPS to HTTP on port 443, now switches to port 80 - When falling back from HTTP to HTTPS on port 80, now switches to port 443 - Prevents 'plain HTTP request sent to HTTPS port' errors during fallback - Added test cases for determineMostLikelySchemeOrder and port fallback logic
- When falling back from HTTPS to HTTP on port 443, now switches to port 80 - When falling back from HTTP to HTTPS on port 80, now switches to port 443 - Uses net.JoinHostPort() to properly handle IPv6 addresses - Prevents 'plain HTTP request sent to HTTPS port' errors during fallback - Added test cases for determineMostLikelySchemeOrder and port fallback logic
…ctdiscovery/httpx into fix-protocol-port-fallback
WalkthroughAdds unit tests for port-to-scheme determination and fallback logic in the runner package. Implements automatic protocol and port switching in the HTTP/HTTPS retry flow: when fallback from HTTPS to HTTP is needed, port 443 switches to 80; HTTP to HTTPS fallback switches 80 to 443. Fixes incorrect HTTP requests to HTTPS ports causing 400 errors. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
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 (2)
runner/runner.go (1)
1756-1775: Protocol + port fallback logic matches requirements; consider aligning error-key port as a follow-upThe new fallback block correctly:
- Only triggers once for
HTTPorHTTPSinputs (!retried && origProtocol == httpx.HTTPorHTTPS).- Flips scheme and, when on default ports, switches
443 → 80(HTTPS→HTTP) and80 → 443(HTTP→HTTPS).- Uses
net.JoinHostPort(URL.Hostname(), ...), which is the right choice for IPv6-safetarget.Hostreconstruction before re‑parsing.One nuance to be aware of:
hostPort(used withHostErrorsCache) is computed once before this block and not recomputed after you changeURLandtarget.Host. When the fallback switches from:443to:80(or vice versa), any subsequent error is still counted under the originalhostPortstring. Functionally this is acceptable (the user-specified targethost:portis what gets marked as “unresponsive”), but if you ever wantHostMaxErrorsto track the actual dialed port, you’ll need to recomputehostPortafter adjustingURLand before incrementing the cache.Given the current PR scope, the behavior is logically consistent and fixes the reported bug; the
hostPortdetail can be handled as a later cleanup if desired.You may want to double-check how
urlutil.URLpopulatesHostvsHostname()to ensurenet.JoinHostPort(URL.Host, URL.Port())(earlier in this function) never receives ahoststring that already contains a port. Verifying this against the currenturlutil.ParseURLimplementation and its docs would confirm there are no edge cases around IPv6 literals orhost:portinputs.runner/ports_optimization_test.go (1)
61-115: Port-fallback tests correctly encode 443↔80 behavior; watch for duplication with production logic
TestSwitchPortForFallbackplusgetPortForFallbackaccurately capture the intended mapping:
- HTTPS on 443 → HTTP on 80
- HTTP on 80 → HTTPS on 443
- Non-default ports (8080, 8443) remain unchanged
That matches the retry behavior in
analyzeand will prevent regressions in the default-port flip.One thing to keep in mind is that
getPortForFallbacklives only in tests and duplicates the logic inrunner.go. If the production fallback ever changes (e.g., additional ports or conditions), tests might still pass while validating only this helper. Longer term, you might consider moving this mapping into a small non-test helper and using it from bothanalyzeand the tests, but for this PR the current approach is fine.If you do extract a shared helper later, verify its behavior against the current test expectations (especially for non-default ports) and ensure you’re still using the
httpx.HTTP/httpx.HTTPSconstants rather than string literals, so changes in those constants don’t silently desynchronize the mapping.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
runner/ports_optimization_test.go(1 hunks)runner/runner.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
runner/runner.go (1)
common/httpx/http2.go (2)
HTTPS(17-17)HTTP(15-15)
runner/ports_optimization_test.go (1)
common/httpx/http2.go (2)
HTTP(15-15)HTTPS(17-17)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: Lint Test
- GitHub Check: release-test
- GitHub Check: Functional Test (ubuntu-latest)
- GitHub Check: Functional Test (macOS-latest)
- GitHub Check: Analyze (go)
- GitHub Check: Functional Test (windows-latest)
🔇 Additional comments (1)
runner/ports_optimization_test.go (1)
10-59: Scheme-selection tests look solid and capture key port heuristicsThe table-driven coverage for
determineMostLikelySchemeOrderis good: it locks in behavior for the main ports (80, 8080, 443, 8443), no-port hostnames, and the IP+port cases that triggered the original issue. This is a useful safety net around the scheme-guessing heuristics without overfitting to implementation details.
|
merge conflict |
|
Added in afd5951 |
Fixes #2335
net.JoinHostPort()for proper IPv6 supportSummary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.