A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
use super::resolver::{MAX_PUBLIC_DNS_ADDRESSES, ResolveError, screen_resolved_addresses};
use super::*;
use fluxer_common::public_ip::is_public_ip;
use std::net::SocketAddr;
fn public(literal: &str) -> bool {
is_public_ip(literal.parse().expect("test address literal parses"))
}
fn socket_addresses(literal: &str, count: usize) -> Vec<SocketAddr> {
let address = literal.parse().expect("test address literal parses");
(0..count).map(|_| SocketAddr::new(address, 443)).collect()
}
#[test]
fn blocks_private_and_special_ip_literals() {
assert!(!public("127.0.0.1"));
assert!(!public("10.1.2.3"));
assert!(!public("::1"));
assert!(!public("::ffff:192.168.1.1"));
assert!(public("8.8.8.8"));
assert!(public("2606:4700:4700::1111"));
}
#[test]
fn blocks_every_documented_ipv4_ssrf_range() {
for ip in [
"0.0.0.0",
"10.0.0.1",
"100.64.0.1",
"127.0.0.1",
"169.254.169.254",
"172.16.5.5",
"192.0.0.1",
"192.0.2.5",
"192.88.99.5",
"192.168.1.1",
"198.18.0.1",
"198.51.100.5",
"203.0.113.5",
"224.0.0.1",
"240.0.0.1",
"255.255.255.255",
] {
assert!(!public(ip), "{ip}");
}
}
#[test]
fn blocks_every_documented_ipv6_ssrf_range() {
for ip in [
"::",
"::1",
"2001:db8::1",
"fc00::1",
"fd00::1",
"fe80::1",
"ff00::1",
"64:ff9b::a9fe:a9fe",
"::7f00:1",
"2002:a9fe:a9fe::",
] {
assert!(!public(ip), "{ip}");
}
}
#[test]
fn rejects_urls_with_userinfo() {
assert_eq!(
Err(Error::BlockedUrl),
validate_url("https://user:[email protected]/")
);
}
#[test]
fn rejects_non_http_s_schemes() {
assert_eq!(Err(Error::BlockedUrl), validate_url("file:///etc/passwd"));
assert_eq!(
Err(Error::BlockedUrl),
validate_url("gopher://example.com/")
);
}
#[test]
fn redirect_with_dot_dot_cannot_escape_host() {
let r = resolve_redirect("https://example.com/a", "../../../etc").unwrap();
assert!(r.starts_with("https://example.com/"));
}
#[test]
fn ipv6_outside_global_unicast_is_blocked_by_default() {
for literal in [
"100::1",
"1fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
"2001::1",
"2002::1",
"3fff::1",
"4000::",
"5f00::1",
"fec0::1",
"ff02::1",
] {
assert!(!public(literal), "{literal}");
}
for literal in [
"2000::",
"2001:200::1",
"2001:db7:ffff:ffff:ffff:ffff:ffff:ffff",
"2001:db9::1",
"2003::1",
"2606:4700:4700::1111",
"2a00:1450:4001:800::200e",
"3fff:1000::1",
] {
assert!(public(literal), "{literal}");
}
}
#[test]
fn ipv4_written_in_ipv6_notation_is_revalidated_against_the_v4_table() {
for literal in [
"::ffff:127.0.0.1",
"::ffff:10.0.0.1",
"::ffff:169.254.169.254",
"::93.184.216.34",
"64:ff9b::10.0.0.1",
"64:ff9b::169.254.169.254",
"2002:5db8:d822::",
] {
assert!(!public(literal), "{literal}");
}
for literal in ["::ffff:93.184.216.34", "64:ff9b::93.184.216.34"] {
assert!(public(literal), "{literal}");
}
}
#[test]
fn public_urls_are_restricted_to_the_standard_web_ports() {
assert_eq!(Ok(()), validate_url("https://example.com/a"));
assert_eq!(Ok(()), validate_url("https://example.com:443/a"));
assert_eq!(Ok(()), validate_url("http://example.com:80/a"));
for url in [
"https://example.com:8080/a",
"http://example.com:8080/a",
"https://example.com:22/a",
"https://[2606:4700:4700::1111]:8443/a",
] {
assert_eq!(Err(Error::BlockedUrl), validate_url(url), "{url}");
}
for url in ["https://example.com:0/a", "https://example.com:99999/a"] {
assert_eq!(Err(Error::InvalidUrl), validate_url(url), "{url}");
}
}
#[test]
fn resolved_address_sets_are_bounded_and_fail_closed() {
assert_eq!(
Err(ResolveError::HostResolvedToNoAddress),
screen_resolved_addresses(std::iter::empty())
);
let at_limit = socket_addresses("93.184.216.34", MAX_PUBLIC_DNS_ADDRESSES);
assert_eq!(
Ok(at_limit.clone()),
screen_resolved_addresses(at_limit.into_iter())
);
let over_limit = socket_addresses("93.184.216.34", MAX_PUBLIC_DNS_ADDRESSES + 1);
assert_eq!(
Err(ResolveError::TooManyAddresses),
screen_resolved_addresses(over_limit.into_iter())
);
let mut mixed = socket_addresses("93.184.216.34", 2);
mixed.extend(socket_addresses("169.254.169.254", 1));
assert_eq!(
Err(ResolveError::BlockedAddress),
screen_resolved_addresses(mixed.into_iter())
);
}
#[test]
fn url_validation_uses_the_canonical_ipv4_host() {
for url in [
"http://0x7f.0x0.0x0.0x1/",
"http://0xa9.0xfe.0xa9.0xfe/latest/meta-data/",
"http://169.254.169.0xfe/latest/meta-data/",
"http://127.0.0.0x1/",
"http://0xa.0x0.0x0.0x1/",
"http://0xc0.0xa8.0x0.0x1/",
"http://0x0.0x0.0x0.0x0/",
"http://0x7f000001/",
"http://2130706433/",
"http://0177.0.0.1/",
"http://0x7f.1/",
"http://127.1/",
"https://0XA9.0XFE.0XA9.0XFE./",
] {
assert_eq!(Err(Error::BlockedUrl), validate_url(url), "{url}");
}
assert_eq!(Ok(()), validate_url("https://0x8.0x8.0x8.0x8/a"));
}
#[test]
fn redirect_targets_are_checked_on_their_canonical_host() {
let next = resolve_redirect(
"https://example.com/start.gif",
"http://0xa9.0xfe.0xa9.0xfe/latest/meta-data/",
)
.unwrap();
assert_eq!(Err(Error::BlockedUrl), validate_url(&next));
let next = resolve_redirect("https://example.com/start.gif", "//0x7f.0x0.0x0.0x1/a").unwrap();
assert_eq!(Err(Error::BlockedUrl), validate_url(&next));
}
use fluxer_common::external_media_path::{
build_external_media_proxy_path, reconstruct_original_url,
};
#[test]
fn a_signed_path_never_decodes_into_a_url_the_fetch_policy_refuses() {
let credentialed = "https://reader:[email protected]/i.png";
assert_eq!(
Err(super::Error::BlockedUrl),
super::parse_url(credentialed)
);
let path = build_external_media_proxy_path(credentialed).expect("path builds");
let decoded = reconstruct_original_url(&path).expect("path decodes");
assert_eq!("https://cdn.example.com/i.png", decoded);
assert!(super::parse_url(&decoded).is_ok());
}