Source: https://frameworkrecipes.dev/problems/authorization-at-the-write/

[Agent repair workflows by failure](https://frameworkrecipes.dev/problems/)

# Keep authorization inside the database write

Keep the authenticated tenant or owner in the database operation that reads or changes the record. Save that requirement beside the code and check it after a refactor, including edits that leave a route-level permission check intact.

## How do I keep an agent from removing tenant authorization?

Choose the implementation for your framework. Each linked guide includes the source, integration prerequisites, failure-path tests, saved requirement and recorded check.

Existing captured source checks; these are individual examples

| Implementation | Saved requirement | Broken source | Corrected source | Capture date |
| --- | --- | --- | --- | --- |
| [Preserve tenant authorization inside an atomic FastAPI update](https://frameworkrecipes.dev/fastapi/tenant-cancellation/) | tenant-scoped-update | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/fastapi/tenant-cancellation/capture.json) |
| [Keep Django cancellation writes scoped to the authenticated tenant](https://frameworkrecipes.dev/django/scoped-cancellation/) | tenant-scoped-lock | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/django/scoped-cancellation/capture.json) |
| [Keep Axum’s verified tenant inside the SQLx order update](https://frameworkrecipes.dev/axum/tenant-conditional-write/) | tenant-is-in-update | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/axum/tenant-conditional-write/capture.json) |
| [Authorize a SvelteKit form action before its conditional write](https://frameworkrecipes.dev/sveltekit/server-action-authorization/) | action-tenant-predicate | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/sveltekit/server-action-authorization/capture.json) |

## Inspect the requirement before installing it

The [Preserve tenant authorization inside an atomic FastAPI update](https://frameworkrecipes.dev/fastapi/tenant-cancellation/) example uses this requirement on `orders.py`:

```
- name: tenant-scoped-update
  where: "orders.py"
  ensure: >-
    The cancellation UPDATE must constrain tenant_id to actor.tenant_id before changing the order. An id-only or owner-only update breaks this requirement.
```

[Exact corrected source and broken edit](https://frameworkrecipes.dev/fastapi/tenant-cancellation/source.json) · [Recorded Perch output](https://frameworkrecipes.dev/fastapi/tenant-cancellation/capture.json) · [Download this requirement](https://reporules.fyi/fastapi/tenant-cancellation/rule-pack.zip)

## Make the agent check the requirement after its edit

Keep your existing perch.yaml. The [scoped rule pack](https://reporules.fyi/fastapi/tenant-cancellation/) installs the saved requirement under .perch/rules/. Review its file selector and use the implementation’s integration prerequisites before adapting it to your application.

### Install Perch and configure your agent

```
npm install -g @lakeday/perch
perch login
# Choose the setup command for your agent:
perch setup claude-code
# Or: perch setup codex
# Or: perch setup cursor
```

### Check the edit and recheck the repair

```
curl -fL 'https://reporules.fyi/fastapi/tenant-cancellation/rule-pack.zip' -o requirement.zip
unzip -n requirement.zip
perch check orders.py --rules tenant-scoped-update --json
# Read the finding, repair the implementation, then repeat:
perch check orders.py --rules tenant-scoped-update --json
```

Run the same check on the repaired working file before committing. Keep the application regression tests alongside it. Fresh model judgments can vary; an unchanged request can reuse its cached answer. A passing check does not prove application correctness.

## Compare the implementations before choosing a rule

- **[Preserve tenant authorization inside an atomic FastAPI update](https://frameworkrecipes.dev/fastapi/tenant-cancellation/)**

  A read-then-write authorization check has two boundaries: who can see the row, and whether the write still applies the same scope. Put tenant, owner, and pending status into the UPDATE itself. A concurrent cancellation can then change at most one pending row. [Inspect its rule pack](https://reporules.fyi/fastapi/tenant-cancellation/).
- **[Keep Django cancellation writes scoped to the authenticated tenant](https://frameworkrecipes.dev/django/scoped-cancellation/)**

  A locked row prevents concurrent state changes while this transaction decides whether cancellation is allowed. The tenant restriction belongs in the lookup that acquires the row; a lock by itself is not an access check. [Inspect its rule pack](https://reporules.fyi/django/scoped-cancellation/).
- **[Keep Axum’s verified tenant inside the SQLx order update](https://frameworkrecipes.dev/axum/tenant-conditional-write/)**

  Middleware can authenticate an Axum request while the handler still writes globally. Extract the verified actor and include tenant and owner in the SQL statement that changes state. The affected-row check rejects inaccessible or already transitioned rows. [Inspect its rule pack](https://reporules.fyi/axum/tenant-conditional-write/).
- **[Authorize a SvelteKit form action before its conditional write](https://frameworkrecipes.dev/sveltekit/server-action-authorization/)**

  A protected page load does not protect its form action. Server actions must enforce their own identity and resource scope. Passing the verified tenant into the conditional mutation makes that boundary visible to both the code reviewer and the rule. [Inspect its rule pack](https://reporules.fyi/sveltekit/server-action-authorization/).

## Keep the check tied to its source

The table reports saved checks on the exact linked source versions. Adapt the paths, adapters and regression tests to your repository. The example source may differ from the released framework or your service; the recorded outcome belongs to that example.

[Other agent-edit failure paths](https://frameworkrecipes.dev/problems/) · [All framework recipes](https://frameworkrecipes.dev/)

**Framework Recipes**

Implementation, failure path, check.

Published by Perch · © 2026 Verglas LLC. Framework Recipes is a Perch publication.

Perch’s MIT license covers the CLI; hosted model analysis is a paid service. Relevant source is sent to the configured model service.

[Perch CLI MIT license](https://github.com/lakeday-org/perch/blob/main/LICENSE) · [Article text](https://frameworkrecipes.dev/problems/authorization-at-the-write/index.md) · [Resource index](https://frameworkrecipes.dev/llms.txt)

[Privacy](https://frameworkrecipes.dev/privacy/) ·
