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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

aditi-khare-mongoDB
Copy link
Contributor

@aditi-khare-mongoDB aditi-khare-mongoDB commented Aug 15, 2024

Description

Downstream changes for DRIVERS-2922 (PR).

What is changing?

  • Remove logic asserting that SRV URIs need 3 parts
  • When we check if a returned address matches its parent SRV, an SRV with <3 parts must assert that the returned address contains an additional domain level
  • Add in prose tests.
Is there new documentation needed for these changes?

No

What is the motivation for this change?

Do not throw an error on valid URI formats pre-DNS resolution, and require stricter domain matching post-DNS resolution.

Release Highlight

Allow SRV hostnames with less than three . separated parts

The client now accepts SRV hostname strings with one or two . separated parts.

For example, the following code no longer throws an error.

await new MongoClient('mongodb+srv://localhost').connect();

For security reasons, the returned addresses of SRV strings with less than three parts must end with the entire SRV hostname and contain at least one additional domain level. This is because this added validation ensures that the returned address(es) are from a known host. In future releases, we plan on extending this validation to SRV strings with three or more parts, as well.

// Example 1: Validation fails since the returned address doesn't end with the entire SRV hostname
'mongodb+srv://mySite.com' => 'myEvilSite.com' 

// Example 2: Validation fails since the returned address is identical to the SRV hostname
'mongodb+srv://mySite.com' => 'mySite.com' 

// Example 3: Validation passes since the returned address ends with the entire SRV hostname and contains an additional domain level
'mongodb+srv://mySite.com' => 'cluster_1.mySite.com' 

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@aditi-khare-mongoDB aditi-khare-mongoDB changed the title feat(NODE-6289): DRIVERS-2922 Downstream Changes PoC feat(NODE-6289): DRIVERS 2922 Downstream Changes PoC Aug 15, 2024
@aditi-khare-mongoDB aditi-khare-mongoDB changed the title feat(NODE-6289): DRIVERS 2922 Downstream Changes PoC feat(NODE-6289): Allow valid SRV hostnames with less than 3 parts Sep 5, 2024
@aditi-khare-mongoDB aditi-khare-mongoDB changed the title feat(NODE-6289): Allow valid SRV hostnames with less than 3 parts feat(NODE-6289): allow valid srv hostnames with less than 3 parts Sep 5, 2024
@aditi-khare-mongoDB aditi-khare-mongoDB marked this pull request as ready for review September 19, 2024 15:45
@W-A-James W-A-James self-assigned this Sep 19, 2024
@W-A-James W-A-James self-requested a review September 19, 2024 15:47
@W-A-James W-A-James added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Sep 19, 2024

describe(
'Initial DNS Seedlist Discovery (Prose Tests)',
{ requires: { topology: 'single' } },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The metadata doesn't actually apply to context or describe blocks, only to it blocks, if you check this against a replica set, it still runs.

Also, why do we only want these tests to run against single topologies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why I originally made it only single topologies, if it's running and working for all I'll remove this line.

client.close();
});

it('do not error on an SRV because it has two domain levels', async function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('do not error on an SRV because it has two domain levels', async function () {
it('does not error on an SRV because it has two domain levels', async function () {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the change!

sinon.restore();
});

it('do not error on an SRV because it has one domain level', async function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('do not error on an SRV because it has one domain level', async function () {
it('does not error on an SRV because it has one domain level', async function () {

});
});

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw is one of the few chai clauses that always needs to be called

Suggested change
.throw;
.throw();

Comment on lines 977 to 979
expect(() => checkParentDomainMatch(exampleHostNameWithoutDot, exampleSrvName)).to.not
.throw;
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(() => checkParentDomainMatch(exampleHostNameWithoutDot, exampleSrvName)).to.not
.throw;
});
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(() => checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)).to.not.throw;
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(matchesParentDomain(exampleHostNamesWithDot, exampleSrvName)).to.be.true;
expect(() => checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)).to.not.throw;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(() => checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)).to.not.throw;
expect(() => checkParentDomainMatch(exampleHostNamesWithDot, exampleSrvName)).to.not.throw();

});

context(
'When given a host from DNS resolution that does NOT end with the original SRVs domain name',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the number of the prose test. Makes it quicker to map from our test implementation to the test in the spec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, added them in!

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 () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 () {

});

context(
'2) When given a host from DNS resolution that does NOT end with the original SRVs domain name',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'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',

);

context(
'3) When given a host from DNS resolution that is identical to the original SRVs hostname',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'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',


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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be sure to await client.connect() and client.close()

Suggested change
client.connect();
await client.connect();

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these tests, I'd move the client.close() call to an afterEach hook so that even if the connect fails, we clean up any resources created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Primary Review In Review with primary reviewer, not yet ready for team's eyes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants