Repository navigation
fix(util): validate the port in parse_host() - #2793
Closed
tony1233200 wants to merge 3 commits into
Closed
tony1233200 wants to merge 3 commits into
tony1233200 wants to merge 3 commits into
Conversation
parse_host() converted the port substring with a bare int(), so a malformed port raised a generic ValueError that never mentioned the port, while forms int() happens to accept (such as 1_000, -1, or non-ASCII digits) slipped through even though RFC 3986 only allows ASCII digits in the port subcomponent. Validate the port through a small helper: an empty port is treated as absent (RFC 3986) and falls back to the default port, a port that is not composed of ASCII digits only raises a descriptive ValueError, and anything else is converted with int() as before.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2793 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 64 64
Lines 8121 8127 +6
Branches 1131 1133 +2
=========================================
+ Hits 8121 8127 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
vytas7
requested changes
Nov 11, 2026
Member
There was a problem hiding this comment.
Thanks for the PR @tony1233200, looks good for most it.
Do we have a test checking how an IPv6 address without a port is handled (should get a default port)?
See also the inline comment about the Sphinx and intersphinx links.
Edit: you do need to run a full tox gate too. Now you happened to miss typing issues, could you also take a look at these?
- tests: cover IPv6 addresses without a port, expecting the default port (bracketed address with and without a trailing colon). - fix(typing): move _parse_port() below parse_host() so the @overload variants are immediately followed by their implementation; mypy previously reported no-overload-impl and no-redef for falcon/util/uri.py. - docs: link parse_host() and ValueError in the 2756 newsfragment with Sphinx cross-references instead of double backticks.
This comment was marked as low quality.
This comment was marked as low quality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
falcon.uri.parse_host()converted the port substring with a bareint():example.com:abcraised a genericValueError: invalid literal for int()...that never mentions the port;int()happens to accept slipped through although they are not valid ports per RFC 3986 (port = *DIGIT, ASCII only):1_000,-1,+80, a leading space, and non-ASCII digits (e.g. Arabic-Indic٨٠, whichint()parses as80);example.com:) raisedValueError, although RFC 3986 allows an empty port subcomponent, equivalent to an absent port.This PR routes both port conversions in
parse_host()(domain/IPv4 and bracketed IPv6) through a small_parse_port()helper infalcon/util/uri.py:default_port;ValueError, e.g.'abc' is not a valid port (only ASCII digits are allowed);int()exactly as before.The exception type stays
ValueErrorand no public API changes; the only behavioural changes are the rejectedint()-accepted forms above, and the empty-port handling.Decisions taken (this issue is tagged
needs-decision)I went with the fix suggested in the issue itself and the direction the discussion converged on, and deliberately kept the change minimal:
int()parsing (1_000, negative numbers,+80, whitespace, non-ASCII digits) are now rejected. Happy to narrow this if you prefer a softer transition.port = *DIGIT, so an empty port is syntactically valid and equivalent to omitting it; it now falls back to the default port instead of raising.400at theRequestlayer (e.g. viaHTTPInvalidHeader):req.port/req.hoststill surface theValueErrorwhen application code accesses them, the same mechanism as today, just with a message that names the port;access_routefor RFC 7239 obfuscated ports (for="1.2.3.4:_hidden"): a malformed port in aForwardedhop raises the sameValueErroras before this change;99999is returned as-is, exactly as before.Tests
test_parse_host_malformed_portintests/test_utils.py: empty port (domain with/withoutdefault_port, bracketed IPv6) plus nine malformed forms asserted to raiseValueErrormentioning ASCII digits (abc,1_000,-1,+80, leading space, Arabic-Indic digits, superscript two,[::1]:abc,[::1]:-1).pytest tests/test_utils.py— 405 passed.pytest tests/test_request_access_route.py tests/test_request_attrs.py tests/asgi/test_request_asgi.py— 308 passed (covers the WSGI and ASGI call sites ofparse_host()).ruff checkandruff format --checkon the changed files — clean.toxgate was not run in my environment; the focused runs above were used instead.Disclosure
In line with the "Use of LLMs" section of the contributing guide: an LLM-based coding assistant helped draft this changeset. The change was reviewed line by line and tested as described above before submission, and no LLM is credited as a commit author.
Related Issues
Closes #2756.
Pull Request Checklist
falcon.util.uri.parse_host(), used by both flavours.)tox. (Full tox gate not run; focused pytest files and ruff were run instead — see Tests above.)docs/. (The new helper is module-private; the publicparse_host()docstring was updated instead.)docs/.versionadded,versionchanged, ordeprecateddirectives. (Added.. versionchanged:: 4.5toparse_host().)docs/_newsfragments/, with the file name format{issue_number}.{fragment_type}.rst. (Added2756.bugfix.rst.)