kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Content node tree endpoints return 500 for invalid depth and next__gt query parameters

Open sanchit955 opened this issue 2 weeks ago โ€ข 6 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

Requesting a content node tree with an invalid depth or next__gt query parameter returns 500 instead of 400.

TreeQueryMixin.validate_and_return_params (kolibri/core/content/viewsets/contentnode/tree.py) raises Django's ValidationError for out-of-range or unparseable values, but DRF's exception handler does not convert a Django ValidationError into a 400 response, so the request dies with an unhandled exception. A next__gt above SQLite's 64-bit integer range passes validation and raises OverflowError in the query.

Steps to reproduce

GET any of /api/content/contentnode_tree/<id>/, /api/public/v2/contentnode_tree/<id>/ or /api/content/contentnodeprogress/<id>/tree/ with any of:

  • ?depth=abc, ?depth=1.5, ?depth=0, ?depth=3 (valid depth is 1 or 2)
  • ?next__gt=abc, ?next__gt=1.5, ?next__gt=0, ?next__gt=-1, ?next__gt=99999999999999999999 (must be a positive integer if specified)

Expected

400 naming the invalid field (depth or next__gt), and the current behavior for valid values.

Actual

500 (Internal Server Error logged). Verified: every invalid value above returns 500, while ?depth=1 returns 200.

Possible fix

Replace validate_and_return_params with a Serializer that validates request.query_params, as #15838's Guidance describes for query parameters that don't filter: depth = IntegerField(min_value=1, max_value=2, default=2) and next__gt = IntegerField(min_value=1, max_value=2147483647, required=False). The maximum is the safe range of lft's PositiveIntegerField.

sanchit955 avatar Nov 14 '26 09:11 sanchit955

๐Ÿ” 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 14 '26 09:11 learning-equality-bot

Verdict: claim holds Evidence: kolibri/core/content/viewsets/contentnode/tree.py:4, :45, :55 on develop at fe298f5449

  • TreeQueryMixin.validate_and_return_params raises django.core.exceptions.ValidationError for a bad depth or next__gt; DRF's handler (wrapped by custom_exception_handler) does not convert it, so the request 500s.
  • A test-client run at fe298f5449 returned 500 for all eight invalid values listed in the issue, and 200 for ?depth=1, on each route that uses the mixin:
    • /api/content/contentnode_tree/<id>/
    • /api/public/v2/contentnode_tree/<id>/
    • /api/content/contentnodeprogress/<id>/tree/
  • The URL in the issue, /api/content/contentnode/<id>/tree/, does not exist: ContentNodeViewset has no tree action. The steps now list the three routes above.
  • Not a duplicate: #15826 (random's max_results) and #15829 (notifications' limit) are other endpoints.

Changes

  • Steps to reproduce: replaced /api/content/contentnode/<id>/tree/ with the three routes that use TreeQueryMixin. The original route does not exist (ContentNodeViewset has no tree action), and all three return 500 for every listed value.
  • Title: "endpoint returns" โ†’ "endpoints return", because the steps now name three routes.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 14 '26 09:11 rtibblesbot

Hi! ๐Ÿ‘‹

This issue is available for contribution and supports self-assignment. Here's how to get started:

  • Comment /assign to assign yourself to this issue
  • You can have up to 2 issues assigned at a time across all community repos
  • Dropping an issue has a 7-day cooldown before the slot opens up
  • Link your pull request to this issue when you submit it

๐Ÿ“– Read the Contributing guidelines before starting.

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

learning-equality-bot avatar Nov 15 '26 21:11 learning-equality-bot

/assign

Sanjays2402 avatar Nov 15 '26 23:11 Sanjays2402

Hi! ๐Ÿ‘‹

You've been assigned to this issue. Here's what to do next:

  • Read the issue description carefully and make sure you understand the requirements
  • Link your pull request to this issue when you submit it
  • If you can no longer work on this, unassign yourself so others can pick it up

Good luck! ๐Ÿ˜Š

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

learning-equality-bot avatar Nov 15 '26 23:11 learning-equality-bot

Updated the expected behavior and possible fix to follow #15838: validate depth and next__gt with a Serializer on request.query_params, so the 400 names the field. Also added ?next__gt=99999999999999999999, which passes the current check and returns 500 from an SQLite OverflowError.

rtibbles avatar Nov 17 '26 22:11 rtibbles