1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! DNS resolver wrapper that refuses blocked resolved addresses before a connection is made.
use AddrGuard;
use crate;
use Arc;
/// Error returned by [`BlockedAddrResolver`] when a DNS resolution cannot be trusted for
/// connection use — either it produced no address, or at least one resolved address falls into
/// a blocked [`crate::net_policy::HostClass`].
///
/// Kept distinct from [`DepsError`](crate::error::DepsError) since this crosses into `reqwest::dns::Resolve`'s own
/// `BoxError` (`Box<dyn std::error::Error + Send + Sync>`) return type, not this crate's own
/// error type.
pub
/// Fails closed when a resolution produced nothing, rather than treating "nothing resolved" as
/// "nothing to block".
/// Validates every address `tokio::net::lookup_host` returned for `host`, rejecting the whole
/// resolution if any is blocked — an attacker's public A record alongside a blocked one must not
/// keep the probe alive (FR-003). `guard`'s tier additionally rejects a resolved address whose
/// class the *tier* does not allow (issue #455: an RFC1918/CGNAT-range name rebound at
/// connect time), on top of the policy-independent [`HostClass::never_a_registry`](crate::net_policy::HostClass::never_a_registry)
/// check every tier enforces.
pub
/// The synthetic-lookup function signature [`TestLookup`] wraps.
type SyntheticLookupFn = dyn Fn + Send + Sync;
/// Test-only override for [`BlockedAddrResolver::resolve`], replacing `tokio::net::lookup_host`
/// with a synthetic lookup — lets a test exercise the resolver-guard wiring against an address
/// chosen by the test (e.g. an RFC1918 literal) without depending on real DNS. A newtype rather
/// than a hand-written `Debug` directly on [`BlockedAddrResolver`], so that struct's own
/// `#[derive(Debug)]` stays valid under both `cfg(test)` and not.
pub );
/// Connect-time DNS resolver that closes the rebinding TOCTOU gap left by [`ensure_https`](super::guard::ensure_https)/
/// [`hop_targets_blocked_host`](super::guard::hop_targets_blocked_host)'s URL-string-only classification (issue #449): those check the
/// declared hostname, but `reqwest`'s connector resolves DNS independently, later, and an
/// attacker who controls the hostname's DNS can rebind it to a blocked address in between.
///
/// Wired into every client `build_client_inner` builds, so all 11 ecosystem crates sharing
/// the baseline client pool inherit it with zero per-crate plumbing (FR-006/NFR-002).
///
/// # Scope
///
/// This resolver's guard tier decides how much a resolved address is scrutinized: under
/// [`AddrGuard::Baseline`] this enforces only the policy-independent
/// [`crate::net_policy::HostClass::never_a_registry`] tier (loopback, link-local,
/// cloud-metadata, unspecified, reserved), the same tier [`hop_targets_blocked_host`](super::guard::hop_targets_blocked_host) already
/// applies — closing issue #449's filed exploit (cloud-metadata rebinding) but not full `PublicOnly`
/// semantics. Under [`AddrGuard::WorkspaceDeclared`], [`validate_resolved_addrs`] additionally
/// rejects any resolved address outside the snapshotted [`crate::net_policy::WorkspaceRegistryAccess`]
/// policy's allowed classes — closing issue #455 (a workspace-declared name that legitimately
/// resolves to `HostClass::Global` at parse time, then rebinds to an RFC1918/CGNAT address at
/// connect time).
///
/// # Fail-closed (NFR-004)
///
/// Returns `Err` — never `Ok`, never a fallback resolver — on a `lookup_host` error, zero
/// addresses, or any resolved address [`validate_resolved_addrs`] rejects for `self.guard`'s
/// tier.
///
/// # Known limitations
///
/// - [`ClientBuilder::resolve`](reqwest::ClientBuilder::resolve)/
/// [`resolve_to_addrs`](reqwest::ClientBuilder::resolve_to_addrs) overrides wrap *outside* the
/// configured resolver (`reqwest`'s `DnsResolverWithOverrides`) and would bypass this guard
/// entirely if ever called — this workspace does not call them today.
/// - A configured system proxy (`HTTPS_PROXY`) resolves the target hostname itself; this
/// resolver then only ever sees the proxy's own address. Operator configuration, not
/// attacker-controlled, so NOT claimed as a defended case.
/// - Never applies to an IP-literal host (`https://169.254.169.254/`): `hyper-util`'s connector
/// parses those directly and never calls the configured resolver, so
/// [`classify_host`](crate::net_policy::classify_host) (via [`ensure_https`](super::guard::ensure_https)/
/// [`hop_targets_blocked_host`](super::guard::hop_targets_blocked_host)/[`redirect_policy`](super::redirect::redirect_policy)'s tier term) remains the sole guard for
/// literals — a disjoint domain from this resolver's name-based one, not a gap. This is also
/// why the cache layer gives [`HttpCache::get_cached_workspace`](super::HttpCache::get_cached_workspace) zero protection against an
/// *initial* request URL that is itself an IP literal — see that method's docs.
/// - Unlike [`ensure_https`](super::guard::ensure_https)/[`hop_targets_blocked_host`](super::guard::hop_targets_blocked_host), this resolver has **no** `test-util`
/// carve-out for `Loopback`: it blocks a `localhost`/`127.0.0.1` *name* unconditionally, in
/// every build. A downstream `test-util` consumer that mocks by binding an IP literal (as
/// this workspace's own `mockito` usage does) is unaffected — literals never reach this
/// resolver at all — but one that mocks via a `localhost` *name* would be newly blocked.
pub