T-28: Dev QA-Mongo guard bypass: raw '#' or a second '@' in the host list (guard and driver split the URI authority differently)

This commit is contained in:
Studio 2026-11-27 13:32:01 -05:00
parent 34023ee88a
commit 2206515681
4 changed files with 300 additions and 24 deletions

View file

@ -296,25 +296,52 @@ function parseMongoHostPort(
return { host, port };
}
function hasAsciiControlChar(value: string): boolean {
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i);
if (code <= 0x1f || code === 0x7f) {
return true;
}
}
return false;
}
function parseMongoTarget(uri?: string): ParsedMongoTarget {
const trimmed = uri?.trim();
if (!trimmed) {
return { hosts: [] };
if (!uri) {
throw new Error('Safety guard: MongoDB URI is missing or empty.');
}
if (hasAsciiControlChar(uri)) {
throw new Error(
'Safety guard: MongoDB URI contains forbidden whitespace or control characters.'
);
}
const trimmed = uri.trim();
if (!trimmed || /\s/.test(trimmed)) {
throw new Error('Safety guard: MongoDB URI is empty or contains forbidden whitespace.');
}
const schemeMatch = /^(mongodb(?:\+srv)?):\/\/(.*)$/i.exec(trimmed);
const schemeMatch = /^(mongodb(?:\+srv)?):\/\/(.*)$/is.exec(trimmed);
const rawScheme = schemeMatch?.[1];
const rest = schemeMatch?.[2];
if (!rawScheme || rest === undefined) {
return { hosts: [] };
throw new Error('Safety guard: MongoDB URI must use the mongodb:// or mongodb+srv:// scheme.');
}
const scheme = rawScheme.toLowerCase();
const defaultPort = scheme === 'mongodb' ? 27017 : undefined;
const delimiterMatch = /[/?#]/.exec(rest);
// Match mongodb-connection-string-url: authority ends at '/' or '?', never '#'
const delimiterMatch = /[/?]/.exec(rest);
const authority = delimiterMatch ? rest.slice(0, delimiterMatch.index) : rest;
const remainder = delimiterMatch ? rest.slice(delimiterMatch.index) : '';
if (!authority) {
throw new Error('Safety guard: MongoDB URI authority is empty.');
}
if (authority.includes('#')) {
throw new Error(
'Safety guard: MongoDB URI authority cannot contain an unencoded "#" character.'
);
}
let database: string | undefined;
if (remainder.startsWith('/')) {
@ -323,9 +350,21 @@ function parseMongoTarget(uri?: string): ParsedMongoTarget {
database = firstSegment || undefined;
}
const atIndex = authority.lastIndexOf('@');
const userinfo = atIndex >= 0 ? authority.slice(0, atIndex) : '';
const hostList = atIndex >= 0 ? authority.slice(atIndex + 1) : authority;
const firstAtIndex = authority.indexOf('@');
if (firstAtIndex === 0) {
throw new Error('Safety guard: MongoDB URI authority cannot have empty userinfo before "@".');
}
if (firstAtIndex !== -1 && authority.indexOf('@', firstAtIndex + 1) !== -1) {
throw new Error(
'Safety guard: MongoDB URI authority cannot contain multiple unencoded "@" characters.'
);
}
const userinfo = firstAtIndex >= 0 ? authority.slice(0, firstAtIndex) : '';
const hostList = firstAtIndex >= 0 ? authority.slice(firstAtIndex + 1) : authority;
if (!hostList) {
throw new Error('Safety guard: MongoDB URI host list is empty.');
}
let username: string | undefined;
if (userinfo) {
@ -347,6 +386,9 @@ function parseMongoTarget(uri?: string): ParsedMongoTarget {
hosts.push(parsedHost);
}
}
if (hosts.length === 0) {
throw new Error('Safety guard: MongoDB URI did not contain any valid host.');
}
return {
hosts,
@ -356,13 +398,16 @@ function parseMongoTarget(uri?: string): ParsedMongoTarget {
};
}
function isQaMongoTarget(uri: string | undefined): boolean {
const target = parseMongoTarget(uri);
function isQaMongoTargetHosts(target: ParsedMongoTarget): boolean {
return target.hosts.some(
entry => isLoopbackHost(entry.host) && (entry.port === undefined || entry.port === 27017)
);
}
function isQaMongoTarget(uri: string | undefined): boolean {
return isQaMongoTargetHosts(parseMongoTarget(uri));
}
function isQaPostgresTarget(pg: StockAppConfig['database']['postgres']): boolean {
return isLoopbackHost(pg.host) && pg.port === 5432 && pg.database === 'trading_bot';
}
@ -401,7 +446,7 @@ export function assertEnvironmentSafety(
const effectiveUri = mongo.readUri ?? mongo.uri;
const readTarget = parseMongoTarget(effectiveUri);
if (
isQaMongoTarget(effectiveUri) &&
isQaMongoTargetHosts(readTarget) &&
(mongo.readOnly !== true || readTarget.username === 'trading_admin')
) {
throw new Error(
@ -447,7 +492,9 @@ export function assertEnvironmentSafety(
if (FORBIDDEN_PROD_SECRETS.has(config.database.postgres.password)) {
throw new Error('Safety guard: production PostgreSQL password cannot use a default dev/qa credential.');
}
const mongoTarget = parseMongoTarget(config.database.mongodb.uri);
const mongoTarget = config.database.mongodb.enabled
? parseMongoTarget(config.database.mongodb.uri)
: undefined;
const mongoUriHasDevPass = Array.from(FORBIDDEN_PROD_SECRETS).some(secret =>
config.database.mongodb.uri.includes(`:${secret}@`)
);
@ -456,7 +503,7 @@ export function assertEnvironmentSafety(
(config.database.mongodb.password && FORBIDDEN_PROD_SECRETS.has(config.database.mongodb.password))
) {
throw new Error(
`Safety guard: production MongoDB (${mongoTarget.host ?? 'configured URI'}) cannot use a default dev/qa credential.`
`Safety guard: production MongoDB (${mongoTarget?.host ?? 'configured URI'}) cannot use a default dev/qa credential.`
);
}
if (