Skip to content

[Bug]: ResourceModule raises 500 on stale or invalid ids in edit/delete flows #5744

@moksha-hub

Description

@moksha-hub

[Bug]: ResourceModule raises 500 on stale or invalid ids in edit/delete flows

esp/esp/program/modules/handlers/resourcemodule.py performs several edit/delete admin flows by calling .objects.get(id=request.GET['id']) directly, without guarding against missing, stale, or invalid ids.

This affects multiple handler paths in the same module:

  • resources_timeslot()
  • resources_restype()
  • resources_classroom()
  • resources_equipment()

Relevant code paths include direct lookups such as:

  • Event.objects.get(id=request.GET['id'])
  • ResourceType.objects.get(id=request.GET['id'])
  • Resource.objects.get(id=request.GET['id'])

Why this matters

These are admin-facing edit/delete workflows. If an id is stale, tampered with, or simply points to a deleted object, the handler raises DoesNotExist and returns a 500 instead of failing gracefully.

That makes the resource-management UI fragile in normal real-world situations such as:

  • bookmarked old admin links
  • two admins working concurrently
  • deleting an item and later revisiting its edit/delete URL
  • manually modified query parameters

Steps to Reproduce

  1. Open esp/esp/program/modules/handlers/resourcemodule.py.
  2. Locate these branches:
    • resources_timeslot() with op=edit / op=delete
    • resources_restype() with op=edit / op=delete
    • resources_classroom() with op=edit / op=delete
    • resources_equipment() with op=edit / op=delete
  3. Note that each branch dereferences request.GET['id'] via .objects.get(...) without any try/except, .filter().first(), or validation guard.
  4. Request one of those views with a missing/deleted/nonexistent id.

Expected Behavior

The handler should fail gracefully, for example by:

  • showing an error message
  • redirecting back to the resource-management page
  • returning a 404 or other controlled response

Actual Behavior

The handler can raise Event.DoesNotExist, ResourceType.DoesNotExist, or Resource.DoesNotExist, which surfaces as a 500.

Impact

A stale link in the admin UI can crash core resource-management pages for timeslots, resource types, classrooms, and equipment. Because the same pattern appears in several flows in one module, this is broader than a single broken endpoint.

Suggested Fix

Guard these lookups before using them, for example by:

  • validating that id is present and integer-like
  • using filter(...).first() where appropriate
  • catching DoesNotExist and returning a controlled error/redirect
  • centralizing the lookup/error handling so the four affected flows behave consistently

Additional Context

I checked the current tracker for existing issues covering ResourceModule stale/invalid id crashes across these edit/delete flows and did not find an existing open issue matching this behavior.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions