T-182: M2.1r — Stdio host composition for the MCP copilot: parseResearchCopilotHostConfig (injected env → validated config) + runResearchCopilotStdioHost (injected executor / store / library-reader factories → createPostgresResearchCopilotServer + createBarSessionDatesPort → runStdioServer, fail-closed, resources always closed) in libs/research/src/mcp/host/stdio-host.ts (no new deps, no live Postgres/DuckDB, no bin)

This commit is contained in:
Studio 2026-12-02 18:30:38 -05:00
parent 1236647fa0
commit c9b20584de
2 changed files with 182 additions and 20 deletions

View file

@ -200,6 +200,19 @@ function parsePostgresUrl(raw: string): string {
`Invalid configuration variable "${ENV_POSTGRES_URL}": expected a postgres:// or postgresql:// URL with a non-empty host.`
);
}
try {
if (parsed.username.length > 0) {
decodeURIComponent(parsed.username);
}
if (parsed.password.length > 0) {
decodeURIComponent(parsed.password);
}
} catch {
throw new ResearchCopilotHostConfigError(
ENV_POSTGRES_URL,
`Invalid configuration variable "${ENV_POSTGRES_URL}": URL credentials contain invalid percent-encoding.`
);
}
return raw;
}
@ -526,6 +539,21 @@ export function parseResearchCopilotHostConfig(
};
}
function addRawAndDecodedNeedle(needles: Set<string>, raw: string): void {
if (raw.length === 0) {
return;
}
needles.add(raw);
try {
const decoded = decodeURIComponent(raw);
if (decoded.length > 0) {
needles.add(decoded);
}
} catch {
// Malformed percent-encoding: raw needle is already registered.
}
}
function collectSecretNeedles(postgresUrl: string): readonly string[] {
const needles = new Set<string>();
if (postgresUrl.length > 0) {
@ -536,15 +564,30 @@ function collectSecretNeedles(postgresUrl: string): readonly string[] {
if (parsed.href.length > 0) {
needles.add(parsed.href);
}
if (parsed.password.length > 0) {
needles.add(parsed.password);
try {
const decoded = decodeURIComponent(parsed.password);
if (decoded.length > 0) {
needles.add(decoded);
addRawAndDecodedNeedle(needles, parsed.password);
for (const [key, value] of parsed.searchParams.entries()) {
if (key.toLowerCase() === 'password' && value.length > 0) {
needles.add(value);
}
}
if (parsed.search.length > 1) {
const pairs = parsed.search.slice(1).split('&');
for (let i = 0; i < pairs.length; i += 1) {
const pair = pairs[i] as string;
const eqIdx = pair.indexOf('=');
if (eqIdx <= 0) {
continue;
}
const rawKey = pair.slice(0, eqIdx);
let decodedKey = rawKey;
try {
decodedKey = decodeURIComponent(rawKey);
} catch {
// Keep rawKey if percent-decoding fails.
}
if (decodedKey.toLowerCase() === 'password') {
addRawAndDecodedNeedle(needles, pair.slice(eqIdx + 1));
}
} catch {
// Malformed percent-encoding in password: raw password needle is already covered.
}
}
} catch {
@ -589,6 +632,19 @@ function isErrorSafeToSurface(
if (cause !== undefined && !isErrorSafeToSurface(cause, needles, visited, depth + 1)) {
return false;
}
if (error instanceof AggregateError || 'errors' in error) {
const nested = (error as { readonly errors?: unknown }).errors;
if (nested !== undefined) {
if (!Array.isArray(nested)) {
return false;
}
for (let i = 0; i < nested.length; i += 1) {
if (!isErrorSafeToSurface(nested[i], needles, visited, depth + 1)) {
return false;
}
}
}
}
return true;
} catch {
return false;
@ -736,7 +792,7 @@ export async function runResearchCopilotStdioHost(
store: storeHandle.store,
vaultContext: { vaultCutoffTs: config.vaultCutoffTs },
principalId: config.principalId,
listLibraryCards: libraryReader.listLibraryCards,
listLibraryCards: query => libraryReader.listLibraryCards(query),
sessionDates,
...(config.earlyCloses !== undefined ? { earlyCloses: config.earlyCloses } : {}),
newId: deps.newId,