steid

@jamesgill /

Rust8.0 KBCode·Blame·History·Raw
1//! Which addresses and names Steid is willing to connect to on a visitor's say-so.
2//!
3//! Two functions, each used by both features that dial out:
4//! [`ImportSource`](super::import) and [`WebhookUrl`](super::webhook). They arrived on
5//! separate branches a day apart and each carried its own copy of [`is_public`], which
6//! had already drifted — import knew about `0.0.0.0/8` and the benchmarking range,
7//! webhooks knew about IETF protocol assignments and the reserved top of IPv4 space.
8//! Neither was wrong; each was missing what the other had, and a *second* copy of a
9//! security boundary is the shape of the bug rather than the bug itself.
10//!
11//! [`names_local_network`] is the same lesson learned a second time: webhooks refused
12//! `.local` and `.internal` while imports refused only `localhost`, until launch 2.18
13//! moved the webhook's lists here for both.
14//!
15//! The url-shape rules stay where they were. `WebhookUrl` refuses the packed spellings
16//! of an address (`127.1`, `2130706433`) by refusing a last label that begins with a
17//! digit, and `ImportSource` refuses anything but `https://`; both are rules about text,
18//! not about addresses, and only one of the two types wants each of them.
19
20use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
21
22/// Whether an address is one Steid will connect to.
23///
24/// The allowlist is "the public internet", expressed as a denylist of everything that is
25/// not, because the ranges that are *not* routable are the finite, enumerable half.
26/// Anything unrecognised is therefore allowed — which is the right way round only
27/// because the reserved ranges below are fixed by IANA and do not grow quietly.
28///
29/// Named as a positive so that a caller reads `if !is_public(address) { refuse }`: the
30/// negation is at the call site, where the refusal is.
31pub fn is_public(address: IpAddr) -> bool {
32 match address {
33 // An IPv4 address wearing an IPv6 costume reaches the same host as the v4 one,
34 // so it has to answer the same way: `::ffff:127.0.0.1` is loopback however it is
35 // spelled.
36 IpAddr::V6(address) => match address.to_ipv4_mapped() {
37 Some(mapped) => is_public_v4(mapped),
38 None => is_public_v6(address),
39 },
40 IpAddr::V4(address) => is_public_v4(address),
41 }
42}
43
44/// Host names that name this machine by convention rather than by address.
45const LOCAL_NAMES: [&str; 3] = ["localhost", "localhost.localdomain", "ip6-localhost"];
46
47/// Suffixes that resolve inside the local network by convention: mDNS, and the
48/// spellings container and cloud platforms use for their own services.
49const LOCAL_SUFFIXES: [&str; 4] = [".localhost", ".local", ".internal", ".localdomain"];
50
51/// Whether a host name names this machine or its own network by convention.
52///
53/// A rule about *spelling*, made before anything is resolved, so it can be refused at
54/// the moment a person can be told why. It is not the defence on its own: a public name
55/// can resolve to a private address, which only the adapter that dials can see. What
56/// this adds is the names that never reach ordinary DNS at all — an mDNS `.local` is
57/// answered by whichever machine on the LAN claims it, so there is no record to check
58/// that would mean anything.
59///
60/// Case-insensitive, and a trailing dot is ignored: `LOCALHOST.` is the fully qualified
61/// spelling of the same name, and a resolver treats it as one.
62pub fn names_local_network(host: &str) -> bool {
63 let lowered = host.trim_end_matches('.').to_ascii_lowercase();
64
65 LOCAL_NAMES.contains(&lowered.as_str())
66 || LOCAL_SUFFIXES
67 .iter()
68 .any(|suffix| lowered.ends_with(suffix))
69}
70
71fn is_public_v4(address: Ipv4Addr) -> bool {
72 let [a, b, ..] = address.octets();
73
74 !(address.is_unspecified()
75 || address.is_loopback()
76 || address.is_private()
77 || address.is_link_local()
78 || address.is_multicast()
79 || address.is_broadcast()
80 || address.is_documentation()
81 // 0.0.0.0/8 — "this network". Reaches the local host on some stacks.
82 || a == 0
83 // 100.64.0.0/10, carrier-grade NAT: a provider's internal space, not the
84 // internet, and what container runtimes and Tailscale hand out. `Ipv4Addr` has
85 // no predicate for it.
86 || (a == 100 && (64..128).contains(&b))
87 // 192.0.0.0/24, IETF protocol assignments.
88 || address.octets()[..3] == [192, 0, 0]
89 // 198.18.0.0/15, benchmarking. Routed to lab equipment where it is used at all.
90 || (a == 198 && (b == 18 || b == 19))
91 // 240.0.0.0/4, reserved — and 255.255.255.255, which `is_broadcast` has already
92 // caught.
93 || a >= 240)
94}
95
96fn is_public_v6(address: Ipv6Addr) -> bool {
97 let first = address.segments()[0];
98
99 !(address.is_unspecified()
100 || address.is_loopback()
101 || address.is_multicast()
102 // fc00::/7, unique local — IPv6's answer to RFC 1918.
103 || (first & 0xfe00) == 0xfc00
104 // fe80::/10, link-local.
105 || (first & 0xffc0) == 0xfe80)
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 fn v4(value: &str) -> IpAddr {
113 IpAddr::V4(value.parse::<Ipv4Addr>().expect("a v4 address"))
114 }
115
116 fn v6(value: &str) -> IpAddr {
117 IpAddr::V6(value.parse::<Ipv6Addr>().expect("a v6 address"))
118 }
119
120 /// The server-side request forgery boundary, address by address. Each of these, if
121 /// allowed through, is a connection Steid makes from inside its own network because
122 /// somebody typed a URL into a form.
123 #[test]
124 fn nothing_on_this_machine_or_its_own_network_is_public() {
125 for address in [
126 "0.0.0.0",
127 "0.1.2.3",
128 "127.0.0.1",
129 "127.1.2.3",
130 "10.0.0.5",
131 "172.16.4.1",
132 "192.168.1.1",
133 // The AWS/GCP metadata endpoint, the canonical SSRF target.
134 "169.254.169.254",
135 "100.64.0.1",
136 "100.127.255.255",
137 "192.0.0.1",
138 "198.18.0.1",
139 "198.19.255.255",
140 "192.0.2.1",
141 "224.0.0.1",
142 "240.0.0.1",
143 "255.255.255.255",
144 ] {
145 assert!(
146 !is_public(v4(address)),
147 "{address} is not on the public internet"
148 );
149 }
150
151 for address in ["::", "::1", "fd00::1", "fe80::1", "ff02::1"] {
152 assert!(
153 !is_public(v6(address)),
154 "{address} is not on the public internet"
155 );
156 }
157 }
158
159 #[test]
160 fn names_that_mean_this_machine_or_its_network_are_local() {
161 for host in [
162 "localhost",
163 "LOCALHOST",
164 "localhost.",
165 "localhost.localdomain",
166 "ip6-localhost",
167 "api.localhost",
168 "steid.local",
169 "Steid.Local.",
170 "db.internal",
171 "metadata.google.internal",
172 "box.localdomain",
173 ] {
174 assert!(names_local_network(host), "{host} names the local network");
175 }
176 }
177
178 #[test]
179 fn ordinary_names_are_not_local() {
180 for host in [
181 "example.com",
182 "github.com",
183 // A suffix match, not a substring one: these merely contain the words.
184 "local.example.com",
185 "internal.example.com",
186 "notlocal",
187 "mylocalhost.com",
188 ] {
189 assert!(!names_local_network(host), "{host} is an ordinary name");
190 }
191 }
192
193 #[test]
194 fn an_ipv4_address_in_an_ipv6_costume_answers_the_same_way() {
195 assert!(!is_public(v6("::ffff:127.0.0.1")));
196 assert!(!is_public(v6("::ffff:10.0.0.1")));
197 assert!(is_public(v6("::ffff:1.1.1.1")));
198 }
199
200 #[test]
201 fn ordinary_public_addresses_are_public() {
202 for address in ["1.1.1.1", "8.8.8.8", "140.82.121.4", "100.63.255.255"] {
203 assert!(is_public(v4(address)), "{address} is on the internet");
204 }
205
206 for address in ["2606:4700::1111", "2001:4860:4860::8888"] {
207 assert!(is_public(v6(address)), "{address} is on the internet");
208 }
209 }
210}