Skip to content

feat(auth)!: split auth, roles and claims config; strict config - #83

Merged
archlemon merged 1 commit into
mainfrom
feat/v1-auth-config
Nov 23, 2026
Merged

archlemon merged 1 commit into
mainfrom
feat/v1-auth-config

Conversation

@archlemon

@archlemon archlemon commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Pre-1.0 config and security contract changes (readiness review B1, B2, B3) plus the shared bugs, in the engine. pdbq and pdbr follow in their own PRs against this commit.

Changelog

Breaking

  • Identity config split (confx): the rls section is replaced by auth, roles and claims (shape below). rls.enabled: false no longer turns authentication off; roles.switch: false runs requests as the connection role while credentials are still verified and claims still set.
  • Fail closed (auth): auth.New now returns (*Authenticator, error) and rejects an unknown mode, a jwt mode without a secret or JWKS URL, and a headers mode without trusted proxies (an empty TrustedProxies no longer trusts every peer). auth.Mode is a typed string (ModeJWT, ModeHeaders, ModeNone). Config.Enabled is gone; SkipRoleSwitch replaces it.
  • Anonymous access is opt-in: an empty AnonymousRole refuses requests without credentials with ErrAuthRequired (products answer 401 with WWW-Authenticate, see auth.Challenge). A credential that resolves to no role at all is refused with ErrRoleNotAllowed; a role claim that is not a non-empty string is ErrInvalidToken (it used to fall back to the default role).
  • Claims settings (pgexec): DefaultClaimsPrefix is removed from pgexec; confx.DefaultClaimsPrefix is jwt.claims (PostGraphile's namespace). An empty Identity.ClaimsPrefix now sets no per-claim settings instead of defaulting. The test fixture's policies read jwt.claims.user_id.
  • Strict config (confx.Load): unknown keys in the YAML file are errors; so are environment variables with the product prefix whose first segment names (or nearly names) a config section. Messages name the key a removed key moved to (MovedKeys, WithMoved) or the closest valid key. Variables outside the config sections (PDBQ_TEST_DATABASE_URL) are ignored.
  • Typed plugin settings: confx.Plugins is generic, Plugins[S], with the product's typed settings struct instead of map[string]map[string]any.
  • CORS API (httpx): CORS, Preflight and CORSWithPreflight take a CORSPolicy{Origins, AllowMethods, ExposeHeaders}.
  • confx.ValidateRLS → ValidateIdentity(Auth, Roles, Claims); confx.AuthConfig(RLS) → AuthConfig(Auth, Roles) (auth.Config, error).

Added

  • auth: case-insensitive Bearer scheme; Leeway; RoleClaimPath (app_metadata.role, realm_access.roles[0], "https://example.com/role") and ValidateClaimPath; HeaderRoleClaim; ErrAuthRequired; Challenge(err) for WWW-Authenticate; ParseMode.
  • pgexec: Identity.ClaimsJSONSetting sets every claim as one JSON object ({} without claims), e.g. request.jwt.claims, so PostgREST/Supabase policies port unchanged; ValidateClaimsSetting.
  • confx: auth.jwt.secret_file (JWT.ResolveSecret), auth.jwt.leeway, auth.jwt.role_claim_path, auth.headers.role_claim, claims.json_setting; DefaultAuth, DefaultRoles, DefaultClaims; IdentityWarnings; ValidatePluginNames (did-you-mean); ValidateLog checks log.format.
  • httpx: CORSPolicy.ExposeHeaders (Access-Control-Expose-Headers), AddVary.

Fixed

  • CORS no longer replaces Vary: Accept-Encoding; it adds Vary: Origin to every response (and the request-method/headers to preflights), and gzip adds rather than appends blindly.
  • SQLSTATE class 21 (ON CONFLICT DO UPDATE command cannot affect row a second time, a scalar subquery returning several rows) is user input with its message passed through in prod, instead of an internal error.
  • Environment-supplied plugin settings were strings the plugins ignored; typed settings decode them like core keys.
  • Unknown config keys were silently ignored.

Shared config migration (both products)

0.x key 1.0 key Notes
rls.enabled: true roles.switch: true default
rls.enabled: false roles.switch: false still verifies credentials; for no auth at all also set auth.mode: none
rls.anonymous_role (default anonymous) roles.anonymous (default empty) empty refuses unauthenticated requests with 401; set it to keep anonymous access
rls.default_role roles.default
rls.allowed_roles roles.allowed
rls.role_claim auth.jwt.role_claim_path / auth.headers.role_claim jwt takes a path
rls.claims_prefix (default pdbq.claims) claims.prefix (default jwt.claims) set claims.prefix: pdbq.claims to keep old policies, or update them
— claims.json_setting e.g. request.jwt.claims
rls.auth.mode auth.mode
rls.auth.jwt_secret auth.jwt.secret or auth.jwt.secret_file
rls.auth.jwks_url auth.jwt.jwks_url
rls.auth.jwks_cache_ttl auth.jwt.jwks_cache_ttl
rls.auth.jwt_issuer auth.jwt.issuer
rls.auth.jwt_audience auth.jwt.audience
— auth.jwt.leeway default 0s
rls.auth.header_prefix auth.headers.prefix
rls.auth.header_claims auth.headers.claims
rls.auth.trusted_proxies auth.headers.trusted_proxies required in headers mode
rls.auth.jwt_type auth.mint_type
log.format: <anything> log.format: text or json other values are rejected

New config shape:

auth:
  mode: jwt              # jwt | headers | none
  jwt: {secret, secret_file, jwks_url, jwks_cache_ttl, issuer, audience, leeway, role_claim_path}
  headers: {prefix, claims, role_claim, trusted_proxies}
  mint_type: ""
roles: {switch: true, anonymous: "", default: "", allowed: []}
claims: {prefix: jwt.claims, json_setting: ""}

The same table and the Go API changes are in docs/upgrading.md ("0.x → 1.0", a section next to the catalog format 10 notes).

The claim header prefix stays per product (X-Pdbq-Claim- / X-Pdbr-Claim-): names SQL sees are product-neutral so policies port; names on the wire between a gateway and the server are product-namespaced like every other product header (README).

Closes #76
Closes #77
Closes #78
Closes #80

Not in this PR: the B13 overload-order item listed in #80 (catalog work), B6's pdbr base_path/etags/schema-endpoint defaults (later phases); the statement-timeout default lives in the products.

Tests

  • go test -race ./..., go vet, golangci-lint (0 issues), act lint-test.
  • Rebased onto catalog format 10 (v0.17.0). DB suites (testutil pgexec watch introspect postgis contrib metrics cache) on fresh postgis/postgis:18-3.7 and 14-3.7: pass, including a new policy test reading current_setting('request.jwt.claims', true)::json->>'sub' through RunTx and QueryTx.
  • New unit tests: auth modes and New errors, anonymous refusal and challenges, case-insensitive Bearer, leeway, role claim paths, SkipRoleSwitch; strict loading (moved keys, did-you-mean for file and env, ignored unrelated env, list entries, map keys), env decoding into typed plugin settings; CORS expose headers and Vary composition; JSON claims setting. FuzzClaimPath (10s, 4 workers) clean.

Identity configuration is now three shared confx sections: auth (mode,
jwt, headers, mint_type), roles (switch, anonymous, default, allowed) and
claims (prefix, json_setting). The rls section is gone.

auth: typed Mode, New returns an error for unknown modes, a jwt mode
without a key and a headers mode without trusted proxies (headers mode
no longer trusts every peer). The Bearer scheme is case-insensitive, an
optional leeway covers clock skew, the role claim is a path
(app_metadata.role, realm_access.roles[0]) and must be a string, and an
empty anonymous role refuses requests without credentials with
ErrAuthRequired. Challenge gives the WWW-Authenticate value.
SkipRoleSwitch replaces the old Enabled flag: credentials are always
verified.

pgexec: the default claims namespace moves out of pgexec; an empty
ClaimsPrefix sets no per-claim settings, and ClaimsJSONSetting exposes
every claim as one JSON setting (request.jwt.claims). SQLSTATE class 21
is user input instead of internal. The test fixture's policies read
jwt.claims.

confx: Load rejects unknown keys in the file and in environment
variables of known sections, naming the key they moved to or the
closest valid key; values decode with weak typing into typed plugin
settings (Plugins[S]). ValidateLog checks log.format,
ValidatePluginNames checks plugins.disabled, and JWT.secret_file reads
the HMAC secret from a file.

httpx: CORS takes a CORSPolicy with exposed headers, and CORS and gzip
add to Vary instead of replacing it.

BREAKING CHANGE: confx.RLS, confx.Auth's flat fields, auth.Config.Enabled,
auth.New's signature, pgexec.DefaultClaimsPrefix, the CORS function
signatures and confx.Plugins' map settings change; unknown config keys
are errors.
@archlemon
archlemon merged commit 0ac2ed0 into main Nov 23, 2026
3 checks passed
@archlemon
archlemon deleted the feat/v1-auth-config branch November 23, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant