mbox series

[layerindex-web,0/4] Return 400 or 404 instead of server errors for bad URLs

Message ID 20261116230401.42917-1-tim.orling@konsulko.com
Headers show
Series Return 400 or 404 instead of server errors for bad URLs | expand

Message

Tim Orling Nov. 16, 2026, 11:03 p.m. UTC
Fuzzing the URLs of a test instance loaded with a copy of the production
database found about 160 requests per pass that gave a 500 and about 350
that gave a 502, from a handful of causes. These fix four of them.

1: Bug 16517: an unknown field in an API "filter" or "orderby" raised a
   bare BaseException, which killed the worker; a value of the wrong
   type, such as filter=id:abc, raised ValueError. Both are now a 400.
   In Piotr Buliński's analysis of the production access logs from July
   to November, 909 of the 912 API server errors were malformed filters.
2: Bug 16518: the API never applied "orderby" at all.
3: Bug 16519: a non-numeric id in a URL, or in branch_comparison's
   from_branch and to_branch, gave a 500; it is now a 404.
4: Bug 16520: a NUL character in the URL gave a 500 on PostgreSQL. It is
   now a 400 on every database.

With these, the fuzz runs give no 500s on PostgreSQL and one on MariaDB,
the 4-byte emoji case of bug 16507, which these don't address. The 502s
that remain are 5000-character URLs, which uWSGI rejects with its default
4 KB buffer; gunicorn returns a 400 for them.

Each fix comes with tests that fail without it. This applies on
master-next, where "layerindex: add an RSS feed of recently published
layers" changed the neighbouring line of layerindex/urls.py.

Tim Orling (4):
  layerindex/querysethelper.py: reject an invalid API filter or orderby
    with a 400
  layerindex/querysethelper.py: apply the API "orderby" parameter
  layerindex: give a 404 for a non-numeric id in a URL
  layerindex/middleware.py: reject NUL characters in the URL with a 400

 docker/settings.py            |  1 +
 layerindex/middleware.py      | 21 +++++++++++-
 layerindex/querysethelper.py  | 41 +++++++++++++++++++----
 layerindex/urls.py            | 18 +++++-----
 layerindex/views.py           | 32 +++++++++---------
 settings.py                   |  1 +
 tests/test_nonnumeric_ids.py  | 51 ++++++++++++++++++++++++++++
 tests/test_null_characters.py | 45 +++++++++++++++++++++++++
 tests/test_restapi.py         | 62 +++++++++++++++++++++++++++++++++++
 9 files changed, 240 insertions(+), 32 deletions(-)
 create mode 100644 tests/test_nonnumeric_ids.py
 create mode 100644 tests/test_null_characters.py