Anchor is_valid_scheme regex so invalid schemes are rejected (#192)#193
Open
apoorvdarshan wants to merge 1 commit into
Open
Anchor is_valid_scheme regex so invalid schemes are rejected (#192)#193apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
is_valid_scheme matched with re.match against an unanchored pattern, so
any string whose prefix looked like a scheme was accepted -- e.g.
furl('a b:c').scheme returned 'a b' instead of None, corrupting the parse.
Anchor the pattern with ^...$ like the module's other validators, and add
0-9 to the character class (valid in schemes per RFC 3986) so digit-bearing
schemes such as 'ws2' keep parsing. Fixes gruns#192.
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.
Fixes #192.
Problem
is_valid_schemematches withre.matchagainst an unanchored pattern:re.matchanchors only the start, so any string whose prefix looks like a scheme is accepted. A URL likea b:cis therefore mis-parsed as schemea b+ pathc:The module's other validators (
is_valid_encoded_path_segment,is_valid_encoded_query_key,is_valid_encoded_query_value) all anchor with^...$; this one didn't.Fix
Anchor the scheme pattern at both ends, matching the sibling validators — and add
0-9to the character class:@static_vars(regex=re.compile(r'^[a-zA-Z][a-zA-Z0-9\-\.\+]*$'))The
0-9matters: RFC 3986 allows digits in schemes (scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )). Without it, adding the anchor alone would regress digit-bearing schemes likews2orh2c— the current unanchored regex accepts them today via prefix-match (furl('ws2://host').scheme == 'ws2'), so they must keep working.After:
Testing
test_schemewith the reported case (a b:c→ no scheme,a%20b:cpath), another spaced input, and two digit-scheme cases (ws2,a1b) to guard the RFC behavior. The spaced cases fail onmasterand pass with this change.74 passed. Three pre-existing failures (test_hosts,test_netloc,test_odd_urls) are unrelated IPv6/urllib.parseissues on newer Python (see ipv6 addresses now need to be valid in 3.9+ #182, test_odd_urls fails with the recent fixes in urllib.parse #176); they fail identically with and without this change.flake8is clean on the changed lines.Disclosure: this change was prepared with the assistance of an AI tool (Claude Code). I reproduced the issue, verified the fix avoids regressing valid digit schemes, added and ran the tests and linter, and take responsibility for the contribution and will respond to review feedback personally.