Commit 969f373
Closes the common case where a public-looking hostname's authoritative
DNS points at a private address — e.g. `metadata.example.com` →
`169.254.169.254`. `_check_safe_host` is purely string-level and would
let this pass; the new `_dns_validate_host` resolves the hostname once
via `socket.getaddrinfo` (in an executor) and gates every returned
address through the same IP-block check.
Plumbed in front of every outbound HTTP request in adagents discovery:
- `_fetch_adagents_url` (publisher hop, authoritative_location hop,
conditional-refresh hop)
- `_fetch_ads_txt_managerdomains` (MANAGERDOMAIN fallback fetch — fails
closed to "no MANAGERDOMAIN" rather than surfacing a validation error,
matching the existing best-effort semantics of that path)
IP literals short-circuit (already gated by `_check_safe_host`). RFC
2606 / 6761 reserved domains (`.example`, `.test`, `.invalid`,
`.localhost`, `example.com`/`.net`/`.org`) also short-circuit so tests
using these names don't need DNS mocks.
Test infrastructure adds an autouse `_stub_getaddrinfo` fixture in
test_adagents.py that returns a benign public IP (`8.8.8.8`) for every
host by default. Tests that want to exercise the DNS gate
(`test_resolved_private_ip_rejected_before_connect`,
`test_resolved_dns_failure_surfaces_as_validation_error`,
`test_resolved_mixed_public_and_private_is_rejected`) override via
`monkeypatch.setattr(socket, "getaddrinfo", ...)`.
Residual rebinding window (still open after this PR): a determined
attacker controlling the authoritative DNS can return a public IP on
this lookup and a private IP on httpx's connect lookup milliseconds
later. Closing that window requires intercepting httpx's network
backend to pin the connection IP — left tracked on #757 as the
remaining work after this resolve-and-validate gate ships.
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 01c6491 commit 969f373
2 files changed
Lines changed: 139 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
199 | 201 | | |
200 | 202 | | |
201 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
202 | 259 | | |
203 | 260 | | |
204 | 261 | | |
| |||
636 | 693 | | |
637 | 694 | | |
638 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
639 | 700 | | |
640 | 701 | | |
641 | 702 | | |
| |||
970 | 1031 | | |
971 | 1032 | | |
972 | 1033 | | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
973 | 1039 | | |
974 | 1040 | | |
975 | 1041 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
29 | 51 | | |
30 | 52 | | |
31 | 53 | | |
| |||
756 | 778 | | |
757 | 779 | | |
758 | 780 | | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
759 | 832 | | |
760 | 833 | | |
761 | 834 | | |
| |||
0 commit comments