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:
parent
34023ee88a
commit
2206515681
4 changed files with 300 additions and 24 deletions
|
|
@ -88,12 +88,22 @@ function isValidMongoHostSegment(segment: string, allowPort: boolean): boolean {
|
|||
return port >= 1 && port <= 65535;
|
||||
}
|
||||
|
||||
function hasControlOrWhitespace(value: string): boolean {
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
const code = value.charCodeAt(i);
|
||||
if (code <= 0x20 || code === 0x7f) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return /\s/.test(value);
|
||||
}
|
||||
|
||||
function isValidMongoUri(uri: string): boolean {
|
||||
if (!uri || /\s/.test(uri)) {
|
||||
if (!uri || hasControlOrWhitespace(uri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const schemeMatch = /^(mongodb(?:\+srv)?):\/\/(.*)$/i.exec(uri);
|
||||
const schemeMatch = /^(mongodb(?:\+srv)?):\/\/(.*)$/is.exec(uri);
|
||||
const rawScheme = schemeMatch?.[1];
|
||||
const rest = schemeMatch?.[2];
|
||||
if (!rawScheme || !rest) {
|
||||
|
|
@ -101,14 +111,22 @@ function isValidMongoUri(uri: string): boolean {
|
|||
}
|
||||
|
||||
const isSrv = rawScheme.toLowerCase() === 'mongodb+srv';
|
||||
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;
|
||||
if (!authority) {
|
||||
if (!authority || authority.includes('#')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const atIndex = authority.lastIndexOf('@');
|
||||
const hostList = atIndex >= 0 ? authority.slice(atIndex + 1) : authority;
|
||||
const firstAtIndex = authority.indexOf('@');
|
||||
if (firstAtIndex === 0) {
|
||||
return false;
|
||||
}
|
||||
if (firstAtIndex !== -1 && authority.indexOf('@', firstAtIndex + 1) !== -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const hostList = firstAtIndex >= 0 ? authority.slice(firstAtIndex + 1) : authority;
|
||||
if (!hostList) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -615,12 +615,31 @@ describe('Config Schemas', () => {
|
|||
expect(seedListEmptyPort.uri).toBe('mongodb://u:p@mongo1.prod.internal:,[::1]:/stock');
|
||||
});
|
||||
|
||||
it('should accept percent-encoded @ in credentials, passwordless user, and @/# after / or ?', () => {
|
||||
const validUris = [
|
||||
'mongodb://dev_admin:p%40ss@localhost:27018/stock_dev',
|
||||
'mongodb://dev_admin:p%40ss@localhost:27018/stock_dev?appName=a@b#note',
|
||||
'mongodb://localhost:27018?authSource=admin#frag',
|
||||
'mongodb://dev_user@localhost:27018/stock_dev',
|
||||
];
|
||||
for (const uri of validUris) {
|
||||
const config = mongodbConfigSchema.parse({
|
||||
uri,
|
||||
readUri: uri,
|
||||
database: 'stock_dev',
|
||||
});
|
||||
expect(config.uri).toBe(uri);
|
||||
expect(config.readUri).toBe(uri);
|
||||
}
|
||||
});
|
||||
|
||||
it('should validate URI format', () => {
|
||||
const invalidUris = [
|
||||
'invalid-uri',
|
||||
'http://localhost:27017/stock',
|
||||
'mongodb://',
|
||||
'mongodb://u:p@',
|
||||
'mongodb://@localhost:27018/stock',
|
||||
'mongodb://u:p@localhost:27017,',
|
||||
'mongodb://u:p@:27017/stock',
|
||||
'mongodb://u:p@localhost:0/stock',
|
||||
|
|
@ -628,9 +647,20 @@ describe('Config Schemas', () => {
|
|||
'mongodb://u:p@[::1]:0/stock',
|
||||
'mongodb://u:p@[::1]:99999/stock',
|
||||
'mongodb://u:p@localhost:abc/stock',
|
||||
'mongodb://localhost:27018#,localhost/stock',
|
||||
'mongodb://localhost:27018#frag',
|
||||
'mongodb://u:p@localhost:27018#x,127.0.0.1/stock',
|
||||
'mongodb://u:p@localhost:27017@x:27018/stock',
|
||||
'mongodb://trading_admin:p@ss@localhost:27018/stock_dev',
|
||||
'mongodb://u:p@local\nhost:27017/stock',
|
||||
'mongodb://u:p@local\thost:27017/stock',
|
||||
'mongodb://u:p@localhost:27018/stock\x00',
|
||||
'mongodb://u:p@localhost:27018/stock?a=1\rb=2',
|
||||
'mongodb+srv://u:p@localhost:/stock',
|
||||
'mongodb+srv://u:p@localhost:27017/stock',
|
||||
'mongodb+srv://u:p@host1.example.com,host2.example.com/stock',
|
||||
'mongodb+srv://u:p@cluster.example.com#x/stock',
|
||||
'mongodb+srv://u:p@cluster.example.com@other.example.com/stock',
|
||||
];
|
||||
for (const uri of invalidUris) {
|
||||
expect(() =>
|
||||
|
|
@ -639,6 +669,13 @@ describe('Config Schemas', () => {
|
|||
database: 'testdb',
|
||||
})
|
||||
).toThrow();
|
||||
expect(() =>
|
||||
mongodbConfigSchema.parse({
|
||||
uri: 'mongodb://localhost:27018/stock_dev',
|
||||
readUri: uri,
|
||||
database: 'testdb',
|
||||
})
|
||||
).toThrow();
|
||||
}
|
||||
expect(() =>
|
||||
mongodbConfigSchema.parse({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue