Skip to content

[Bug]: LineItemsModule raises 500 on stale or invalid line item ids in edit and delete flows #5734

@moksha-hub

Description

@moksha-hub

Description

esp/esp/program/modules/handlers/lineitemsmodule.py uses direct .objects.get(...) calls on request-supplied IDs in several edit/delete/import paths without graceful handling for invalid or stale IDs.

Why this matters

LineItemsModule is an admin-facing accounting/configuration workflow. If an item has been deleted, an ID is stale, or the request is tampered with, the module should show a recoverable error rather than crashing.

Current flows include patterns such as:

  • LineItemType.objects.get(id=request.GET['id'])
  • LineItemType.objects.get(id=request.POST['id'])
  • Program.objects.get(id=request.POST['program'])

with no surrounding DoesNotExist or parsing guards.

Evidence

Relevant code in esp/esp/program/modules/handlers/lineitemsmodule.py:

  • edit flow:
    LineItemType.objects.get(id=request.GET['id'])
  • delete confirmation:
    LineItemType.objects.get(id=request.GET['id'])
  • confirmed deletion:
    LineItemType.objects.get(id=request.POST['id'])
  • import confirmation:
    Program.objects.get(id=request.POST['program'])

These request-derived lookups are performed directly with no defensive handling.

Steps to Reproduce

  1. Open the Line Items management UI.
  2. Trigger an edit, delete, or import path using an invalid, deleted, or tampered ID in the request.
  3. Observe that the handler performs direct .objects.get(...) lookups from request parameters.
  4. If the referenced object does not exist, the flow can raise an unhandled exception.

Expected Behavior

The module should detect invalid IDs and return a clear admin-facing error, without crashing the entire page.

Actual Behavior

Stale or invalid IDs can lead to DoesNotExist exceptions and 500 responses.

Suggested Fix

Wrap request-derived object lookups in validation and exception handling:

  • catch LineItemType.DoesNotExist
  • catch Program.DoesNotExist
  • catch ValueError where IDs are parsed
  • return a clean ESPError instead of a raw exception

Additional Context

I checked the current issue tracker with searches around lineitemsmodule, LineItemType.objects.get, and line-item edit/delete crashes. I only found the test-coverage issue #5210, not an existing bug issue for this invalid-ID crash path.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions