Repository navigation
fix(validators): match URL schemes case-insensitively and reject out-of-range ports - #2425
Open
waw666waw666 wants to merge 1 commit into
Open
waw666waw666 wants to merge 1 commit into
waw666waw666 wants to merge 1 commit into
Conversation
…of-range ports URLValidator lowercased the scheme of the value being checked but not the configured allowed_schemes, so an allowlist such as ['HTTPS'] rejected every URL, unlike EmailValidator whose allowed_domains were made case-insensitive in tortoise#2403. It also never range-checked the port, so URLs with ports above 65535 (e.g. https://example.com:99999/) were accepted even though no such port can exist. Normalize allowed_schemes to lowercase on construction, and access urlsplit's port property after the scheme check, converting its ValueError for out-of-range ports into InvalidURL.
This branch has not been deployed
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.
URLValidatorhas two correctness gaps next to the recently fixedEmailValidatorones. (1) The scheme of the value is lowercased before comparison, but the configuredallowed_schemesare not, soURLValidator(allowed_schemes=["HTTPS"])rejects every URL — whileEmailValidator'sallowed_domainswere made case-insensitive in #2403, this side was missed, and schemes are case-insensitive per RFC 3986 §3.1. (2) The port is only checked by a digit regex and never range-checked, sohttps://example.com:99999/validates even though ports stop at 65535.Normalize
allowed_schemesto lowercase in__init__(None default and the empty-list semantics from #2394 unchanged). In__call__, after the scheme check, accessurlsplit'sportproperty and convert itsValueErrorfor out-of-range ports intoInvalidURL. Tests for both directions of case matching, still-rejected schemes, out-of-range ports (including IPv6) and the 0/65535 boundaries; CHANGELOG entry.How it was tested:
Reverting only
tortoise/validators.pymakes the 6 new bug-covering tests fail again (red–green–red verified).