Skip to content

Commit 9bea1e8

Browse files
author
Victoria Hall
committed
CodeQL fixes
1 parent 139af01 commit 9bea1e8

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ dev = [
4848
"flask",
4949
"fastapi~=0.103.2",
5050
"pydantic",
51-
"pycryptodome==3.*",
5251
"flake8==5.*; python_version == '3.7'",
5352
"flake8==6.*; python_version >= '3.8'",
5453
"mypy",
@@ -69,7 +68,8 @@ dev = [
6968
"pandas",
7069
"numpy",
7170
"pre-commit",
72-
"invoke"
71+
"invoke",
72+
"cryptography"
7373
]
7474
test-http-v2 = [
7575
"azurefunctions-extensions-http-fastapi",

tests/unittests/test_third_party_http_functions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pathlib
66
import re
77
import typing
8-
import urllib.parse
8+
import base64
99

1010
from unittest.mock import patch
1111

@@ -133,15 +133,18 @@ def test_raw_body_bytes(self):
133133
image_file = parent_dir / 'unittests/resources/functions.png'
134134
with open(image_file, 'rb') as image:
135135
img = image.read()
136-
sanitized_image = urllib.parse.quote(img)
137-
sanitized_img_len = len(sanitized_image)
136+
encoded_image = base64.b64encode(img).decode('utf-8')
137+
html_img_tag = \
138+
f'<img src="data:image/png;base64,{encoded_image}" alt="PNG Image"/>' # noqa
139+
sanitized_img_len = len(html_img_tag)
138140
r = self.webhost.request('POST', 'raw_body_bytes', data=img,
139141
no_prefix=True)
140142

141143
received_body_len = int(r.headers['body-len'])
142144
self.assertEqual(received_body_len, sanitized_img_len)
143145

144-
body = urllib.parse.unquote_to_bytes(r.content)
146+
encoded_image_data = encoded_image.split(",")[0]
147+
body = base64.b64decode(encoded_image_data)
145148
try:
146149
received_img_file = parent_dir / 'received_img.png'
147150
with open(received_img_file, 'wb') as received_img:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ async def return_http(request: Request):
150150

151151
@fast_app.get("/return_http_redirect")
152152
async def return_http_redirect(request: Request, code: str = ''):
153-
allowed_url_pattern = r"^http://.+"
153+
allowed_url_pattern = r"^http://127\.0\.0\.1:\d+/return_http_redirect\?code=*"
154154

155155
location = 'return_http?code={}'.format(code)
156-
redirect_url = f"http://{request.url.components[1]}/{location}"
156+
redirect_url = f"http://127.0.0.1/{location}"
157157
if re.match(allowed_url_pattern, redirect_url):
158158
# Redirect URL is in the expected format
159159
return RedirectResponse(status_code=302,

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import sys
33
from urllib.request import urlopen
4-
import urllib.parse
4+
import base64
55

66
import azure.functions as func
77
from flask import Flask, Response, redirect, request, url_for
@@ -62,8 +62,11 @@ def print_logging():
6262
def raw_body_bytes():
6363
body = request.get_data()
6464

65-
sanitized_body = urllib.parse.quote(body)
66-
return Response(sanitized_body, headers={'body-len': str(len(sanitized_body))})
65+
base64_encoded = base64.b64encode(body).decode('utf-8')
66+
html_img_tag = \
67+
f'<img src="data:image/png;base64,{base64_encoded}" alt="PNG Image"/>'
68+
69+
return Response(html_img_tag, headers={'body-len': str(len(html_img_tag))})
6770

6871

6972
@flask_app.get("/return_http_no_body")

tests/utils/testutils_lc.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from zipfile import ZipFile
1717

1818
import requests
19-
from Crypto.Cipher import AES
20-
from Crypto.Hash.SHA256 import SHA256Hash
21-
from Crypto.Util.Padding import pad
19+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
20+
from cryptography.hazmat.backends import default_backend
21+
from cryptography.hazmat.primitives import hashes
22+
from cryptography.hazmat.primitives import padding
23+
2224
from tests.utils.constants import PROJECT_ROOT
2325

2426
# Linux Consumption Testing Constants
@@ -287,19 +289,35 @@ def _encrypt_context(cls, encryption_key: str, plain_text: str) -> str:
287289
"""Encrypt plain text context into a encrypted message which can
288290
be accepted by the host
289291
"""
292+
# Decode the encryption key
290293
encryption_key_bytes = base64.b64decode(encryption_key.encode())
291-
plain_text_bytes = pad(plain_text.encode(), 16)
294+
295+
# Pad the plaintext to be a multiple of the AES block size
296+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
297+
plain_text_bytes = padder.update(plain_text.encode()) + padder.finalize()
298+
299+
# Initialization vector (IV) (fixed value for simplicity)
292300
iv_bytes = '0123456789abcedf'.encode()
293301

294-
# Start encryption
295-
cipher = AES.new(encryption_key_bytes, AES.MODE_CBC, iv=iv_bytes)
296-
encrypted_bytes = cipher.encrypt(plain_text_bytes)
302+
# Create AES cipher with CBC mode
303+
cipher = Cipher(algorithms.AES(encryption_key_bytes),
304+
modes.CBC(iv_bytes), backend=default_backend())
297305

298-
# Prepare final result
306+
# Perform encryption
307+
encryptor = cipher.encryptor()
308+
encrypted_bytes = encryptor.update(plain_text_bytes) + encryptor.finalize()
309+
310+
# Compute SHA256 hash of the encryption key
311+
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
312+
digest.update(encryption_key_bytes)
313+
key_sha256 = digest.finalize()
314+
315+
# Encode IV, encrypted message, and SHA256 hash in base64
299316
iv_base64 = base64.b64encode(iv_bytes).decode()
300317
encrypted_base64 = base64.b64encode(encrypted_bytes).decode()
301-
key_sha256 = SHA256Hash(encryption_key_bytes).digest()
302318
key_sha256_base64 = base64.b64encode(key_sha256).decode()
319+
320+
# Return the final result
303321
return f'{iv_base64}.{encrypted_base64}.{key_sha256_base64}'
304322

305323
def __enter__(self):

0 commit comments

Comments
 (0)