Skip to content

fix(validators): match URL schemes case-insensitively and reject out-of-range ports - #2425

Open
waw666waw666 wants to merge 1 commit into
tortoise:developfrom
waw666waw666:fix/url-validator-scheme-port
Open

waw666waw666 wants to merge 1 commit into
tortoise:developfrom
waw666waw666:fix/url-validator-scheme-port

Conversation

@waw666waw666

Copy link
Copy Markdown

URLValidator has two correctness gaps next to the recently fixed EmailValidator ones. (1) The scheme of the value is lowercased before comparison, but the configured allowed_schemes are not, so URLValidator(allowed_schemes=["HTTPS"]) rejects every URL — while EmailValidator's allowed_domains were 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, so https://example.com:99999/ validates even though ports stop at 65535.

Normalize allowed_schemes to lowercase in __init__ (None default and the empty-list semantics from #2394 unchanged). In __call__, after the scheme check, access urlsplit's port property and convert its ValueError for out-of-range ports into InvalidURL. 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:

python -m pytest tests/test_validators.py -q   # 94 passed (83 before, +11 new)
TORTOISE_TEST_DB=sqlite://:memory: python -m pytest tests/ -q -m "not benchmark" -n 2  # 2256 passed, 0 failed (baseline 2245)
ruff check tortoise/validators.py tests/test_validators.py  # clean

Reverting only tortoise/validators.py makes the 6 new bug-covering tests fail again (red–green–red verified).

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant