Skip to content

Commit

Permalink
fix(serving): fix cannot decode base64 encoded binary data (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyclimse authored Jul 15, 2024
1 parent bfb080f commit 493390d
Show file tree
Hide file tree
Showing 7 changed files with 459 additions and 412 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ default_language_version:
python: python3.10
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.0.0
rev: v3.3.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: []
- repo: https://github.com/psf/black
rev: 23.12.1
rev: 24.4.2
hooks:
- id: black
args: [--safe, --quiet]
Expand All @@ -40,7 +40,7 @@ repos:
types: [python]
args: [-rn, -sn, --rcfile=pyproject.toml]
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
rev: 7.1.0
hooks:
- id: flake8
args: [--config=.flake8]
Expand All @@ -51,15 +51,15 @@ repos:
additional_dependencies:
- tomli # for reading config from pyproject.toml
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
rev: v1.10.1
hooks:
- id: mypy
exclude: "^tests/" # See: https://github.com/pre-commit/mirrors-mypy/issues/1
args: [--ignore-missing-imports] # Needed because pre-commit runs mypy in a venv
additional_dependencies:
- typing_extensions
- repo: https://github.com/PyCQA/bandit
rev: 1.7.6
rev: 1.7.9
hooks:
- id: bandit
args: [--skip, B101, --recursive, clumper]
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added a simple server to test with multiple handlers

## [0.2.1] - 2024-07-15

### Fixed

- Returning a base64 encoded response would not be decoded by the framework
840 changes: 440 additions & 400 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "scaleway-functions-python"
version = "0.2.0"
version = "0.2.1"
description = "Utilities for testing your Python handlers for Scaleway Serverless Functions."
authors = ["Scaleway Serverless Team <[email protected]>"]

Expand Down
6 changes: 3 additions & 3 deletions scaleway_functions_python/local/serving.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from base64 import b64decode
from json import JSONDecodeError
from typing import TYPE_CHECKING, Any, ClassVar, List, Optional, cast
from typing import TYPE_CHECKING, Any, ClassVar, List, Optional, Union, cast

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

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

Expand Down
3 changes: 2 additions & 1 deletion tests/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

# pylint: disable=missing-function-docstring

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

def handler_returns_base64_encoded_body(_event, _context): # noqa
return {
"body": base64.b64encode(HELLO_WORLD.encode("utf-8")).decode("utf-8"),
"body": base64.b64encode(NON_UTF8_BINARY_DATA).decode("utf-8"),
"isBase64Encoded": True,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_local/test_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_serve_handler_b64_parameter_correct(client):
)
def test_serve_handler_with_b64_encoded_body(client):
resp = client.get("/")
assert resp.text == h.HELLO_WORLD
assert resp.data == h.NON_UTF8_BINARY_DATA


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

0 comments on commit 493390d

Please sign in to comment.