Return 400 instead of 500 for malformed JSON request bodies #15

Open
opened 2026-11-10 19:47:06 +00:00 by martin · 0 comments
Owner

Summary

Requests with an unparsable JSON body crash the request handler: the backend logs a full traceback at ERROR and returns 500 instead of a 400 InvalidUsage response.

Context

app/helpers/validate_args.py calls Schema.loads(), which delegates to json.loads(). A json.JSONDecodeError is not caught by the ValidationError handler, so it reaches Flask's generic error handler. The behaviour is identical in 0.7.10 and 0.8.1.

Reproduction steps

  1. Send an empty body to an endpoint that uses validate_args:
curl -sS -o /dev/null -w '%{http_code}\n' -X POST https://<host>/api/auth/callback --data ''

Expected behavior

A 400 response, one warning, and no traceback.

Actual behavior

A 500 response and an ERROR log with the full JSON decode traceback. A crawler produced 134 such requests in about 50 seconds on one deployment.

Scope

  • Catch json.JSONDecodeError where validate_args parses the body and raise InvalidUsage.
  • Add a regression test.
  • Decide whether to carry the fix as a patch until upstream releases it.

Acceptance criteria

  • A malformed body produces 400 with the standard "Request invalid" response.
  • The journal shows a warning instead of an ERROR traceback.
  • A test covers the malformed-body case.

Dependencies

  • The proper fix belongs upstream (TomBursch/kitchenowl); link the upstream issue or PR once filed.
## Summary Requests with an unparsable JSON body crash the request handler: the backend logs a full traceback at ERROR and returns 500 instead of a 400 InvalidUsage response. ## Context `app/helpers/validate_args.py` calls `Schema.loads()`, which delegates to `json.loads()`. A `json.JSONDecodeError` is not caught by the `ValidationError` handler, so it reaches Flask's generic error handler. The behaviour is identical in 0.7.10 and 0.8.1. ## Reproduction steps 1. Send an empty body to an endpoint that uses `validate_args`: ```sh curl -sS -o /dev/null -w '%{http_code}\n' -X POST https://<host>/api/auth/callback --data '' ``` ## Expected behavior A 400 response, one warning, and no traceback. ## Actual behavior A 500 response and an ERROR log with the full JSON decode traceback. A crawler produced 134 such requests in about 50 seconds on one deployment. ## Scope - Catch `json.JSONDecodeError` where `validate_args` parses the body and raise `InvalidUsage`. - Add a regression test. - Decide whether to carry the fix as a patch until upstream releases it. ## Acceptance criteria - [ ] A malformed body produces 400 with the standard "Request invalid" response. - [ ] The journal shows a warning instead of an ERROR traceback. - [ ] A test covers the malformed-body case. ## Dependencies - The proper fix belongs upstream (TomBursch/kitchenowl); link the upstream issue or PR once filed.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Personal/kitchenowl-flake#15
No description provided.