Skip to content

Commit 493390d

Browse files
authored
fix(serving): fix cannot decode base64 encoded binary data (#103)
1 parent bfb080f commit 493390d

File tree

7 files changed

+459
-412
lines changed

7 files changed

+459
-412
lines changed

Diff for: .pre-commit-config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ default_language_version:
99
python: python3.10
1010
repos:
1111
- repo: https://github.com/pre-commit/pre-commit-hooks
12-
rev: v4.5.0
12+
rev: v4.6.0
1313
hooks:
1414
- id: trailing-whitespace
1515
- id: end-of-file-fixer
1616
- id: check-yaml
1717
- id: check-added-large-files
1818
- repo: https://github.com/compilerla/conventional-pre-commit
19-
rev: v3.0.0
19+
rev: v3.3.0
2020
hooks:
2121
- id: conventional-pre-commit
2222
stages: [commit-msg]
2323
args: []
2424
- repo: https://github.com/psf/black
25-
rev: 23.12.1
25+
rev: 24.4.2
2626
hooks:
2727
- id: black
2828
args: [--safe, --quiet]
@@ -40,7 +40,7 @@ repos:
4040
types: [python]
4141
args: [-rn, -sn, --rcfile=pyproject.toml]
4242
- repo: https://github.com/pycqa/flake8
43-
rev: 7.0.0
43+
rev: 7.1.0
4444
hooks:
4545
- id: flake8
4646
args: [--config=.flake8]
@@ -51,15 +51,15 @@ repos:
5151
additional_dependencies:
5252
- tomli # for reading config from pyproject.toml
5353
- repo: https://github.com/pre-commit/mirrors-mypy
54-
rev: v1.8.0
54+
rev: v1.10.1
5555
hooks:
5656
- id: mypy
5757
exclude: "^tests/" # See: https://github.com/pre-commit/mirrors-mypy/issues/1
5858
args: [--ignore-missing-imports] # Needed because pre-commit runs mypy in a venv
5959
additional_dependencies:
6060
- typing_extensions
6161
- repo: https://github.com/PyCQA/bandit
62-
rev: 1.7.6
62+
rev: 1.7.9
6363
hooks:
6464
- id: bandit
6565
args: [--skip, B101, --recursive, clumper]

Diff for: CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
### Added
2929

3030
- Added a simple server to test with multiple handlers
31+
32+
## [0.2.1] - 2024-07-15
33+
34+
### Fixed
35+
36+
- Returning a base64 encoded response would not be decoded by the framework

Diff for: poetry.lock

+440-400
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "scaleway-functions-python"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "Utilities for testing your Python handlers for Scaleway Serverless Functions."
55
authors = ["Scaleway Serverless Team <[email protected]>"]
66

Diff for: scaleway_functions_python/local/serving.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from base64 import b64decode
33
from json import JSONDecodeError
4-
from typing import TYPE_CHECKING, Any, ClassVar, List, Optional, cast
4+
from typing import TYPE_CHECKING, Any, ClassVar, List, Optional, Union, cast
55

66
from flask import Flask, json, jsonify, make_response, request
77
from flask.views import View
@@ -128,9 +128,9 @@ def resp_record_to_flask_response(
128128
self, record: "hints.ResponseRecord"
129129
) -> "FlaskResponse":
130130
"""Transform the ReponseRecord into an http reponse."""
131-
body = record.get("body", "")
131+
body: Union[str, bytes] = record.get("body", "")
132132
if record.get("isBase64Encoded") and body:
133-
body = b64decode(body.encode("utf-8"), validate=True).decode("utf-8")
133+
body = b64decode(cast(str, body).encode("utf-8"), validate=True)
134134

135135
resp = make_response(body, record.get("statusCode"))
136136

Diff for: tests/handlers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
HELLO_WORLD = "Hello World"
77
EXCEPTION_MESSAGE = "oops"
8+
NON_UTF8_BINARY_DATA = b"\x80\x81\x82\x83\x84\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
89

910
# pylint: disable=missing-function-docstring
1011

@@ -39,7 +40,7 @@ def handler_returns_is_base_64_encoded(event, _context): # noqa
3940

4041
def handler_returns_base64_encoded_body(_event, _context): # noqa
4142
return {
42-
"body": base64.b64encode(HELLO_WORLD.encode("utf-8")).decode("utf-8"),
43+
"body": base64.b64encode(NON_UTF8_BINARY_DATA).decode("utf-8"),
4344
"isBase64Encoded": True,
4445
}
4546

Diff for: tests/test_local/test_serving.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_serve_handler_b64_parameter_correct(client):
6161
)
6262
def test_serve_handler_with_b64_encoded_body(client):
6363
resp = client.get("/")
64-
assert resp.text == h.HELLO_WORLD
64+
assert resp.data == h.NON_UTF8_BINARY_DATA
6565

6666

6767
@pytest.mark.parametrize("client", [h.handler_returns_exception], indirect=True)

0 commit comments

Comments
 (0)