Coach notifications return 500 for a negative `limit`
🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:
- Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that don’t follow the guidelines won’t be answered.
- Confirm that you’ve read the guidelines in your comment.
Summary
ClassroomNotificationsViewset._limit (kolibri/plugins/coach/viewsets/classroom_notifications.py) turns the
limit query parameter into an int and ignores it when it is not numeric, but it lets a negative number through.
list() then slices the queryset with it (queryset[self._limit :].exists(), and queryset[: self._limit] in
annotate_queryset), and Django rejects a negative slice, so the request returns 500.
Steps to reproduce
On develop at fe298f5449, logged in as a coach of the classroom:
GET /coach/api/notifications/?classroom_id=<id>&limit=-1returns 500
with:
File "kolibri/plugins/coach/viewsets/classroom_notifications.py", line 237, in list
more_results = bool(self._limit) and queryset[self._limit :].exists()
AssertionError: Negative indexing is not supported.
For comparison, limit=abc and limit=0 return 200 with the limit ignored.
Expected behaviour
A limit that isn't an integer from 1 to 100 returns 400 naming the field. A request without limit behaves as it does today.
I'd like to work on this; I have a fix with a test ready.
🔍 Thanks for opening this issue! @rtibblesbot will check it against the code and comment with its findings.
What happens next:
- The
community-issue-reviewlabel stays on until the review comment is posted. - The comment identifies the files, lines, or commits examined so you can follow the reasoning and respond if you disagree.
- If the check confirms the issue, @rtibblesbot may also correct the title and description. GitHub preserves the edit history, so the original text remains available.
- @rtibblesbot's comments are LLM-generated, so please evaluate them critically.
A maintainer decides what happens next. This check does not close the issue.
Verdict: claim holds
Evidence: kolibri/plugins/coach/viewsets/classroom_notifications.py:131-143,237 on develop at fe298f5449; Django 3.2.25 (pinned in pyproject.toml), django/db/models/query.py:294-297
_limitreturnsint(limit), orNoneonly onKeyError/ValueError, so-1passes through.list()line 237:bool(-1)is true, soqueryset[-1:].exists()runs.- Django 3.2's
QuerySet.__getitem__assertsk.start >= 0, raisingAssertionError: Negative indexing is not supported.; nothing catches it, so the response is 500. annotate_querysetslicesqueryset[:-1]the same way, but line 237 raises first.limit=abc(→None) andlimit=0(falsy) both skip the slices, as the report states.- Not a duplicate: #15826 reports the same failure for
max_resultsinContentNodeViewset.random, a separate endpoint and handler.
Written by rtibblesbot, an LLM-based coding agent.
I've read the contributing guidelines, including Using generative AI. Could I be assigned to this? The fix and a test are ready.
Updated Expected behaviour to follow the validation standard in #15838. A non-numeric, zero, negative or over-100 limit now returns 400 instead of being ignored. The coach notifications page sends limit=25.