The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,136 lines 41.5 KB
Raw
1 import { PgConnection } from "@effect/sql-pg"
2 import { assert, describe, it } from "@effect/vitest"
3 import { Effect, Exit, Fiber, Option, Redacted, Scheduler, Scope, Stream } from "effect"
4 import * as TestClock from "effect/testing/TestClock"
5 import { mkdtemp, rm } from "node:fs/promises"
6 import * as Net from "node:net"
7 import { tmpdir } from "node:os"
8 import { join } from "node:path"
9 import { Duplex } from "node:stream"
10 import type * as Tls from "node:tls"
11 import { vi } from "vitest"
12
13 const tlsOptions = new WeakMap<Duplex, Tls.ConnectionOptions | undefined>()
14
15 vi.mock("node:net", async (importOriginal) => ({
16 ...await importOriginal<typeof Net>()
17 }))
18
19 // Only simulate TLS for registered streams; other connections use native TLS.
20 vi.mock("node:tls", async (importOriginal) => {
21 const original = await importOriginal<typeof Tls>()
22 return {
23 ...original,
24 connect(options: Tls.ConnectionOptions) {
25 const socket = options.socket
26 if (socket === undefined || !tlsOptions.has(socket)) {
27 return original.connect(options)
28 }
29 tlsOptions.set(socket, options)
30 queueMicrotask(() => socket.emit("secureConnect"))
31 return socket
32 }
33 }
34 })
35
36 const backendMessage = (tag: string, payload: Uint8Array): Buffer => {
37 const length = Buffer.allocUnsafe(4)
38 length.writeInt32BE(payload.length + 4)
39 return Buffer.concat([Buffer.from(tag), length, payload])
40 }
41
42 const int32 = (value: number): Buffer => {
43 const bytes = Buffer.allocUnsafe(4)
44 bytes.writeInt32BE(value)
45 return bytes
46 }
47
48 const int16 = (value: number): Buffer => {
49 const bytes = Buffer.allocUnsafe(2)
50 bytes.writeInt16BE(value)
51 return bytes
52 }
53
54 const authentication = (method: number, payload: Uint8Array = Buffer.alloc(0)): Buffer =>
55 backendMessage("R", Buffer.concat([int32(method), payload]))
56
57 const authenticationOk = authentication(0)
58 const backendKeyData = backendMessage("K", Buffer.concat([int32(1234), int32(5678)]))
59 const readyForQuery = backendMessage("Z", Buffer.from("I"))
60 const sslRequest = Buffer.concat([int32(8), int32(80877103)])
61 const cancelRequest = Buffer.concat([int32(16), int32(80877102), int32(1234), int32(5678)])
62
63 const makeSslStream = (reply: "S" | "N", cancel: boolean) => {
64 const writes: Array<Buffer> = []
65 const socket: Duplex = new Duplex({
66 read() {},
67 write(chunk: Buffer, _encoding, callback) {
68 writes.push(Buffer.from(chunk))
69 queueMicrotask(() => {
70 if (writes.length === 1) {
71 socket.push(Buffer.from(reply))
72 } else if (cancel) {
73 socket.destroy()
74 } else if (writes.length === 2) {
75 socket.push(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
76 }
77 })
78 callback()
79 }
80 })
81 tlsOptions.set(socket, undefined)
82 return { socket, writes }
83 }
84
85 const emptyQueryResult = Buffer.concat([
86 backendMessage("1", Buffer.alloc(0)),
87 backendMessage("2", Buffer.alloc(0)),
88 backendMessage("n", Buffer.alloc(0)),
89 backendMessage("C", Buffer.from("SELECT 0\0")),
90 readyForQuery
91 ])
92
93 const frontendTags = (message: Buffer): ReadonlyArray<string> => {
94 const tags: Array<string> = []
95 let offset = 0
96 while (offset < message.length) {
97 tags.push(String.fromCharCode(message[offset]))
98 offset += 1 + message.readInt32BE(offset + 1)
99 }
100 return tags
101 }
102
103 const frontendPreparedNames = (message: Buffer): ReadonlyArray<string> => {
104 const names: Array<string> = []
105 let offset = 0
106 while (offset < message.length) {
107 const tag = String.fromCharCode(message[offset])
108 const length = message.readInt32BE(offset + 1)
109 if (tag === "P") {
110 const nameStart = offset + 5
111 const nameEnd = message.indexOf(0, nameStart)
112 names.push(message.subarray(nameStart, nameEnd).toString())
113 }
114 offset += 1 + length
115 }
116 return names
117 }
118
119 const consumeFrontend = (
120 socket: Net.Socket,
121 onMessage: (tag: string | undefined, message: Buffer) => void
122 ): void => {
123 let startup = true
124 let buffered = Buffer.alloc(0)
125 socket.on("data", (chunk: Buffer) => {
126 buffered = Buffer.concat([buffered, chunk])
127 while (true) {
128 const lengthOffset = startup ? 0 : 1
129 if (buffered.length < lengthOffset + 4) return
130 const length = buffered.readInt32BE(lengthOffset)
131 const messageLength = lengthOffset + length
132 if (buffered.length < messageLength) return
133 const message = buffered.subarray(0, messageLength)
134 buffered = buffered.subarray(messageLength)
135 const tag = startup ? undefined : String.fromCharCode(message[0])
136 startup = tag === undefined && message.equals(sslRequest)
137 onMessage(tag, message)
138 }
139 })
140 }
141
142 const startupParameters = (message: Buffer): ReadonlyMap<string, string> => {
143 const values = message.subarray(8, -1).toString().split("\0")
144 const parameters = new Map<string, string>()
145 for (let index = 0; index < values.length - 1 && values[index] !== ""; index += 2) {
146 parameters.set(values[index], values[index + 1])
147 }
148 return parameters
149 }
150
151 const errorResponse = (fields: ReadonlyArray<readonly [string, string]>): Buffer =>
152 backendMessage(
153 "E",
154 Buffer.concat([
155 ...fields.map(([code, value]) => Buffer.from(`${code}${value}\0`)),
156 Buffer.from([0])
157 ])
158 )
159
160 const withTcpServer = (
161 onConnection: (socket: Net.Socket) => void
162 ) =>
163 Effect.acquireRelease(
164 Effect.tryPromise({
165 try: () =>
166 new Promise<{ readonly port: number; readonly server: Net.Server }>((resolve, reject) => {
167 const server = Net.createServer(onConnection)
168 server.once("error", reject)
169 server.listen(0, "127.0.0.1", () => {
170 server.off("error", reject)
171 const address = server.address()
172 if (address === null || typeof address === "string") {
173 reject(new Error("Test server did not bind to a TCP port"))
174 return
175 }
176 resolve({ port: address.port, server })
177 })
178 }),
179 catch: (cause) => cause
180 }),
181 ({ server }) =>
182 Effect.promise(() =>
183 new Promise<void>((resolve) => {
184 server.close(() => resolve())
185 })
186 )
187 )
188
189 const withUnixServer = (
190 onConnection: (socket: Net.Socket) => void
191 ) =>
192 Effect.acquireRelease(
193 Effect.tryPromise({
194 try: async () => {
195 const directory = await mkdtemp(join(tmpdir(), "effect-pg-"))
196 const socketPath = join(directory, ".s.PGSQL.5432")
197 const server = Net.createServer(onConnection)
198 await new Promise<void>((resolve, reject) => {
199 server.once("error", reject)
200 server.listen(socketPath, () => {
201 server.off("error", reject)
202 resolve()
203 })
204 })
205 return { directory, server }
206 },
207 catch: (cause) => cause
208 }),
209 ({ directory, server }) =>
210 Effect.promise(async () => {
211 await new Promise<void>((resolve) => server.close(() => resolve()))
212 await rm(directory, { recursive: true, force: true })
213 })
214 )
215
216 interface KeepAliveConnection {
217 readonly socket: Net.Socket
218 readonly options: Net.NetConnectOpts
219 }
220
221 const withKeepAliveServer = Effect.fnUntraced(function*(options?: { readonly tls?: boolean }) {
222 const { port } = yield* withTcpServer((socket) => {
223 consumeFrontend(socket, (tag, message) => {
224 if (tag === undefined && message.equals(sslRequest)) socket.write(Buffer.from("S"))
225 else if (tag === undefined && message.equals(cancelRequest)) socket.end()
226 else if (tag === undefined) socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
227 else if (tag === "S") socket.write(emptyQueryResult)
228 })
229 })
230 const connections: Array<KeepAliveConnection> = []
231 const original = Net.connect
232 yield* Effect.acquireRelease(
233 Effect.sync(() =>
234 vi.spyOn(Net, "connect").mockImplementation(
235 ((connectOptions: Net.NetConnectOpts) => {
236 const socket = original(connectOptions)
237 if ("port" in connectOptions && connectOptions.port === port) {
238 connections.push({ socket, options: connectOptions })
239 if (options?.tls) tlsOptions.set(socket, undefined)
240 }
241 return socket
242 }) as typeof Net.connect
243 )
244 ),
245 (spy) => Effect.sync(() => spy.mockRestore())
246 )
247 return { config: { host: "127.0.0.1", port, username: "test" }, connections }
248 })
249
250 describe("PgConnection in-process server", () => {
251 describe("TCP keepalive", { concurrent: false }, () => {
252 it.live("leaves keepalives disabled by default", () =>
253 Effect.scoped(Effect.gen(function*() {
254 const { config, connections } = yield* withKeepAliveServer()
255 const connection = yield* PgConnection.make(config)
256 yield* connection.interrupt
257 assert.deepStrictEqual(connections.map(({ options }) => options.keepAlive), [undefined, undefined])
258 })))
259
260 it.live("enables keepalive at TCP connect time but not for cancellation", () =>
261 Effect.scoped(Effect.gen(function*() {
262 const { config, connections } = yield* withKeepAliveServer()
263 const connection = yield* PgConnection.make({ ...config, keepAlive: "30 seconds" })
264 yield* connection.interrupt
265 assert.deepStrictEqual(connections.map(({ options }) => [options.keepAlive, options.keepAliveInitialDelay]), [
266 [true, 30_000],
267 [undefined, undefined]
268 ])
269 })))
270
271 it.live("configures keepalive before TLS", () =>
272 Effect.scoped(Effect.gen(function*() {
273 const { config, connections } = yield* withKeepAliveServer({ tls: true })
274 yield* PgConnection.make({
275 ...config,
276 keepAlive: "15 seconds",
277 ssl: { rejectUnauthorized: false }
278 })
279 assert.deepStrictEqual(connections.map(({ options }) => [options.keepAlive, options.keepAliveInitialDelay]), [
280 [true, 15_000]
281 ])
282 assert.strictEqual(tlsOptions.get(connections[0]!.socket)?.rejectUnauthorized, false)
283 })))
284
285 it.effect("leaves caller-supplied streams unconfigured", () =>
286 Effect.gen(function*() {
287 const stream = makeSslStream("S", false)
288 const setKeepAlive = vi.fn()
289 Object.assign(stream.socket, { setKeepAlive })
290 yield* PgConnection.make({
291 username: "test",
292 ssl: true,
293 keepAlive: "30 seconds",
294 stream: () => stream.socket
295 })
296 assert.strictEqual(setKeepAlive.mock.calls.length, 0)
297 }))
298
299 it.live("leaves Unix sockets unconfigured", () =>
300 Effect.scoped(Effect.gen(function*() {
301 const { directory } = yield* withUnixServer((socket) => {
302 consumeFrontend(socket, (tag) => {
303 if (tag === undefined) socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
304 })
305 })
306 const spy = yield* Effect.acquireRelease(
307 Effect.sync(() => vi.spyOn(Net.Socket.prototype, "setKeepAlive")),
308 (spy) => Effect.sync(() => spy.mockRestore())
309 )
310 yield* PgConnection.make({ host: directory, username: "test", keepAlive: "30 seconds" })
311 assert.deepStrictEqual(spy.mock.calls, [])
312 })))
313 })
314
315 it.live("forwards startup defaults and lets explicit fields override URL values", () =>
316 Effect.scoped(Effect.gen(function*() {
317 let parameters: ReadonlyMap<string, string> | undefined
318 const { port } = yield* withTcpServer((socket) => {
319 consumeFrontend(socket, (tag, message) => {
320 if (tag === undefined) {
321 parameters = startupParameters(message)
322 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
323 }
324 })
325 })
326 const config = {
327 host: "127.0.0.1",
328 port,
329 url: Redacted.make(
330 "postgres://test@localhost/db?application_name=url-app&options=-c%20statement_timeout%3D1234"
331 ),
332 startupParameters: { SEARCH_PATH: "public", CLIENT_ENCODING: "uTf-8", application_name: "named-app" }
333 }
334 yield* PgConnection.make(config)
335 assert.deepStrictEqual(Object.fromEntries(parameters!), {
336 user: "test",
337 database: "db",
338 application_name: "named-app",
339 search_path: "public",
340 client_encoding: "UTF8",
341 options: "-c statement_timeout=1234"
342 })
343
344 yield* PgConnection.make({ ...config, applicationName: "explicit-app", startupOptions: "-c lock_timeout=2345" })
345 assert.strictEqual(parameters!.get("application_name"), "explicit-app")
346 assert.strictEqual(parameters!.get("options"), "-c lock_timeout=2345")
347 })))
348
349 it.effect("accepts backend messages over the default limit when configured", () =>
350 Effect.gen(function*() {
351 const fieldSize = 16 * 1024 * 1024
352 const field = Buffer.alloc(fieldSize, 0x78)
353 const rowDescription = backendMessage(
354 "T",
355 Buffer.concat([
356 int16(1),
357 Buffer.from("big\0"),
358 int32(0),
359 int16(0),
360 int32(25),
361 int16(-1),
362 int32(-1),
363 int16(1)
364 ])
365 )
366 const dataRow = backendMessage("D", Buffer.concat([int16(1), int32(field.length), field]))
367 const result = Buffer.concat([
368 backendMessage("1", Buffer.alloc(0)),
369 backendMessage("2", Buffer.alloc(0)),
370 rowDescription,
371 dataRow,
372 backendMessage("C", Buffer.from("SELECT 1\0")),
373 readyForQuery
374 ])
375 let writes = 0
376 const socket: Duplex = new Duplex({
377 read() {},
378 write(_chunk: Buffer, _encoding, callback) {
379 writes++
380 queueMicrotask(() => {
381 socket.push(
382 writes === 1
383 ? Buffer.concat([authenticationOk, backendKeyData, readyForQuery])
384 : writes === 2
385 ? result
386 : emptyQueryResult
387 )
388 })
389 callback()
390 }
391 })
392 const connection = yield* PgConnection.make({
393 username: "test",
394 stream: () => socket,
395 maxMessageSize: 17 * 1024 * 1024
396 })
397
398 const oversized = yield* connection.query("SELECT repeat('x', 16777216) AS big")
399 assert.strictEqual(typeof oversized.rows[0].big, "string")
400 assert.strictEqual((oversized.rows[0].big as string).length, fieldSize)
401 assert.deepStrictEqual((yield* connection.query("SELECT 1")).rows, [])
402 }))
403
404 it.effect("flushes concurrently submitted multiplexed queries in one write", () =>
405 Effect.gen(function*() {
406 const writes: Array<Buffer> = []
407 const socket: Duplex = new Duplex({
408 read() {},
409 write(chunk: Buffer, _encoding, callback) {
410 const message = Buffer.from(chunk)
411 writes.push(message)
412 if (writes.length === 1) {
413 queueMicrotask(() => socket.push(Buffer.concat([authenticationOk, backendKeyData, readyForQuery])))
414 } else {
415 const syncs = frontendTags(message).filter((tag) => tag === "S").length
416 queueMicrotask(() => socket.push(Buffer.concat(Array.from({ length: syncs }, () => emptyQueryResult))))
417 }
418 callback()
419 }
420 })
421 const connection = yield* PgConnection.make({
422 username: "test",
423 stream: () => socket,
424 multiplex: true
425 })
426
427 yield* Effect.all([
428 connection.query("SELECT 1"),
429 connection.query("SELECT 2")
430 ], { concurrency: "unbounded" })
431
432 // Both cycles left in one write, each keeping its own Sync.
433 assert.strictEqual(writes.length, 2)
434 assert.deepStrictEqual(frontendTags(writes[1]), ["P", "B", "D", "E", "S", "P", "B", "D", "E", "S"])
435 }))
436
437 it.effect("uses distinct prepared statement names across connections", () =>
438 Effect.gen(function*() {
439 const writes: Array<Array<Buffer>> = []
440 const makeStream = () => {
441 const connectionWrites: Array<Buffer> = []
442 const socket: Duplex = new Duplex({
443 read() {},
444 write(chunk: Buffer, _encoding, callback) {
445 connectionWrites.push(Buffer.from(chunk))
446 queueMicrotask(() => {
447 socket.push(
448 connectionWrites.length === 1
449 ? Buffer.concat([authenticationOk, backendKeyData, readyForQuery])
450 : emptyQueryResult
451 )
452 })
453 callback()
454 }
455 })
456 writes.push(connectionWrites)
457 return socket
458 }
459
460 const first = yield* PgConnection.make({ username: "test", stream: makeStream })
461 const second = yield* PgConnection.make({ username: "test", stream: makeStream })
462 yield* first.query("SELECT $1", [1])
463 yield* second.query("SELECT $1, $2", [1, 2])
464
465 const firstName = frontendPreparedNames(writes[0][1])
466 const secondName = frontendPreparedNames(writes[1][1])
467 assert.strictEqual(firstName.length, 1)
468 assert.strictEqual(secondName.length, 1)
469 assert.notStrictEqual(firstName[0], secondName[0])
470 }))
471
472 it.effect("reuses a prepared name after a pending pipeline query is interrupted", () =>
473 Effect.gen(function*() {
474 const writes: Array<Buffer> = []
475 const socket: Duplex = new Duplex({
476 read() {},
477 write(chunk: Buffer, _encoding, callback) {
478 const message = Buffer.from(chunk)
479 writes.push(message)
480 if (writes.length === 1) {
481 queueMicrotask(() => socket.push(Buffer.concat([authenticationOk, backendKeyData, readyForQuery])))
482 } else {
483 queueMicrotask(() => socket.push(emptyQueryResult))
484 }
485 callback()
486 }
487 })
488 const connection = yield* PgConnection.make({
489 username: "test",
490 stream: () => socket,
491 multiplex: true
492 })
493
494 const interrupted = Effect.runFork(connection.query("SELECT 1"))
495 interrupted.interruptUnsafe()
496 yield* Effect.promise(() => new Promise<void>((resolve) => queueMicrotask(resolve)))
497 yield* connection.query("SELECT 1")
498
499 assert.strictEqual(writes.length, 2)
500 const names = frontendPreparedNames(writes[1])
501 assert.strictEqual(names.length, 1)
502 assert.match(names[0], /^effect_[0-9a-f]{16}_1$/)
503 }))
504
505 it.effect("ends a custom stream after writing Terminate", () =>
506 Effect.gen(function*() {
507 const writes: Array<Buffer> = []
508 const socket: Duplex = new Duplex({
509 read() {},
510 write(chunk: Buffer, _encoding, callback) {
511 writes.push(Buffer.from(chunk))
512 if (writes.length === 1) {
513 queueMicrotask(() => socket.push(Buffer.concat([authenticationOk, backendKeyData, readyForQuery])))
514 }
515 callback()
516 }
517 })
518
519 yield* Effect.scoped(PgConnection.make({ username: "test", stream: () => socket }))
520
521 assert.strictEqual(writes.at(-1)?.toString("hex"), "5800000004")
522 assert.isTrue(socket.writableEnded)
523 assert.isFalse(socket.destroyed)
524 }))
525
526 it.live.each(["statement", "stream"] as const)(
527 "does not start a %s while its pin is being released",
528 (kind) =>
529 Effect.gen(function*() {
530 // Each budget makes the work first yield at a later step of its admission,
531 // so the pin release lands in every gap. Budgets below 3 make no progress.
532 for (let budget = 3; budget <= 40; budget++) {
533 yield* Effect.scoped(Effect.gen(function*() {
534 let writes = 0
535 // Answers startup and never answers a statement.
536 const socket: Duplex = new Duplex({
537 read() {},
538 write(_chunk: Buffer, _encoding, callback) {
539 if (++writes === 1) {
540 queueMicrotask(() => socket.push(Buffer.concat([authenticationOk, backendKeyData, readyForQuery])))
541 }
542 callback()
543 }
544 })
545 const connection = yield* PgConnection.make({ username: "test", stream: () => socket })
546 const scope = yield* Scope.make()
547 const pinned = yield* Scope.provide(connection.pin, scope)
548 const work = kind === "statement" ? pinned.query("SELECT 1") : Stream.runDrain(pinned.stream("SELECT 1"))
549 const fiber = yield* Effect.forkChild(Effect.provideService(work, Scheduler.MaxOpsBeforeYield, budget), {
550 startImmediately: true
551 })
552 const closing = yield* Effect.forkDetach(Scope.close(scope, Exit.void))
553 const closed = yield* Fiber.await(closing).pipe(Effect.timeoutOption("2 seconds"))
554 if (Option.isNone(closed)) socket.destroy()
555 assert.isTrue(Option.isSome(closed), `pin release waited on a ${kind} at budget ${budget}`)
556 const exit = yield* Fiber.await(fiber).pipe(Effect.timeoutOption("2 seconds"))
557 assert.isTrue(Option.isSome(exit) && Exit.isFailure(exit.value), `${kind} did not end at budget ${budget}`)
558 }))
559 }
560 })
561 )
562
563 describe("TLS servername", () => {
564 const cases: ReadonlyArray<{
565 readonly name: string
566 readonly config: PgConnection.Config
567 readonly servername: string | undefined
568 }> = [
569 { name: "DNS host with ssl=true", config: { host: "db.example.com", ssl: true }, servername: "db.example.com" },
570 {
571 name: "DNS host with SSL options",
572 config: { host: "db.example.com", ssl: { rejectUnauthorized: false } },
573 servername: "db.example.com"
574 },
575 {
576 name: "DNS host from a URL",
577 config: { url: Redacted.make("postgres://test@db.example.com/db?sslmode=require") },
578 servername: "db.example.com"
579 },
580 {
581 name: "ssl=true overrides sslmode=disable",
582 config: { url: Redacted.make("postgres://test@db.example.com/db?sslmode=disable"), ssl: true },
583 servername: "db.example.com"
584 },
585 ...["prefer", "allow"].flatMap((sslmode) => [
586 {
587 name: `sslmode=${sslmode} enables TLS`,
588 config: { url: Redacted.make(`postgresql://test@db.example.com/db?sslmode=${sslmode}`) },
589 servername: "db.example.com"
590 },
591 {
592 name: `SSL options override sslmode=${sslmode}`,
593 config: {
594 url: Redacted.make(`postgres://test@db.example.com/db?sslmode=${sslmode}`),
595 ssl: { servername: "routing.example.com", rejectUnauthorized: false }
596 },
597 servername: "routing.example.com"
598 }
599 ]),
600 { name: "IPv4 host", config: { host: "127.0.0.1", ssl: true }, servername: undefined },
601 { name: "IPv6 host", config: { host: "::1", ssl: true }, servername: undefined },
602 ...["db.example.com", "127.0.0.1", "::1"].map((host) => ({
603 name: `explicit servername for ${host}`,
604 config: { host, ssl: { servername: "routing.example.com", rejectUnauthorized: false } },
605 servername: "routing.example.com"
606 })),
607 {
608 name: "explicit empty servername",
609 config: { host: "db.example.com", ssl: { servername: "" } },
610 servername: ""
611 }
612 ]
613
614 for (const path of ["startup", "cancel"] as const) {
615 describe(path, () => {
616 it.effect.each(cases)("$name", ({ config, servername }) =>
617 Effect.gen(function*() {
618 const streams: Array<ReturnType<typeof makeSslStream>> = []
619 const connection = yield* PgConnection.make({
620 username: "test",
621 ...config,
622 stream: () => {
623 const stream = makeSslStream("S", streams.length > 0)
624 streams.push(stream)
625 return stream.socket
626 }
627 })
628 if (path === "cancel") {
629 yield* connection.interrupt
630 }
631
632 assert.strictEqual(streams.length, path === "cancel" ? 2 : 1)
633 const stream = streams[path === "cancel" ? 1 : 0]
634 assert.deepStrictEqual(stream.writes[0], sslRequest)
635 if (path === "cancel") {
636 assert.deepStrictEqual(stream.writes[1], cancelRequest)
637 } else {
638 assert.strictEqual(stream.writes[1].readInt32BE(4), 196608)
639 assert.strictEqual(startupParameters(stream.writes[1]).get("user"), "test")
640 }
641 const options = tlsOptions.get(stream.socket)
642 assert.isDefined(options)
643 assert.strictEqual(options!.servername, servername)
644 if (typeof config.ssl === "object") {
645 assert.strictEqual(options!.rejectUnauthorized, config.ssl.rejectUnauthorized)
646 } else {
647 assert.notStrictEqual(options!.rejectUnauthorized, false)
648 }
649 }))
650 })
651 }
652 })
653
654 it.effect.each(["prefer", "allow"])(
655 "ssl=false overrides sslmode=%s with plaintext startup",
656 (sslmode) =>
657 Effect.gen(function*() {
658 const writes: Array<Buffer> = []
659 const socket: Duplex = new Duplex({
660 read() {},
661 write(chunk: Buffer, _encoding, callback) {
662 writes.push(Buffer.from(chunk))
663 if (writes.length === 1) {
664 queueMicrotask(() => socket.push(Buffer.concat([authenticationOk, backendKeyData, readyForQuery])))
665 }
666 callback()
667 }
668 })
669 tlsOptions.set(socket, undefined)
670
671 yield* PgConnection.make({
672 url: Redacted.make(`postgresql://test@db.example.com/db?sslmode=${sslmode}`),
673 ssl: false,
674 stream: () => socket
675 })
676
677 assert.strictEqual(writes.length, 1)
678 assert.strictEqual(writes[0].readInt32BE(4), 196608)
679 assert.strictEqual(startupParameters(writes[0]).get("user"), "test")
680 assert.isUndefined(tlsOptions.get(socket))
681 })
682 )
683
684 it.effect.each(["require", "verify-ca", "verify-full"])(
685 "sslmode=%s fails when the server refuses TLS",
686 (sslmode) =>
687 Effect.gen(function*() {
688 const writes: Array<Buffer> = []
689 const socket: Duplex = new Duplex({
690 read() {},
691 write(chunk: Buffer, _encoding, callback) {
692 writes.push(Buffer.from(chunk))
693 queueMicrotask(() => socket.push(Buffer.from("N")))
694 callback()
695 }
696 })
697 const error = yield* Effect.flip(PgConnection.make({
698 url: Redacted.make(`postgres://test@db.example.com/db?sslmode=${sslmode}`),
699 stream: () => socket
700 }))
701
702 assert.deepStrictEqual(writes, [Buffer.concat([int32(8), int32(80877103)])])
703 assert.strictEqual(error.reason._tag, "ConnectionError")
704 assert.strictEqual(error.reason.message, "PgConnection: Server refused TLS")
705 assert.isTrue(socket.destroyed)
706 })
707 )
708
709 describe("TLS fallback", () => {
710 it.effect.each(["prefer", "allow"])("sslmode=%s starts in plaintext after N", (sslmode) =>
711 Effect.gen(function*() {
712 const stream = makeSslStream("N", false)
713 let connections = 0
714 yield* PgConnection.make({
715 url: Redacted.make(`postgres://test@db.example.com/db?sslmode=${sslmode}`),
716 stream: () => {
717 connections++
718 return stream.socket
719 }
720 })
721
722 assert.strictEqual(connections, 1)
723 assert.strictEqual(stream.writes.length, 2)
724 assert.deepStrictEqual(stream.writes[0], sslRequest)
725 assert.strictEqual(stream.writes[1].readInt32BE(4), 196608)
726 assert.strictEqual(startupParameters(stream.writes[1]).get("user"), "test")
727 assert.isUndefined(tlsOptions.get(stream.socket))
728 }))
729
730 it.effect.each(["prefer", "allow"])(
731 "sslmode=%s allows plaintext cancellation for a plaintext session",
732 (sslmode) =>
733 Effect.gen(function*() {
734 const streams: Array<ReturnType<typeof makeSslStream>> = []
735 const connection = yield* PgConnection.make({
736 url: Redacted.make(`postgres://test@db.example.com/db?sslmode=${sslmode}`),
737 stream: () => {
738 const stream = makeSslStream("N", streams.length > 0)
739 streams.push(stream)
740 return stream.socket
741 }
742 })
743 yield* connection.interrupt
744
745 assert.strictEqual(streams.length, 2)
746 assert.isUndefined(tlsOptions.get(streams[0].socket))
747 assert.deepStrictEqual(streams[0].writes[0], sslRequest)
748 assert.strictEqual(streams[0].writes[1].readInt32BE(4), 196608)
749 assert.deepStrictEqual(streams[1].writes, [sslRequest, cancelRequest])
750 assert.isUndefined(tlsOptions.get(streams[1].socket))
751 assert.isTrue(streams[1].socket.destroyed)
752 })
753 )
754
755 it.effect.each(["prefer", "allow", "require"])(
756 "sslmode=%s refuses plaintext cancellation for a TLS session",
757 (sslmode) =>
758 Effect.gen(function*() {
759 const streams: Array<ReturnType<typeof makeSslStream>> = []
760 const connection = yield* PgConnection.make({
761 url: Redacted.make(`postgres://test@db.example.com/db?sslmode=${sslmode}`),
762 stream: () => {
763 const cancel = streams.length > 0
764 const stream = makeSslStream(cancel ? "N" : "S", cancel)
765 streams.push(stream)
766 return stream.socket
767 }
768 })
769 yield* connection.interrupt
770
771 assert.strictEqual(streams.length, 2)
772 assert.isDefined(tlsOptions.get(streams[0].socket))
773 assert.deepStrictEqual(streams[1].writes, [sslRequest])
774 assert.isUndefined(tlsOptions.get(streams[1].socket))
775 assert.isTrue(streams[1].socket.destroyed)
776 })
777 )
778
779 for (const sslmode of ["prefer", "allow"]) {
780 it.effect.each([true, { rejectUnauthorized: false }] as const)(
781 `explicit ssl=%j prevents sslmode=${sslmode} fallback`,
782 (ssl) =>
783 Effect.gen(function*() {
784 const stream = makeSslStream("N", false)
785 const error = yield* Effect.flip(PgConnection.make({
786 url: Redacted.make(`postgres://test@db.example.com/db?sslmode=${sslmode}`),
787 ssl,
788 stream: () => stream.socket
789 }))
790
791 assert.deepStrictEqual(stream.writes, [sslRequest])
792 assert.strictEqual(error.reason._tag, "ConnectionError")
793 assert.strictEqual(error.reason.message, "PgConnection: Server refused TLS")
794 assert.isUndefined(tlsOptions.get(stream.socket))
795 assert.isTrue(stream.socket.destroyed)
796 })
797 )
798 }
799 })
800
801 it.live("preserves an ErrorResponse returned for SSLRequest", () =>
802 Effect.scoped(Effect.gen(function*() {
803 const { port } = yield* withTcpServer((socket) => {
804 socket.once("data", () => {
805 socket.end(errorResponse([
806 ["S", "FATAL"],
807 ["C", "08004"],
808 ["M", "TLS is disabled by the proxy"]
809 ]))
810 })
811 })
812
813 const error = yield* Effect.flip(PgConnection.make({
814 host: "127.0.0.1",
815 port,
816 username: "test",
817 ssl: true
818 }))
819
820 assert.strictEqual(error.reason._tag, "ConnectionError")
821 assert.strictEqual(error.reason.message, "PgConnection: Failed to negotiate TLS")
822 assert.instanceOf(error.reason.cause, Error)
823 assert.strictEqual(error.reason.cause.message, "TLS is disabled by the proxy")
824 })))
825
826 it.live("rejects SCRAM authentication without server verification", () =>
827 Effect.scoped(Effect.gen(function*() {
828 const { port } = yield* withTcpServer((socket) => {
829 consumeFrontend(socket, (tag) => {
830 if (tag === undefined) {
831 socket.write(authentication(10, Buffer.from("SCRAM-SHA-256\0\0")))
832 } else if (tag === "p") {
833 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
834 }
835 })
836 })
837
838 const error = yield* Effect.flip(PgConnection.make({
839 host: "127.0.0.1",
840 port,
841 username: "test",
842 password: Redacted.make("secret")
843 }))
844
845 assert.strictEqual(error.reason._tag, "AuthenticationError")
846 assert.strictEqual(error.reason.message, "PgConnection: SCRAM exchange did not complete")
847 })))
848
849 it.live("rejects incomplete SCRAM when AuthenticationOk is omitted", () =>
850 Effect.scoped(Effect.gen(function*() {
851 const { port } = yield* withTcpServer((socket) => {
852 consumeFrontend(socket, (tag) => {
853 if (tag === undefined) {
854 socket.write(authentication(10, Buffer.from("SCRAM-SHA-256\0\0")))
855 } else if (tag === "p") {
856 socket.write(Buffer.concat([backendKeyData, readyForQuery]))
857 }
858 })
859 })
860
861 const error = yield* Effect.flip(PgConnection.make({
862 host: "127.0.0.1",
863 port,
864 username: "test",
865 password: Redacted.make("secret")
866 }))
867
868 assert.strictEqual(error.reason._tag, "AuthenticationError")
869 assert.strictEqual(error.reason.message, "PgConnection: SCRAM exchange did not complete")
870 })))
871
872 it.live("lets URL query host and port override the authority", () =>
873 Effect.scoped(Effect.gen(function*() {
874 const { port } = yield* withTcpServer((socket) => {
875 consumeFrontend(socket, (tag) => {
876 if (tag === undefined) {
877 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
878 }
879 })
880 })
881
882 const connection = yield* PgConnection.make({
883 url: Redacted.make(`postgres://test@invalid.invalid:1/db?host=127.0.0.1&port=${port}`)
884 })
885
886 assert.strictEqual(connection.processId, 1234)
887 })))
888
889 it.effect("treats URL connect_timeout=0 as no timeout", () =>
890 Effect.scoped(Effect.gen(function*() {
891 let sendReady: (() => void) | undefined
892 let signalStartup!: () => void
893 const startup = new Promise<void>((resolve) => {
894 signalStartup = resolve
895 })
896 const { port } = yield* withTcpServer((socket) => {
897 consumeFrontend(socket, (tag) => {
898 if (tag === undefined) {
899 sendReady = () => socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
900 signalStartup()
901 }
902 })
903 })
904
905 const fiber = yield* PgConnection.make({
906 url: Redacted.make(`postgres://test@127.0.0.1:${port}/db?connect_timeout=0`)
907 }).pipe(Effect.forkScoped)
908 yield* Effect.promise(() => startup)
909 yield* TestClock.adjust("6 seconds")
910 sendReady!()
911
912 const connection = yield* Fiber.join(fiber)
913 assert.strictEqual(connection.processId, 1234)
914 })))
915
916 it.live("classifies startup SQLSTATE errors with the shared table", () =>
917 Effect.scoped(Effect.gen(function*() {
918 const { port } = yield* withTcpServer((socket) => {
919 consumeFrontend(socket, (tag) => {
920 if (tag === undefined) {
921 socket.end(errorResponse([
922 ["S", "ERROR"],
923 ["C", "23505"],
924 ["M", "duplicate key value violates unique constraint"],
925 ["n", " users_email_key "]
926 ]))
927 }
928 })
929 })
930
931 const error = yield* Effect.flip(PgConnection.make({
932 host: "127.0.0.1",
933 port,
934 username: "test"
935 }))
936
937 assert.strictEqual(error.reason._tag, "UniqueViolation")
938 if (error.reason._tag === "UniqueViolation") {
939 assert.strictEqual(error.reason.constraint, "users_email_key")
940 }
941 })))
942
943 it.live("preserves a FATAL ErrorResponse sent before the server closes the connection", () =>
944 Effect.scoped(Effect.gen(function*() {
945 const { port } = yield* withTcpServer((socket) => {
946 consumeFrontend(socket, (tag) => {
947 if (tag === undefined) {
948 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
949 } else if (tag === "S") {
950 socket.end(errorResponse([
951 ["S", "FATAL"],
952 ["C", "25P04"],
953 ["M", "terminating connection due to transaction timeout"]
954 ]))
955 }
956 })
957 })
958 const connection = yield* PgConnection.make({ host: "127.0.0.1", port, username: "test" })
959
960 const error = yield* Effect.flip(connection.query("SELECT pg_sleep(1)"))
961
962 assert.strictEqual(error.reason._tag, "UnknownError")
963 assert.strictEqual(error.reason.message, "PgConnection: Query failed")
964 assert.instanceOf(error.reason.cause, Error)
965 assert.strictEqual(error.reason.cause.message, "terminating connection due to transaction timeout")
966 assert.strictEqual((error.reason.cause as { readonly code?: string }).code, "25P04")
967 })))
968
969 it.live("sends Terminate when the connection scope closes", () =>
970 Effect.scoped(Effect.gen(function*() {
971 let signalTerminate!: (message: Buffer) => void
972 const terminated = new Promise<Buffer>((resolve) => {
973 signalTerminate = resolve
974 })
975 const { port } = yield* withTcpServer((socket) => {
976 consumeFrontend(socket, (tag, message) => {
977 if (tag === undefined) {
978 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
979 } else if (tag === "X") {
980 signalTerminate(message)
981 }
982 })
983 })
984
985 yield* Effect.scoped(PgConnection.make({ host: "127.0.0.1", port, username: "test" }))
986 const message = yield* Effect.promise(() => terminated)
987
988 assert.strictEqual(message.toString("hex"), "5800000004")
989 })))
990
991 it.live("authenticates with a cleartext password request", () =>
992 Effect.scoped(Effect.gen(function*() {
993 let password: string | undefined
994 const { port } = yield* withTcpServer((socket) => {
995 consumeFrontend(socket, (tag, message) => {
996 if (tag === undefined) {
997 socket.write(authentication(3))
998 } else if (tag === "p") {
999 password = message.subarray(5, -1).toString()
1000 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
1001 }
1002 })
1003 })
1004
1005 const connection = yield* PgConnection.make({
1006 host: "127.0.0.1",
1007 port,
1008 username: "test",
1009 password: Redacted.make("secret")
1010 })
1011
1012 assert.strictEqual(connection.processId, 1234)
1013 assert.strictEqual(password, "secret")
1014 })))
1015
1016 it.live("resolves a password Effect for each connection", () =>
1017 Effect.gen(function*() {
1018 const passwords: Array<string> = []
1019 const { port } = yield* withTcpServer((socket) => {
1020 consumeFrontend(socket, (tag, message) => {
1021 if (tag === undefined) {
1022 socket.write(authentication(3))
1023 } else if (tag === "p") {
1024 passwords.push(message.subarray(5, -1).toString())
1025 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
1026 }
1027 })
1028 })
1029 let calls = 0
1030 const acquire = PgConnection.make({
1031 host: "127.0.0.1",
1032 port,
1033 username: "test",
1034 password: Effect.sync(() => Redacted.make(`secret-${++calls}`))
1035 })
1036
1037 yield* Effect.scoped(acquire)
1038 yield* acquire
1039
1040 assert.strictEqual(calls, 2)
1041 assert.deepStrictEqual(passwords, ["secret-1", "secret-2"])
1042 }))
1043
1044 it.live("authenticates with an MD5 password request", () =>
1045 Effect.scoped(Effect.gen(function*() {
1046 let password: string | undefined
1047 const { port } = yield* withTcpServer((socket) => {
1048 consumeFrontend(socket, (tag, message) => {
1049 if (tag === undefined) {
1050 socket.write(authentication(5, Buffer.from([1, 2, 3, 4])))
1051 } else if (tag === "p") {
1052 password = message.subarray(5, -1).toString()
1053 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
1054 }
1055 })
1056 })
1057
1058 const connection = yield* PgConnection.make({
1059 host: "127.0.0.1",
1060 port,
1061 username: "test",
1062 password: Redacted.make("secret")
1063 })
1064
1065 assert.strictEqual(connection.processId, 1234)
1066 assert.strictEqual(password, "md5594f15006e55e17dff918116ee00778f")
1067 })))
1068
1069 it.live("uses a caller-supplied stream factory", () =>
1070 Effect.scoped(Effect.gen(function*() {
1071 let streamCalls = 0
1072 const { port } = yield* withTcpServer((socket) => {
1073 consumeFrontend(socket, (tag) => {
1074 if (tag === undefined) {
1075 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
1076 }
1077 })
1078 })
1079
1080 const connection = yield* PgConnection.make({
1081 host: "invalid.invalid",
1082 port: 1,
1083 username: "test",
1084 stream: () => {
1085 streamCalls++
1086 return Net.connect({ host: "127.0.0.1", port })
1087 }
1088 })
1089
1090 assert.strictEqual(connection.processId, 1234)
1091 assert.strictEqual(streamCalls, 1)
1092 })))
1093
1094 it.live("derives a unix socket path from a slash-prefixed host", () =>
1095 Effect.scoped(Effect.gen(function*() {
1096 const { directory } = yield* withUnixServer((socket) => {
1097 consumeFrontend(socket, (tag) => {
1098 if (tag === undefined) {
1099 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
1100 }
1101 })
1102 })
1103
1104 const connection = yield* PgConnection.make({ host: directory, username: "test" })
1105
1106 assert.strictEqual(connection.processId, 1234)
1107 })))
1108
1109 it.live("lets explicit fields override URL connection and startup values", () =>
1110 Effect.scoped(Effect.gen(function*() {
1111 let parameters: ReadonlyMap<string, string> | undefined
1112 const { port } = yield* withTcpServer((socket) => {
1113 consumeFrontend(socket, (tag, message) => {
1114 if (tag === undefined) {
1115 parameters = startupParameters(message)
1116 socket.write(Buffer.concat([authenticationOk, backendKeyData, readyForQuery]))
1117 }
1118 })
1119 })
1120
1121 const connection = yield* PgConnection.make({
1122 url: Redacted.make("postgres://url-user@invalid.invalid:1/url-db?application_name=url-app"),
1123 host: "127.0.0.1",
1124 port,
1125 username: "explicit-user",
1126 database: "explicit-db",
1127 applicationName: "explicit-app"
1128 })
1129
1130 assert.strictEqual(connection.processId, 1234)
1131 assert.strictEqual(parameters!.get("user"), "explicit-user")
1132 assert.strictEqual(parameters!.get("database"), "explicit-db")
1133 assert.strictEqual(parameters!.get("application_name"), "explicit-app")
1134 assert.strictEqual(parameters!.get("client_encoding"), "UTF8")
1135 })))
1136 })