Skip to content

Commit 79f41e4

Browse files
ready for rereivew
1 parent 4879b78 commit 79f41e4

File tree

3 files changed

+60
-49
lines changed

3 files changed

+60
-49
lines changed

src/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ export function checkParentDomainMatch(address: string, srvHost: string): void {
11601160
const normalizedSrvHost = srvHost.endsWith('.') ? srvHost.slice(0, srvHost.length - 1) : srvHost;
11611161

11621162
const allCharacterBeforeFirstDot = /^.*?\./;
1163-
const srvIsLessThanThreeParts = srvHost.split('.').length < 3;
1163+
const srvIsLessThanThreeParts = normalizedSrvHost.split('.').length < 3;
11641164
// Remove all characters before first dot
11651165
// Add leading dot back to string so
11661166
// an srvHostDomain = '.trusted.site'
@@ -1177,11 +1177,9 @@ export function checkParentDomainMatch(address: string, srvHost: string): void {
11771177
srvIsLessThanThreeParts &&
11781178
normalizedAddress.split('.').length <= normalizedSrvHost.split('.').length
11791179
) {
1180-
// TODO(NODE-3484): Replace with MongoConnectionStringError
1181-
throw new MongoAPIError('Server record does not have least one more domain than parent URI');
1180+
throw new MongoAPIError('Server record does not have at least one more domain level than parent URI');
11821181
}
11831182
if (!addressDomain.endsWith(srvHostDomain)) {
1184-
// TODO(NODE-3484): Replace with MongoConnectionStringError
11851183
throw new MongoAPIError('Server record does not share hostname with parent URI');
11861184
}
11871185
}

test/integration/initial-dns-seedlist-discovery/initial_dns_seedlist_discovery.prose.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
154154
.catch(e => e);
155155
expect(err).to.be.instanceOf(MongoAPIError);
156156
expect(err.message).to.equal(
157-
'Server record does not have least one more domain than parent URI'
157+
'Server record does not have at least one more domain level than parent URI'
158158
);
159159
});
160160

@@ -175,7 +175,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
175175
.catch(e => e);
176176
expect(err).to.be.instanceOf(MongoAPIError);
177177
expect(err.message).to.equal(
178-
'Server record does not have least one more domain than parent URI'
178+
'Server record does not have at least one more domain level than parent URI'
179179
);
180180
});
181181
}

test/unit/utils.test.ts

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -940,56 +940,69 @@ describe('driver utils', function () {
940940
});
941941

942942
describe('checkParentDomainMatch()', () => {
943-
const exampleSrvName = 'i-love-javascript.mongodb.io';
944-
const exampleSrvNameWithDot = 'i-love-javascript.mongodb.io.';
945-
const exampleHostNameWithoutDot = 'i-love-javascript-00.mongodb.io';
946-
const exampleHostNamesWithDot = exampleHostNameWithoutDot + '.';
947-
const exampleHostNamThatDoNotMatchParent = 'i-love-javascript-00.evil-mongodb.io';
948-
const exampleHostNamThatDoNotMatchParentWithDot = 'i-love-javascript-00.evil-mongodb.io.';
949943

950-
context('when address does not match parent domain', () => {
951-
it('without a trailing dot throws', () => {
952-
expect(() =>
953-
checkParentDomainMatch(exampleHostNamThatDoNotMatchParent, exampleSrvName)
954-
).to.throw('Server record does not share hostname with parent URI');
955-
});
944+
const exampleSrvName = ['i-love-js', 'i-love-js.mongodb', 'i-love-javascript.mongodb.io'];
945+
const exampleSrvNameWithDot = ['i-love-js.', 'i-love-js.mongodb.', 'i-love-javascript.mongodb.io.'];
946+
const exampleHostNameWithoutDot = ['js-00.i-love-js', 'js-00.i-love-js.mongodb', 'i-love-javascript-00.mongodb.io'];
947+
const exampleHostNamesWithDot = ['js-00.i-love-js.', 'js-00.i-love-js.mongodb.', 'i-love-javascript-00.mongodb.io.'];
948+
const exampleHostNameThatDoNotMatchParent = ['js-00.i-love-js-a-little','js-00.i-love-js-a-little.mongodb', 'i-love-javascript-00.evil-mongodb.io'];
949+
const exampleHostNameThatDoNotMatchParentWithDot = ['i-love-js','', 'i-love-javascript-00.evil-mongodb.io.'];
950+
951+
for (let num = 0; num < 3; num += 1) {
952+
context(`when srvName has ${num + 1} part${num !== 0 ? 's' : ''}`, () => {
953+
context('when address does not match parent domain', () => {
954+
it('without a trailing dot throws', () => {
955+
expect(() =>
956+
checkParentDomainMatch(exampleHostNameThatDoNotMatchParent[num], exampleSrvName[num])
957+
).to.throw('Server record does not share hostname with parent URI');
958+
});
956959

957-
it('with a trailing dot throws', () => {
958-
expect(() =>
959-
checkParentDomainMatch(exampleHostNamThatDoNotMatchParentWithDot, exampleSrvName)
960-
).to.throw('Server record does not share hostname with parent URI');
961-
});
962-
});
960+
it('with a trailing dot throws', () => {
961+
expect(() =>
962+
checkParentDomainMatch(exampleHostNameThatDoNotMatchParentWithDot[num], exampleSrvName[num])
963+
).to.throw();
964+
});
965+
});
963966

964-
context('when addresses in SRV record end with a dot', () => {
965-
it('accepts address since it is considered to still match the parent domain', () => {
966-
expect(() =>
967-
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)
968-
).to.not.throw();
969-
});
970-
});
967+
context('when addresses in SRV record end with a dot', () => {
968+
it('accepts address since it is considered to still match the parent domain', () => {
969+
expect(() =>
970+
checkParentDomainMatch(exampleHostNamesWithDot[num], exampleSrvName[num])
971+
).to.not.throw();
972+
});
973+
});
971974

972-
context('when SRV host ends with a dot', () => {
973-
it('accepts address if it ends with a dot', () => {
974-
expect(() =>
975-
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvNameWithDot)
976-
).to.not.throw();
977-
});
975+
context('when SRV host ends with a dot', () => {
976+
it('accepts address if it ends with a dot', () => {
977+
expect(() =>
978+
checkParentDomainMatch(exampleHostNamesWithDot[num], exampleSrvNameWithDot[num])
979+
).to.not.throw();
980+
});
978981

979-
it('accepts address if it does not end with a dot', () => {
980-
expect(() =>
981-
checkParentDomainMatch(exampleHostNameWithoutDot, exampleSrvName)
982-
).to.not.throw();
983-
});
984-
});
982+
it('accepts address if it does not end with a dot', () => {
983+
expect(() =>
984+
checkParentDomainMatch(exampleHostNameWithoutDot[num], exampleSrvNameWithDot[num])
985+
).to.not.throw();
986+
});
987+
988+
if (num < 2) {
989+
it('does not accept address if it does not contain an extra domain level', () => {
990+
expect(() =>
991+
checkParentDomainMatch(exampleSrvNameWithDot[num], exampleSrvNameWithDot[num])
992+
).to.throw('Server record does not have at least one more domain level than parent URI');
993+
});
994+
}
995+
});
985996

986-
context('when addresses in SRV record end without dots', () => {
987-
it('accepts address since it matches the parent domain', () => {
988-
expect(() =>
989-
checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)
990-
).to.not.throw();
997+
context('when addresses in SRV record end without dots', () => {
998+
it('accepts address since it matches the parent domain', () => {
999+
expect(() =>
1000+
checkParentDomainMatch(exampleHostNamesWithDot[num], exampleSrvName[num])
1001+
).to.not.throw();
1002+
});
1003+
});
9911004
});
992-
});
1005+
}
9931006
});
9941007

9951008
describe('isUint8Array()', () => {

0 commit comments

Comments
 (0)