Skip to content

Commit 0898736

Browse files
committed
Merge branch '241' into 'main'
Replace regex with urlparse in validate_uri See merge request yaal/canaille!245
2 parents 814a8bf + 1250988 commit 0898736

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

canaille/app/__init__.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import hashlib
33
import hmac
44
import json
5-
import re
65
from base64 import b64encode
76
from io import BytesIO
7+
from urllib.parse import urlparse
88

99
from flask import current_app
1010
from flask import request
@@ -52,16 +52,10 @@ def get_current_mail_domain():
5252

5353

5454
def validate_uri(value):
55-
regex = re.compile(
56-
r"^(?:[A-Z0-9\\.-]+)s?://" # scheme + ://
57-
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain...
58-
r"[A-Z0-9\\.-]+|" # hostname...
59-
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # ...or ip
60-
r"(?::\d+)?" # optional port
61-
r"(?:/?|[/?]\S+)$",
62-
re.IGNORECASE,
55+
parsed = urlparse(value)
56+
return (parsed.scheme in ["http", "https"] or "." in parsed.scheme) and bool(
57+
parsed.netloc
6358
)
64-
return re.match(regex, value) is not None
6559

6660

6761
class classproperty:

tests/app/test_apputils.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44
def test_validate_uri():
55
assert validate_uri("https://canaille.test")
6-
assert validate_uri("scheme.with.dots://canaille.test")
6+
assert validate_uri("http://canaille.test")
7+
assert validate_uri("scheme.with.dots://canaille.tld")
78
assert validate_uri("scheme.with.dots://localhost")
89
assert validate_uri("scheme.with.dots://oauth")
10+
assert validate_uri("http://127.0.0.1")
11+
assert validate_uri("http://127.0.0.1:8000")
12+
assert not validate_uri("data://canaille.test")
13+
assert not validate_uri("file://canaille.test")
14+
assert not validate_uri("javascript:alert()")
915
assert not validate_uri("invalid")

0 commit comments

Comments
 (0)