Repository navigation
fix(services): fetch linked documents and listed layers within the reach of the document that linked them #2744
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
Changes from all commits
4484ca7
3442a95
936f4ce
37b5628
0b22bb7
f165bb5
c702bbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,8 +4,8 @@ | |||||
| //! point at this computer or the local network: a self-hosted tile server. Addresses that come | ||||||
| //! from somewhere else (inside a style or TileJSON document, or a redirect) may not reach into | ||||||
| //! the local network unless the document itself came from there, so a style found on the web | ||||||
| //! can't make the app probe local services. Link-local addresses, where cloud metadata services | ||||||
| //! answer, are never fetched. | ||||||
| //! can't make the app probe local services. Link-local addresses and the other addresses cloud | ||||||
| //! metadata services answer at are never fetched. | ||||||
| //! | ||||||
| //! These spelling checks also run after template expansion. Native fetchers additionally | ||||||
| //! filter the resolved IPs before connecting: a public document cannot select a local | ||||||
|
|
@@ -119,8 +119,19 @@ fn host(url: &str) -> Option<String> { | |||||
| Some(host.trim_end_matches('.').to_ascii_lowercase()) | ||||||
| } | ||||||
|
|
||||||
| /// Cloud instance metadata services at addresses outside the link-local ranges (every link-local | ||||||
| /// address is forbidden on its own): one unique-local IPv6 and one carrier-grade NAT address. Only | ||||||
| /// these two: the ranges around them stay local, for self-hosted servers there. | ||||||
| const METADATA: [IpAddr; 2] = | ||||||
| [IpAddr::V6(Ipv6Addr::new(0xfd00, 0x0ec2, 0, 0, 0, 0, 0, 0x0254)), IpAddr::V4(Ipv4Addr::new(100, 100, 100, 200))]; | ||||||
|
|
||||||
| /// How far an IP address reaches. | ||||||
| pub fn ip_reach(ip: IpAddr) -> Reach { | ||||||
| // For the IPv6 metadata address. The IPv4 one is also checked in `v4_reach`, which every | ||||||
| // spelling of it reaches (mapped, NAT64, IPv4-compatible, 6to4, the `inet_aton` forms in `reach`). | ||||||
| if METADATA.contains(&ip) { | ||||||
|
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. The PR says the two metadata addresses are never fetched "in any spelling", but the IPv4-compatible form Practical impact is low, because the OS does not route IPv4-compatible IPv6 destinations to the IPv4 metadata endpoint. The gap still contradicts the stated guarantee. Suggested fix, not compiled: in the Confidence: high on the behaviour (read from the code), moderate on the exact fix. Category: security
Member
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. Fixed in c702bbf. |
||||||
| return Reach::Forbidden; | ||||||
| } | ||||||
| match ip { | ||||||
| IpAddr::V4(v4) => v4_reach(v4), | ||||||
| IpAddr::V6(v6) => { | ||||||
|
|
@@ -132,6 +143,15 @@ pub fn ip_reach(ip: IpAddr) -> Reach { | |||||
| if let [0x64, 0xff9b, 0, 0, 0, 0, hi, lo] = v6.segments() { | ||||||
| return v4_reach(Ipv4Addr::from((u32::from(hi) << 16) | u32::from(lo))); | ||||||
| } | ||||||
| // IPv4-compatible (`::a.b.c.d`, RFC 4291 §2.5.5.1) and 6to4 (`2002:a.b.c.d::/48`, | ||||||
| // RFC 3056) addresses embed one too. `::` and `::1` keep their own arms below. | ||||||
| match v6.segments() { | ||||||
| [0, 0, 0, 0, 0, 0, hi, lo] if hi != 0 || lo > 1 => { | ||||||
| return v4_reach(Ipv4Addr::from((u32::from(hi) << 16) | u32::from(lo))); | ||||||
| } | ||||||
| [0x2002, hi, lo, ..] => return v4_reach(Ipv4Addr::from((u32::from(hi) << 16) | u32::from(lo))), | ||||||
|
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. This 6to4 arm is a behaviour change the PR description does not mention. Before this PR, Category: quality
Member
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. The description already says it: the third bullet of the Summary ends "IPv4-compatible ( |
||||||
| _ => {} | ||||||
| } | ||||||
| let seg0 = v6.segments().first().copied().unwrap_or(0); | ||||||
| if v6.is_unspecified() || (seg0 & 0xffc0) == 0xfe80 { | ||||||
| Reach::Forbidden | ||||||
|
|
@@ -145,7 +165,7 @@ pub fn ip_reach(ip: IpAddr) -> Reach { | |||||
| } | ||||||
|
|
||||||
| fn v4_reach(v4: Ipv4Addr) -> Reach { | ||||||
| if v4.is_unspecified() || v4.is_link_local() || v4.is_broadcast() { | ||||||
| if v4.is_unspecified() || v4.is_link_local() || v4.is_broadcast() || METADATA.contains(&IpAddr::V4(v4)) { | ||||||
| Reach::Forbidden | ||||||
| } else if v4.is_loopback() || v4.is_private() || (v4.octets()[0] == 100 && (v4.octets()[1] & 0xc0) == 64) { | ||||||
| // 100.64.0.0/10 (carrier-grade NAT) counts as local too. | ||||||
|
|
@@ -313,6 +333,40 @@ mod tests { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// IPv4-compatible (`::a.b.c.d`) and 6to4 (`2002:a.b.c.d::`) addresses keep the reach of the | ||||||
| /// IPv4 address they embed; `::1` stays loopback and `::` unspecified. | ||||||
| #[test] | ||||||
| fn embedded_ipv4_destinations_keep_their_reach() { | ||||||
| for (v4, expected) in [ | ||||||
| ("100.100.100.200", Reach::Forbidden), | ||||||
| ("169.254.169.254", Reach::Forbidden), | ||||||
| ("127.0.0.1", Reach::Local), | ||||||
| ("10.0.0.1", Reach::Local), | ||||||
| ("203.0.113.1", Reach::Public), | ||||||
| ] { | ||||||
| let ip: Ipv4Addr = v4.parse().unwrap(); | ||||||
| let [a, b, c, d] = ip.octets(); | ||||||
| let six_to_four = Ipv6Addr::new(0x2002, u16::from_be_bytes([a, b]), u16::from_be_bytes([c, d]), 0, 0, 0, 0, 1); | ||||||
| for peer in [format!("::{v4}").parse::<IpAddr>().unwrap(), IpAddr::V6(six_to_four)] { | ||||||
| let url = format!("http://[{peer}]/tile"); | ||||||
| assert_eq!(ip_reach(peer), expected, "{peer}"); | ||||||
| assert_eq!(reach(&url), expected, "{url}"); | ||||||
| assert_eq!(check(&url, None).is_ok(), expected != Reach::Forbidden, "{url}"); | ||||||
| assert_eq!(check(&url, Some("https://public.example/style.json")).is_ok(), expected == Reach::Public, "{url}"); | ||||||
| let observed = Origin::connected("https://dns.example/tile", peer); | ||||||
| if expected == Reach::Forbidden { | ||||||
| assert!(observed.is_err(), "DNS cannot hide a forbidden embedded peer {peer}"); | ||||||
| } else { | ||||||
| assert_eq!(observed.unwrap().reach, expected, "{peer}"); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| assert_eq!(ip_reach(Ipv6Addr::LOCALHOST.into()), Reach::Local); | ||||||
| assert_eq!(reach("http://[::1]/"), Reach::Local); | ||||||
| assert_eq!(ip_reach(Ipv6Addr::UNSPECIFIED.into()), Reach::Forbidden); | ||||||
| assert_eq!(reach("http://[::]/"), Reach::Forbidden); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn judges_addresses() { | ||||||
| assert_eq!(reach("https://tile.openstreetmap.org/1/0/0.png"), Reach::Public); | ||||||
|
|
@@ -339,6 +393,31 @@ mod tests { | |||||
| assert_eq!(reach("http://tiles.example.com/"), Reach::Public); | ||||||
| } | ||||||
|
|
||||||
| /// The two metadata addresses outside the link-local ranges are never fetched, however they | ||||||
| /// are spelled; their neighbours stay local (self-hosted servers on those ranges keep working). | ||||||
| #[test] | ||||||
| fn cloud_metadata_addresses_are_forbidden_and_their_ranges_stay_local() { | ||||||
| for url in [ | ||||||
| "http://[fd00:ec2::254]/latest/meta-data/", | ||||||
| "http://[::ffff:100.100.100.200]/", | ||||||
| "http://100.100.100.200/latest/meta-data/", | ||||||
| "http://[64:ff9b::6464:64c8]/", | ||||||
| "http://[::100.100.100.200]/", | ||||||
| "http://[2002:6464:64c8::1]/", | ||||||
| "http://1684301000/", | ||||||
| ] { | ||||||
| assert_eq!(reach(url), Reach::Forbidden, "{url}"); | ||||||
| assert!(check(url, None).is_err(), "{url}"); | ||||||
| } | ||||||
| for peer in ["fd00:ec2::254", "100.100.100.200", "::100.100.100.200", "2002:6464:64c8::1"] { | ||||||
| assert!(Origin::connected("https://selected.example/", peer.parse().unwrap()).is_err(), "{peer}"); | ||||||
| } | ||||||
| for url in ["http://[fd00:ec2::253]/", "http://[fd12:3456::1]/", "http://100.100.100.201/", "http://100.64.0.1/"] { | ||||||
|
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. The NAT64 branch in
Suggested change
Category: quality
Member
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. The NAT64 branch isn't new in this PR. It came with #1560, and |
||||||
| assert_eq!(reach(url), Reach::Local, "{url}"); | ||||||
| assert!(check(url, None).is_ok(), "{url}"); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn checks_where_addresses_come_from() { | ||||||
| // The user's own choice may be local; a web document's may not. | ||||||
|
|
||||||
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.
zipsilently truncates ifpublicandlayersever differ in length, and the collections past the shorter one are then dropped without a notice. The comment above says the lengths can't differ, which holds today, but a future filter inservice_jsonwould turn that into a silent loss. The "Never silent" rule argues for an explicit check here: return an error whenpublic.len() != layers.len(), which costs one line and fails loudly instead of quietly.Category: quality
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 two lists are built one entry per layer by construction (the code comment says so), so
zipcannot drop an entry. The explicit length check is left for the release quality pass.