Skip to content

Commit ed3e514

Browse files
committed
SimpleChatTC:SimpleProxy:UrlValidator initial go
Check if the specified scheme is allowed or not. If allowed then call corresponding validator to check remaining part of the url is fine or not
1 parent 8443007 commit ed3e514

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

tools/server/public_simplechat/local.tools/urlvalidator.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@
1313
@dataclass(frozen=True)
1414
class UrlVResponse:
1515
"""
16-
Used to return result wrt urlreq helper below.
16+
Used to return detailed results below.
1717
"""
1818
callOk: bool
1919
statusCode: int
2020
statusMsg: str = ""
2121

2222

23-
def validator_ok():
24-
pass
25-
26-
27-
def validate_url(url: str, tag: str):
28-
"""
29-
Implement a re based filter logic on the specified url.
30-
"""
31-
tag=f"VU:{tag}"
23+
def validator_ok(tag: str):
3224
if (not gMe.get('--allowed.domains')):
3325
return UrlVResponse(False, 400, f"DBUG:{tag}:MissingAllowedDomains")
34-
urlParts = urllib.parse.urlparse(url)
35-
print(f"DBUG:ValidateUrl:{urlParts}, {urlParts.hostname}")
26+
if (not gMe.get('--allowed.schemes')):
27+
return UrlVResponse(False, 400, f"DBUG:{tag}:MissingAllowedSchemes")
28+
return UrlVResponse(True, 100)
29+
30+
31+
def validate_fileurl(urlParts: urllib.parse.ParseResult, tag: str):
32+
return UrlVResponse(True, 100)
33+
34+
35+
def validate_weburl(urlParts: urllib.parse.ParseResult, tag: str):
36+
# Cross check hostname
3637
urlHName = urlParts.hostname
3738
if not urlHName:
3839
return UrlVResponse(False, 400, f"WARN:{tag}:Missing hostname in Url")
@@ -43,3 +44,25 @@ def validate_url(url: str, tag: str):
4344
if not bMatched:
4445
return UrlVResponse(False, 400, f"WARN:{tag}:requested hostname not allowed")
4546
return UrlVResponse(True, 200)
47+
48+
49+
def validate_url(url: str, tag: str):
50+
"""
51+
Implement a re based filter logic on the specified url.
52+
"""
53+
tag=f"VU:{tag}"
54+
vok = validator_ok(tag)
55+
if (not vok.callOk):
56+
return vok
57+
urlParts = urllib.parse.urlparse(url)
58+
print(f"DBUG:{tag}:{urlParts}, {urlParts.hostname}")
59+
# Cross check scheme
60+
urlScheme = urlParts.scheme
61+
if not urlScheme:
62+
return UrlVResponse(False, 400, f"WARN:{tag}:Missing scheme in Url")
63+
if not (urlScheme in gMe['--allowed.schemes']):
64+
return UrlVResponse(False, 400, f"WARN:{tag}:requested scheme not allowed")
65+
if urlScheme == 'file':
66+
return validate_fileurl(urlParts, tag)
67+
else:
68+
return validate_weburl(urlParts, tag)

0 commit comments

Comments
 (0)