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

DRIVERS-2922: Allow valid SRV hostnames with fewer than 3 parts #1628

Merged
merged 28 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f27364c
feat(DRIVERS-2922): loosen options parser restrictions
aditi-khare-mongoDB Aug 8, 2024
2385382
add test criteria
aditi-khare-mongoDB Aug 14, 2024
1873743
grammar fix
aditi-khare-mongoDB Aug 14, 2024
fbacddb
wording fix
aditi-khare-mongoDB Aug 15, 2024
b052a3a
temp commit - changing terminology
aditi-khare-mongoDB Aug 20, 2024
73f7dac
change terminology
aditi-khare-mongoDB Aug 20, 2024
726d576
update changelog
aditi-khare-mongoDB Aug 20, 2024
81f43da
add in prose test ref
aditi-khare-mongoDB Aug 20, 2024
d363089
add parent matching requirements
aditi-khare-mongoDB Aug 23, 2024
9144879
update changelod
aditi-khare-mongoDB Aug 23, 2024
da90490
added in new prose test requirements + fixed formatting
aditi-khare-mongoDB Sep 3, 2024
ff800b4
requested changes
aditi-khare-mongoDB Sep 3, 2024
b26afc2
requested changes 2
aditi-khare-mongoDB Sep 4, 2024
a029753
uniform formatting + fix typo
aditi-khare-mongoDB Sep 5, 2024
43785b2
team review requested changes
aditi-khare-mongoDB Sep 6, 2024
be32cbb
team review requested changes
aditi-khare-mongoDB Sep 6, 2024
cf83c95
team review requested changes 2
aditi-khare-mongoDB Sep 13, 2024
6d953e3
update deprecation comment
aditi-khare-mongoDB Sep 13, 2024
9db03a7
typo
aditi-khare-mongoDB Sep 13, 2024
c2d075c
Merge branch 'master' into drivers-2922/uri-validation
aditi-khare-mongoDB Sep 16, 2024
f4175e9
clarify subdomain
aditi-khare-mongoDB Sep 20, 2024
54a5555
add in Shanes test
aditi-khare-mongoDB Sep 23, 2024
0a90e7c
update changelog date
aditi-khare-mongoDB Sep 24, 2024
e1f6d67
add in specific cases
aditi-khare-mongoDB Sep 24, 2024
fbc8b29
fix test cases
aditi-khare-mongoDB Sep 24, 2024
d658107
fix tests
aditi-khare-mongoDB Sep 24, 2024
c4bb2b4
fix tests
aditi-khare-mongoDB Sep 24, 2024
63f99bb
grammar fix
aditi-khare-mongoDB Sep 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ ______________________________________________________________________

Presently, seeding a driver with an initial list of ReplicaSet or MongoS addresses is somewhat cumbersome, requiring a
comma-delimited list of host names to attempt connections to. A standardized answer to this problem exists in the form
of SRV records, which allow administrators to configure a single domain to return a list of host names. Supporting this
feature would assist our users by decreasing maintenance load, primarily by removing the need to maintain seed lists at
an application level.
of SRV records, which allow administrators to configure a single SRV record to return a list of host names. Supporting
this feature would assist our users by decreasing maintenance load, primarily by removing the need to maintain seed
lists at an application level.

This specification builds on the [Connection String](../connection-string/connection-string-spec.md) specification. It
adds a new protocol scheme and modifies how the
Expand All @@ -31,12 +31,36 @@ step before it considers the connection string and SDAM specifications. In this
host names is replaced with a single host name. The format is:

```
mongodb+srv://{hostname}.{domainname}/{options}
mongodb+srv://{hostname}/{options}
```

`{options}` refers to the optional elements from the [Connection String](../connection-string/connection-string-spec.md)
specification following the `Host Information`. This includes the `Auth database` and `Connection Options`.

For the purposes of this document, `{hostname}` will be divided using the following terminology. If an SRV `{hostname}`
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
has:

1. Three or more `.` separated parts, then the left-most part is the `{subdomain}` and the remaining portion is the
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
`{domainname}`.

- Examples:
- `{hostname}` = `cluster_1.tests.mongodb.co.uk`

- `{subdomain}` = `cluster_1`
- `{domainname}` = `tests.mongodb.co.uk`

- `{hostname}` = `hosts_34.example.com`

- `{subdomain}` = `hosts_34`
- `{domainname}` = `example.com`

2. One or two `.` separated part(s), then the `{hostname}` is equivalent to the `{domainname}`, and there is no
subdomain.

- Examples:
- `{hostname}` = `{domainname}` = `localhost`
- `{hostname}` = `{domainname}` = `mongodb.local`

### MongoClient Configuration

#### srvMaxHosts
Expand Down Expand Up @@ -81,31 +105,32 @@ parse error and MUST NOT do DNS resolution or contact hosts.
It is an error to specify more than one host name in a connection string with the `mongodb+srv` protocol, and the driver
MUST raise a parse error and MUST NOT do DNS resolution or contact hosts.

A driver MUST verify that in addition to the `{hostname}`, the `{domainname}` consists of at least two parts: the domain
name, and a TLD. Drivers MUST raise an error and MUST NOT contact the DNS server to obtain SRV (or TXT records) if the
full URI does not consist of at least three parts.

If `mongodb+srv` is used, a driver MUST implicitly also enable TLS. Clients can turn this off by passing `tls=false` in
either the Connection String, or options passed in as parameters in code to the MongoClient constructor (or equivalent
API for each driver), but not through a TXT record (discussed in a later section).

#### Querying DNS

In this preprocessing step, the driver will query the DNS server for SRV records on `{hostname}.{domainname}`, prefixed
with the SRV service name and protocol. The SRV service name is provided in the `srvServiceName` URI option and defaults
to `mongodb`. The protocol is always `tcp`. After prefixing, the URI should look like:
`_{srvServiceName}._tcp.{hostname}.{domainname}`. This DNS query is expected to respond with one or more SRV records.
aditi-khare-mongoDB marked this conversation as resolved.
Show resolved Hide resolved
In this preprocessing step, the driver will query the DNS server for SRV records on the hostname, prefixed with the SRV
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
service name and protocol. The SRV service name is provided in the `srvServiceName` URI option and defaults to
`mongodb`. The protocol is always `tcp`. After prefixing, the URI should look like: `_{srvServiceName}._tcp.{hostname}`.
This DNS query is expected to respond with one or more SRV records.

The priority and weight fields in returned SRV records MUST be ignored.

If the DNS result returns no SRV records, or no records at all, or a DNS error happens, an error MUST be raised
indicating that the URI could not be used to find hostnames. The error SHALL include the reason why they could not be
found.

A driver MUST verify that the host names returned through SRV records have the same parent `{domainname}`. Drivers MUST
raise an error and MUST NOT initiate a connection to any returned host name which does not share the same
A driver MUST verify that the host names returned through SRV records share the original SRV's `{domainname}`. Drivers
MUST raise an error and MUST NOT initiate a connection to any returned host name which does not share the same
`{domainname}`.

In the case that the SRV record name has less than three `.` separated parts, the returned address' `{domainname}` MUST
be the SRV's entire `{hostname}` and MUST have at least one more domain level than the `{hostname}`. The next major
version MUST no longer allow an SRV record, with any number of parts, to return an address that doesn't contain the
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
SRVs' entire `{hostname}`. Drivers MUST document this in a prior minor release.

The driver MUST NOT attempt to connect to any hosts until the DNS query has returned its results.

If `srvMaxHosts` is zero or greater than or equal to the number of hosts in the DNS result, the driver MUST populate the
Expand All @@ -118,12 +143,12 @@ randomization.

### Default Connection String Options

As a second preprocessing step, a Client MUST also query the DNS server for TXT records on `{hostname}.{domainname}`. If
aditi-khare-mongoDB marked this conversation as resolved.
Show resolved Hide resolved
available, a TXT record provides default connection string options. The maximum length of a TXT record string is 255
characters, but there can be multiple strings per TXT record. A Client MUST support multiple TXT record strings and
concatenate them as if they were one single string in the order they are defined in each TXT record. The order of
multiple character strings in each TXT record is guaranteed. A Client MUST NOT allow multiple TXT records for the same
host name and MUST raise an error when multiple TXT records are encountered.
As a second preprocessing step, a Client MUST also query the DNS server for TXT records on `{hostname}`. If available, a
TXT record provides default connection string options. The maximum length of a TXT record string is 255 characters, but
there can be multiple strings per TXT record. A Client MUST support multiple TXT record strings and concatenate them as
if they were one single string in the order they are defined in each TXT record. The order of multiple character strings
in each TXT record is guaranteed. A Client MUST NOT allow multiple TXT records for the same host name and MUST raise an
error when multiple TXT records are encountered.

Information returned within a TXT record is a simple URI string, just like the `{options}` in a connection string.

Expand All @@ -148,10 +173,10 @@ the Connection String spec.

### CNAME not supported

The use of DNS CNAME records is not supported. Clients MUST NOT check for a CNAME record on `{hostname}.{domainname}`. A
system's DNS resolver could transparently handle CNAME, but because of how clients validate records returned from SRV
queries, use of CNAME could break validation. Seedlist discovery therefore does not recommend or support the use of
CNAME records in concert with SRV or TXT records.
The use of DNS CNAME records is not supported. Clients MUST NOT check for a CNAME record on `{hostname}`. A system's DNS
resolver could transparently handle CNAME, but because of how clients validate records returned from SRV queries, use of
CNAME could break validation. Seedlist discovery therefore does not recommend or support the use of CNAME records in
concert with SRV or TXT records.

## Example

Expand All @@ -169,7 +194,7 @@ _mongodb._tcp.server.mongodb.com. 86400 IN SRV 0 5 27317 mongodb1.
_mongodb._tcp.server.mongodb.com. 86400 IN SRV 0 5 27017 mongodb2.mongodb.com.
```

The returned host names (`mongodb1.mongodb.com` and `mongodb2.mongodb.com`) must share the same parent domain name
The returned host names (`mongodb1.mongodb.com` and `mongodb2.mongodb.com`) must share the same domain name
(`mongodb.com`) as the provided host name (`server.mongodb.com`).

The driver also needs to request the DNS server for the TXT records on `server.mongodb.com`. This could return:
Expand Down Expand Up @@ -200,6 +225,12 @@ mongodb://mongodb1.mongodb.com:27317,mongodb2.mongodb.com:27107/?ssl=true&replic

## Test Plan

### Prose Tests

See README.md in the accompanying [test directory](tests).

### Spec Tests

See README.md in the accompanying [test directory](tests).

Additionally, see the `mongodb+srv` test `invalid-uris.yml` in the
Expand Down Expand Up @@ -254,6 +285,11 @@ In the future we could consider using the priority and weight fields of the SRV

## ChangeLog

- 2024-08-20: Removed requirement for URI to have three '.' separated parts; these SRVs have stricter parent domain
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
matching requirements for security. SRVs with three or more parts should have stricter matching requirements in next
major version. Create terminology section. Remove usage of term `{TLD}`. The `{hostname}` now refers to the entire
hostname, not just the `{subdomain}`.

- 2024-03-06: Migrated from reStructuredText to Markdown.

- 2022-10-05: Revise spec front matter and reformat changelog.
Expand Down
35 changes: 35 additions & 0 deletions source/initial-dns-seedlist-discovery/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@
This directory contains platform-independent tests that drivers can use to prove their conformance to the Initial DNS
Seedlist Discovery spec.

## Prose Tests

For the following prose tests, it is assumed drivers are be able to stub DNS results to easily test invalid DNS
resolution results.

### 1. Allow SRVs with less than 3 `.` separated parts

For the following test, run each of these cases: SRVs with one and two `.` separated parts.
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved

When running validation on an SRV string before DNS resolution, do not throw a error due to number of SRV parts.

For example, `mongodb+srv://localhost` is a valid SRV string with one part/one domain level, and
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
`mongodb+srv://mongo.local` is a valid SRV string with two parts/two domain levels.

### 2. Throw when return address does not end with SRV domain
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved

For the following test, run each of these cases: SRVs with one, two, and three `.` separated parts.

When given a returned address that does NOT end with the original SRV's domain name, throw a runtime error.

For example, the SRV `mongodb+srv://blogs.mongodb.com` resolving to `blogs.evil.com` should prompt an error, since the
returned address does not end with `mongodb.com`.

Importantly, the domain of an SRV with one or two parts is its entire hostname. For example, the domain of
`mongodb+srv://mySite.com` is `mySite.com`. If this SRV resolves to `evil.com`, this should prompt an error.

### 3. Throw when return address is identical to SRV hostname

For the following test, run each of these cases: SRVs with one and two `.` separated parts.

When given a returned address that is identical to the original hostname, throw a runtime error.

For example, the SRV `mongodb+srv://mongo.local` resolving to `mongo.local` should prompt an error since the returned
address is identical to the original hostname.

## Test Setup

The tests in the `replica-set` directory MUST be executed against a three-node replica set on localhost ports 27017,
Expand Down