Skip to content

fix(views): enforce authz, row scoping and close query oracles - #590

Merged
dodjer88 merged 4 commits into
developfrom
fix/v058-authz-hardening
Oct 9, 2026
Merged

dodjer88 merged 4 commits into
developfrom
fix/v058-authz-hardening

Conversation

@yevheniidehtiar

Copy link
Copy Markdown
Owner

Why

Several admin handlers had live security gaps (Owner decision 4: standalone fix(views) work, not gated by the SDD):

  • Inline cell edit/save, inline add-row, the update form, file delete and single actions skipped the model or object permission checks.
  • get_queryset row scoping lived on the shared adapter instance, so it was skipped on several handlers and could leak between concurrent requests.
  • DELETE /{model}/{id}/file/{field} could delete any server file, through a text column or a path outside the storage root.
  • Inline formsets trusted a submitted child pk (IDOR and reparenting).
  • Search, sort, URL filters and the FK choices endpoint could each be used to read a secret column such as password_hash, and the detail and edit pages showed it directly.

Spec: docs/specs/bring-your-own-app.md sections B.6, A.3, A.6, D.2, D.3 and Owner decision 7. Stories: st-v058-byoa-10 and st-v058-byoa-53.

Changes

st-v058-byoa-10: authorization and row scoping

  • Row scoping is request-scoped. core/adapters.queryset_scope holds it in a ContextVar, and every DynamicModelView handler runs inside it.
  • Model and object permission checks were added to inline edit/save (change), inline add-row (add or change), the update form, delete-file, upload, and single actions (action_<name>, after loading the row under the scope).
  • Bulk actions load each row under the request scope.
  • Delete-file and upload accept only file fields. Every storage path goes through the new core/storage_paths.resolve_storage_path, which refuses absolute paths, .. and symlink escapes.
  • Inline formsets: a child pk that is not an existing child of the parent returns 404 before anything is written. The inline model's own add/change/delete permissions are enforced. Primary and foreign keys are never rewritten on update.
  • The choices endpoint requires view on the target model as well.

st-v058-byoa-53: query oracles

  • New core/sensitive.py and AdminOptions.sensitive_fields (overrides either way). A field is sensitive if it carries the marker or its name matches (^|_)(password|secret|token|hash)(_|$). User.password_hash is marked.
  • Inferred search, list columns and filters skip sensitive fields, and so do the adapters' fallback search and the choices q search.
  • URL filters are honoured only for fields in list_filter. sort_by is limited to displayed, non-sensitive columns; anything else falls back to the default sort with a 200.
  • The detail view hides sensitive fields. Forms treat them as write-only: an empty submission keeps the stored value.
  • The choices endpoint forwards only declared cascade keys and applies the target admin's get_queryset.
  • CHANGELOG security note (also in docs/changelog.md), with an upgrade note for columns such as secret_name or token_count that now count as sensitive.

Verification

  • uv run poe lint: passed.
  • uv run pytest tests -o addopts="" -q -n 4 --ignore=tests/e2e: 996 passed.
  • uv run pytest tests/e2e -o addopts="" -q -n 4: 136 passed.
  • Regression tests in tests/unit/test_dynamic_authz_gaps.py and tests/unit/test_query_oracles.py were run against the unfixed code: 22 of 27 and 18 of 36 fail. Every vulnerability test is among the failures; the rest are positive controls.

Manual test scenarios

  1. As a user with only view_<model>, POST an inline cell save: expect 403 and no change.
  2. With a ModelAdmin.get_queryset that scopes by tenant, open, inline-edit and run an action on another tenant's row: expect 404, and the action does not run.
  3. Bulk-run an action with one visible and one hidden id: the hidden row is reported "not found" and the handler runs only for the visible one.
  4. DELETE /admin/<model>/<id>/file/<text field>: expect 404, and the field and any file it names are untouched.
  5. In an Order edit form, change an inline row's hidden pk to another order's line id and save: expect 404, and the other line is unchanged.
  6. Open /admin/user?sort_by=password_hash, ?sort_by=nope, ?search=<part of a hash> and ?filter_password_hash=<hash>: expect the default order and no leaked matches, all with status 200.
  7. Open a User detail page and its edit form: no password hash is shown. Save the edit form with the hash field empty: the stored hash is kept.
  8. GET /admin/<model>/choices/<fk>?password_hash=x: the parameter is ignored. With a target admin that hides inactive rows, inactive rows are not offered. Without view_<target>, expect 403.

🤖 Generated with Claude Code

Files (all under /Users/yevheniidehtiar/.projects/hyper-admin/.claude/worktrees/wf_0411676f-73e-2):

  • New: src/hyperadmin/core/storage_paths.py, src/hyperadmin/core/sensitive.py, src/hyperadmin/adapters/_search.py, tests/unit/test_dynamic_authz_gaps.py, tests/unit/test_query_oracles.py
  • Changed: src/hyperadmin/views/dynamic.py, src/hyperadmin/views/forms.py, src/hyperadmin/core/adapters.py, src/hyperadmin/core/introspection.py, src/hyperadmin/core/options.py, src/hyperadmin/adapters/sqlmodel.py, src/hyperadmin/adapters/sqlalchemy.py, src/hyperadmin/auth/models.py, src/hyperadmin/routing.py, CHANGELOG.md, docs/changelog.md

@yevheniidehtiar
yevheniidehtiar force-pushed the fix/v058-authz-hardening branch from f97748f to 5a58792 Compare October 9, 2026 15:24
Claude Code added 4 commits October 9, 2026 23:54
…, file and choices handlers (st-v058-byoa-10)

- request-scoped queryset filter held in a ContextVar (core/adapters.queryset_scope); every DynamicModelView handler runs inside it
- model + object permission checks on inline edit/save, inline add-row, update form, delete-file, upload and single actions
- delete-file/upload accept file fields only; storage paths resolved through core/storage_paths.resolve_storage_path (no escape from the storage root)
- inline formsets reject child pks not owned by the parent (404 before any write) and enforce the inline model's add/change/delete permissions
- choices endpoint requires view on the target model
…d choices (st-v058-byoa-53)

- core/sensitive.is_sensitive + AdminOptions.sensitive_fields overrides; User.password_hash marked
- inferred search/columns/filters, adapter fallback search and choices q search skip sensitive fields
- URL filters whitelisted to list_filter; sort_by whitelisted to displayed non-sensitive columns (unknown -> default sort, 200)
- detail view hides sensitive fields; forms treat them as write-only (empty keeps stored value)
- choices endpoint forwards only declared cascade keys and applies the target admin get_queryset
- changelog security note
…byoa-10)

- Gate every disclosure of related rows on view_<target> through one helper:
  choices endpoint, list-page relation filters and create/edit relation
  widgets (only the selected key is kept, labelled Model (pk)).
- Unregistered inline models and relation targets fall back to the parent or
  source permission, since no permission row can exist for them.
- Uploads ignore the client path, never overwrite a stored file, stay inside
  the storage root and return a relative name.
- Document the inline_child_pks requirement for custom adapters.
…v058-byoa-53)

- Label related rows with a sensitive-aware display name, never SQLModel's
  default __str__, in choices, FK filters, relation widgets and list cells.
- Apply each model admin's sensitive_fields overrides inside adapters
  (fallback search, choices q, cascade guard) via a request-scoped resolver;
  an explicit empty search_fields disables search.
- Inline rows treat sensitive child fields as write-only.
- Limit the name heuristic to text fields; marked non-text fields are left
  out of the edit form and keep their stored value.
- Refuse FK writes to rows the target admin's get_queryset hides.
- Document the SQLModel-compatible sensitive marker.
@yevheniidehtiar
yevheniidehtiar force-pushed the fix/v058-authz-hardening branch from 5a58792 to 0dd8a35 Compare October 9, 2026 21:59
@dodjer88
dodjer88 merged commit da27d93 into develop Oct 9, 2026
8 checks passed
@dodjer88
dodjer88 deleted the fix/v058-authz-hardening branch October 9, 2026 22:47
dodjer88 pushed a commit that referenced this pull request Oct 11, 2026
Done (#590, #591, #592): st-v058-byoa-10..14, 16..20, 53.
In progress (wave 1): 15, 21, 23, 25, 33, 34.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants