SimpleChatTC:SimpleProxy: Validate deps wrt enabled service paths

helps ensure only service paths that can be serviced are enabled

Use same to check for pypdf wrt pdftext
This commit is contained in:
hanishkvc 2025-11-03 14:35:23 +05:30
parent e6fd0ed05a
commit 2cdf3f574c
1 changed files with 14 additions and 3 deletions

View File

@ -48,7 +48,11 @@ gConfigType = {
gConfigNeeded = [ '--allowed.schemes', '--allowed.domains', '--bearer.insecure' ] gConfigNeeded = [ '--allowed.schemes', '--allowed.domains', '--bearer.insecure' ]
gAllowedCalls = [ "urltext", "urlraw", "pdftext" ] gAllowedCalls = {
"urltext": [],
"urlraw": [],
"pdftext": [ "pypdf" ]
}
def bearer_transform(): def bearer_transform():
@ -157,6 +161,7 @@ def handle_aum(ph: ProxyHandler, pr: urllib.parse.ParseResult):
Handle requests to aum path, which is used in a simple way to Handle requests to aum path, which is used in a simple way to
verify that one is communicating with this proxy server verify that one is communicating with this proxy server
""" """
import importlib
queryParams = urllib.parse.parse_qs(pr.query) queryParams = urllib.parse.parse_qs(pr.query)
url = queryParams['url'] url = queryParams['url']
print(f"DBUG:HandleAUM:Url:{url}") print(f"DBUG:HandleAUM:Url:{url}")
@ -165,9 +170,15 @@ def handle_aum(ph: ProxyHandler, pr: urllib.parse.ParseResult):
ph.send_error(400, f"WARN:HandleAUM:MissingUrl/UnknownQuery?!") ph.send_error(400, f"WARN:HandleAUM:MissingUrl/UnknownQuery?!")
return return
urlParts = url.split('.',1) urlParts = url.split('.',1)
if not (urlParts[0] in gAllowedCalls): if gAllowedCalls.get(urlParts[0], None) == None:
ph.send_error(403, f"WARN:HandleAUM:Forbidded:{urlParts[0]}") ph.send_error(403, f"WARN:HandleAUM:Forbidden:{urlParts[0]}")
return return
for dep in gAllowedCalls[urlParts[0]]:
try:
importlib.import_module(dep)
except ImportError as exc:
ph.send_error(400, f"WARN:HandleAUM:{urlParts[0]}:Support module [{dep}] missing or has issues")
return
print(f"INFO:HandleAUM:Availability ok for:{urlParts[0]}") print(f"INFO:HandleAUM:Availability ok for:{urlParts[0]}")
ph.send_response_only(200, "bharatavarshe") ph.send_response_only(200, "bharatavarshe")
ph.send_header('Access-Control-Allow-Origin', '*') ph.send_header('Access-Control-Allow-Origin', '*')