kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Coach notifications return 500 for a negative `limit`

Open matthewalberto opened this issue 3 weeks ago • 4 comments

🙂 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=-1 returns 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.

matthewalberto avatar Nov 13 '26 03:11 matthewalberto

🔍 Thanks for opening this issue! @rtibblesbot will check it against the code and comment with its findings.

What happens next:

  • The community-issue-review label 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.

learning-equality-bot avatar Nov 13 '26 03:11 learning-equality-bot

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

  • _limit returns int(limit), or None only on KeyError/ValueError, so -1 passes through.
  • list() line 237: bool(-1) is true, so queryset[-1:].exists() runs.
  • Django 3.2's QuerySet.__getitem__ asserts k.start >= 0, raising AssertionError: Negative indexing is not supported.; nothing catches it, so the response is 500.
  • annotate_queryset slices queryset[:-1] the same way, but line 237 raises first.
  • limit=abc (→ None) and limit=0 (falsy) both skip the slices, as the report states.
  • Not a duplicate: #15826 reports the same failure for max_results in ContentNodeViewset.random, a separate endpoint and handler.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 13 '26 03:11 rtibblesbot

I've read the contributing guidelines, including Using generative AI. Could I be assigned to this? The fix and a test are ready.

matthewalberto avatar Nov 13 '26 03:11 matthewalberto

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.

rtibbles avatar Nov 15 '26 19:11 rtibbles