Skip to content

Commit f36b754

Browse files
fix(NODE-7478): OIDC host allowlist fix (#4905)
1 parent b2cb05e commit f36b754

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/utils.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,27 @@ export function isUint8Array(value: unknown): value is Uint8Array {
6969
*/
7070
export function hostMatchesWildcards(host: string, wildcards: string[]): boolean {
7171
for (const wildcard of wildcards) {
72-
if (
73-
host === wildcard ||
74-
(wildcard.startsWith('*.') && host?.endsWith(wildcard.substring(2, wildcard.length))) ||
75-
(wildcard.startsWith('*/') && host?.endsWith(wildcard.substring(2, wildcard.length)))
76-
) {
72+
// Exact match always wins
73+
if (host === wildcard) {
7774
return true;
7875
}
76+
77+
// Wildcard match with leading *.
78+
if (wildcard.startsWith('*.')) {
79+
const suffix = wildcard.substring(2);
80+
// Exact match or strict subdomain match
81+
if (host === suffix || host.endsWith(`.${suffix}`)) {
82+
return true;
83+
}
84+
}
85+
// Wildcard match with leading */
86+
if (wildcard.startsWith('*/')) {
87+
const suffix = wildcard.substring(2);
88+
// Exact match or strict subpath match
89+
if (host === suffix || host.endsWith(`/${suffix}`)) {
90+
return true;
91+
}
92+
}
7993
}
8094
return false;
8195
}

test/unit/utils.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
checkParentDomainMatch,
1111
compareObjectId,
1212
decorateWithExplain,
13+
DEFAULT_ALLOWED_HOSTS,
1314
Explain,
1415
hasAtomicOperators,
1516
HostAddress,
@@ -149,6 +150,26 @@ describe('driver utils', function () {
149150
});
150151
});
151152

153+
context('when the wildcard starts with *.', function () {
154+
it('returns false', function () {
155+
expect(hostMatchesWildcards('test-mongodb.com', ['*.mongodb.com', 'test2'])).to.be
156+
.false;
157+
});
158+
});
159+
160+
context('when using default allowed hosts', function () {
161+
it('returns false', function () {
162+
for (const host of DEFAULT_ALLOWED_HOSTS) {
163+
// Only test the wildcard hosts, the non-wildcard hosts are tested in other test cases
164+
if (!host.startsWith('*.')) {
165+
continue;
166+
}
167+
const wrongHost = host.replace('*.', 'test-');
168+
expect(hostMatchesWildcards(wrongHost, DEFAULT_ALLOWED_HOSTS)).to.be.false;
169+
}
170+
});
171+
});
172+
152173
context('when the host matches a FQDN', function () {
153174
it('returns true', function () {
154175
expect(hostMatchesWildcards('mongodb.net', ['*.mongodb.net', 'other'])).to.be.true;
@@ -222,6 +243,14 @@ describe('driver utils', function () {
222243
.to.be.false;
223244
});
224245
});
246+
247+
context('when the host does not match partial matches', function () {
248+
it('returns false', function () {
249+
expect(
250+
hostMatchesWildcards('/tmp/test-mongodb-27017.sock', ['*/mongodb-27017.sock', 'test2'])
251+
).to.be.false;
252+
});
253+
});
225254
});
226255
});
227256

0 commit comments

Comments
 (0)