kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Return semantic API responses for malformed API requests

Open rtibbles opened this issue 2 weeks ago • 2 comments

Overview

Kolibri's API endpoints return a 500 for many malformed requests, such as a non-UUID id, a non-integer query parameter, or a request body that isn't a JSON object. Recent issues reported these one endpoint at a time (#15728, #15790, #15797, #15806, #15826, #15829, #15835), and each fix used a different validation approach. This tracking issue sets one approach for validating API request input, so endpoints return a 400 for invalid input and a 404 for an object that doesn't exist.

Scope

  • Included: DRF endpoints and API-like Django views (file_checksums, the facility CSV export views, set_language) that return a 500 for malformed request input.
  • Included: fixes in the shared API code (the DRF exception handler, KolibriAuthPermissions, pagination, morango's UUIDField), the per-endpoint fixes still needed after them, a regression test across every API route, and the validation standard in docs/backend_architecture/api_patterns.rst.
  • Excluded: zip content serving, management commands, and API bugs not caused by unvalidated input, such as #15797.

Guidance

Every fix under this tracker follows this standard:

  • Query parameters are declared on a FilterSet with typed filters, as in FacilityUserFilter. Query parameters that don't filter, such as a result count, are declared on a Serializer that validates request.query_params.
  • Request bodies are validated by a Serializer with is_valid(raise_exception=True), as in StartSessionSerializer. Views don't read request.data directly.
  • Ids from the URL are looked up with get_object() or serialize_object(), never .get(pk=...) on the raw value.
  • UUIDs in request input use HexOnlyUUIDField.
  • A malformed, out-of-range or unknown id in the URL path returns 404. Malformed or out-of-range query parameters and body fields return 400 naming the field.
  • Every fix adds a test that sends the malformed input and asserts the 400 or 404.

Strategy

  • The shared API code fixes land first. They turn many 500s into 400s or 404s without changes to individual endpoints.
  • Each per-endpoint sub-issue lists only the endpoints that still return a 500 with the shared fixes in place, and is blocked by the shared fixes it depends on.
  • The morango UUIDField fix ships in a morango release, then Kolibri upgrades morango.
  • The route-wide regression test lands early with an ignore list of the routes that still fail, and each per-endpoint fix removes its routes from that list.

AI usage

I had Claude Code sweep Kolibri's API code for unvalidated request input and reproduce each 500 with a test. I chose the validation standard, the scope and the split into sub-issues. Claude Code drafted the sections with me.

rtibbles avatar Nov 15 '26 19:11 rtibbles

/assign

ArpitaSamanta10 avatar Nov 28 '26 04:11 ArpitaSamanta10

Hi! 👋

Thanks so much for your interest! This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added.

We really appreciate your willingness to help. 😊

Are you preparing for Google Summer of Code? See our GSoC guidelines.

learning-equality-bot avatar Nov 28 '26 04:11 learning-equality-bot