| 1 |
|
| 2 |
|
| 3 |
use pulldown_cmark::{Event, Parser, Tag}; |
| 4 |
use reqwest::header::CONTENT_TYPE; |
| 5 |
|
| 6 |
|
| 7 |
const MAX_URLS: usize = 3; |
| 8 |
|
| 9 |
|
| 10 |
const MAX_BODY_SIZE: usize = 1_048_576; |
| 11 |
|
| 12 |
|
| 13 |
fn is_private_ip(ip: std::net::IpAddr) -> bool { |
| 14 |
match ip { |
| 15 |
std::net::IpAddr::V4(v4) => { |
| 16 |
let o = v4.octets(); |
| 17 |
v4.is_loopback() |
| 18 |
|| v4.is_private() |
| 19 |
|| v4.is_link_local() |
| 20 |
|| v4.is_broadcast() |
| 21 |
|| v4.is_unspecified() |
| 22 |
|| o[0] == 0 |
| 23 |
|| o[0] == 100 && (o[1] & 0xC0) == 64 |
| 24 |
|| o[0] == 192 && o[1] == 0 && o[2] == 0 |
| 25 |
|| o[0] == 198 && (o[1] & 0xFE) == 18 |
| 26 |
|| o[0] >= 240 |
| 27 |
} |
| 28 |
std::net::IpAddr::V6(v6) => { |
| 29 |
let seg = v6.segments(); |
| 30 |
v6.is_loopback() |
| 31 |
|| v6.is_unspecified() |
| 32 |
|| (seg[0] & 0xfe00) == 0xfc00 |
| 33 |
|| (seg[0] & 0xffc0) == 0xfe80 |
| 34 |
|
| 35 |
|| (seg[0] == 0x0064 && seg[1] == 0xff9b |
| 36 |
&& is_private_ip(std::net::IpAddr::V4(std::net::Ipv4Addr::new( |
| 37 |
(seg[6] >> 8) as u8, seg[6] as u8, (seg[7] >> 8) as u8, seg[7] as u8, |
| 38 |
)))) |
| 39 |
|
| 40 |
|
| 41 |
|| matches!(v6.to_ipv4(), Some(v4) if is_private_ip(std::net::IpAddr::V4(v4))) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
fn validate_url(url: &str) -> bool { |
| 63 |
let Ok(parsed) = url::Url::parse(url) else { |
| 64 |
return false; |
| 65 |
}; |
| 66 |
match parsed.scheme() { |
| 67 |
"http" | "https" => {} |
| 68 |
_ => return false, |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if let Some(p) = parsed.port() |
| 75 |
&& p != 80 |
| 76 |
&& p != 443 |
| 77 |
{ |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
match parsed.host() { |
| 86 |
Some(url::Host::Ipv4(v4)) => !is_private_ip(std::net::IpAddr::V4(v4)), |
| 87 |
Some(url::Host::Ipv6(v6)) => !is_private_ip(std::net::IpAddr::V6(v6)), |
| 88 |
Some(url::Host::Domain(domain)) => { |
| 89 |
!domain.is_empty() && !domain.eq_ignore_ascii_case("localhost") |
| 90 |
} |
| 91 |
None => false, |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
pub fn extract_urls(input: &str) -> Vec<String> { |
| 98 |
let parser = Parser::new(input); |
| 99 |
let mut seen = std::collections::HashSet::new(); |
| 100 |
let mut urls = Vec::new(); |
| 101 |
|
| 102 |
for event in parser { |
| 103 |
if let Event::Start(Tag::Link { dest_url, .. }) = event { |
| 104 |
let url = dest_url.to_string(); |
| 105 |
if (url.starts_with("http://") || url.starts_with("https://")) |
| 106 |
&& seen.insert(url.clone()) |
| 107 |
{ |
| 108 |
urls.push(url); |
| 109 |
if urls.len() >= MAX_URLS { |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
urls |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
struct SsrfSafeResolver; |
| 128 |
|
| 129 |
impl reqwest::dns::Resolve for SsrfSafeResolver { |
| 130 |
fn resolve(&self, name: reqwest::dns::Name) -> reqwest::dns::Resolving { |
| 131 |
Box::pin(async move { |
| 132 |
let host = name.as_str().to_string(); |
| 133 |
let addrs = tokio::net::lookup_host((host.as_str(), 0)).await?; |
| 134 |
let public: Vec<std::net::SocketAddr> = |
| 135 |
addrs.filter(|a| !is_private_ip(a.ip())).collect(); |
| 136 |
if public.is_empty() { |
| 137 |
return Err("no public address for host (SSRF guard)".into()); |
| 138 |
} |
| 139 |
let iter: reqwest::dns::Addrs = Box::new(public.into_iter()); |
| 140 |
Ok(iter) |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
pub fn build_preview_client() -> reqwest::Client { |
| 148 |
crate::tls::builder() |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
.timeout(std::time::Duration::from_secs(10)) |
| 153 |
.connect_timeout(std::time::Duration::from_secs(5)) |
| 154 |
.dns_resolver(std::sync::Arc::new(SsrfSafeResolver)) |
| 155 |
.redirect(reqwest::redirect::Policy::custom(|attempt| { |
| 156 |
if !validate_url(attempt.url().as_str()) || attempt.previous().len() >= 5 { |
| 157 |
attempt.stop() |
| 158 |
} else { |
| 159 |
attempt.follow() |
| 160 |
} |
| 161 |
})) |
| 162 |
.build() |
| 163 |
.expect("failed to build preview HTTP client") |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#[derive(Clone)] |
| 170 |
pub enum LinkPreviewFetcher { |
| 171 |
Http(reqwest::Client), |
| 172 |
Noop, |
| 173 |
} |
| 174 |
|
| 175 |
impl LinkPreviewFetcher { |
| 176 |
pub async fn fetch(&self, url: &str) -> Option<(Option<String>, Option<String>)> { |
| 177 |
match self { |
| 178 |
Self::Http(client) => fetch_og_metadata(client, url).await, |
| 179 |
Self::Noop => None, |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
#[tracing::instrument(skip_all)] |
| 187 |
pub async fn fetch_og_metadata( |
| 188 |
http: &reqwest::Client, |
| 189 |
url: &str, |
| 190 |
) -> Option<(Option<String>, Option<String>)> { |
| 191 |
if !validate_url(url) { |
| 192 |
tracing::warn!(%url, "link preview blocked: url failed scheme/host SSRF validation"); |
| 193 |
return None; |
| 194 |
} |
| 195 |
|
| 196 |
let resp = match http |
| 197 |
.get(url) |
| 198 |
.timeout(std::time::Duration::from_secs(5)) |
| 199 |
.header("User-Agent", "Multithreaded/LinkPreview") |
| 200 |
.send() |
| 201 |
.await |
| 202 |
{ |
| 203 |
Ok(r) => r, |
| 204 |
Err(e) => { |
| 205 |
tracing::debug!(%url, error = ?e, "link preview fetch failed (transport/timeout/blocked resolver)"); |
| 206 |
return None; |
| 207 |
} |
| 208 |
}; |
| 209 |
|
| 210 |
if !resp.status().is_success() { |
| 211 |
tracing::debug!(%url, status = %resp.status(), "link preview fetch: non-success status"); |
| 212 |
return None; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
let is_html = resp |
| 218 |
.headers() |
| 219 |
.get(CONTENT_TYPE) |
| 220 |
.and_then(|ct| ct.to_str().ok()) |
| 221 |
.is_some_and(|ct| ct.starts_with("text/html")); |
| 222 |
if !is_html { |
| 223 |
return None; |
| 224 |
} |
| 225 |
|
| 226 |
let mut body = Vec::new(); |
| 227 |
let mut stream = resp; |
| 228 |
while body.len() < MAX_BODY_SIZE { |
| 229 |
let Some(chunk) = stream.chunk().await.ok()? else { |
| 230 |
break; |
| 231 |
}; |
| 232 |
let remaining = MAX_BODY_SIZE - body.len(); |
| 233 |
body.extend_from_slice(&chunk[..chunk.len().min(remaining)]); |
| 234 |
} |
| 235 |
|
| 236 |
let html = String::from_utf8_lossy(&body); |
| 237 |
|
| 238 |
let og_title = extract_og_meta(&html, "og:title"); |
| 239 |
let og_desc = extract_og_meta(&html, "og:description"); |
| 240 |
|
| 241 |
let title = og_title.or_else(|| extract_html_title(&html)); |
| 242 |
|
| 243 |
if title.is_some() || og_desc.is_some() { |
| 244 |
Some((title, og_desc)) |
| 245 |
} else { |
| 246 |
None |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
fn allowed_image_content_type(ct: &str) -> Option<&'static str> { |
| 255 |
match ct.split(';').next().unwrap_or("").trim() { |
| 256 |
"image/png" => Some("image/png"), |
| 257 |
"image/jpeg" => Some("image/jpeg"), |
| 258 |
"image/gif" => Some("image/gif"), |
| 259 |
"image/webp" => Some("image/webp"), |
| 260 |
_ => None, |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
#[tracing::instrument(skip_all)] |
| 272 |
pub async fn fetch_image(http: &reqwest::Client, url: &str) -> Option<(Vec<u8>, &'static str)> { |
| 273 |
if !validate_url(url) { |
| 274 |
tracing::warn!(%url, "image proxy blocked: url failed scheme/host SSRF validation"); |
| 275 |
return None; |
| 276 |
} |
| 277 |
|
| 278 |
let resp = match http |
| 279 |
.get(url) |
| 280 |
.timeout(std::time::Duration::from_secs(5)) |
| 281 |
.header("User-Agent", "Multithreaded/ImageProxy") |
| 282 |
.send() |
| 283 |
.await |
| 284 |
{ |
| 285 |
Ok(r) => r, |
| 286 |
Err(e) => { |
| 287 |
tracing::debug!(%url, error = ?e, "image proxy fetch failed (transport/timeout/blocked resolver)"); |
| 288 |
return None; |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
if !resp.status().is_success() { |
| 293 |
tracing::debug!(%url, status = %resp.status(), "image proxy fetch: non-success status"); |
| 294 |
return None; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
let content_type = resp |
| 299 |
.headers() |
| 300 |
.get(CONTENT_TYPE) |
| 301 |
.and_then(|ct| ct.to_str().ok()) |
| 302 |
.and_then(allowed_image_content_type)?; |
| 303 |
|
| 304 |
|
| 305 |
let mut body = Vec::new(); |
| 306 |
let mut stream = resp; |
| 307 |
while body.len() < MAX_BODY_SIZE { |
| 308 |
let Some(chunk) = stream.chunk().await.ok()? else { |
| 309 |
break; |
| 310 |
}; |
| 311 |
let remaining = MAX_BODY_SIZE - body.len(); |
| 312 |
body.extend_from_slice(&chunk[..chunk.len().min(remaining)]); |
| 313 |
} |
| 314 |
|
| 315 |
if body.is_empty() { |
| 316 |
return None; |
| 317 |
} |
| 318 |
Some((body, content_type)) |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
fn extract_og_meta(html: &str, property: &str) -> Option<String> { |
| 323 |
static OG_RE: std::sync::LazyLock<regex_lite::Regex> = std::sync::LazyLock::new(|| { |
| 324 |
regex_lite::Regex::new( |
| 325 |
r#"<meta\s[^>]*?property\s*=\s*"([^"]*)"[^>]*?content\s*=\s*"([^"]*)"[^>]*?>"#, |
| 326 |
) |
| 327 |
.unwrap() |
| 328 |
}); |
| 329 |
static OG_RE_REV: std::sync::LazyLock<regex_lite::Regex> = std::sync::LazyLock::new(|| { |
| 330 |
regex_lite::Regex::new( |
| 331 |
r#"<meta\s[^>]*?content\s*=\s*"([^"]*)"[^>]*?property\s*=\s*"([^"]*)"[^>]*?>"#, |
| 332 |
) |
| 333 |
.unwrap() |
| 334 |
}); |
| 335 |
|
| 336 |
for caps in OG_RE.captures_iter(html) { |
| 337 |
if &caps[1] == property { |
| 338 |
let val = caps[2].trim().to_string(); |
| 339 |
if !val.is_empty() { |
| 340 |
return Some(val); |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
for caps in OG_RE_REV.captures_iter(html) { |
| 346 |
if &caps[2] == property { |
| 347 |
let val = caps[1].trim().to_string(); |
| 348 |
if !val.is_empty() { |
| 349 |
return Some(val); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
None |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
fn extract_html_title(html: &str) -> Option<String> { |
| 358 |
static TITLE_RE: std::sync::LazyLock<regex_lite::Regex> = std::sync::LazyLock::new(|| { |
| 359 |
regex_lite::Regex::new(r"<title[^>]*>([^<]+)</title>").unwrap() |
| 360 |
}); |
| 361 |
TITLE_RE.captures(html).map(|c| c[1].trim().to_string()) |
| 362 |
} |
| 363 |
|
| 364 |
#[cfg(test)] |
| 365 |
mod tests { |
| 366 |
use super::*; |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn extract_urls_from_markdown() { |
| 370 |
let input = "Check [this](https://example.com) and [that](https://other.com/page)."; |
| 371 |
let urls = extract_urls(input); |
| 372 |
assert_eq!(urls, vec!["https://example.com", "https://other.com/page"]); |
| 373 |
} |
| 374 |
|
| 375 |
#[test] |
| 376 |
fn image_proxy_content_type_allowlist() { |
| 377 |
|
| 378 |
|
| 379 |
assert_eq!(allowed_image_content_type("image/png"), Some("image/png")); |
| 380 |
assert_eq!( |
| 381 |
allowed_image_content_type("image/jpeg; charset=binary"), |
| 382 |
Some("image/jpeg") |
| 383 |
); |
| 384 |
assert_eq!(allowed_image_content_type("image/webp"), Some("image/webp")); |
| 385 |
assert_eq!(allowed_image_content_type("image/svg+xml"), None); |
| 386 |
assert_eq!(allowed_image_content_type("text/html"), None); |
| 387 |
assert_eq!(allowed_image_content_type(""), None); |
| 388 |
} |
| 389 |
|
| 390 |
#[test] |
| 391 |
fn extract_urls_skips_non_http() { |
| 392 |
let input = "[mail](mailto:a@b.com) [site](https://x.com)"; |
| 393 |
let urls = extract_urls(input); |
| 394 |
assert_eq!(urls, vec!["https://x.com"]); |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
fn extract_urls_caps_at_three() { |
| 399 |
let input = "[a](https://1.com) [b](https://2.com) [c](https://3.com) [d](https://4.com)"; |
| 400 |
let urls = extract_urls(input); |
| 401 |
assert_eq!(urls.len(), 3); |
| 402 |
} |
| 403 |
|
| 404 |
#[test] |
| 405 |
fn extract_urls_deduplicates() { |
| 406 |
let input = "[a](https://same.com) [b](https://same.com)"; |
| 407 |
let urls = extract_urls(input); |
| 408 |
assert_eq!(urls, vec!["https://same.com"]); |
| 409 |
} |
| 410 |
|
| 411 |
#[test] |
| 412 |
fn extract_urls_no_links() { |
| 413 |
let urls = extract_urls("no links here"); |
| 414 |
assert!(urls.is_empty()); |
| 415 |
} |
| 416 |
|
| 417 |
#[test] |
| 418 |
fn og_meta_property_first() { |
| 419 |
let html = r#"<meta property="og:title" content="My Page">"#; |
| 420 |
assert_eq!( |
| 421 |
extract_og_meta(html, "og:title"), |
| 422 |
Some("My Page".to_string()) |
| 423 |
); |
| 424 |
} |
| 425 |
|
| 426 |
#[test] |
| 427 |
fn og_meta_content_first() { |
| 428 |
let html = r#"<meta content="Description here" property="og:description">"#; |
| 429 |
assert_eq!( |
| 430 |
extract_og_meta(html, "og:description"), |
| 431 |
Some("Description here".to_string()) |
| 432 |
); |
| 433 |
} |
| 434 |
|
| 435 |
#[test] |
| 436 |
fn og_meta_missing() { |
| 437 |
let html = r#"<meta property="og:image" content="img.png">"#; |
| 438 |
assert_eq!(extract_og_meta(html, "og:title"), None); |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn html_title_fallback() { |
| 443 |
let html = "<html><head><title>Page Title</title></head></html>"; |
| 444 |
assert_eq!(extract_html_title(html), Some("Page Title".to_string())); |
| 445 |
} |
| 446 |
|
| 447 |
#[test] |
| 448 |
fn html_title_missing() { |
| 449 |
let html = "<html><head></head></html>"; |
| 450 |
assert_eq!(extract_html_title(html), None); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
#[test] |
| 456 |
fn validate_url_allows_https() { |
| 457 |
assert!(validate_url("https://example.com")); |
| 458 |
assert!(validate_url("https://example.com/path?q=1")); |
| 459 |
} |
| 460 |
|
| 461 |
#[test] |
| 462 |
fn validate_url_allows_http() { |
| 463 |
assert!(validate_url("http://example.com")); |
| 464 |
} |
| 465 |
|
| 466 |
#[test] |
| 467 |
fn validate_url_blocks_non_http_schemes() { |
| 468 |
assert!(!validate_url("ftp://example.com")); |
| 469 |
assert!(!validate_url("file:///etc/passwd")); |
| 470 |
assert!(!validate_url("javascript:alert(1)")); |
| 471 |
assert!(!validate_url("data:text/html,<h1>hi</h1>")); |
| 472 |
} |
| 473 |
|
| 474 |
#[test] |
| 475 |
fn validate_url_blocks_localhost() { |
| 476 |
assert!(!validate_url("http://localhost")); |
| 477 |
assert!(!validate_url("http://localhost:8080")); |
| 478 |
assert!(!validate_url("http://127.0.0.1")); |
| 479 |
assert!(!validate_url("http://127.0.0.1:3000")); |
| 480 |
assert!(!validate_url("http://0.0.0.0")); |
| 481 |
assert!(!validate_url("http://[::1]")); |
| 482 |
assert!(!validate_url("http://[::1]:8080")); |
| 483 |
} |
| 484 |
|
| 485 |
#[test] |
| 486 |
fn validate_url_blocks_private_10() { |
| 487 |
assert!(!validate_url("http://10.0.0.1")); |
| 488 |
assert!(!validate_url("http://10.255.255.255")); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn validate_url_blocks_private_192_168() { |
| 493 |
assert!(!validate_url("http://192.168.0.1")); |
| 494 |
assert!(!validate_url("http://192.168.1.100:8080")); |
| 495 |
} |
| 496 |
|
| 497 |
#[test] |
| 498 |
fn validate_url_blocks_private_172_16() { |
| 499 |
assert!(!validate_url("http://172.16.0.1")); |
| 500 |
assert!(!validate_url("http://172.31.255.255")); |
| 501 |
|
| 502 |
assert!(validate_url("http://172.15.0.1")); |
| 503 |
assert!(validate_url("http://172.32.0.1")); |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn validate_url_blocks_link_local() { |
| 508 |
assert!(!validate_url("http://169.254.0.1")); |
| 509 |
assert!(!validate_url("http://169.254.169.254")); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn validate_url_blocks_ipv6_private() { |
| 514 |
assert!(!validate_url("http://[fd00::1]")); |
| 515 |
assert!(!validate_url("http://[fe80::1]")); |
| 516 |
} |
| 517 |
|
| 518 |
#[test] |
| 519 |
fn validate_url_allows_public_ips() { |
| 520 |
assert!(validate_url("http://8.8.8.8")); |
| 521 |
assert!(validate_url("https://93.184.216.34")); |
| 522 |
} |
| 523 |
|
| 524 |
#[test] |
| 525 |
fn validate_url_blocks_userinfo_literal_ip() { |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
assert!(!validate_url("http://@10.0.0.1/")); |
| 530 |
assert!(!validate_url("http://@169.254.169.254/")); |
| 531 |
assert!(!validate_url("http://user:pass@127.0.0.1/")); |
| 532 |
assert!(!validate_url("http://foo@192.168.1.1:80/path")); |
| 533 |
assert!(!validate_url("http://a@b@10.0.0.1/")); |
| 534 |
assert!(!validate_url("http://@[::1]/")); |
| 535 |
|
| 536 |
|
| 537 |
assert!(validate_url("http://user@example.com/")); |
| 538 |
} |
| 539 |
|
| 540 |
#[test] |
| 541 |
fn validate_url_blocks_empty_host() { |
| 542 |
|
| 543 |
assert!(!validate_url("http://@/")); |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
assert!(validate_url("http:///path")); |
| 549 |
} |
| 550 |
|
| 551 |
#[test] |
| 552 |
fn validate_url_blocks_numeric_ip_encodings() { |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
assert!(!validate_url("http://2130706433/")); |
| 559 |
assert!(!validate_url("http://0x7f000001/")); |
| 560 |
assert!(!validate_url("http://0177.0.0.1/")); |
| 561 |
assert!(!validate_url("http://2852039166/")); |
| 562 |
assert!(!validate_url("http://0xa9fea9fe/")); |
| 563 |
assert!(!validate_url("http://017700000001/")); |
| 564 |
|
| 565 |
|
| 566 |
assert!(validate_url("http://134744072/")); |
| 567 |
} |
| 568 |
|
| 569 |
#[test] |
| 570 |
fn validate_url_blocks_reserved_ranges() { |
| 571 |
assert!(!validate_url("http://0.1.2.3")); |
| 572 |
assert!(!validate_url("http://192.0.0.1")); |
| 573 |
assert!(!validate_url("http://198.18.0.1")); |
| 574 |
assert!(!validate_url("http://198.19.255.255")); |
| 575 |
assert!(!validate_url("http://240.0.0.1")); |
| 576 |
assert!(!validate_url("http://255.255.255.254")); |
| 577 |
} |
| 578 |
|
| 579 |
#[test] |
| 580 |
fn is_private_ip_blocks_v6_embedded_v4() { |
| 581 |
use std::net::{IpAddr, Ipv6Addr}; |
| 582 |
|
| 583 |
assert!(is_private_ip(IpAddr::V6( |
| 584 |
"::ffff:10.0.0.1".parse::<Ipv6Addr>().unwrap() |
| 585 |
))); |
| 586 |
|
| 587 |
assert!(!is_private_ip(IpAddr::V6( |
| 588 |
"::ffff:8.8.8.8".parse::<Ipv6Addr>().unwrap() |
| 589 |
))); |
| 590 |
|
| 591 |
assert!(is_private_ip(IpAddr::V6( |
| 592 |
"64:ff9b::169.254.169.254".parse::<Ipv6Addr>().unwrap() |
| 593 |
))); |
| 594 |
} |
| 595 |
|
| 596 |
#[tokio::test] |
| 597 |
async fn noop_fetcher_returns_none_without_network() { |
| 598 |
let fetcher = LinkPreviewFetcher::Noop; |
| 599 |
|
| 600 |
assert!(fetcher.fetch("https://example.com").await.is_none()); |
| 601 |
} |
| 602 |
} |
| 603 |
|