From 6cab95657f8481abd33a482bd8de6e6b587fd15a Mon Sep 17 00:00:00 2001 From: hanishkvc Date: Sun, 2 Nov 2025 13:39:37 +0530 Subject: [PATCH] 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 --- .../local.tools/urlvalidator.py | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/tools/server/public_simplechat/local.tools/urlvalidator.py b/tools/server/public_simplechat/local.tools/urlvalidator.py index dec5f11c5a..59f796d430 100644 --- a/tools/server/public_simplechat/local.tools/urlvalidator.py +++ b/tools/server/public_simplechat/local.tools/urlvalidator.py @@ -13,26 +13,27 @@ gMe = { @dataclass(frozen=True) class UrlVResponse: """ - Used to return result wrt urlreq helper below. + Used to return detailed results below. """ callOk: bool statusCode: int statusMsg: str = "" -def validator_ok(): - pass - - -def validate_url(url: str, tag: str): - """ - Implement a re based filter logic on the specified url. - """ - tag=f"VU:{tag}" +def validator_ok(tag: str): if (not gMe.get('--allowed.domains')): return UrlVResponse(False, 400, f"DBUG:{tag}:MissingAllowedDomains") - urlParts = urllib.parse.urlparse(url) - print(f"DBUG:ValidateUrl:{urlParts}, {urlParts.hostname}") + if (not gMe.get('--allowed.schemes')): + return UrlVResponse(False, 400, f"DBUG:{tag}:MissingAllowedSchemes") + return UrlVResponse(True, 100) + + +def validate_fileurl(urlParts: urllib.parse.ParseResult, tag: str): + return UrlVResponse(True, 100) + + +def validate_weburl(urlParts: urllib.parse.ParseResult, tag: str): + # Cross check hostname urlHName = urlParts.hostname if not urlHName: return UrlVResponse(False, 400, f"WARN:{tag}:Missing hostname in Url") @@ -43,3 +44,25 @@ def validate_url(url: str, tag: str): if not bMatched: return UrlVResponse(False, 400, f"WARN:{tag}:requested hostname not allowed") return UrlVResponse(True, 200) + + +def validate_url(url: str, tag: str): + """ + Implement a re based filter logic on the specified url. + """ + tag=f"VU:{tag}" + vok = validator_ok(tag) + if (not vok.callOk): + return vok + urlParts = urllib.parse.urlparse(url) + print(f"DBUG:{tag}:{urlParts}, {urlParts.hostname}") + # Cross check scheme + urlScheme = urlParts.scheme + if not urlScheme: + return UrlVResponse(False, 400, f"WARN:{tag}:Missing scheme in Url") + if not (urlScheme in gMe['--allowed.schemes']): + return UrlVResponse(False, 400, f"WARN:{tag}:requested scheme not allowed") + if urlScheme == 'file': + return validate_fileurl(urlParts, tag) + else: + return validate_weburl(urlParts, tag)