Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,27 @@ export function isUint8Array(value: unknown): value is Uint8Array {
*/
export function hostMatchesWildcards(host: string, wildcards: string[]): boolean {
for (const wildcard of wildcards) {
if (
host === wildcard ||
(wildcard.startsWith('*.') && host?.endsWith(wildcard.substring(2, wildcard.length))) ||
(wildcard.startsWith('*/') && host?.endsWith(wildcard.substring(2, wildcard.length)))
) {
// Exact match always wins
if (host === wildcard) {
return true;
}

// Wildcard match with leading *.
if (wildcard.startsWith('*.')) {
const suffix = wildcard.substring(2);
// Exact match or strict subdomain match
if (host === suffix || host.endsWith(`.${suffix}`)) {
return true;
}
}
// Wildcard match with leading */
if (wildcard.startsWith('*/')) {
const suffix = wildcard.substring(2);
// Exact match or strict subpath match
if (host === suffix || host.endsWith(`/${suffix}`)) {
return true;
}
}
Comment thread
tadjik1 marked this conversation as resolved.
}
return false;
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ describe('driver utils', function () {
});
});

context('when the wildcard starts with *.', function () {
it('returns false', function () {
expect(hostMatchesWildcards('test-mongodb.com', ['*.mongodb.com', 'test2'])).to.be
.false;
});
});

context('when the host matches a FQDN', function () {
it('returns true', function () {
expect(hostMatchesWildcards('mongodb.net', ['*.mongodb.net', 'other'])).to.be.true;
Expand Down Expand Up @@ -221,6 +228,14 @@ describe('driver utils', function () {
.to.be.false;
});
});

context('when the host does not match partial matches', function () {
it('returns false', function () {
expect(
hostMatchesWildcards('/tmp/test-mongodb-27017.sock', ['*/mongodb-27017.sock', 'test2'])
).to.be.false;
});
});
});
});

Expand Down