T-211: M2.2v — Export listing_venues.tsv and extend field-shape probe & BSON fixture exporter for M2 collections
This commit is contained in:
parent
ba20f9a2e5
commit
f05259a1ae
4 changed files with 202 additions and 7 deletions
|
|
@ -6,17 +6,88 @@ export interface ReadOnlyMongoDiagnosticSafetyOptions {
|
|||
readonly diagnosticLabel: string;
|
||||
}
|
||||
|
||||
const FORBIDDEN_DIAGNOSTIC_MONGO_USERNAMES: ReadonlySet<string> = new Set(['trading_admin']);
|
||||
|
||||
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 extractMongoUsernameFromUri(uri: string): string | undefined {
|
||||
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)?):\/\/(.*)$/is.exec(trimmed);
|
||||
const rest = schemeMatch?.[2];
|
||||
if (!schemeMatch?.[1] || rest === undefined) {
|
||||
throw new Error('Safety guard: MongoDB URI must use the mongodb:// or mongodb+srv:// scheme.');
|
||||
}
|
||||
|
||||
const delimiterMatch = /[/?]/.exec(rest);
|
||||
const authority = delimiterMatch ? rest.slice(0, delimiterMatch.index) : rest;
|
||||
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.'
|
||||
);
|
||||
}
|
||||
|
||||
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.'
|
||||
);
|
||||
}
|
||||
if (firstAtIndex < 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const userinfo = authority.slice(0, firstAtIndex);
|
||||
const colonIndex = userinfo.indexOf(':');
|
||||
const rawUsername = colonIndex >= 0 ? userinfo.slice(0, colonIndex) : userinfo;
|
||||
if (!rawUsername) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return decodeURIComponent(rawUsername);
|
||||
} catch (error) {
|
||||
if (error instanceof URIError) {
|
||||
throw new Error('Safety guard: invalid percent-encoding in MongoDB URI username.');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared safety guard for strictly read-only MongoDB diagnostic CLIs
|
||||
* (`probe-source-field-shapes` and `run-export-raw-bson`).
|
||||
*
|
||||
* Enforces:
|
||||
* 1. MongoDB is enabled and configured with `readOnly === true`.
|
||||
* 2. Environment safety for `dataPipeline` (with PostgreSQL disabled and
|
||||
* 2. The exact effective URI resolved by `resolveReadOnlyMongoClientConfig`
|
||||
* (falling back from unset/blank `readUri` to `uri`) cannot authenticate
|
||||
* as the QA admin principal (`trading_admin`), regardless of hostname
|
||||
* (localhost, Compose service `mongodb`, or remote) or `NODE_ENV`.
|
||||
* 3. Environment safety for `dataPipeline` (with PostgreSQL disabled and
|
||||
* `STOCK_BOT_ALLOW_DEV_QA_MONGO_WRITE` stripped).
|
||||
* 3. The exact effective URI resolved by `resolveReadOnlyMongoClientConfig`
|
||||
* (falling back from unset/blank `readUri` to `uri`) cannot target QA
|
||||
* MongoDB with admin credentials (`trading_admin`), regardless of `NODE_ENV`.
|
||||
*/
|
||||
export function assertReadOnlyMongoDiagnosticSafety(
|
||||
config: StockAppConfig,
|
||||
|
|
@ -35,6 +106,17 @@ export function assertReadOnlyMongoDiagnosticSafety(
|
|||
|
||||
const effectiveMongoConfig = resolveReadOnlyMongoClientConfig(config);
|
||||
const effectiveUri = effectiveMongoConfig.uri;
|
||||
const effectiveUsername =
|
||||
(effectiveUri ? extractMongoUsernameFromUri(effectiveUri) : undefined) ??
|
||||
effectiveMongoConfig.username?.trim();
|
||||
|
||||
if (effectiveUsername && FORBIDDEN_DIAGNOSTIC_MONGO_USERNAMES.has(effectiveUsername)) {
|
||||
throw new Error(
|
||||
`Safety guard: ${config.environment} ${options.diagnosticLabel} cannot connect to MongoDB ` +
|
||||
`with admin credentials ("${effectiveUsername}"). ` +
|
||||
'Configure MONGODB_READ_URI with the read-only user (studio_stock_bot_read) and enable mongoReadOnly: true.'
|
||||
);
|
||||
}
|
||||
|
||||
const { STOCK_BOT_ALLOW_DEV_QA_MONGO_WRITE: _stripped, ...safeEnv } = env;
|
||||
const mongoOnlyConfig: StockAppConfig = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue