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
This commit is contained in:
hanishkvc 2025-11-02 13:39:37 +05:30
parent c8407a1240
commit 6cab95657f
1 changed files with 35 additions and 12 deletions

View File

@ -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)