Data Portal registration returns 500 for a missing, malformed or nonexistent facility_id
🙂 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/:
{}(nofacility_id) raisesFacility.DoesNotExist{"facility_id": "abc"}raisesValueError: 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.
🔍 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/core/api.py:81 on develop at da32925bce
KolibriDataPortalViewSet.registercallsFacility.objects.get(id=request.data.get("facility_id"))before itstry, which only catchesNetworkLocationResponseFailureandNetworkClientError.- The viewset sets no
permission_classes, andREST_FRAMEWORKinkolibri/deployment/default/settings/base.pysets noDEFAULT_PERMISSION_CLASSES, so DRF'sAllowAnyapplies. custom_exception_handleronly reformats exceptions DRF already handles.DoesNotExistandValueErrorare not among them, so both surface as 500s.- An anonymous
APIClientPOST 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) turnsTypeError,ValueErrorandValidationErrorintoHttp404.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.
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.
Thanks for confirming — noted, I'll wait for the team to assign an issue before opening a PR in future.