Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ def view_func(path):
description=(
"Function was defined with FUNCTION_SIGNATURE_TYPE=cloudevent but"
" parsing CloudEvent failed and converting from background event to"
f" CloudEvent also failed.\nGot HTTP headers: {request.headers}\nGot"
f" data: {request.get_data()}\nGot CloudEvent exception: {repr(ce_exception)}"
f"\nGot background event conversion exception: {repr(e)}"
f" CloudEvent also failed.\nGot CloudEvent exception: {type(ce_exception).__name__}"
f"\nGot background event conversion exception: {type(e).__name__}"
),
)
return "OK"
Expand Down
3 changes: 2 additions & 1 deletion src/functions_framework/aio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ async def handler(request):
event = from_http(request.headers, data)
except Exception as e:
raise HTTPException(
400, detail=f"Bad Request: Got CloudEvent exception: {repr(e)}"
400,
detail=f"Bad Request: Got CloudEvent exception: {type(e).__name__}",
)
if is_async:
await function(event)
Expand Down
81 changes: 81 additions & 0 deletions tests/test_cloud_event_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,84 @@ def test_background_event(converted_background_event_client, background_event):
print(resp.text)
assert resp.status_code == 200
assert resp.text == "OK"


@pytest.mark.parametrize(
"content_type,body,expected_text",
[
(
"application/json",
b'{"secret_body_key": "top-secret-payload"}',
"MissingRequiredFields",
),
(
"application/cloudevents+json",
b"raw-secret-body",
"Bad Request",
),
],
)
def test_cloud_event_400_does_not_reflect_headers_or_body(
client, content_type, body, expected_text
):
headers = {
"Content-Type": content_type,
"Authorization": "Bearer super-secret-token",
"X-Goog-Iap-Jwt-Assertion": "secret-iap-jwt",
"X-Serverless-Authorization": "Bearer secret-serverless-jwt",
"Cookie": "session=secret-cookie",
"X-Custom-Sensitive": "secret-custom-header",
}
resp = client.post("/", headers=headers, data=body)

assert resp.status_code == 400
assert expected_text in resp.text
for leaked in [
"Got HTTP headers",
"Got data:",
"Authorization",
"super-secret-token",
"X-Goog-Iap-Jwt-Assertion",
"secret-iap-jwt",
"X-Serverless-Authorization",
"secret-serverless-jwt",
"Cookie",
"secret-cookie",
"X-Custom-Sensitive",
"secret-custom-header",
"secret_body_key",
"top-secret-payload",
"raw-secret-body",
]:
assert leaked not in resp.text


def test_cloud_event_400_invalid_specversion_does_not_reflect_headers_or_body(
client,
):
headers = {
"ce-id": "my-id",
"ce-source": "from-galaxy-far-far-away",
"ce-type": "cloud_event.greet.you",
"ce-specversion": "secret-specversion",
"Authorization": "Bearer super-secret-token",
"X-Goog-Iap-Jwt-Assertion": "secret-iap-jwt",
}
resp = client.post(
"/", headers=headers, json={"secret_body_key": "top-secret-payload"}
)

assert resp.status_code == 400
assert "InvalidRequiredFields" in resp.text
for leaked in [
"Got HTTP headers",
"Got data:",
"Authorization",
"super-secret-token",
"X-Goog-Iap-Jwt-Assertion",
"secret-iap-jwt",
"secret-specversion",
"secret_body_key",
"top-secret-payload",
]:
assert leaked not in resp.text
Loading