fix(presigned-url): keep the database claim across request-lane statements - #1616
Merged
Conversation
…the database claim survives Presigned upload/download resolvers ran multi-statement request work under withPgClient(pgSettings, cb), which applies jwt claims as transaction-LOCAL set_config without opening an explicit transaction. Each statement ran in its own autocommit transaction, so jwt.claims.database_id was gone by the next statement and jwt_private.current_database_id() raised DATABASE_CLAIM_REQUIRED on uploadAppFile. Add withRequestPgClient: acquire the client in the system lane (null pgSettings), open one transaction, apply the request settings inside it, then run the callback. Route resolveDatabaseId, bucket/file reads, single/bulk uploads, and delete-middleware reads through it. The system-lane physical_name record-write now carries the tenant database_id claim too, since the buckets catalog-sync trigger calls current_database_id().
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
uploadAppFile(and the filedownloadUrlresolver) failed over HTTP withDATABASE_CLAIM_REQUIREDeven though the request carriedjwt.claims.database_id.Root cause: the presigned-url plugin ran multi-statement request work as
The
@dataplan/pgadaptor appliespgSettingsas transaction-LOCALset_config(key, value, is_local => true)but does not open an explicit transaction around the callback. So eachclient.query(...)runs in its own implicit (autocommit) transaction, and the LOCAL claim set for one statement is already gone by the next —jwt_private.current_database_id()then raisesDATABASE_CLAIM_REQUIRED. (Confirmed at runtime: the callback's first statement saw the claim, the next did not.)Fix — a small helper that mirrors the existing
pg-query-contextpattern (newsrc/request-pg-client.ts):All request-lane reads/writes now go through it (
plugin.ts,download-url-field.ts):resolveDatabaseId, bucket/file reads, single + bulkprocessSingleFile, and the delete-middleware pre-read/refcount reads. System-lane config reads (loadAllStorageModules) stay onwithPgClient(null, ...).One extra case:
provisionAndRecordPhysicalBucket'sphysical_nameUPDATEis intentionally a system-lane write (privileged role, so it bypasses the RLS that blocks request roles). But the buckets table's catalog-sync trigger callscurrent_database_id(), so a bare system-lane write hit the same claim error. It now runs throughwithRequestPgClient(withPgClient, { 'jwt.claims.database_id': databaseId }, ...)— the claim is applied inside the transaction without switching off the privileged role, so RLS stays bypassed and the trigger resolves.Verification
build(tsc typecheck) +lintclean.__tests__/request-pg-client.test.ts(no live DB; models the adaptor's transaction-LOCAL semantics): reproduces the bug (claim lost across autocommit statements), proves the fix (claim durable across statements in the transaction), asserts the system-lane acquire +BEGIN → set_config → work → COMMITordering, noset_configwhen there are no settings, and error propagation +ROLLBACK(no swallowing).fun up --k8scluster (graphql-public rebuilt with thisdist/):uploadAppFilereturns a presigned URL,PUT→200, and a second upload of identical content returnsdeduplicated: truewithuploadUrl: nulland the samefileId.Link to Devin session: https://app.devin.ai/sessions/2ffbeace70364c6aa00b2534a5eadd06
Requested by: @pyramation