Skip to content

Commit a48c39f

Browse files
author
Gokhan MANKARA
committed
Fix a false positive urls
'h://f.xx' 'http://w..com'
1 parent 5f28c39 commit a48c39f

2 files changed

Lines changed: 11 additions & 17 deletions

File tree

‎tests/test_url.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
from validators import url, ValidationFailure
55

66

7-
@pytest.mark.parametrize(('address', 'require_tld'), [
7+
@pytest.mark.parametrize(('address',), [
88
('http://foobar.dk', True),
99
('http://foobar.dk', True),
1010
('http://foobar.museum/foobar', True),
1111
('http://127.0.0.1/foobar', True),
1212
('http://127.0.0.1:9000/fake', True),
1313
('http://localhost/foobar', False),
1414
('http://foobar', False),
15+
('htp://foobar.com', False),
16+
('http://foobar..com', False),
17+
('http://fo..com', False),
18+
('http://fo.com', True),
1519
])
16-
def test_returns_true_on_valid_url(address, require_tld):
17-
assert url(address, require_tld=require_tld)
18-
20+
def test_returns_true_on_valid_url(address):
21+
assert url(address)
1922

2023
@pytest.mark.parametrize(('address',), [
2124
('http://foobar',),

‎validators/url.py‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
from .utils import validator
44

5-
regex = (
6-
r'^[a-z]+://([^/:]+{tld}|([0-9]{{1,3}}\.)'
7-
r'{{3}}[0-9]{{1,3}})(:[0-9]+)?(\/.*)?$'
8-
)
5+
regex = (r'^(http(s)?://)?(www\.)?((\w|-)+)(\.(\w|-)+)+(:\d*)?(/(\w|-|\&|\=|\#)*)*')
96

10-
pattern_with_tld = re.compile(regex.format(tld=r'\.[a-z]{2,10}'))
11-
pattern_without_tld = re.compile(regex.format(tld=''))
7+
pattern = re.compile(regex)
128

139

1410
@validator
15-
def url(value, require_tld=True):
11+
def url(value):
1612
"""
1713
Return whether or not given value is a valid URL.
1814
@@ -29,9 +25,6 @@ def url(value, require_tld=True):
2925
>>> url('http://foobar.dk')
3026
True
3127
32-
>>> url('http://localhost/foobar', require_tld=False)
33-
True
34-
3528
>>> url('http://foobar.d')
3629
ValidationFailure(func=url, ...)
3730
@@ -40,6 +33,4 @@ def url(value, require_tld=True):
4033
:param value: URL address string to validate
4134
"""
4235

43-
if require_tld:
44-
return pattern_with_tld.match(value)
45-
return pattern_without_tld.match(value)
36+
return pattern.match(value)

0 commit comments

Comments
 (0)