-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
base: main
Are you sure you want to change the base?
Changes from 12 commits
73ca011
ad151f7
15dc8ee
586f7c0
36b0b49
bea037e
ae79ac5
b2f843e
154e2ad
1059175
d1209df
9dd438e
f680380
4d3efd8
062c139
a3b489c
7895835
4879b78
79f41e4
eafaf61
1de28de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import type { FindCursor } from './cursor/find_cursor'; | |
import type { Db } from './db'; | ||
import { | ||
type AnyError, | ||
MongoAPIError, | ||
MongoCompatibilityError, | ||
MongoInvalidArgumentError, | ||
MongoNetworkTimeoutError, | ||
|
@@ -1142,29 +1143,46 @@ export function parseUnsignedInteger(value: unknown): number | null { | |
} | ||
|
||
/** | ||
* Determines whether a provided address matches the provided parent domain. | ||
* This function throws a MongoAPIError in the event that either of the following is true: | ||
* * If the provided address domain does not match the provided parent domain | ||
* * If the parent domain contains less than three `.` separated parts and the provided address does not contain at least one more domain level than its parent | ||
* | ||
* If a DNS server were to become compromised SRV records would still need to | ||
* advertise addresses that are under the same domain as the srvHost. | ||
* | ||
* @param address - The address to check against a domain | ||
* @param srvHost - The domain to check the provided address against | ||
* @returns Whether the provided address matches the parent domain | ||
* @returns void | ||
*/ | ||
export function matchesParentDomain(address: string, srvHost: string): boolean { | ||
export function checkParentDomainMatch(address: string, srvHost: string): void { | ||
// Remove trailing dot if exists on either the resolved address or the srv hostname | ||
const normalizedAddress = address.endsWith('.') ? address.slice(0, address.length - 1) : address; | ||
const normalizedSrvHost = srvHost.endsWith('.') ? srvHost.slice(0, srvHost.length - 1) : srvHost; | ||
|
||
const allCharacterBeforeFirstDot = /^.*?\./; | ||
const srvIsLessThanThreeParts = srvHost.split('.').length < 3; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the correct check? I would think we are interested in checking whether the normalized host has fewer than 3 parts, since I don't think we want to consider the trailing dot on a 2- part host any differently, if I understand correctly. If you agree, could you make sure that there are new tests in the unit test section that fail with the code as it currently is and pass when the code is updated to check the normalized part count instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, you're right, I've added in a new test that would have failed otherwise (the 'extra domain level' one) and fixed a typo in these section. |
||
// Remove all characters before first dot | ||
// Add leading dot back to string so | ||
// an srvHostDomain = '.trusted.site' | ||
// will not satisfy an addressDomain that endsWith '.fake-trusted.site' | ||
const addressDomain = `.${normalizedAddress.replace(allCharacterBeforeFirstDot, '')}`; | ||
const srvHostDomain = `.${normalizedSrvHost.replace(allCharacterBeforeFirstDot, '')}`; | ||
|
||
return addressDomain.endsWith(srvHostDomain); | ||
const addressDomain = srvIsLessThanThreeParts | ||
? normalizedAddress | ||
: `.${normalizedAddress.replace(allCharacterBeforeFirstDot, '')}`; | ||
const 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 ( | ||
srvIsLessThanThreeParts && | ||
normalizedAddress.split('.').length <= normalizedSrvHost.split('.').length | ||
) { | ||
// TODO(NODE-3484): Replace with MongoConnectionStringError | ||
throw new MongoAPIError('Server record does not have least one more domain than parent URI'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the grammar on this needs some adjustment - could you double check the wording? I think it was probably meant to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! |
||
} | ||
} | ||
|
||
interface RequestOptions { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import { expect } from 'chai'; | ||
import * as dns from 'dns'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { MongoAPIError, Server, ServerDescription, Topology } from '../../mongodb'; | ||
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 () { | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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', | ||
port: 27017, | ||
weight: 0, | ||
priority: 0 | ||
} | ||
]; | ||
}); | ||
|
||
sinon.stub(dns.promises, 'resolveTxt').callsFake(async () => { | ||
throw { code: 'ENODATA' }; | ||
}); | ||
|
||
sinon.stub(Topology.prototype, 'selectServer').callsFake(async () => { | ||
return new Server( | ||
topologyWithPlaceholderClient([], {} as any), | ||
new ServerDescription('a:1'), | ||
{} as any | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(async function () { | ||
sinon.restore(); | ||
}); | ||
|
||
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(); | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client.close(); | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
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(); | ||
}); | ||
}); | ||
|
||
context( | ||
'2) When given a host from DNS resolution that does NOT end with the original SRVs domain name', | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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: 'localhost.mongodb', // this string contains the SRV but does not end with it | ||
port: 27017, | ||
weight: 0, | ||
priority: 0 | ||
} | ||
]; | ||
}); | ||
const err = await this.configuration | ||
.newClient('mongodb+srv://localhost', {}) | ||
.connect() | ||
.catch((e: any) => 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: 'evil.localhost', // 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', {}) | ||
.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 or more domain levels causes a runtime error', async function () { | ||
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { | ||
return [ | ||
{ | ||
name: 'blogs.evil.co.uk', | ||
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'); | ||
}); | ||
} | ||
); | ||
|
||
context( | ||
'3) When given a host from DNS resolution that is identical to the original SRVs hostname', | ||
W-A-James marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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: 'localhost', | ||
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 have least one more domain than parent URI' | ||
); | ||
}); | ||
|
||
it('an SRV with two domain levels causes a runtime error', async function () { | ||
sinon.stub(dns.promises, 'resolveSrv').callsFake(async () => { | ||
return [ | ||
{ | ||
name: 'mongodb.localhost', | ||
port: 27017, | ||
weight: 0, | ||
priority: 0 | ||
} | ||
]; | ||
}); | ||
const err = await this.configuration | ||
.newClient('mongodb+srv://mongodb.localhost', {}) | ||
.connect() | ||
.catch(e => e); | ||
expect(err).to.be.instanceOf(MongoAPIError); | ||
expect(err.message).to.equal( | ||
'Server record does not have least one more domain than parent URI' | ||
); | ||
}); | ||
} | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { expect } from 'chai'; | |
import { | ||
BufferPool, | ||
ByteUtils, | ||
checkParentDomainMatch, | ||
compareObjectId, | ||
decorateWithExplain, | ||
Explain, | ||
|
@@ -12,7 +13,6 @@ import { | |
isUint8Array, | ||
LEGACY_HELLO_COMMAND, | ||
List, | ||
matchesParentDomain, | ||
MongoDBCollectionNamespace, | ||
MongoDBNamespace, | ||
MongoRuntimeError, | ||
|
@@ -939,7 +939,7 @@ describe('driver utils', function () { | |
}); | ||
}); | ||
|
||
describe('matchesParentDomain()', () => { | ||
describe('checkParentDomainMatch()', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add analogous tests for what happens when we have a trailing dot on a 2- or 1- part srv? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! Just added them in |
||
const exampleSrvName = 'i-love-javascript.mongodb.io'; | ||
const exampleSrvNameWithDot = 'i-love-javascript.mongodb.io.'; | ||
const exampleHostNameWithoutDot = 'i-love-javascript-00.mongodb.io'; | ||
|
@@ -948,35 +948,46 @@ describe('driver utils', function () { | |
const exampleHostNamThatDoNotMatchParentWithDot = 'i-love-javascript-00.evil-mongodb.io.'; | ||
|
||
context('when address does not match parent domain', () => { | ||
it('without a trailing dot returns false', () => { | ||
expect(matchesParentDomain(exampleHostNamThatDoNotMatchParent, exampleSrvName)).to.be.false; | ||
it('without a trailing dot throws', () => { | ||
expect(() => | ||
checkParentDomainMatch(exampleHostNamThatDoNotMatchParent, exampleSrvName) | ||
).to.throw('Server record does not share hostname with parent URI'); | ||
}); | ||
|
||
it('with a trailing dot returns false', () => { | ||
expect(matchesParentDomain(exampleHostNamThatDoNotMatchParentWithDot, exampleSrvName)).to.be | ||
.false; | ||
it('with a trailing dot throws', () => { | ||
expect(() => | ||
checkParentDomainMatch(exampleHostNamThatDoNotMatchParentWithDot, exampleSrvName) | ||
).to.throw('Server record does not share hostname with parent URI'); | ||
}); | ||
}); | ||
|
||
context('when addresses in SRV record end with a dot', () => { | ||
it('accepts address since it is considered to still match the parent domain', () => { | ||
expect(matchesParentDomain(exampleHostNamesWithDot, exampleSrvName)).to.be.true; | ||
expect(() => | ||
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName) | ||
).to.not.throw(); | ||
}); | ||
}); | ||
|
||
context('when SRV host ends with a dot', () => { | ||
it('accepts address if it ends with a dot', () => { | ||
expect(matchesParentDomain(exampleHostNamesWithDot, exampleSrvNameWithDot)).to.be.true; | ||
expect(() => | ||
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvNameWithDot) | ||
).to.not.throw(); | ||
}); | ||
|
||
it('accepts address if it does not end with a dot', () => { | ||
expect(matchesParentDomain(exampleHostNameWithoutDot, exampleSrvName)).to.be.true; | ||
expect(() => | ||
checkParentDomainMatch(exampleHostNameWithoutDot, exampleSrvName) | ||
).to.not.throw(); | ||
}); | ||
}); | ||
|
||
context('when addresses in SRV record end without dots', () => { | ||
it('accepts address since it matches the parent domain', () => { | ||
expect(matchesParentDomain(exampleHostNamesWithDot, exampleSrvName)).to.be.true; | ||
expect(() => | ||
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName) | ||
).to.not.throw(); | ||
}); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find a kickoff document for this work, so I am not sure if this is something that has already been discussed, but how frequently is this function invoked? Is it in the hot path for anything? This construction, while cleaner, is less performant (since try catch is more expensive than a simple if/then, and the additional function call for the no-op may or may not get optimized).