kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Data Portal registration returns 500 for a missing, malformed or nonexistent facility_id

Open sanchit955 opened this issue 1 month 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

The Data Portal register action in KolibriDataPortalViewSet (kolibri/core/api.py) looks the facility up with Facility.objects.get(id=request.data.get("facility_id")) outside its try block. A missing, malformed or nonexistent facility_id therefore raises Facility.DoesNotExist / ValueError and returns a 500. The endpoint sets no permission classes, so it is reachable without an account and any client can trigger it.

Steps to reproduce

Post anonymously to /api/auth/portal/register/:

  • {} (no facility_id) raises Facility.DoesNotExist
  • {"facility_id": "abc"} raises ValueError: badly formed hexadecimal UUID string
  • a well-formed but nonexistent id raises Facility.DoesNotExist

Each is an unhandled exception, logged as Internal Server Error: /api/auth/portal/register/.

Expected behaviour

All three cases should return 404 — the facility being registered against does not exist or was not identified — without reaching the registration call. This matches how the channel thumbnail view handles an unparseable channel id.

Suggested fix

Look the facility up with DRF's get_object_or_404 (not Django's: only DRF's converts the malformed-id ValueError into a 404, as well as ObjectDoesNotExist and ValidationError).

I have a fix ready and will open a PR for this issue.

sanchit955 avatar Nov 03 '26 05: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 03 '26 05:11 learning-equality-bot

Verdict: claim holds Evidence: kolibri/core/api.py:81 on develop at da32925bce

  • KolibriDataPortalViewSet.register calls Facility.objects.get(id=request.data.get("facility_id")) before its try, which only catches NetworkLocationResponseFailure and NetworkClientError.
  • The viewset sets no permission_classes, and REST_FRAMEWORK in kolibri/deployment/default/settings/base.py sets no DEFAULT_PERMISSION_CLASSES, so DRF's AllowAny applies.
  • custom_exception_handler only reformats exceptions DRF already handles. DoesNotExist and ValueError are not among them, so both surface as 500s.
  • An anonymous APIClient POST to /api/auth/portal/register/ raises:
    • {}: Facility.DoesNotExist
    • {"facility_id": "abc"}: ValueError: badly formed hexadecimal UUID string
    • a well-formed, nonexistent id: Facility.DoesNotExist
  • DRF's get_object_or_404 (rest_framework/generics.py:13) turns TypeError, ValueError and ValidationError into Http404. ChannelThumbnailView (kolibri/core/content/views.py:90) already uses it for the same reason.
  • No other open issue, or issue closed in the last 30 days, reports this.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 03 '26 15:11 rtibblesbot

Thanks, this is valid. We deliberately limit the number of help wanted issues so we can review contributor PRs in a timely fashion. In future, please wait for our team to assign you an issue before opening a PR.

rtibbles avatar Nov 07 '26 00:11 rtibbles

Thanks for confirming — noted, I'll wait for the team to assign an issue before opening a PR in future.

sanchit955 avatar Nov 07 '26 02:11 sanchit955