Skip to content

Commit 1583fea

Browse files
author
Victoria Hall
committed
asgi function app fixes
1 parent 9bea1e8 commit 1583fea

File tree

1 file changed

+28
-13
lines changed
  • tests/unittests/third_party_http_functions/stein/asgi_function

1 file changed

+28
-13
lines changed

tests/unittests/third_party_http_functions/stein/asgi_function/function_app.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
import sys
55
from urllib.request import urlopen
6-
import urllib.parse
6+
import base64
77

88
import azure.functions as func
99
from fastapi import FastAPI, Request, Response
@@ -132,10 +132,13 @@ async def print_logging(message: str = "", flush: str = 'false',
132132

133133
@fast_app.post("/raw_body_bytes")
134134
async def raw_body_bytes(request: Request):
135-
raw_body = await request.body()
136-
sanitized_body = urllib.parse.quote(raw_body)
137-
return Response(content=sanitized_body,
138-
headers={'body-len': str(len(sanitized_body))})
135+
body = await request.body()
136+
137+
base64_encoded = base64.b64encode(body).decode('utf-8')
138+
html_img_tag = \
139+
f'<img src="data:image/png;base64,{base64_encoded}" alt="PNG Image"/>'
140+
141+
return Response(html_img_tag, headers={'body-len': str(len(html_img_tag))})
139142

140143

141144
@fast_app.get("/return_http_no_body")
@@ -150,17 +153,29 @@ async def return_http(request: Request):
150153

151154
@fast_app.get("/return_http_redirect")
152155
async def return_http_redirect(request: Request, code: str = ''):
153-
allowed_url_pattern = r"^http://127\.0\.0\.1:\d+/return_http_redirect\?code=*"
156+
# Expected format: 127.0.0.1:<port>
157+
host_and_port = request.url.components[1]
158+
159+
# Validate to ensure it's a valid host and port structure
160+
match = re.match(r'^127\.0\.0\.1:(\d+)$', host_and_port)
161+
if not match:
162+
return Response("Invalid request", status_code=400)
163+
164+
# Validate port is within specific range
165+
port = int(match.group(1))
166+
if port < 50000 or port > 65999:
167+
return Response("Invalid port", status_code=400)
168+
169+
# Validate the code param
170+
allowed_codes = ['', 'testFunctionKey']
171+
if code not in allowed_codes:
172+
return Response("Invalid code", status_code=400)
154173

174+
# Return after all validation succeeds
155175
location = 'return_http?code={}'.format(code)
156-
redirect_url = f"http://127.0.0.1/{location}"
157-
if re.match(allowed_url_pattern, redirect_url):
158-
# Redirect URL is in the expected format
159-
return RedirectResponse(status_code=302,
160-
url=redirect_url)
161-
# Redirect URL was not in the expected format
162176
return RedirectResponse(status_code=302,
163-
url='/')
177+
url=f"http://{host_and_port}/"
178+
f"{location}")
164179

165180

166181
@fast_app.get("/unhandled_error")

0 commit comments

Comments
 (0)