fix(mutation): apply the auth context to table mutations (closes #73)#75
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #73.
What & why
Table mutations (
POST/PATCH/PUT/DELETE) ran with no per-request auth context — noSET LOCAL ROLE, norequest.*GUCs, nodb-pre-request. Reads (query_executor.ex) and RPC (rpc.ex) already wrap their DB work inBier.Auth.with_context/4, butmutation.exdid not, so the JWTrole,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-superuserauthenticator), an anonymous write ran as the connecting role instead of the anon role — a security gap.Fix
lib/bier/mutation.exrun/4now threads the context (conn.assigns[:bier_auth]) into the mutation transaction and wrapsexecute/4inBier.Auth.with_context/4, mapping a Postgres error throughBier.Auth.map_error/2— exactly mirroring the read path. When no auth is configured (bier_authabsent), the path is an exact no-op, so behavior is unchanged.WWW-Authenticate: BearerSET LOCAL ROLE+request.jwt.claimsVerification
mix test: 825 passed, 0 failures (no auth-schema table-mutation conformance case existed, so no regression).test/bier/mutation_auth_test.exs: anonPOSTto a table the anon role can't write → 401; author-JWTPOST→ 201, and acurrent_setting('request.jwt.claims')-defaulted column reflects the token'sidclaim (two distinct claim values).mix format/credo --strict/docs --warnings-as-errorsclean.Follow-ups (non-blocking)
execute/4's double-wrapped rollback error tuples (pre-existing convention) somap_write_error/2needn't match two shapes.🤖 Generated with Claude Code