chore(m2): execute host/quant verification, fix live blockers, and record M2 closure evidence (T-215)

This commit is contained in:
Boki 2026-12-06 11:25:07 -05:00
parent 87ae19e5f9
commit a5184bb417
21 changed files with 1370 additions and 74 deletions

View file

@ -62,6 +62,7 @@ const STRIPPED_TIMEOUT_PARAMS: ReadonlySet<string> = new Set([
const POSTGRES_URL_PREFIX_RE = /^postgres(?:ql)?:\/\//i;
const PORT_DIGITS_RE = /^\d+$/;
const CONTROL_CHAR_RE = /[\r\n\0]/;
const SCHEMA_IDENT_RE = /^[a-z_][a-z0-9_]*$/;
/**
* Sanitized error thrown by {@link openResearchCopilotExecutor} and the
@ -82,6 +83,7 @@ export class ResearchCopilotExecutorError extends Error {
/** Configuration accepted by {@link openResearchCopilotExecutor}. */
export interface ResearchCopilotExecutorConfig {
readonly postgresUrl: string;
readonly postgresSchema?: string;
}
/** Minimal `pg.Pool` surface required by {@link openResearchCopilotExecutor}. */
@ -237,6 +239,14 @@ function parseAndNormalizePoolConfig(config: ResearchCopilotExecutorConfig): Poo
const envOptions = typeof process.env.PGOPTIONS === 'string' ? process.env.PGOPTIONS.trim() : '';
const baseOptions = existingOptions.length > 0 ? existingOptions.join(' ') : envOptions;
let schemaOption = '';
if (config.postgresSchema !== undefined) {
if (typeof config.postgresSchema !== 'string' || !SCHEMA_IDENT_RE.test(config.postgresSchema)) {
throw invalidConfigError();
}
schemaOption = `-c search_path=${config.postgresSchema},public`;
}
const poolConfig: PoolConfig = {
connectionString: parsed.toString(),
max: RESEARCH_COPILOT_PG_POOL_MAX,
@ -246,8 +256,9 @@ function parseAndNormalizePoolConfig(config: ResearchCopilotExecutorConfig): Poo
lock_timeout: RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS,
};
if (baseOptions.length > 0) {
poolConfig.options = `${baseOptions} -c statement_timeout=${RESEARCH_COPILOT_PG_STATEMENT_TIMEOUT_MS} -c lock_timeout=${RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS}`;
const combinedBase = [baseOptions, schemaOption].filter(s => s.length > 0).join(' ');
if (combinedBase.length > 0) {
poolConfig.options = `${combinedBase} -c statement_timeout=${RESEARCH_COPILOT_PG_STATEMENT_TIMEOUT_MS} -c lock_timeout=${RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS}`;
}
return poolConfig;

View file

@ -156,6 +156,58 @@ describe('openResearchCopilotExecutor', () => {
}
});
test('applies validated postgresSchema to pool search_path options and rejects invalid schemas', async () => {
const previousPgOptions = process.env.PGOPTIONS;
delete process.env.PGOPTIONS;
try {
const capturedConfigs: PoolConfig[] = [];
const fake = createFakePool();
const handle = await openResearchCopilotExecutor(
{ postgresUrl: VALID_URL, postgresSchema: 'm2_ec1_signoff' },
{
createPool(poolConfig) {
capturedConfigs.push(poolConfig);
return fake.pool;
},
}
);
expect(capturedConfigs).toHaveLength(1);
expect(capturedConfigs[0]?.options).toBe(
`-c search_path=m2_ec1_signoff,public -c statement_timeout=${RESEARCH_COPILOT_PG_STATEMENT_TIMEOUT_MS} -c lock_timeout=${RESEARCH_COPILOT_PG_LOCK_TIMEOUT_MS}`
);
await handle.close();
let createPoolCalls = 0;
for (const badSchema of ['', 'UPPER', '1bad', 'a-b', 'public; DROP TABLE x', 'a b']) {
let caught: unknown;
try {
await openResearchCopilotExecutor(
{ postgresUrl: VALID_URL, postgresSchema: badSchema },
{
createPool() {
createPoolCalls += 1;
return createFakePool().pool;
},
}
);
} catch (err) {
caught = err;
}
assertNoSecretsLeaked(caught);
expect((caught as ResearchCopilotExecutorError).code).toBe('INVALID_CONFIG');
}
expect(createPoolCalls).toBe(0);
} finally {
if (previousPgOptions === undefined) {
delete process.env.PGOPTIONS;
} else {
process.env.PGOPTIONS = previousPgOptions;
}
}
});
test('rejects invalid URLs with INVALID_CONFIG before constructing a pool', async () => {
const invalidUrls = [
'',