-
Notifications
You must be signed in to change notification settings - Fork 1
fix(auth): set search_path per connection, not only via startup options #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wmadden-electric
wants to merge
1
commit into
main
Choose a base branch
from
claude/auth-search-path-on-connect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: prisma/composer
Length of output: 21969
🏁 Script executed:
Repository: prisma/composer
Length of output: 4238
🌐 Web query:
node-postgres pool onConnect hook async client query SET search_path rejects connection💡 Result:
In node-postgres, the
onConnectconfiguration option is the official and recommended way to perform setup tasks, such as executingSET search_path, on a new client [1][2]. If the asynchronous function provided toonConnectthrows an error or returns a rejected promise, node-postgres will destroy the client and propagate the error to the caller who requested the connection (e.g., viapool.connect()orpool.query()) [1][2]. This behavior ensures that the pool does not hand out incorrectly configured clients [1]. Example usage: const pool = new Pool({ onConnect: async (client) => { await client.query('SET search_path TO my_schema'); }, }); Historically, users attempted to use the pool's 'connect' event for this purpose, but that event is not designed to wait for asynchronous operations, which often led to race conditions where queries were executed before the setup was complete [2][3][4]. UsingonConnectavoids these issues by ensuring setup is performed before the client is made available to the pool [1][2]. Alternatively, for simple configuration tasks like setting the search path, you can also use the connection options directly in the pool configuration, which is set once at connection time and does not require executing a separate query for every new client: const pool = new Pool({ options: '-c search_path=my_schema', }); This approach is often more efficient than running a setup query viaonConnect[5][6].Citations:
onConnectcallback which allows "setup" of a pooled client brianc/node-postgres#3620🏁 Script executed:
Repository: prisma/composer
Length of output: 284
🌐 Web query:
node-postgres 8.22.0 Pool onConnect TypeScript onConnect?: number return removed client destroyed💡 Result:
The
onConnectcallback was introduced innode-postgresversion 8.20.0 [1][2] to allow for the asynchronous initialization of newly created and connected pooled clients [1]. When you defineonConnectin thePoolconfiguration, it is executed exactly once for each new client immediately after the client has successfully established a connection to the PostgreSQL backend, but before it is made available for use in the pool [3]. Regarding the behavior if the function fails: If theonConnectfunction throws an error or returns a promise that rejects, the library automatically destroys that specific client instance [3][4]. The associated error is then returned to the original caller (e.g., the code that calledpool.connect()orpool.query()) that triggered the pool to create that new client [4]. Regarding TypeScript usage: TheonConnectcallback is typed to accept a client instance and may return eithervoidor aPromise<void>[3]. Example implementation: const pool = new Pool({ onConnect: async (client) => { await client.query('SET search_path TO my_schema'); }, }); This mechanism is the recommended approach for running setup commands (such as setting the search path, time zones, or other session-specific configurations) on pooled clients, replacing the older and less reliable practice of using thepool.on('connect', ...)event listener for initialization [3][4][5].Citations:
onConnectcallback which allows "setup" of a pooled client brianc/node-postgres#3620Fail closed when setting
search_path.pool.on('connect')is not awaited and the current handler treatsSETfailures as only logged errors, so Better Auth can use a client configured with the wrong schema. Use the PoolonConnecthook, await the setup query, and throw so node-postgres removes the client and fails the connection instead of exposing an untrusted search path.🧰 Tools
🪛 ast-grep (0.44.1)
[error] 68-69: Avoid SQL injection
Context: client
.query(
SET search_path TO ${AUTH_SCHEMA})Note: [CWE-89] Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
(sql-injection-typescript)
🪛 OpenGrep (1.25.0)
[ERROR] 69-70: SQL query built via string concatenation or template literal passed to query()/execute(). Use parameterized queries instead.
(coderabbit.sql-injection.raw-query-concat-js)
🤖 Prompt for AI Agents
Source: MCP tools