diff --git a/src/functions_framework/__init__.py b/src/functions_framework/__init__.py index 31169f4c..5f1d11f0 100644 --- a/src/functions_framework/__init__.py +++ b/src/functions_framework/__init__.py @@ -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" diff --git a/src/functions_framework/aio/__init__.py b/src/functions_framework/aio/__init__.py index a56fe942..65d4355e 100644 --- a/src/functions_framework/aio/__init__.py +++ b/src/functions_framework/aio/__init__.py @@ -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) diff --git a/tests/test_cloud_event_functions.py b/tests/test_cloud_event_functions.py index 2e7c281d..46da9561 100644 --- a/tests/test_cloud_event_functions.py +++ b/tests/test_cloud_event_functions.py @@ -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