Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
.. currentmodule:: wtforms

Version 3.X.X
-------------

Unreleased

- :class:`~validators.URL` validator is based on `urllib.urlparse` and
allows `username:password@` values.

Version 3.1.2
-------------

Expand Down
28 changes: 16 additions & 12 deletions src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import math
import re
import uuid
from urllib.parse import urlparse

__all__ = (
"DataRequired",
Expand Down Expand Up @@ -505,9 +506,9 @@ def __call__(self, form, field):
super().__call__(form, field, message)


class URL(Regexp):
class URL:
"""
Simple regexp based url validation. Much like the email validator, you
Simple url validation. Much like the email validator, you
probably want to validate the url later by other means if the url must
resolve.

Expand All @@ -522,14 +523,7 @@ class URL(Regexp):
"""

def __init__(self, require_tld=True, allow_ip=True, message=None):
regex = (
r"^[a-z]+://"
r"(?P<host>[^\/\?:]+)"
r"(?P<port>:[0-9]+)?"
r"(?P<path>\/.*?)?"
r"(?P<query>\?.*)?$"
)
super().__init__(regex, re.IGNORECASE, message)
self.message = message
self.validate_hostname = HostnameValidation(
require_tld=require_tld, allow_ip=allow_ip
)
Expand All @@ -539,8 +533,18 @@ def __call__(self, form, field):
if message is None:
message = field.gettext("Invalid URL.")

match = super().__call__(form, field, message)
if not self.validate_hostname(match.group("host")):
try:
r = urlparse(field.data)
except ValueError as exc:
raise ValidationError(message) from exc

if not r.scheme:
raise ValidationError(message)

if not r.hostname:
raise ValidationError(message)

if not self.validate_hostname(r.hostname):
raise ValidationError(message)


Expand Down
5 changes: 5 additions & 0 deletions tests/validators/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"\u0625\u062e\u062a\u0628\u0627\u0631/foo.com", # Arabic
"http://उदाहरण.परीक्षा/", # Hindi
"http://실례.테스트", # Hangul
"http://username:password@foobar.dk",
"http://username@foobar.dk",
"http://usern@me:p@ssword@foobar.dk",
"http://username:password@foobar.dk:1234/path?query=parm",
],
)
def test_valid_url_passes(url_val, dummy_form, dummy_field):
Expand All @@ -42,6 +46,7 @@ def test_valid_url_passes(url_val, dummy_form, dummy_field):
"http://foobar:5000?query=param&foo=faa",
"http://foobar/path?query=param&foo=faa",
"http://foobar:1234/path?query=param&foo=faa",
"http://user:password@foobar:1234/path?query=param&foo=faa",
],
)
def test_valid_url_notld_passes(url_val, dummy_form, dummy_field):
Expand Down