-
Notifications
You must be signed in to change notification settings - Fork 1.2k
PYTHON-5814 Configurable DNS domain validation for SRV records #2868
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
base: main
Are you sure you want to change the base?
Changes from all commits
2cd02da
499a3ec
3423d39
3e30434
1ac4967
a8a3b4e
9663485
a71dd7a
4a6ba01
466a47e
7902127
c8f8c9f
2c8ad29
14ae604
1fc7fe2
fa34923
aaad72f
a2c5cbd
071a285
63d57ca
7c4e57a
ae72df1
c950d35
8f58480
820c648
d99b4e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Copyright 2024-present MongoDB, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you | ||
| # may not use this file except in compliance with the License. You | ||
| # may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| # implied. See the License for the specific language governing | ||
| # permissions and limitations under the License. | ||
|
|
||
| """Public Suffix List lookup for srvAllowedHostsSuffix validation.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| _PUBLIC_SUFFIXES: Optional[tuple[set[str], set[str], set[str]]] = None | ||
|
|
||
|
|
||
| def _load_public_suffixes() -> tuple[set[str], set[str], set[str]]: | ||
| path = Path(__file__).parent / "public_suffix_list.dat" | ||
| suffixes: set[str] = set() | ||
| wildcards: set[str] = set() | ||
| exceptions: set[str] = set() | ||
| with open(path, encoding="utf-8") as f: | ||
| for line in f: | ||
| line = line.strip() # noqa: PLW2901 | ||
| if not line or line.startswith("//"): | ||
| continue | ||
| if line.startswith("!"): | ||
| exceptions.add(line[1:].lower()) | ||
| elif line.startswith("*."): | ||
| wildcards.add(line[2:].lower()) | ||
| else: | ||
| suffixes.add(line.lower()) | ||
| return suffixes, wildcards, exceptions | ||
|
|
||
|
|
||
| def is_public_suffix(domain: str) -> bool: | ||
| """Return True if domain is a public suffix per the bundled Public Suffix List.""" | ||
| global _PUBLIC_SUFFIXES # noqa: PLW0603 | ||
| if _PUBLIC_SUFFIXES is None: | ||
| _PUBLIC_SUFFIXES = _load_public_suffixes() | ||
| suffixes, wildcards, exceptions = _PUBLIC_SUFFIXES | ||
|
|
||
| domain = domain.lower().strip(".") | ||
| if domain in exceptions: | ||
| return False | ||
| if domain in suffixes: | ||
| return True | ||
| parts = domain.split(".") | ||
| # this logic is to handle the wildcard rule, the domain could still be a public suffix if: | ||
| # - either `parts` is a single label, and thus it is a public suffix list (per the `*`) rule | ||
| # - or another wildcard rule such as *.xyz exists (stored as just xyz in `wildcards`), thus we check | ||
| # if `parts[1:]` is in the list of wildcard rules. | ||
| return len(parts) == 1 or (len(parts) > 1 and ".".join(parts[1:]) in wildcards) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a short comment explaining this line? I'm not following how this results in a suffix being public or not.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i assume "this line" is the return line, and if so, done! lmk if the comment isn't clear tho |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this will resync every time there is a change to the Public Suffix List?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it syncs from the spec's copy of the PSL -- which is automatically updated once a month but could be manually triggered (in the specs repo)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(the actual PSL has like an avg of 3 changes per week which is far too much noise to always keep up to date)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And our automated weekly sync will pick up that monthly change in the spec repo?