Skip to content

Commit

Permalink
ready for rereivew
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Oct 8, 2024
1 parent 4879b78 commit 79f41e4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
6 changes: 2 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ export function checkParentDomainMatch(address: string, srvHost: string): void {
const normalizedSrvHost = srvHost.endsWith('.') ? srvHost.slice(0, srvHost.length - 1) : srvHost;

const allCharacterBeforeFirstDot = /^.*?\./;
const srvIsLessThanThreeParts = srvHost.split('.').length < 3;
const srvIsLessThanThreeParts = normalizedSrvHost.split('.').length < 3;
// Remove all characters before first dot
// Add leading dot back to string so
// an srvHostDomain = '.trusted.site'
Expand All @@ -1177,11 +1177,9 @@ export function checkParentDomainMatch(address: string, srvHost: string): void {
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');
throw new MongoAPIError('Server record does not have at least one more domain level than parent URI');
}
if (!addressDomain.endsWith(srvHostDomain)) {
// TODO(NODE-3484): Replace with MongoConnectionStringError
throw new MongoAPIError('Server record does not share hostname with parent URI');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
.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'
'Server record does not have at least one more domain level than parent URI'
);
});

Expand All @@ -175,7 +175,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
.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'
'Server record does not have at least one more domain level than parent URI'
);
});
}
Expand Down
99 changes: 56 additions & 43 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,56 +940,69 @@ describe('driver utils', function () {
});

describe('checkParentDomainMatch()', () => {
const exampleSrvName = 'i-love-javascript.mongodb.io';
const exampleSrvNameWithDot = 'i-love-javascript.mongodb.io.';
const exampleHostNameWithoutDot = 'i-love-javascript-00.mongodb.io';
const exampleHostNamesWithDot = exampleHostNameWithoutDot + '.';
const exampleHostNamThatDoNotMatchParent = 'i-love-javascript-00.evil-mongodb.io';
const exampleHostNamThatDoNotMatchParentWithDot = 'i-love-javascript-00.evil-mongodb.io.';

context('when address does not match parent domain', () => {
it('without a trailing dot throws', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamThatDoNotMatchParent, exampleSrvName)
).to.throw('Server record does not share hostname with parent URI');
});
const exampleSrvName = ['i-love-js', 'i-love-js.mongodb', 'i-love-javascript.mongodb.io'];
const exampleSrvNameWithDot = ['i-love-js.', 'i-love-js.mongodb.', 'i-love-javascript.mongodb.io.'];
const exampleHostNameWithoutDot = ['js-00.i-love-js', 'js-00.i-love-js.mongodb', 'i-love-javascript-00.mongodb.io'];
const exampleHostNamesWithDot = ['js-00.i-love-js.', 'js-00.i-love-js.mongodb.', 'i-love-javascript-00.mongodb.io.'];
const exampleHostNameThatDoNotMatchParent = ['js-00.i-love-js-a-little','js-00.i-love-js-a-little.mongodb', 'i-love-javascript-00.evil-mongodb.io'];
const exampleHostNameThatDoNotMatchParentWithDot = ['i-love-js','', 'i-love-javascript-00.evil-mongodb.io.'];

for (let num = 0; num < 3; num += 1) {
context(`when srvName has ${num + 1} part${num !== 0 ? 's' : ''}`, () => {
context('when address does not match parent domain', () => {
it('without a trailing dot throws', () => {
expect(() =>
checkParentDomainMatch(exampleHostNameThatDoNotMatchParent[num], exampleSrvName[num])
).to.throw('Server record does not share hostname with parent URI');
});

it('with a trailing dot throws', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamThatDoNotMatchParentWithDot, exampleSrvName)
).to.throw('Server record does not share hostname with parent URI');
});
});
it('with a trailing dot throws', () => {
expect(() =>
checkParentDomainMatch(exampleHostNameThatDoNotMatchParentWithDot[num], exampleSrvName[num])
).to.throw();
});
});

context('when addresses in SRV record end with a dot', () => {
it('accepts address since it is considered to still match the parent domain', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)
).to.not.throw();
});
});
context('when addresses in SRV record end with a dot', () => {
it('accepts address since it is considered to still match the parent domain', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamesWithDot[num], exampleSrvName[num])
).to.not.throw();
});
});

context('when SRV host ends with a dot', () => {
it('accepts address if it ends with a dot', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvNameWithDot)
).to.not.throw();
});
context('when SRV host ends with a dot', () => {
it('accepts address if it ends with a dot', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamesWithDot[num], exampleSrvNameWithDot[num])
).to.not.throw();
});

it('accepts address if it does not end with a dot', () => {
expect(() =>
checkParentDomainMatch(exampleHostNameWithoutDot, exampleSrvName)
).to.not.throw();
});
});
it('accepts address if it does not end with a dot', () => {
expect(() =>
checkParentDomainMatch(exampleHostNameWithoutDot[num], exampleSrvNameWithDot[num])
).to.not.throw();
});

if (num < 2) {
it('does not accept address if it does not contain an extra domain level', () => {
expect(() =>
checkParentDomainMatch(exampleSrvNameWithDot[num], exampleSrvNameWithDot[num])
).to.throw('Server record does not have at least one more domain level than parent URI');
});
}
});

context('when addresses in SRV record end without dots', () => {
it('accepts address since it matches the parent domain', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)
).to.not.throw();
context('when addresses in SRV record end without dots', () => {
it('accepts address since it matches the parent domain', () => {
expect(() =>
checkParentDomainMatch(exampleHostNamesWithDot[num], exampleSrvName[num])
).to.not.throw();
});
});
});
});
}
});

describe('isUint8Array()', () => {
Expand Down

0 comments on commit 79f41e4

Please sign in to comment.