T-200: edits made in the worktree
This commit is contained in:
parent
872debd9c6
commit
e720c879df
4 changed files with 158 additions and 179 deletions
|
|
@ -1,9 +1,8 @@
|
|||
import { EventEmitter } from 'node:events';
|
||||
import { createServer } from 'node:net';
|
||||
import { type Socket, createServer } from 'node:net';
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { PoolConfig } from 'pg';
|
||||
import {
|
||||
RESEARCH_COPILOT_PG_CONNECT_TIMEOUT_MS,
|
||||
ResearchCopilotExecutorError,
|
||||
type ResearchCopilotPoolLike,
|
||||
openResearchCopilotExecutor,
|
||||
|
|
@ -197,10 +196,15 @@ describe('openResearchCopilotExecutor edge cases', () => {
|
|||
});
|
||||
|
||||
test(
|
||||
'enforces real pg.Pool connectionTimeoutMillis against a silent local TCP listener',
|
||||
'rejects silent local TCP listener via real pg.Pool connectionTimeoutMillis without leaking credentials',
|
||||
async () => {
|
||||
const sockets = new Set<Socket>();
|
||||
const server = createServer(socket => {
|
||||
// Intentionally accept and hold the TCP connection silent without PostgreSQL handshake.
|
||||
sockets.add(socket);
|
||||
socket.on('close', () => {
|
||||
sockets.delete(socket);
|
||||
});
|
||||
socket.on('error', () => {});
|
||||
});
|
||||
|
||||
|
|
@ -217,7 +221,6 @@ describe('openResearchCopilotExecutor edge cases', () => {
|
|||
});
|
||||
|
||||
const secret = 'silent_tcp_secret_99';
|
||||
const startedAt = Date.now();
|
||||
let caught: unknown;
|
||||
try {
|
||||
await openResearchCopilotExecutor({
|
||||
|
|
@ -226,21 +229,20 @@ describe('openResearchCopilotExecutor edge cases', () => {
|
|||
} catch (error) {
|
||||
caught = error;
|
||||
} finally {
|
||||
for (const socket of sockets) {
|
||||
socket.destroy();
|
||||
}
|
||||
await new Promise<void>(resolve => {
|
||||
server.close(() => resolve());
|
||||
});
|
||||
}
|
||||
|
||||
const elapsedMs = Date.now() - startedAt;
|
||||
expect(caught).toBeInstanceOf(ResearchCopilotExecutorError);
|
||||
const err = caught as ResearchCopilotExecutorError;
|
||||
expect(err.code).toBe('CONNECT_FAILED');
|
||||
expect(err.message).not.toContain(secret);
|
||||
expect(err.stack ?? '').not.toContain(secret);
|
||||
expect('cause' in err).toBe(false);
|
||||
// Bounded around the 5s connectionTimeoutMillis (allow scheduling jitter).
|
||||
expect(elapsedMs).toBeGreaterThanOrEqual(RESEARCH_COPILOT_PG_CONNECT_TIMEOUT_MS - 500);
|
||||
expect(elapsedMs).toBeLessThan(RESEARCH_COPILOT_PG_CONNECT_TIMEOUT_MS + 5_000);
|
||||
},
|
||||
15_000
|
||||
);
|
||||
|
|
|
|||
|
|
@ -77,11 +77,23 @@ describe('openResearchCopilotExecutor', () => {
|
|||
{ postgresUrl: 'postgresql:/research_db?host=%2Fvar%2Frun%2Fpostgresql' },
|
||||
],
|
||||
['missing host and host query param', { postgresUrl: 'postgres:///research_db' }],
|
||||
['hostaddr-only URL (ignored by pg)', { postgresUrl: 'postgres:///research_db?hostaddr=127.0.0.1' }],
|
||||
['empty authority with port only', { postgresUrl: 'postgres://:5432/research_db' }],
|
||||
['missing database name', { postgresUrl: 'postgres://u:supersecret@localhost:5432' }],
|
||||
['trailing slash only (missing database)', { postgresUrl: 'postgres://u:supersecret@localhost:5432/' }],
|
||||
[
|
||||
'dbname-only URL with empty path (pg falls back to PGDATABASE)',
|
||||
{ postgresUrl: 'postgres://copilot:supersecret@db.example/?dbname=research' },
|
||||
],
|
||||
[
|
||||
'dbname-only URL without trailing slash',
|
||||
{ postgresUrl: 'postgres://copilot@db.example?dbname=research' },
|
||||
],
|
||||
['blank percent-encoded database', { postgresUrl: 'postgres://u:supersecret@localhost:5432/%20' }],
|
||||
['port zero', { postgresUrl: 'postgres://u:supersecret@localhost:0/research_db' }],
|
||||
['query port zero overriding authority port', { postgresUrl: 'postgres://u:supersecret@localhost:5432/research_db?port=0' }],
|
||||
['query port out of range', { postgresUrl: 'postgres://u:supersecret@localhost:5432/research_db?port=70000' }],
|
||||
['non-numeric query port', { postgresUrl: 'postgres://u:supersecret@localhost:5432/research_db?port=abc' }],
|
||||
['invalid percent-encoding in password', { postgresUrl: 'postgres://u:%ZZsupersecret@localhost:5432/research_db' }],
|
||||
['control character in URL', { postgresUrl: 'postgres://u:supersecret@localhost:5432/research_db\n' }],
|
||||
[
|
||||
|
|
@ -267,7 +279,7 @@ describe('openResearchCopilotExecutor', () => {
|
|||
});
|
||||
|
||||
describe('idle-client error handling', () => {
|
||||
test('registers error listener and sanitizes credential-bearing idle errors', async () => {
|
||||
test('registers error listener and always wraps idle errors with a fixed credential-free error', async () => {
|
||||
const fake = createFakePool();
|
||||
const received: Error[] = [];
|
||||
const secret = 'idle_secret_99';
|
||||
|
|
@ -285,16 +297,25 @@ describe('openResearchCopilotExecutor', () => {
|
|||
|
||||
expect(fake.emitter.listenerCount('error')).toBe(1);
|
||||
|
||||
const safeErr = new Error('terminating connection due to administrator command');
|
||||
fake.emitter.emit('error', safeErr);
|
||||
const benignLookingErr = Object.assign(
|
||||
new Error('terminating connection due to administrator command', {
|
||||
cause: new Error('env PGPASSWORD=from_env_secret'),
|
||||
}),
|
||||
{ detail: 'pgpass credential leak', extraDiag: 'hidden' }
|
||||
);
|
||||
fake.emitter.emit('error', benignLookingErr);
|
||||
fake.emitter.emit('error', new Error(`fatal idle error with ${secret}`));
|
||||
|
||||
expect(received).toHaveLength(2);
|
||||
expect(received[0]).toBe(safeErr);
|
||||
expect(received[1]).toBeInstanceOf(ResearchCopilotExecutorError);
|
||||
expect((received[1] as ResearchCopilotExecutorError).code).toBe('POOL_ERROR');
|
||||
expect(received[1]?.message).not.toContain(secret);
|
||||
expect('cause' in (received[1] as Error)).toBe(false);
|
||||
for (const err of received) {
|
||||
expect(err).not.toBe(benignLookingErr);
|
||||
expect(err).toBeInstanceOf(ResearchCopilotExecutorError);
|
||||
expect((err as ResearchCopilotExecutorError).code).toBe('POOL_ERROR');
|
||||
expect(err.message).toBe('PostgreSQL research copilot pool encountered an idle client error.');
|
||||
expect('cause' in err).toBe(false);
|
||||
expect('detail' in err).toBe(false);
|
||||
expect('extraDiag' in err).toBe(false);
|
||||
}
|
||||
|
||||
await handle.close();
|
||||
});
|
||||
|
|
@ -363,17 +384,37 @@ describe('openResearchCopilotExecutor', () => {
|
|||
expect(fake.getEndCalls()).toBe(1);
|
||||
});
|
||||
|
||||
test('forwards queries with cloned params and preserves SQLSTATE 23505', async () => {
|
||||
test('forwards queries with cloned params, copies only SQLSTATE 23505, and never exposes raw driver errors or fields', async () => {
|
||||
const rawDuplicateErr = Object.assign(
|
||||
new Error('duplicate key value violates unique constraint', {
|
||||
cause: new Error('raw driver cause with env password'),
|
||||
}),
|
||||
{
|
||||
code: '23505',
|
||||
detail: 'Key (id)=(f-1) already exists.',
|
||||
connectionDiagnostics: { password: 'env_password' },
|
||||
}
|
||||
);
|
||||
const rawOtherSqlStateErr = Object.assign(
|
||||
new Error('relation "missing" does not exist', {
|
||||
cause: new Error('inner pgpass secret'),
|
||||
}),
|
||||
{
|
||||
code: '42P01',
|
||||
detail: 'sensitive detail',
|
||||
}
|
||||
);
|
||||
|
||||
const fake = createFakePool({
|
||||
onQuery: async (text, values) => {
|
||||
if (text === 'SELECT 1') {
|
||||
return { rows: [{ ok: 1 }] };
|
||||
}
|
||||
if (text.startsWith('INSERT')) {
|
||||
const pgErr = Object.assign(new Error('duplicate key value violates unique constraint'), {
|
||||
code: '23505',
|
||||
});
|
||||
throw pgErr;
|
||||
throw rawDuplicateErr;
|
||||
}
|
||||
if (text === 'SELECT * FROM missing') {
|
||||
throw rawOtherSqlStateErr;
|
||||
}
|
||||
return { rows: [{ echo: values }] };
|
||||
},
|
||||
|
|
@ -394,7 +435,26 @@ describe('openResearchCopilotExecutor', () => {
|
|||
} catch (error) {
|
||||
duplicateErr = error;
|
||||
}
|
||||
expect(duplicateErr).not.toBe(rawDuplicateErr);
|
||||
expect(duplicateErr).toBeInstanceOf(Error);
|
||||
expect((duplicateErr as { code?: string }).code).toBe('23505');
|
||||
expect((duplicateErr as Error).message).toBe('PostgreSQL research copilot query failed.');
|
||||
expect('cause' in (duplicateErr as Error)).toBe(false);
|
||||
expect('detail' in (duplicateErr as Error)).toBe(false);
|
||||
expect('connectionDiagnostics' in (duplicateErr as Error)).toBe(false);
|
||||
|
||||
let otherErr: unknown;
|
||||
try {
|
||||
await handle.executor.query('SELECT * FROM missing');
|
||||
} catch (error) {
|
||||
otherErr = error;
|
||||
}
|
||||
expect(otherErr).not.toBe(rawOtherSqlStateErr);
|
||||
expect(otherErr).toBeInstanceOf(ResearchCopilotExecutorError);
|
||||
expect((otherErr as ResearchCopilotExecutorError).code).toBe('QUERY_FAILED');
|
||||
expect((otherErr as Error).message).toBe('PostgreSQL research copilot query failed.');
|
||||
expect('cause' in (otherErr as Error)).toBe(false);
|
||||
expect('detail' in (otherErr as Error)).toBe(false);
|
||||
|
||||
await handle.close();
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue