Return 4xx instead of 500 for malformed API requests

Aperta
#6,387 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
20/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
django, python
Ambito
api, backend

Direzione di ricerca

This umbrella issue covers malformed input across DRF viewsets, /api/sync/, plain Django views, kolibri_public, and search; it names shared patterns and regression tests but leaves the individual root causes to sub-issues. Start by checking whether a scoped sub-issue exists, then read its cited view or serializer and reproduce the malformed request in a regression test. Done means that input returns an appropriate 400 or 404, with a test covering it; the issue says contributions are not open here.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

DEV: backend

❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.

Overview

Studio's API returns HTTP 500 for many malformed requests: a body with the wrong JSON shape, a query parameter of the wrong type, a value out of range, or a malformed id. In every case the request is the client's error and should get a 400 or 404 that says what is wrong. An audit of every endpoint found the remaining cases and grouped them by root cause. Each sub-issue fixes one root cause wherever it occurs, using the validation pattern set out below.

Scope
  • Included: validation of request input (body, query params, URL kwargs) across DRF viewsets, /api/sync/, plain Django views, and the public kolibri_public and search APIs.
  • Included: sync change handlers that error on malformed changes instead of returning a validation error for that change.
  • Excluded: the /api/internal/* endpoints used by ricecooker, which are to be deprecated and removed in favour of the sync endpoint.
  • Excluded: permission checks and data-integrity bugs, which have their own issues.
Guidance
  • Target hotfixes.
  • Each fix needs a regression test that sends the malformed input and asserts the 4xx. In tests, the DRF test client re-raises view exceptions, so the current behaviour shows up as an exception rather than a 500 status.
Strategy
  • Every request body is validated by a DRF serializer with is_valid(raise_exception=True). Plain Django views that parse request.body become DRF @api_view views.
  • Every query parameter is a typed django-filter filter on the viewset's FilterSet, such as UUIDFilter/UUIDInFilter from contentcuration/contentcuration/viewsets/common.py, NumberFilter, BooleanFilter or ChoiceFilter. A CharFilter whose value is parsed by hand doesn't count.
  • Serializer fields carry the model's limits (max_length, min/max value, choices), so a value that passes validation also saves to the database.
  • One custom DRF EXCEPTION_HANDLER maps Django ValidationError to 400 and ObjectDoesNotExist to 404, as a backstop.
  • Studio's ValuesViewset (contentcuration/contentcuration/viewsets/base.py) is an older copy of Kolibri's kolibri/core/api.py. Fixes there follow Kolibri's shape.

AI usage

I scoped the audit, excluded the internal endpoints, and chose the validation strategy. Claude Code audited every endpoint, reproduced each finding with a temporary test, grouped the findings by root cause, and drafted this issue.

Lingua principale
Python
Stelle
212
Fork
321
Merge medio
1g 7h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di learningequality/studio

Tutte le issue di learningequality/studio

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.