From 373936c31237d002e2db3667cb9deeabf32cca88 Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 4 Dec 2026 15:31:29 -0500 Subject: [PATCH] =?UTF-8?q?T-210:=20M2.3s=20=E2=80=94=20Implement=20Market?= =?UTF-8?q?DataStore.macroAsOf,=20crossSection,=20QuestDB=20fx('1m'),=20an?= =?UTF-8?q?d=20copilot=201m=20routing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/data/questdb/src/index.ts | 1 + libs/data/questdb/src/pg-executor.ts | 82 +++++++++------------- libs/data/questdb/test/questdb.test.ts | 83 ++++++++++++++++++----- libs/research/src/mcp/host/stdio-host.ts | 52 +++----------- libs/research/test/mcp-stdio-host.test.ts | 57 ++++++++++++---- scripts/test/questdb-init-m2.test.ts | 22 ++++-- 6 files changed, 168 insertions(+), 129 deletions(-) diff --git a/libs/data/questdb/src/index.ts b/libs/data/questdb/src/index.ts index 79b6f2ae..ffc055d1 100644 --- a/libs/data/questdb/src/index.ts +++ b/libs/data/questdb/src/index.ts @@ -40,6 +40,7 @@ export { export { assertNoConflictingQuestDbSsl, hasConflictingQuestDbUrlSsl, + hasQuestDbUrlTlsOptions, openQuestDbPgExecutor, type OpenQuestDbPgExecutorOptions, type QuestDbPgExecutorConfig, diff --git a/libs/data/questdb/src/pg-executor.ts b/libs/data/questdb/src/pg-executor.ts index 922158ab..f1281acc 100644 --- a/libs/data/questdb/src/pg-executor.ts +++ b/libs/data/questdb/src/pg-executor.ts @@ -47,58 +47,34 @@ export interface OpenQuestDbPgExecutorOptions { const SAFE_ERROR_CODE_RE = /^[A-Z0-9_]{1,32}$/; +function isQuestDbUrlTlsParamKey(rawKey: string): boolean { + const key = rawKey.trim().toLowerCase(); + return key === 'uselibpqcompat' || key === 'ssl' || key.startsWith('ssl'); +} + /** - * Returns `true` when an explicit boolean `ssl` setting conflicts with SSL - * query parameters (`sslmode`, `ssl`, `sslcert`, `sslkey`, `sslrootcert`) - * embedded in `rawUrl`. + * Returns `true` when `rawUrl` contains any TLS/SSL query parameter + * (`ssl`, `sslmode`, `sslcert`, `sslkey`, `sslrootcert`, `uselibpqcompat`, or + * any other `ssl*` parameter). + * + * TLS settings must have a single source of truth: either configure TLS in the + * URL query string (omitting the explicit `ssl` flag) or pass the explicit + * `ssl` flag with a URL that contains no TLS query parameters. */ -export function hasConflictingQuestDbUrlSsl(rawUrl: string, explicitSsl: boolean): boolean { +export function hasQuestDbUrlTlsOptions(rawUrl: string): boolean { let parsed: URL; try { parsed = new URL(rawUrl); } catch { - return false; + try { + parsed = new URL(rawUrl, 'postgres://base'); + } catch { + return false; + } } - for (const [rawKey, rawValue] of parsed.searchParams.entries()) { - const key = rawKey.toLowerCase(); - const value = rawValue.trim().toLowerCase(); - - if (key === 'sslmode') { - if (value === 'disable') { - if (explicitSsl) { - return true; - } - } else if ( - value === 'require' || - value === 'verify-ca' || - value === 'verify-full' || - value === 'no-verify' - ) { - if (!explicitSsl) { - return true; - } - } else { - // `allow`, `prefer`, or any unrecognized sslmode conflicts with an explicit boolean override. - return true; - } - } else if (key === 'ssl') { - if (value === 'false' || value === '0') { - if (explicitSsl) { - return true; - } - } else if (value === 'true' || value === '1') { - if (!explicitSsl) { - return true; - } - } else { - return true; - } - } else if ( - (key === 'sslcert' || key === 'sslkey' || key === 'sslrootcert') && - value.length > 0 && - !explicitSsl - ) { + for (const [rawKey] of parsed.searchParams.entries()) { + if (isQuestDbUrlTlsParamKey(rawKey)) { return true; } } @@ -107,13 +83,21 @@ export function hasConflictingQuestDbUrlSsl(rawUrl: string, explicitSsl: boolean } /** - * Throws a credential-free `RangeError` if `explicitSsl` conflicts with SSL - * query parameters in `rawUrl`. + * Returns `true` when an explicit boolean `ssl` setting is combined with any + * TLS/SSL query parameter embedded in `rawUrl`. */ -export function assertNoConflictingQuestDbSsl(rawUrl: string, explicitSsl: boolean): void { - if (hasConflictingQuestDbUrlSsl(rawUrl, explicitSsl)) { +export function hasConflictingQuestDbUrlSsl(rawUrl: string, _explicitSsl?: boolean): boolean { + return hasQuestDbUrlTlsOptions(rawUrl); +} + +/** + * Throws a credential-free `RangeError` if an explicit `ssl` option is combined + * with any TLS/SSL query parameter in `rawUrl`. + */ +export function assertNoConflictingQuestDbSsl(rawUrl: string, _explicitSsl?: boolean): void { + if (hasQuestDbUrlTlsOptions(rawUrl)) { throw new RangeError( - 'Conflicting QuestDB SSL configuration: explicit "ssl" option conflicts with SSL query parameters in the QuestDB URL.' + 'Conflicting QuestDB TLS/SSL configuration: cannot combine an explicit "ssl" option with TLS/SSL query parameters in the QuestDB URL. Configure TLS in either the URL or the explicit "ssl" option, not both.' ); } } diff --git a/libs/data/questdb/test/questdb.test.ts b/libs/data/questdb/test/questdb.test.ts index ea295b5c..79e45b4e 100644 --- a/libs/data/questdb/test/questdb.test.ts +++ b/libs/data/questdb/test/questdb.test.ts @@ -1,5 +1,6 @@ import { beforeEach, describe, expect, it, mock } from 'bun:test'; import { EventEmitter } from 'node:events'; +import { Client, type PoolConfig } from 'pg'; import { QuestDBHealthMonitor } from '../src/health'; import { QuestDBInfluxWriter } from '../src/influx-writer'; import { @@ -650,25 +651,38 @@ describe('QuestDB', () => { expect(fakePool.ended).toBe(true); }); - it('rejects conflicting URL SSL query parameters and explicit ssl before creating a pool', async () => { + it('rejects any URL TLS/SSL query parameter when explicit ssl is set and verifies pinned pg effective TLS config without connecting', async () => { const secret = 'qdb_ssl_s3cr3t!'; const base = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`; const conflictingCases: ReadonlyArray = [ - [`${base}?sslmode=disable`, true], - [`${base}?SSLMODE=DISABLE`, true], - [`${base}?sslmode=require`, false], - [`${base}?sslmode=verify-ca`, false], - [`${base}?sslmode=verify-full`, false], + [`${base}?sslmode=no-verify`, true], [`${base}?sslmode=no-verify`, false], + [`${base}?sslmode=disable`, true], + [`${base}?sslmode=disable`, false], + [`${base}?SSLMODE=DISABLE`, true], + [`${base}?sslmode=require`, true], + [`${base}?sslmode=require`, false], + [`${base}?sslmode=verify-ca`, true], + [`${base}?sslmode=verify-ca`, false], + [`${base}?sslmode=verify-full`, true], + [`${base}?sslmode=verify-full`, false], [`${base}?sslmode=prefer`, true], [`${base}?sslmode=prefer`, false], [`${base}?sslmode=allow`, true], [`${base}?ssl=false`, true], + [`${base}?ssl=false`, false], [`${base}?ssl=0`, true], + [`${base}?ssl=0`, false], + [`${base}?ssl=true`, true], [`${base}?ssl=true`, false], + [`${base}?ssl=1`, true], [`${base}?ssl=1`, false], + [`${base}?ssl=`, true], + [`${base}?sslrootcert=/etc/ssl/ca.pem`, true], [`${base}?sslrootcert=/etc/ssl/ca.pem`, false], + [`${base}?uselibpqcompat=true`, true], + [`${base}?uselibpqcompat=true`, false], ]; let createPoolCalls = 0; @@ -694,24 +708,57 @@ describe('QuestDB', () => { } expect(createPoolCalls).toBe(0); - // Compatible combinations proceed to pool creation. - const compatibleCases: ReadonlyArray = [ - [base, true], - [`${base}?sslmode=require`, true], - [`${base}?sslmode=disable`, false], - [`${base}?ssl=true`, true], - [`${base}?ssl=false`, false], + // Single-source-of-truth configs proceed to pool creation and produce the + // expected effective TLS settings in the pinned `pg` driver without opening + // a network connection. + const effectiveCases: ReadonlyArray<{ + readonly config: Parameters[0]; + readonly expectedSsl: unknown; + }> = [ + { config: { url: base, ssl: true }, expectedSsl: true }, + { + config: { url: `${base}?application_name=copilot`, ssl: false }, + expectedSsl: false, + }, + { config: { url: `${base}?sslmode=require` }, expectedSsl: {} }, + { config: { url: `${base}?sslmode=disable` }, expectedSsl: false }, + { + config: { url: `${base}?sslmode=no-verify` }, + expectedSsl: { rejectUnauthorized: false }, + }, + { + config: { host: '10.0.0.51', port: 8812, database: 'qdb', ssl: true }, + expectedSsl: true, + }, + { + config: { host: '10.0.0.51', port: 8812, database: 'qdb', ssl: false }, + expectedSsl: false, + }, ]; - for (const [url, ssl] of compatibleCases) { + + for (const { config, expectedSsl } of effectiveCases) { + let capturedPoolConfig: PoolConfig | undefined; const pool = Object.assign(new EventEmitter(), { query: async () => ({ rows: [{ '?column?': 1 }] }), end: async () => {}, }) as unknown as QuestDbPgPoolLike; - const handle = await openQuestDbPgExecutor( - { url, ssl }, - { createPool: () => pool } - ); + const handle = await openQuestDbPgExecutor(config, { + createPool: poolConfig => { + capturedPoolConfig = poolConfig; + return pool; + }, + }); await handle.close(); + + expect(capturedPoolConfig).toBeDefined(); + // Instantiate pg.Client without calling connect() to inspect the pinned + // driver's effective ConnectionParameters.ssl resolution. + const client = new Client(capturedPoolConfig); + const effectiveSsl: unknown = ( + client as unknown as { connectionParameters: { ssl: unknown } } + ).connectionParameters.ssl; + expect(effectiveSsl).toEqual(expectedSsl); + expect(client.ssl as unknown).toEqual(expectedSsl); } }); }); diff --git a/libs/research/src/mcp/host/stdio-host.ts b/libs/research/src/mcp/host/stdio-host.ts index 363cd2f8..2077f60b 100644 --- a/libs/research/src/mcp/host/stdio-host.ts +++ b/libs/research/src/mcp/host/stdio-host.ts @@ -282,7 +282,12 @@ function parseQuestDbPort(raw: string): number { return port; } -function hasConflictingUrlSslSetting(rawUrl: string, explicitSsl: boolean): boolean { +function isQuestDbUrlTlsParamKey(rawKey: string): boolean { + const key = rawKey.trim().toLowerCase(); + return key === 'uselibpqcompat' || key === 'ssl' || key.startsWith('ssl'); +} + +function hasQuestDbUrlTlsOptions(rawUrl: string): boolean { let parsed: URL; try { parsed = new URL(rawUrl); @@ -290,45 +295,8 @@ function hasConflictingUrlSslSetting(rawUrl: string, explicitSsl: boolean): bool return false; } - for (const [rawKey, rawValue] of parsed.searchParams.entries()) { - const key = rawKey.toLowerCase(); - const value = rawValue.trim().toLowerCase(); - - if (key === 'sslmode') { - if (value === 'disable') { - if (explicitSsl) { - return true; - } - } else if ( - value === 'require' || - value === 'verify-ca' || - value === 'verify-full' || - value === 'no-verify' - ) { - if (!explicitSsl) { - return true; - } - } else { - // `allow`, `prefer`, or any unrecognized sslmode conflicts with an explicit boolean override. - return true; - } - } else if (key === 'ssl') { - if (value === 'false' || value === '0') { - if (explicitSsl) { - return true; - } - } else if (value === 'true' || value === '1') { - if (!explicitSsl) { - return true; - } - } else { - return true; - } - } else if ( - (key === 'sslcert' || key === 'sslkey' || key === 'sslrootcert') && - value.length > 0 && - !explicitSsl - ) { + for (const [rawKey] of parsed.searchParams.entries()) { + if (isQuestDbUrlTlsParamKey(rawKey)) { return true; } } @@ -374,10 +342,10 @@ function parseQuestDbConfig(data: { ); } const url = parsePgWireUrl(rawUrl, ENV_QUESTDB_URL); - if (ssl !== undefined && hasConflictingUrlSslSetting(url, ssl)) { + if (ssl !== undefined && hasQuestDbUrlTlsOptions(url)) { throw new ResearchCopilotHostConfigError( ENV_QUESTDB_SSL, - `Conflicting configuration variable "${ENV_QUESTDB_SSL}": conflicts with SSL query parameters in "${ENV_QUESTDB_URL}".` + `Conflicting configuration variable "${ENV_QUESTDB_SSL}": cannot be combined with TLS/SSL query parameters in "${ENV_QUESTDB_URL}". Configure TLS in either the URL or "${ENV_QUESTDB_SSL}", not both.` ); } return { diff --git a/libs/research/test/mcp-stdio-host.test.ts b/libs/research/test/mcp-stdio-host.test.ts index cdd0e785..56b5cc7a 100644 --- a/libs/research/test/mcp-stdio-host.test.ts +++ b/libs/research/test/mcp-stdio-host.test.ts @@ -1243,17 +1243,29 @@ describe('runResearchCopilotStdioHost', () => { }); // 4. Rejects conflicting URL + host, partial port-without-host, bad port, bad ssl, unknown var, - // and conflicting URL SSL query parameters vs RESEARCH_MCP_QUESTDB_SSL without leaking secrets. + // and any URL TLS/SSL query parameters combined with RESEARCH_MCP_QUESTDB_SSL without leaking secrets. const invalidEnvs: Array> = [ { RESEARCH_MCP_QUESTDB_URL: questUrl, RESEARCH_MCP_QUESTDB_HOST: '10.0.0.51' }, { RESEARCH_MCP_QUESTDB_PG_PORT: '8812' }, { RESEARCH_MCP_QUESTDB_HOST: '10.0.0.51', RESEARCH_MCP_QUESTDB_PG_PORT: '99999' }, { RESEARCH_MCP_QUESTDB_HOST: '10.0.0.51', RESEARCH_MCP_QUESTDB_SSL: 'maybe' }, { RESEARCH_MCP_QUESTDB_UNKNOWN: '1' }, + { + RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=no-verify`, + RESEARCH_MCP_QUESTDB_SSL: 'true', + }, { RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`, RESEARCH_MCP_QUESTDB_SSL: 'true', }, + { + RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`, + RESEARCH_MCP_QUESTDB_SSL: 'false', + }, + { + RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=require`, + RESEARCH_MCP_QUESTDB_SSL: 'true', + }, { RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=require`, RESEARCH_MCP_QUESTDB_SSL: 'false', @@ -1262,6 +1274,10 @@ describe('runResearchCopilotStdioHost', () => { RESEARCH_MCP_QUESTDB_URL: `${questUrl}?ssl=false`, RESEARCH_MCP_QUESTDB_SSL: 'true', }, + { + RESEARCH_MCP_QUESTDB_URL: `${questUrl}?ssl=false`, + RESEARCH_MCP_QUESTDB_SSL: 'false', + }, { RESEARCH_MCP_QUESTDB_URL: `${questUrl}?ssl=true`, RESEARCH_MCP_QUESTDB_SSL: 'false', @@ -1274,6 +1290,10 @@ describe('runResearchCopilotStdioHost', () => { RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslrootcert=/etc/ssl/ca.pem`, RESEARCH_MCP_QUESTDB_SSL: 'false', }, + { + RESEARCH_MCP_QUESTDB_URL: `${questUrl}?uselibpqcompat=true`, + RESEARCH_MCP_QUESTDB_SSL: 'true', + }, ]; for (const override of invalidEnvs) { let caughtInvalid: unknown; @@ -1287,27 +1307,34 @@ describe('runResearchCopilotStdioHost', () => { expect((caughtInvalid as Error).message).not.toContain(questUrl); } - // Compatible URL SSL query params + matching RESEARCH_MCP_QUESTDB_SSL are accepted. - const consistentSslEnable = parseResearchCopilotHostConfig( + // Single-source-of-truth TLS configs are accepted (URL-only TLS params without + // RESEARCH_MCP_QUESTDB_SSL, or clean URL with non-TLS query params + RESEARCH_MCP_QUESTDB_SSL). + const urlOnlyTlsEnable = parseResearchCopilotHostConfig( buildValidEnv({ RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=require`, + }) + ); + expect(urlOnlyTlsEnable.questdb).toEqual({ + url: `${questUrl}?sslmode=require`, + }); + const urlOnlyTlsDisable = parseResearchCopilotHostConfig( + buildValidEnv({ + RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`, + }) + ); + expect(urlOnlyTlsDisable.questdb).toEqual({ + url: `${questUrl}?sslmode=disable`, + }); + const flagOnlyTls = parseResearchCopilotHostConfig( + buildValidEnv({ + RESEARCH_MCP_QUESTDB_URL: `${questUrl}?application_name=copilot`, RESEARCH_MCP_QUESTDB_SSL: 'true', }) ); - expect(consistentSslEnable.questdb).toEqual({ - url: `${questUrl}?sslmode=require`, + expect(flagOnlyTls.questdb).toEqual({ + url: `${questUrl}?application_name=copilot`, ssl: true, }); - const consistentSslDisable = parseResearchCopilotHostConfig( - buildValidEnv({ - RESEARCH_MCP_QUESTDB_URL: `${questUrl}?sslmode=disable`, - RESEARCH_MCP_QUESTDB_SSL: 'false', - }) - ); - expect(consistentSslDisable.questdb).toEqual({ - url: `${questUrl}?sslmode=disable`, - ssl: false, - }); // 5. Redacts QuestDB URL / password on openStore and cleanup errors. for (const cfg of [urlConfig, hostFullConfig]) { diff --git a/scripts/test/questdb-init-m2.test.ts b/scripts/test/questdb-init-m2.test.ts index 0e4c1b35..00a3e804 100644 --- a/scripts/test/questdb-init-m2.test.ts +++ b/scripts/test/questdb-init-m2.test.ts @@ -8,11 +8,16 @@ describe('scripts/questdb-init-m2', () => { const baseUrl = `postgresql://admin:${encodeURIComponent(secret)}@10.0.0.51:8812/qdb`; const conflictingEnvs: Array> = [ + { QUESTDB_URL: `${baseUrl}?sslmode=no-verify`, QUESTDB_SSL: 'true' }, { QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'true' }, + { QUESTDB_URL: `${baseUrl}?sslmode=disable`, QUESTDB_SSL: 'false' }, { RESEARCH_MCP_QUESTDB_URL: `${baseUrl}?sslmode=disable`, RESEARCH_MCP_QUESTDB_SSL: 'true' }, + { QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'true' }, { QUESTDB_URL: `${baseUrl}?sslmode=require`, QUESTDB_SSL: 'false' }, { QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'true' }, + { QUESTDB_URL: `${baseUrl}?ssl=false`, QUESTDB_SSL: 'false' }, { QUESTDB_URL: `${baseUrl}?sslmode=prefer`, QUESTDB_SSL: 'true' }, + { QUESTDB_URL: `${baseUrl}?uselibpqcompat=true`, QUESTDB_SSL: 'true' }, ]; let openCalls = 0; @@ -34,7 +39,7 @@ describe('scripts/questdb-init-m2', () => { expect(openCalls).toBe(0); }); - test('rejects invalid SSL flag values and accepts consistent URL + SSL configs', async () => { + test('rejects invalid SSL flag values and accepts single-source-of-truth URL or flag TLS configs', async () => { expect(() => resolveQuestDbInitM2Config({ QUESTDB_HOST: '10.0.0.51', @@ -42,12 +47,19 @@ describe('scripts/questdb-init-m2', () => { }) ).toThrow(RangeError); - const resolved = resolveQuestDbInitM2Config({ + const urlOnlyTls = resolveQuestDbInitM2Config({ QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require', + }); + expect(urlOnlyTls).toEqual({ + url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require', + }); + + const flagOnlyTls = resolveQuestDbInitM2Config({ + QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?application_name=init-m2', QUESTDB_SSL: 'true', }); - expect(resolved).toEqual({ - url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=require', + expect(flagOnlyTls).toEqual({ + url: 'postgresql://admin:pass@10.0.0.51:8812/qdb?application_name=init-m2', ssl: true, }); @@ -55,7 +67,7 @@ describe('scripts/questdb-init-m2', () => { let closed = false; const result = await runQuestDbInitM2( { - QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb?sslmode=disable', + QUESTDB_URL: 'postgresql://admin:pass@10.0.0.51:8812/qdb', QUESTDB_SSL: 'false', }, async () => ({