DRIVERS-3329: Configurable DNS domain validation for SRV records - #1950
DRIVERS-3329: Configurable DNS domain validation for SRV records#1950sleepyStick wants to merge 21 commits into
Conversation
rozza
left a comment
There was a problem hiding this comment.
It looks good, I have one comment about if an invalid srvAllowedHostsSuffix should throw an error explicitly? (if so should we update the yml?)
Also the yml and json has diverged for srvAllowedHostsSuffix-without_dot_pass - which is probably the lint error.
|
|
||
| #### srvAllowedHostsSuffix | ||
|
|
||
| This option is used to validate hosts. If present, its value MUST be treated as the domain for DNS validation. For |
There was a problem hiding this comment.
Optional: Consider linking to the Querying DNS section, which is where the DNS validation is described.
|
Assigned |
…covery.md Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com>
…covery.md Co-authored-by: Matt Dale <9760375+matthewdale@users.noreply.github.com>
| @@ -0,0 +1,6 @@ | |||
| { | |||
| "uri": "mongodb+srv://test12.test.build.10gen.cc/?srvAllowedHostsSuffix=uild.10gen.cc", | |||
There was a problem hiding this comment.
Looks like the convention in yml is no quotes around uri
| This option is used to validate hosts. If present, its value MUST be treated as the `{domainname}` for | ||
| [DNS validation](#querying-dns). For example, `srvAllowedHostsSuffix=.mydomain.net`. If the value does not begin with a | ||
| `.`, for example, `srvAllowedHostsSuffix=mydomain.net`, the `.` MUST be automatically prepended prior to validation. If | ||
| this option is not present, the`{domainname}` MUST be inferred from the `{hostname}` (as described in |
| uri: "mongodb+srv://test12.test.build.10gen.cc/?srvAllowedHostsSuffix=test.build.10gen.cc" | ||
| seeds: [] | ||
| hosts: [] | ||
| error: true No newline at end of file |
There was a problem hiding this comment.
Also a convention apparently is new lines at the end of yaml files.
|
Setting srvAllowedHostsSuffix to a public suffix neuters SRV anti-spoofing, turning mongodb+srv:// into an unbounded DNS redirect. A merely broad private domain doesn't make it unbounded, but widens the redirect to anywhere under that domain. There is no PSL/minimum-label guard to prevent either. Example: Preconditions:
Step-by-step:
|
|
I think the parsing needs to be much better specified and tested. It's important to understand what DNS entries allow and how to safely handle these things. These should be specified at the spec level, so all drivers will get the parsing correct. For example, most of these are not specified or tested:
There needs to be much better test coverage here. |
DNS does not support non-ascii chars e.g. |
|
tldr: i don't think the hook is a better idea, the current approach achieves the same thing. but for more details:
Agreed that the current one wouldn't help and that more is needed. I believe we're still contemplating the best way to "manage" a public suffix list across the drivers but the goal is to address this concern.
I messaged @damieng this morning after i read your reply (mainly because i didn't understand what the other approach was and was hoping for a clearer explanation) -- it appears that Damien didn't have the full context of the problem when he came up with this suggestion but I'll clarify on what this suggestion is (for anyone else reading) and then after share my thoughts on it for further discussion. def is_allow_host(...) -> bool:
# whatever the current validation isand then cusomers can do the following: @override
def is_allow_host(host: str, ...) -> bool:
return host.endswith("<their allow host suffix>"))notably this idea wouldn't involve adding def is_allow_host_hook(...) -> bool:
# this is where we'd place "optional" / "recommended" validation that the customer can then override if they decide they know better
def is_allow_host(...) -> bool:
# whatever validation we deem to be an absolute minimum safeguard that all allowed hosts should satisfy
# AND if those pass then
return is_allow_host_hook(...)and then customers can do the following: @override
def is_allow_host_hook(host: str, ...) -> bool:
return host.endswith("<their allow host suffix>")) # or whatever they wantBut at this point, all that absolute minimum validation is the same as the validation that we need to figure out for |
Nothing, but they would have explicitly chosen to have relaxed that security. It gives them the ultimate flexibility and power but only if they clearly opt into it. The problem with doing things like srvAllowedHostsSuffix is it's very limited. The moment a customer wants to say - use two hosts under different suffixes perhaps on different cloud providers they're out of luck again. With a hook they can either hard-code it, read a comma-separated list in from their own configuration etc. If the user wants to also perform our existing validation that's easy enough to do by them calling down to the base method/hook either before adding their own validation or after having allowed through their exception. |
8122297
adelinowona
left a comment
There was a problem hiding this comment.
Reviewing as the secondary implementer (C# driver). I've read this PR alongside #1972 and also looked at the Pymongo implementation. Most of what's here looks good but I have just a few comments.
Raising this here:
We could use an uppercase public suffix test. Both PSL tests are lowercase (cc, 10gen.cc), so nothing covers srvAllowedHostsSuffix=COM. See my inline comment on the normalization bullets for why that's the one ordering bug the current suite can't catch. Could live here or with the srvAllowedHostsSuffix-psl-* tests in #1972 — your call which.
|
|
||
| This option is used to validate hosts. If present, its value MUST be treated as the `{domainname}` for | ||
| [DNS validation](#querying-dns) and | ||
| [SRV polling](../polling-srv-records-for-mongos-discovery/polling-srv-records-for-mongos-discovery.md). For example, |
There was a problem hiding this comment.
The option description now says it applies to SRV polling but that spec document isn't touched by this PR and still carries its own normative sentence: "A driver MUST verify that the host names returned through SRV records have the same parent {domainname}."
More subtly, the two documents now use {domainname} to mean different things — in the polling spec it's the {hostname}.{domainname} split; here it's the entire configured suffix when the option is set. WE should update SRV polling spec accordingly as well.
There was a problem hiding this comment.
oo thanks for pointing that out! fixed in d2731ce
There was a problem hiding this comment.
There's no security-considerations text for this new option. The Design Rationale section is a single pre-existing sentence and doesn't mention srvAllowedHostsSuffix. I think we could benefit from a subsection covering: why the option exists; that the PSL bounds the blast radius but doesn't make a configuration safe; guidance to use the narrowest suffix that work. PyMongo already has good material for most of this in its MongoClient docstring warning. Also helps future spec readers.
There was a problem hiding this comment.
added in d2731ce
honestly i feel a bit off about my wording though? so lmk if you have any suggestions on the wording here!
There was a problem hiding this comment.
I think the section reads well! I wouldn't change anything. One thing I would suggest adding though is info around the alternative @damieng raised - the driver provided hook. Something like
Why not a validation callback?
An alternative considered was a driver-provided hook....bla.....bla.... bla
| # srvAllowedHostsSuffix must contain at least two labels. A value of .cc (TLD only) | ||
| # has only one label after the leading dot and MUST raise an error. | ||
| # Note: .cc is used (rather than an unrelated TLD) because the DNS records return | ||
| # hosts ending in .cc, ensuring the test fails only due to TLD validation and not | ||
| # a host mismatch. |
There was a problem hiding this comment.
I believe this rationale is now stale — it describes the two-label minimum, which this PR replaced with the PSL rule. The test still passes, but for a different reason now (cc is an ordinary PSL rule).
@sleepyStick Looking at the most recent PyMongo implementation, it still enforces the deleted rule ("srvAllowedHostsSuffix must contain at least two labels"), and that check runs before is_public_suffix — so PyMongo passes this test without the PSL path ever executing.
There was a problem hiding this comment.
fixed the test description and thank you for calling out the pymongo change!! Past Iris thought keeping it would be fine because if it only had one label then it must be a public suffix (and this felt like a "shortcut" for the code to conclude that it was a public suffix, if that makes any sense) but I had forgotten that pymongo gives out a unique error message in that case. Removed it now, thanks for calling it out!
| A driver MUST verify that the host names returned through SRV records share the original SRV's `{domainname}`. In | ||
| addition, SRV records with fewer than three `.` separated parts, the returned hostname MUST have at least one more | ||
| domain level than the SRV record hostname. Drivers MUST raise an error and MUST NOT initiate a connection to any |
There was a problem hiding this comment.
I think this part "In addition, SRV records with fewer than three . separated parts…" doesn't apply when srvAllowedHostsSuffix is set. The clause needs {hostname} to have ≤2 labels, the suffix must have ≥2 labels (a 1-label suffix is already a public suffix under the * fallback), and the mandatory leading dot forces returned hosts to ≥3 labels. So the "at least one more domain level" property holds by construction. I suggest updating this part of the spec.
| For the purposes of this document, `{hostname}` will be divided using the following terminology. If | ||
| `srvAllowedHostsSuffix` has been configured, then that will act as the `{domainname}`. Otherwise, if an SRV `{hostname}` |
There was a problem hiding this comment.
Question for the rationale rather than a change request: nothing requires {hostname} itself to end with the suffix, so mongodb+srv://cluster.example.org/?srvAllowedHostsSuffix=bank.com is legal.
I initially thought that should be forbidden, but I don't think it should — it would break the vanity-CNAME shape, where the SRV name is a corporate alias and the hosts live in a provider domain, which seems like one of the cases this option might be used for. So I'd just like the rationale to say that decoupling is deliberate and why, since it's the point where the trust anchor stops being tied to the name the user typed.
There was a problem hiding this comment.
that is correct, I've added this to the rationale in d2731ce
…covery.md Co-authored-by: Adelin Owona <51498470+adelinowona@users.noreply.github.com>
I've added them in #1972! (and the failing lint check is because I'm linking to the currently non-existent public suffix spec (I assume this PR would be merged after #1972)) |
| Before validating returned hostnames, drivers MUST normalize them as follows: | ||
|
|
||
| - Trailing dots MUST be stripped (e.g. `host.example.com.` becomes `host.example.com`). | ||
| - Hostnames MUST be normalized to lowercase using ASCII case folding. |
There was a problem hiding this comment.
Because this addition sits in a PR titled after the new option, it could be read as a detail of that option — I think it earns its own entry in this document's ChangeLog so other drivers audit their comparison instead of skipping it:
- 2026-08-24: Specify that host names returned through SRV records are normalized -- trailing dot stripped, ASCII lowercase -- before validation.
Also, nothing in the test suite can catch a driver that skips the fold — every build.10gen.cc record returns lowercase targets, so it's unobservable, and srvAllowedHostsSuffix-case-insensitive covers the suffix side only. I think we need a fixture with a mixed-case SRV target, which is test-infra work but worth doing in this PR. Flagging it because a driver can be compliant on the seedlist path and not on the polling path.
There was a problem hiding this comment.
makes sense, added changelog suggestion
oh that's a good catch -- filed a devprod ticket (DEVPROD-42090) I hope that's the right thing to do? (and hopefully it doesn't take long for them to get back to us?)
…covery.md Co-authored-by: Adelin Owona <51498470+adelinowona@users.noreply.github.com>
There was a problem hiding this comment.
We should add a test similar to this one, but using only a period as an allowed hosts suffix. The test should expect an error.
For example:
uri: "mongodb+srv://test1.test.build.10gen.cc/?srvAllowedHostsSuffix=."
seeds: []
hosts: []
error: true
adelinowona
left a comment
There was a problem hiding this comment.
LGTM!
C# implementation here: mongodb/mongo-csharp-driver#2109
|
I'm seconding the hook idea suggested by @damieng instead of hardcoded list of suffixes. If customer is worried about the security - they will have a way to express any possible rules they want, and they will have to do that in code - which means no way attackers can inject anything into the connection string to make driver connect to a wrong host. If customer does not really care about security - yes, they can have And yes, I see this file as hardcoded list of magic string. Even though the current approach most likely will address the immediate needs there are number of problems which we will have to cover:
|
Please complete the following before merging:
Python PR: PYTHON-5814 Configurable DNS domain validation for SRV records mongo-python-driver#2868
clusters).