T-9: Multi-host MongoDB URI bypasses the dev QA-Mongo guard
This commit is contained in:
parent
677658a32f
commit
4b46e809be
5 changed files with 134 additions and 7 deletions
|
|
@ -55,6 +55,9 @@ function isValidMongoHostSegment(segment: string, allowPort: boolean): boolean {
|
|||
return false;
|
||||
}
|
||||
const portStr = after.slice(1);
|
||||
if (portStr === '') {
|
||||
return true;
|
||||
}
|
||||
if (!/^\d+$/.test(portStr)) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -72,7 +75,13 @@ function isValidMongoHostSegment(segment: string, allowPort: boolean): boolean {
|
|||
|
||||
const host = segment.slice(0, colonIdx);
|
||||
const portStr = segment.slice(colonIdx + 1);
|
||||
if (!host || !/^\d+$/.test(portStr)) {
|
||||
if (!host) {
|
||||
return false;
|
||||
}
|
||||
if (portStr === '') {
|
||||
return true;
|
||||
}
|
||||
if (!/^\d+$/.test(portStr)) {
|
||||
return false;
|
||||
}
|
||||
const port = Number(portStr);
|
||||
|
|
|
|||
|
|
@ -599,6 +599,22 @@ describe('Config Schemas', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it('should accept empty port spellings for mongodb:// in hostname and bracketed IPv6 forms', () => {
|
||||
const hostEmptyPort = mongodbConfigSchema.parse({
|
||||
uri: 'mongodb://mongo.prod.internal:/stock',
|
||||
readUri: 'mongodb://[::1]:/stock',
|
||||
database: 'stock',
|
||||
});
|
||||
expect(hostEmptyPort.uri).toBe('mongodb://mongo.prod.internal:/stock');
|
||||
expect(hostEmptyPort.readUri).toBe('mongodb://[::1]:/stock');
|
||||
|
||||
const seedListEmptyPort = mongodbConfigSchema.parse({
|
||||
uri: 'mongodb://u:p@mongo1.prod.internal:,[::1]:/stock',
|
||||
database: 'stock',
|
||||
});
|
||||
expect(seedListEmptyPort.uri).toBe('mongodb://u:p@mongo1.prod.internal:,[::1]:/stock');
|
||||
});
|
||||
|
||||
it('should validate URI format', () => {
|
||||
const invalidUris = [
|
||||
'invalid-uri',
|
||||
|
|
@ -606,7 +622,13 @@ describe('Config Schemas', () => {
|
|||
'mongodb://',
|
||||
'mongodb://u:p@',
|
||||
'mongodb://u:p@localhost:27017,',
|
||||
'mongodb://u:p@:27017/stock',
|
||||
'mongodb://u:p@localhost:0/stock',
|
||||
'mongodb://u:p@localhost:99999/stock',
|
||||
'mongodb://u:p@[::1]:0/stock',
|
||||
'mongodb://u:p@[::1]:99999/stock',
|
||||
'mongodb://u:p@localhost:abc/stock',
|
||||
'mongodb+srv://u:p@localhost:/stock',
|
||||
'mongodb+srv://u:p@localhost:27017/stock',
|
||||
'mongodb+srv://u:p@host1.example.com,host2.example.com/stock',
|
||||
];
|
||||
|
|
|
|||
|
|
@ -35,15 +35,33 @@ export type ContainerRegistrar = (
|
|||
) => void;
|
||||
|
||||
function extractMongoDatabaseFromUri(uri?: string): string | undefined {
|
||||
if (!uri) {
|
||||
const trimmed = uri?.trim();
|
||||
if (!trimmed) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
const db = new URL(uri).pathname.replace(/^\/+/, '').split('/')[0];
|
||||
return db || undefined;
|
||||
} catch {
|
||||
|
||||
const schemeMatch = /^mongodb(?:\+srv)?:\/\/(.*)$/i.exec(trimmed);
|
||||
const rest = schemeMatch?.[1];
|
||||
if (!rest) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const delimiterMatch = /[/?#]/.exec(rest);
|
||||
if (!delimiterMatch || delimiterMatch[0] !== '/') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const pathPart = rest.slice(delimiterMatch.index).split(/[?#]/, 1)[0] ?? '';
|
||||
const rawDb = pathPart.replace(/^\/+/, '').split('/')[0];
|
||||
if (!rawDb) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return decodeURIComponent(rawDb);
|
||||
} catch {
|
||||
return rawDb;
|
||||
}
|
||||
}
|
||||
|
||||
function getInfluxPort(config: { influxPort?: number; ilpPort?: number }): number {
|
||||
|
|
|
|||
|
|
@ -120,6 +120,49 @@ describe('ServiceContainerBuilder production configuration', () => {
|
|||
}
|
||||
});
|
||||
|
||||
it('extracts database from multi-host readUri when mongoReadOnly is enabled', async () => {
|
||||
const config = {
|
||||
...configuration(),
|
||||
database: {
|
||||
...configuration().database,
|
||||
mongodb: {
|
||||
enabled: true,
|
||||
uri: 'mongodb://dev_admin:dev_mongo_pass@localhost:27018/stock_dev?authSource=admin',
|
||||
readUri:
|
||||
'mongodb://studio_stock_bot_read:secret@localhost:27017,localhost:27018/stock?authSource=stock',
|
||||
database: 'stock_dev',
|
||||
},
|
||||
},
|
||||
};
|
||||
const container = await new ServiceContainerBuilder()
|
||||
.withConfig(config)
|
||||
.withOptions({
|
||||
enableMongoDB: true,
|
||||
mongoReadOnly: true,
|
||||
enablePostgres: false,
|
||||
enableQuestDB: false,
|
||||
enableCache: false,
|
||||
enableQueue: false,
|
||||
enableBrowser: false,
|
||||
enableProxy: false,
|
||||
})
|
||||
.skipInitialization()
|
||||
.build();
|
||||
|
||||
try {
|
||||
const resolvedConfig = container.resolve('config');
|
||||
expect(resolvedConfig.mongodb.readOnly).toBe(true);
|
||||
expect(resolvedConfig.mongodb.database).toBe('stock');
|
||||
|
||||
const mongo = container.resolve('mongoClient');
|
||||
expect(mongo).not.toBeNull();
|
||||
expect(mongo!.isReadOnly()).toBe(true);
|
||||
expect(mongo!.getDefaultDatabase()).toBe('stock');
|
||||
} finally {
|
||||
await container.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects invalid settings and missing required app fields with schema errors', () => {
|
||||
expect(() =>
|
||||
new ServiceContainerBuilder().withConfig({
|
||||
|
|
|
|||
|
|
@ -392,7 +392,8 @@ describe('Environment safety & multi-env isolation', () => {
|
|||
const edgeCaseUris = [
|
||||
{ uri: 'mongodb://trading_admin:p@ss@localhost:27018,localhost:27017/stock', malformed: false },
|
||||
{ uri: 'mongodb://localhost:27017,localhost:27018/stock', malformed: false },
|
||||
{ uri: 'mongodb://u:p@localhost:/stock', malformed: true },
|
||||
{ uri: 'mongodb://u:p@localhost:/stock', malformed: false },
|
||||
{ uri: 'mongodb://u:p@[::1]:/stock', malformed: false },
|
||||
{ uri: 'mongodb://u:p@bad:port,localhost:27017/stock', malformed: true },
|
||||
];
|
||||
|
||||
|
|
@ -668,4 +669,38 @@ describe('MongoDBClient read-only enforcement', () => {
|
|||
await shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
test('ServiceContainerBuilder extracts database from multi-host readUri when mongoReadOnly=true', async () => {
|
||||
const cfg = makeConfig({
|
||||
environment: 'development',
|
||||
mongoUri: 'mongodb://dev_admin:dev_mongo_pass@localhost:27018/stock_dev?authSource=admin',
|
||||
mongoReadUri:
|
||||
'mongodb://studio_stock_bot_read:secret@localhost:27017,localhost:27018/stock?authSource=stock',
|
||||
mongoDatabase: 'stock_dev',
|
||||
mongoReadOnly: true,
|
||||
});
|
||||
const { container, shutdown } = await new ServiceContainerBuilder()
|
||||
.withConfig(cfg)
|
||||
.withOptions({
|
||||
enableMongoDB: true,
|
||||
mongoReadOnly: true,
|
||||
enablePostgres: false,
|
||||
enableQuestDB: false,
|
||||
enableCache: false,
|
||||
enableQueue: false,
|
||||
enableBrowser: false,
|
||||
enableProxy: false,
|
||||
skipInitialization: true,
|
||||
})
|
||||
.buildOwned();
|
||||
|
||||
try {
|
||||
const mongo = container.resolve('mongoClient');
|
||||
expect(mongo).not.toBeNull();
|
||||
expect(mongo!.isReadOnly()).toBe(true);
|
||||
expect(mongo!.getDefaultDatabase()).toBe('stock');
|
||||
} finally {
|
||||
await shutdown();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue