fix(server): set principal_id claim on routes without an rlsModule#1462
Closed
pyramation wants to merge 1 commit into
Closed
fix(server): set principal_id claim on routes without an rlsModule#1462pyramation wants to merge 1 commit into
pyramation wants to merge 1 commit into
Conversation
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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.
Summary
Fixes #1439. API surfaces with no
rlsModule(meta-schema routes, provisioning endpoints) returned early from the auth middleware, soreq.tokenwas never set andgraphile.tsfell back toanonSettings— leavingjwt.claims.principal_idunset and breaking any SPRT policy that readscurrent_principal_id().The no-rlsModule branch now attempts platform-level authentication before
next():if (!rlsModule) { - return next(); + const platformToken = await authenticatePlatform(req, pool); // constructive_auth_private.authenticate($1) + if (platformToken) req.token = platformToken; + // device-token cookie is also read on this path now + return next(); }authenticatePlatformis deliberately best-effort and never fails the request: it first probespg_proc/pg_namespaceforconstructive_auth_private.authenticate(databases without the platform auth module simply proceed anonymously), then runs it throughpgQueryContextwith the same ip/origin/user-agent claims as the RLS path. Errors are logged atwarnand the request continues anonymously rather than returningBAD_TOKEN_DEFINITION— this path must not turn provisioning traffic into 200-with-errors responses.Credential resolution (Bearer header, falling back to the
constructive_sessioncookie) and JWT-claim context building were extracted intoresolveCredential/buildAuthContextand are shared by both paths, so cookie sessions work identically on rlsModule-less routes.Once
req.token.user_idis present, the existinggraphile.tslogic setsjwt.claims.principal_id = req.token.principal_id || req.token.user_id, so no change was needed there.Tests:
graphql/server/src/middleware/__tests__/auth-no-rls-module.test.tscovers bearer + cookie population ofprincipal_id, no-credential, missing-function, and throwing-pool cases. The first two fail onmain(req.tokenundefined).Link to Devin session: https://app.devin.ai/sessions/cb8b3fdcce6c4af3abb50d3401450620
Requested by: @pyramation