Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-6289): allow valid srv hostnames with less than 3 parts #4197

Merged
merged 24 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
73ca011
drivers-2922 downstream changes first pass
aditi-khare-mongoDB Aug 14, 2024
ad151f7
temp commit
aditi-khare-mongoDB Aug 19, 2024
15dc8ee
added more prose tests
aditi-khare-mongoDB Sep 5, 2024
586f7c0
wording fix
aditi-khare-mongoDB Sep 5, 2024
36b0b49
Merge branch 'main' into uri-validate-less
aditi-khare-mongoDB Sep 5, 2024
bea037e
remove stray comment
aditi-khare-mongoDB Sep 5, 2024
ae79ac5
fix up
aditi-khare-mongoDB Sep 5, 2024
b2f843e
add back in comment
aditi-khare-mongoDB Sep 5, 2024
154e2ad
fix failing unit tests
aditi-khare-mongoDB Sep 5, 2024
1059175
Merge branch 'main' into uri-validate-less
aditi-khare-mongoDB Sep 11, 2024
d1209df
Merge branch 'main' into uri-validate-less
aditi-khare-mongoDB Sep 17, 2024
9dd438e
requested changes 1
aditi-khare-mongoDB Sep 19, 2024
f680380
requested changes + review from drivers ticket updates
aditi-khare-mongoDB Sep 24, 2024
4d3efd8
fix failing tests
aditi-khare-mongoDB Sep 25, 2024
062c139
fix failing tests 2
aditi-khare-mongoDB Sep 25, 2024
a3b489c
Merge branch 'main' into uri-validate-less
aditi-khare-mongoDB Sep 25, 2024
7895835
await close
aditi-khare-mongoDB Sep 26, 2024
4879b78
Merge branch 'main' into uri-validate-less
aditi-khare-mongoDB Sep 26, 2024
79f41e4
ready for rereivew
aditi-khare-mongoDB Oct 8, 2024
eafaf61
ready for rereivew
aditi-khare-mongoDB Oct 8, 2024
1de28de
Merge branch 'main' into uri-validate-less
aditi-khare-mongoDB Oct 8, 2024
ec63d53
add in comments of prose tests
aditi-khare-mongoDB Oct 14, 2024
6ae4230
Merge branch 'main' into uri-validate-less
aditi-khare-mongoDB Oct 14, 2024
00e235e
Merge branch 'main' into uri-validate-less
W-A-James Oct 15, 2024
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
15 changes: 8 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,16 +1165,13 @@ export function checkParentDomainMatch(address: string, srvHost: string): void {
// Add leading dot back to string so
// an srvHostDomain = '.trusted.site'
// will not satisfy an addressDomain that endsWith '.fake-trusted.site'
const addressDomain = srvIsLessThanThreeParts
? normalizedAddress
: `.${normalizedAddress.replace(allCharacterBeforeFirstDot, '')}`;
const srvHostDomain = srvIsLessThanThreeParts
const addressDomain = `.${normalizedAddress.replace(allCharacterBeforeFirstDot, '')}`;
let srvHostDomain = srvIsLessThanThreeParts
? normalizedSrvHost
: `.${normalizedSrvHost.replace(allCharacterBeforeFirstDot, '')}`;

if (!addressDomain.endsWith(srvHostDomain)) {
// TODO(NODE-3484): Replace with MongoConnectionStringError
throw new MongoAPIError('Server record does not share hostname with parent URI');
if (!srvHostDomain.startsWith('.')) {
srvHostDomain = '.' + srvHostDomain;
}
if (
srvIsLessThanThreeParts &&
Expand All @@ -1183,6 +1180,10 @@ export function checkParentDomainMatch(address: string, srvHost: string): void {
// TODO(NODE-3484): Replace with MongoConnectionStringError
throw new MongoAPIError('Server record does not have least one more domain than parent URI');
dariakp marked this conversation as resolved.
Show resolved Hide resolved
}
if (!addressDomain.endsWith(srvHostDomain)) {
// TODO(NODE-3484): Replace with MongoConnectionStringError
dariakp marked this conversation as resolved.
Show resolved Hide resolved
throw new MongoAPIError('Server record does not share hostname with parent URI');
}
}

interface RequestOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { MongoAPIError, Server, ServerDescription, Topology } from '../../mongod
import { topologyWithPlaceholderClient } from '../../tools/utils';

describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
context('1) When running validation on an SRV string before DNS resolution', function () {
context('1. When running validation on an SRV string before DNS resolution', function () {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
let client;

beforeEach(async function () {
// this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation

sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'resolved.mongodb.localhost',
name: 'resolved.mongo.localhost',
port: 27017,
weight: 0,
priority: 0
Expand All @@ -36,23 +38,22 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {

afterEach(async function () {
sinon.restore();
client.close();
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
});

it('does not error on an SRV because it has one domain level', async function () {
const client = await this.configuration.newClient('mongodb+srv://localhost', {});
client.connect();
client.close();
client = await this.configuration.newClient('mongodb+srv://localhost', {});
await client.connect();
});

it('does not error on an SRV because it has two domain levels', async function () {
const client = await this.configuration.newClient('mongodb+srv://mongodb.localhost', {});
client.connect();
client.close();
client = await this.configuration.newClient('mongodb+srv://mongo.localhost', {});
await client.connect();
});
});

context(
'2) When given a host from DNS resolution that does NOT end with the original SRVs domain name',
'2. When given a host from DNS resolution that does NOT end with the original SRVs domain name',
function () {
beforeEach(async function () {
sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => {
Expand All @@ -68,7 +69,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'localhost.mongodb', // this string contains the SRV but does not end with it
name: 'localhost.mongodb',
port: 27017,
weight: 0,
priority: 0
Expand All @@ -87,15 +88,15 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'evil.localhost', // this string only ends with part of the domain, not all of it!
name: 'test_1.evil.local', // this string only ends with part of the domain, not all of it!
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://mongodb.localhost', {})
.newClient('mongodb+srv://mongo.local', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
Expand All @@ -106,7 +107,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'blogs.evil.co.uk',
name: 'blogs.evil.com',
port: 27017,
weight: 0,
priority: 0
Expand All @@ -124,7 +125,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
);

context(
'3) When given a host from DNS resolution that is identical to the original SRVs hostname',
'3. When given a host from DNS resolution that is identical to the original SRVs hostname',
function () {
beforeEach(async function () {
sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => {
Expand Down Expand Up @@ -161,15 +162,15 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'mongodb.localhost',
name: 'mongo.local',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://mongodb.localhost', {})
.newClient('mongodb+srv://mongo.local', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
Expand All @@ -179,4 +180,76 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
});
}
);

context(
'4. When given a returned address that does NOT share the domain name of the SRV record because its missing a `.`',
function () {
beforeEach(async function () {
sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => {
throw { code: 'ENODATA' };
});
});

afterEach(async function () {
sinon.restore();
});

it('an SRV with one domain level causes a runtime error', async function () {
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'test_1.cluster_1localhost',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://localhost', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});

it('an SRV with two domain levels causes a runtime error', async function () {
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'test_1.my_hostmongo.local',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://mongo.local', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});

it('an SRV with three domain levels causes a runtime error', async function () {
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => {
return [
{
name: 'cluster.testmongodb.com',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://blogs.mongodb.com', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});
}
);
});