also looking at this
fix(client): unsafe() refuses ?-style bindings on Postgres
#1172
3 files
+247
-13
| @@ -0,0 +1,152 @@ | ||
| 1 | /** | |
| 2 | * `unsafe()` refuses `?`-style bindings on Postgres — stacksjs/bun-query-builder#1171. | |
| 3 | * | |
| 4 | * `unsafe('SELECT * FROM users WHERE email = ? LIMIT 1', [email])` typechecks, | |
| 5 | * passes on SQLite and MySQL, and on Postgres reached the server as a bare `?`. | |
| 6 | * Postgres parses that as an operator, hunts for a right operand and rejects the | |
| 7 | * next word, so the error named `LIMIT` while the fault was the `?` 37 characters | |
| 8 | * earlier — and the bound value was never going to arrive. Downstream that took | |
| 9 | * out every login, password recovery, magic link and two-factor exchange in | |
| 10 | * Stacks at once: 126 failures in one CI run, all from this one shape. | |
| 11 | * | |
| 12 | * The check is keyed on bindings arriving with no `$n` in the query, not on the | |
| 13 | * presence of `?`, so a jsonb `?` operator alongside `$1` is still allowed | |
| 14 | * through and raw DDL is never inspected. | |
| 15 | */ | |
| 16 | ||
| 17 | import { afterEach, beforeEach, describe, expect, it } from 'bun:test' | |
| 18 | import { buildDatabaseSchema, buildSchemaMeta, createQueryBuilder } from '../src' | |
| 19 | import { config } from '../src/config' | |
| 20 | ||
| 21 | function qb(): any { | |
| 22 | const models = { | |
| 23 | users: { | |
| 24 | columns: { | |
| 25 | id: { type: 'integer', isPrimaryKey: true }, | |
| 26 | email: { type: 'text' }, | |
| 27 | }, | |
| 28 | }, | |
| 29 | } as any | |
| 30 | return createQueryBuilder<any>({ | |
| 31 | schema: buildDatabaseSchema(models), | |
| 32 | meta: buildSchemaMeta(models), | |
| 33 | autoMigration: { enabled: false } as any, | |
| 34 | }) | |
| 35 | } | |
| 36 | ||
| 37 | const REFUSED = /\[query-builder\] unsafe\(\)/ | |
| 38 | ||
| 39 | describe('unsafe() placeholder style (#1171)', () => { | |
| 40 | let dialect: string | |
| 41 | beforeEach(() => { dialect = config.dialect }) | |
| 42 | afterEach(() => { config.dialect = dialect as any }) | |
| 43 | ||
| 44 | describe('Postgres refuses bindings it cannot bind', () => { | |
| 45 | beforeEach(() => { config.dialect = 'postgres' as any }) | |
| 46 | ||
| 47 | it('throws on the exact call from the issue', () => { | |
| 48 | expect(() => qb().unsafe('SELECT * FROM users WHERE email = ? LIMIT 1', ['a@b.c'])).toThrow(REFUSED) | |
| 49 | }) | |
| 50 | ||
| 51 | it('names the real problem rather than the token Postgres would pick', () => { | |
| 52 | let message = '' | |
| 53 | try { | |
| 54 | qb().unsafe('SELECT * FROM users WHERE email = ? LIMIT 1', ['a@b.c']) | |
| 55 | } | |
| 56 | catch (error) { | |
| 57 | message = (error as Error).message | |
| 58 | } | |
| 59 | // The placeholder style the connection expects, and the one that was used. | |
| 60 | expect(message).toContain('$1') | |
| 61 | expect(message).toContain('1 binding supplied') | |
| 62 | // It blames the placeholder, where the server's own error blamed `LIMIT`. | |
| 63 | expect(message).toContain('`?` placeholder') | |
| 64 | expect(message).not.toContain('syntax error') | |
| 65 | // And it says why the same call passes elsewhere, which is the part that | |
| 66 | // makes a one-dialect-only CI failure legible. | |
| 67 | expect(message).toMatch(/SQLite and MySQL/) | |
| 68 | }) | |
| 69 | ||
| 70 | it('throws a TypeError, synchronously, so the stack names the caller', () => { | |
| 71 | // Not a rejected promise: the point is to fail where the query was written. | |
| 72 | expect(() => qb().unsafe('SELECT ?', [1])).toThrow(TypeError) | |
| 73 | }) | |
| 74 | ||
| 75 | it('counts the placeholders and pluralises', () => { | |
| 76 | let message = '' | |
| 77 | try { | |
| 78 | qb().unsafe('SELECT * FROM users WHERE email = ? OR id = ?', ['a@b.c', 1]) | |
| 79 | } | |
| 80 | catch (error) { | |
| 81 | message = (error as Error).message | |
| 82 | } | |
| 83 | expect(message).toContain('2 bindings supplied') | |
| 84 | expect(message).toContain('2 `?` placeholders') | |
| 85 | }) | |
| 86 | ||
| 87 | it('refuses bindings that nothing in the query references', () => { | |
| 88 | // No `?` either — the values still cannot arrive, and Postgres would have | |
| 89 | // said "bind message supplies 1 parameters, but prepared statement requires 0". | |
| 90 | expect(() => qb().unsafe('SELECT 1', [1])).toThrow(/no \$n placeholder/) | |
| 91 | }) | |
| 92 | }) | |
| 93 | ||
| 94 | describe('Postgres leaves correct and unrelated queries alone', () => { | |
| 95 | beforeEach(() => { config.dialect = 'postgres' as any }) | |
| 96 | ||
| 97 | it('accepts $n, in any order', () => { | |
| 98 | // The shape db.d.ts documents. A throw here is the false positive that | |
| 99 | // would matter, so this asserts nothing is thrown at all. | |
| 100 | expect(refusal(() => qb().unsafe('SELECT $2 AS a, $1 AS b', ['ONE', 'TWO']))).toBeNull() | |
| 101 | }) | |
| 102 | ||
| 103 | it('accepts a jsonb ? operator when $n is also present', () => { | |
| 104 | // `data ? 'key'` is a real Postgres operator, not a placeholder. Keying the | |
| 105 | // check on `?` instead of on the absence of $n would reject this. | |
| 106 | expect(refusal(() => qb().unsafe('SELECT * FROM t WHERE data ? \'key\' AND id = $1', [7]))).toBeNull() | |
| 107 | }) | |
| 108 | ||
| 109 | it('never inspects a query with no bindings', () => { | |
| 110 | // Raw DDL, and the `?` in a literal that is data rather than a placeholder. | |
| 111 | expect(refusal(() => qb().unsafe('CREATE TABLE t (id int)'))).toBeNull() | |
| 112 | expect(refusal(() => qb().unsafe('SELECT * FROM users WHERE name = \'what?\''))).toBeNull() | |
| 113 | expect(refusal(() => qb().unsafe('SELECT * FROM users WHERE email = ?', []))).toBeNull() | |
| 114 | }) | |
| 115 | }) | |
| 116 | ||
| 117 | describe('other dialects are unaffected', () => { | |
| 118 | it('SQLite and MySQL still take ?', () => { | |
| 119 | for (const d of ['sqlite', 'mysql'] as const) { | |
| 120 | config.dialect = d as any | |
| 121 | expect(refusal(() => qb().unsafe('SELECT * FROM users WHERE email = ? LIMIT 1', ['a@b.c']))).toBeNull() | |
| 122 | } | |
| 123 | }) | |
| 124 | }) | |
| 125 | }) | |
| 126 | ||
| 127 | /** | |
| 128 | * The message `fn` threw, or null if it returned. | |
| 129 | * | |
| 130 | * `unsafe` returns a LAZY query: it reaches a database when something awaits it, | |
| 131 | * and not before. So these cases call it and discard the result, which exercises | |
| 132 | * the guard while executing nothing. | |
| 133 | * | |
| 134 | * That is load-bearing, not tidiness. An earlier version of this helper did | |
| 135 | * `void Promise.resolve(out).catch(…)` to ignore driver errors — but | |
| 136 | * `Promise.resolve` calls `.then` on the thenable, so every "leaves it alone" | |
| 137 | * case below really ran, `CREATE TABLE t (id int)` included, against whatever | |
| 138 | * database the environment pointed at. In CI that is the shared `test_db`, and | |
| 139 | * the unawaited queries outlived the file and timed out the next one's | |
| 140 | * `beforeAll` (`closeConnection` → `resetDatabase` → `generateMigration`, on a | |
| 141 | * 5s hook budget). Returning the message instead of swallowing it also means an | |
| 142 | * unexpected throw fails loudly rather than passing as "not refused". | |
| 143 | */ | |
| 144 | function refusal(fn: () => unknown): string | null { | |
| 145 | try { | |
| 146 | fn() | |
| 147 | return null | |
| 148 | } | |
| 149 | catch (error) { | |
| 150 | return (error as Error).message | |
| 151 | } | |
| 152 | } | |