Skip to content
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

URLs with query params should pass URL validator #524

Merged
merged 2 commits into from Nov 11, 2019

Conversation

laurihy
Copy link
Contributor

@laurihy laurihy commented Nov 5, 2019

Fixes #523

Previously URL validator didn't accept URLs with query parameters if there wasn't also a path specified. This was, because in that case the old regex would include query-params to the host, and attempt to call HostnameValidation with the incorrect host.

Old regex:

regex = re.compile(r"^[a-z]+://(?P<host>[^/:]+)(?P<port>:[0-9]+)?(?P<path>\/.*)?$")
matches = regex.match('https://foo.io?no=path')
print(matches.group('host'))
# -> foo.io?no=path

New regex:

regex = (
    r"^[a-z]+://"
    r"(?P<host>[^\/\?:]+)"
    r"(?P<port>:[0-9]+)?"
    r"(?P<path>\/.*?)?"
    r"(?P<query>\?.*)?$"
)
matches = regex.match('https://foo.io?no=path')
print(matches.group('host'))
# -> foo.io
print(matches.group('query'))
# -> ?no=path

Added a bunch of tests to verify this behavior.

tests/test_validators.py Outdated Show resolved Hide resolved
@whb07
Copy link
Contributor

@whb07 whb07 commented Nov 8, 2019

looks good in general just missing the tweaks for the typo and an extra test case for the tld test set

@laurihy
Copy link
Contributor Author

@laurihy laurihy commented Nov 10, 2019

Thanks for the good comments! Added a few more test cases, and fixed the typo in the comment.

@whb07
Copy link
Contributor

@whb07 whb07 commented Nov 11, 2019

Great, I saw the changes and it looks good to me. I also played with it locally and manually to verify some behavior and the fixes work 🙃

@whb07 whb07 merged commit 23f730a into wtforms:master Nov 11, 2019
1 check passed
azmeuk added a commit to azmeuk/wtforms that referenced this issue Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants