Skip to content

fix(mutation): apply the auth context to table mutations (closes #73)#75

Merged
milmazz merged 1 commit into
mainfrom
feat/mutation-auth-context
Jul 14, 2026
Merged

fix(mutation): apply the auth context to table mutations (closes #73)#75
milmazz merged 1 commit into
mainfrom
feat/mutation-auth-context

Conversation

@milmazz

@milmazz milmazz commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Closes #73.

What & why

Table mutations (POST/PATCH/PUT/DELETE) ran with no per-request auth context — no SET LOCAL ROLE, no request.* GUCs, no db-pre-request. Reads (query_executor.ex) and RPC (rpc.ex) already wrap their DB work in Bier.Auth.with_context/4, but mutation.ex did not, so the JWT role, db-anon-role, db-pre-request, and role-based grants/RLS had no effect on writes. In a real deployment (Bier connecting as a non-superuser authenticator), an anonymous write ran as the connecting role instead of the anon role — a security gap.

Fix

lib/bier/mutation.ex run/4 now threads the context (conn.assigns[:bier_auth]) into the mutation transaction and wraps execute/4 in Bier.Auth.with_context/4, mapping a Postgres error through Bier.Auth.map_error/2 — exactly mirroring the read path. When no auth is configured (bier_auth absent), the path is an exact no-op, so behavior is unchanged.

  • Anonymous permission-denied write → 401 + WWW-Authenticate: Bearer
  • Authenticated-but-forbidden write → 403
  • The write now runs under SET LOCAL ROLE + request.jwt.claims

Verification

  • Full mix test: 825 passed, 0 failures (no auth-schema table-mutation conformance case existed, so no regression).
  • New test/bier/mutation_auth_test.exs: anon POST to a table the anon role can't write → 401; author-JWT POST → 201, and a current_setting('request.jwt.claims')-defaulted column reflects the token's id claim (two distinct claim values).
  • mix format / credo --strict / docs --warnings-as-errors clean.

Follow-ups (non-blocking)

  • Add an explicit authenticated-403 write test (the 403 mapping is shared with the tested read path).
  • Normalize execute/4's double-wrapped rollback error tuples (pre-existing convention) so map_write_error/2 needn't match two shapes.

🤖 Generated with Claude Code

Table mutations (POST/PATCH/PUT/DELETE on a relation) ran with no auth
context at all, while reads and RPC already wrapped their DB work in
Bier.Auth.with_context/4 (SET LOCAL ROLE + request.* GUCs + the
db-pre-request hook). This meant a JWT's role claim, db-anon-role,
db-pre-request, and role-based grants/RLS had no effect on writes — an
anonymous request could write as the connecting (often superuser) role.

Bier.Mutation.run/4 now threads conn.assigns[:bier_auth] into the
mutation transaction via Bier.Auth.with_context/4, mirroring the read
and RPC paths, and maps a resulting Postgrex.Error through
Bier.Auth.map_error/2 so an anonymous 42501 surfaces as 401 (WWW-Authenticate:
Bearer) instead of 403 or a silent success.

Bier.Mutation.execute/4 threads the rollback reason through
Postgrex.rollback/2 as the full {:error, _} tuple rather than the bare
reason (unlike the read path), so a write-query failure is double-wrapped
by the time it reaches Postgrex.transaction/2's return; a setup-phase
failure (role switch / db-pre-request) is not. mutation.ex's new
map_write_error/2 matches both shapes rather than touching that existing
convention, keeping the change scoped to the auth gap.

Added test/bier/mutation_auth_test.exs (TDD, run RED against the
pre-fix code) covering both angles against a dedicated
auth-configured instance: an authenticated author's JWT id claim must
reach a DEFAULT-derived column (GUC-based proof, with two different
claims to rule out a hardcoded value), and an anonymous insert into a
table only postgrest_test_author may write must now be denied (401)
instead of silently succeeding as the superuser (privilege-based
proof — the actual security hole).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@milmazz
milmazz merged commit 78e32d2 into main Jul 14, 2026
3 checks passed
@milmazz
milmazz deleted the feat/mutation-auth-context branch July 14, 2026 08:59
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.

Table mutations bypass the auth context (no SET LOCAL ROLE / GUCs / db-pre-request on writes)

1 participant