use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn only_link_local_and_metadata_addresses_are_refused() {
let refused = |text: &str| Err(text.to_owned());
let cases = [
("http://127.0.0.1:3000/", Ok(())),
("http://localhost:5173/", Ok(())),
("http://app.localhost/", Ok(())),
("http://192.168.1.20:8080/", Ok(())),
("http://10.0.0.5/", Ok(())),
("http://[::1]:8000/", Ok(())),
("https://example.com/", Ok(())),
("data:text/html,hi", Ok(())),
(
"http://169.254.169.254/latest/meta-data/",
refused("169.254.169.254 is a link-local or cloud-metadata address, which the browser does not load"),
),
(
"http://169.254.1.1/",
refused("169.254.1.1 is a link-local or cloud-metadata address, which the browser does not load"),
),
(
"http://[fe80::1]/",
refused("[fe80::1] is a link-local or cloud-metadata address, which the browser does not load"),
),
(
"http://[fd00:ec2::254]/",
refused("[fd00:ec2::254] is a link-local or cloud-metadata address, which the browser does not load"),
),
(
"http://[::ffff:169.254.169.254]/",
refused("[::ffff:a9fe:a9fe] (::ffff:169.254.169.254) is a link-local or cloud-metadata address, which the browser does not load"),
),
(
"http://100.100.100.200/",
refused("100.100.100.200 is a link-local or cloud-metadata address, which the browser does not load"),
),
(
"http://metadata.test/computeMetadata/v1/",
refused("metadata.test (169.254.169.254) is a link-local or cloud-metadata address, which the browser does not load"),
),
];
let resolve = |name: &str| match name {
"metadata.test" => vec!["169.254.169.254".parse().unwrap()],
"example.com" => vec!["93.184.215.14".parse().unwrap()],
_ => Vec::new(),
};
for (url, expected) in cases {
let checked = ssrf::with_resolver(resolve, allowed(url)).await;
assert_eq!(checked, expected, "{url}");
}
}