-
Notifications
You must be signed in to change notification settings - Fork 4.1k
fix(sql): don't treat URL pathname as Unix socket path #27714
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
robobun
wants to merge
3
commits into
main
Choose a base branch
from
claude/fix-sql-url-path-27713
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.
+136
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { SQL } from "bun"; | ||
| import { afterAll, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { isWindows } from "harness"; | ||
|
|
||
| // Regression test for https://github.com/oven-sh/bun/issues/27713 | ||
| // Bun SQL was treating the Postgres URL path component (the database name) | ||
| // as a Unix domain socket path, causing FailedToOpenSocket on any URL with | ||
| // a database name. | ||
|
|
||
| describe("SQL should not treat URL pathname as Unix socket path (#27713)", () => { | ||
| const originalEnv = { ...process.env }; | ||
|
|
||
| // prettier-ignore | ||
| const SQL_ENV_VARS = [ | ||
| "DATABASE_URL", "DATABASEURL", | ||
| "TLS_DATABASE_URL", | ||
| "POSTGRES_URL", "PGURL", "PG_URL", | ||
| "TLS_POSTGRES_DATABASE_URL", | ||
| "MYSQL_URL", "MYSQLURL", | ||
| "TLS_MYSQL_DATABASE_URL", | ||
| "PGHOST", "PGUSER", "PGPASSWORD", "PGDATABASE", "PGPORT", | ||
| "PG_HOST", "PG_USER", "PG_PASSWORD", "PG_DATABASE", "PG_PORT", | ||
| "MYSQL_HOST", "MYSQL_USER", "MYSQL_PASSWORD", "MYSQL_DATABASE", "MYSQL_PORT", | ||
| ]; | ||
|
|
||
| beforeEach(() => { | ||
| for (const key of SQL_ENV_VARS) { | ||
| delete process.env[key]; | ||
| delete Bun.env[key]; | ||
| delete import.meta.env[key]; | ||
| } | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| for (const key of SQL_ENV_VARS) { | ||
| if (key in originalEnv) { | ||
| process.env[key] = originalEnv[key]!; | ||
| Bun.env[key] = originalEnv[key]!; | ||
| import.meta.env[key] = originalEnv[key]!; | ||
| } else { | ||
| delete process.env[key]; | ||
| delete Bun.env[key]; | ||
| delete import.meta.env[key]; | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| test("postgres URL with database name should not set path", () => { | ||
| const sql = new SQL("postgres://user:pass@myhost:5432/mydb"); | ||
| expect(sql.options.hostname).toBe("myhost"); | ||
| expect(sql.options.port).toBe(5432); | ||
| expect(sql.options.database).toBe("mydb"); | ||
| // path must not be the database name "/mydb" | ||
| expect(sql.options.path).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("postgres URL passed via url option should not set path", () => { | ||
| const sql = new SQL({ | ||
| url: "postgres://user:pass@myhost:5432/mydb", | ||
| }); | ||
| expect(sql.options.hostname).toBe("myhost"); | ||
| expect(sql.options.port).toBe(5432); | ||
| expect(sql.options.database).toBe("mydb"); | ||
| expect(sql.options.path).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("DATABASE_URL with database name should not set path when using explicit options", () => { | ||
| process.env.DATABASE_URL = "postgres://user:pass@envhost:5432/envdb"; | ||
|
|
||
| const sql = new SQL({ | ||
| hostname: "myhost", | ||
| port: 5432, | ||
| username: "user", | ||
| password: "pass", | ||
| database: "mydb", | ||
| }); | ||
|
|
||
| expect(sql.options.hostname).toBe("myhost"); | ||
| expect(sql.options.database).toBe("mydb"); | ||
| // path must not be "/envdb" from DATABASE_URL | ||
| expect(sql.options.path).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("DATABASE_URL with database name should not set path when used implicitly", () => { | ||
| process.env.DATABASE_URL = "postgres://user:pass@envhost:5432/envdb"; | ||
|
|
||
| const sql = new SQL(); | ||
| expect(sql.options.hostname).toBe("envhost"); | ||
| expect(sql.options.port).toBe(5432); | ||
| expect(sql.options.database).toBe("envdb"); | ||
| // path must not be "/envdb" | ||
| expect(sql.options.path).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("postgres URL with database name matching existing directory should not set path", () => { | ||
| // This is the actual bug: when the URL pathname matches an existing filesystem | ||
| // path (like /tmp), the old code would pass it as a Unix socket path. | ||
| // The database name in postgres://.../<dbname> is "/tmp" here, which exists. | ||
| const sql = new SQL("postgres://user:pass@myhost:5432/tmp"); | ||
| expect(sql.options.hostname).toBe("myhost"); | ||
| expect(sql.options.port).toBe(5432); | ||
| expect(sql.options.database).toBe("tmp"); | ||
| // Before the fix, this would be "/tmp" (or "/tmp/.s.PGSQL.5432" if that exists), | ||
| // causing the connection to use Unix domain socket instead of TCP. | ||
| expect(sql.options.path).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("mysql URL with database name should not set path", () => { | ||
| const sql = new SQL("mysql://user:pass@myhost:3306/mydb"); | ||
| expect(sql.options.hostname).toBe("myhost"); | ||
| expect(sql.options.port).toBe(3306); | ||
| expect(sql.options.database).toBe("mydb"); | ||
| expect(sql.options.path).toBeUndefined(); | ||
| }); | ||
|
|
||
| test.skipIf(isWindows)("unix:// protocol should still use pathname as socket path", () => { | ||
| const socketPath = `/tmp/bun-test-27713-${process.pid}.sock`; | ||
| using sock = Bun.listen({ | ||
| unix: socketPath, | ||
| socket: { | ||
| data: () => {}, | ||
| }, | ||
| }); | ||
|
|
||
| const sql = new SQL(`unix://${sock.unix}`, { adapter: "postgres" }); | ||
| expect(sql.options.path).toBe(socketPath); | ||
| }); | ||
| }); | ||
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.
🟣 Pre-existing issue: For
unix://URLs, the database resolution fallback at line 732 still usesdecodeIfValid((url?.pathname ?? "").slice(1))without aunix:protocol guard. This meansnew SQL("unix:///var/run/postgresql", { adapter: "postgres" })without an explicitdatabaseoption would incorrectly set the database name to"var/run/postgresql". Consider adding the sameurl.protocol === "unix:"guard used forpath(line 623) to the database fallback.Extended reasoning...
What the bug is
The PR correctly fixes the
pathvariable so thaturl.pathnameis only used as a Unix socket path forunix://protocol URLs (line 623). However, further down in the function, the database name resolution for postgres (line 732), mysql (line 743), and mariadb still usesdecodeIfValid((url?.pathname ?? "").slice(1))as a fallback for all protocols, includingunix:.Concrete example
Consider:
new SQL("unix:///var/run/postgresql", { adapter: "postgres" })url.protocol = "unix:",url.pathname = "/var/run/postgresql"path = "/var/run/postgresql"(the socket path) becauseurl.protocol === "unix:"options.databaseis undefinedoptions.dbis undefinedenv.PG_DATABASEis undefinedenv.PGDATABASEis undefineddecodeIfValid("/var/run/postgresql".slice(1))evaluates to"var/run/postgresql"— this is the bug"var/run/postgresql", which is the socket path minus the leading slash, not a valid database name.Why existing code does not prevent it
The
url.protocol === "unix:"guard was only added to thepathresolution (line 623), not to the database resolution (lines 725-745). The database fallback chain unconditionally readsurl.pathnameregardless of protocol.Impact
In practice, most users of
unix://URLs will explicitly specify adatabaseoption, which short-circuits the fallback chain. However, if they omit it, they will get a confusing connection error about a non-existent database named after their socket path. The practical impact is limited but the inconsistency with the path fix is worth addressing.How to fix
Add a similar
url.protocol === "unix:"guard to the database resolution. For example, replacedecodeIfValid((url?.pathname ?? "").slice(1))with(url?.protocol !== "unix:" ? decodeIfValid((url?.pathname ?? "").slice(1)) : null)in all three adapter cases (postgres at line 732, mysql at line 743, mariadb).Pre-existing status
This is a pre-existing issue. The database resolution code at lines 725-745 was not modified by this PR. The
url.pathnamefallback for database names has always applied to all protocols. However, since this PR makesunix://URLs more functional by fixing the socket path issue, it creates a natural place to also address this related problem. As one verifier noted, the typical unix:// usage pattern involves explicitly specifying the database option, so the practical impact is limited.