use url::Url;
pub fn validate_url_not_private(url_str: &str) -> Result<(), &'static str> {
let parsed = Url::parse(url_str).map_err(|_| "Invalid URL")?;
match parsed.scheme() {
"http" | "https" => {}
_ => return Err("Only HTTP(S) URLs are allowed"),
}
match parsed.host() {
Some(url::Host::Ipv4(ip)) => {
let o = ip.octets();
if ip.is_loopback() || ip.is_private() || ip.is_link_local()
|| ip.is_broadcast() || ip.is_unspecified()
|| (o[0] == 100 && o[1] >= 64 && o[1] <= 127)
{
return Err("Private/internal IP addresses are not allowed");
}
}
Some(url::Host::Ipv6(ip)) => {
if ip.is_loopback() || ip.is_unspecified() || is_ipv6_private(&ip) {
return Err("Private/internal IP addresses are not allowed");
}
}
Some(url::Host::Domain(domain)) => {
if domain == "localhost" || domain.ends_with(".local") || domain.ends_with(".internal") {
return Err("Local hostnames are not allowed");
}
}
None => return Err("URL has no host"),
}
Ok(())
}
fn is_ipv6_private(ip: &std::net::Ipv6Addr) -> bool {
if let Some(ipv4) = ip.to_ipv4_mapped() {
return ipv4.is_loopback() || ipv4.is_private() || ipv4.is_link_local();
}
let segments = ip.segments();
if segments[0] & 0xfe00 == 0xfc00 { return true; } if segments[0] & 0xffc0 == 0xfe80 { return true; } false
}
pub use reqwest;
pub use crate::transport::Lane;
pub const TRANSFER_STALL: std::time::Duration = std::time::Duration::from_secs(120);
pub fn transfer_stall() -> std::time::Duration {
crate::transport::budget(crate::transport::Op::TransferStall, TRANSFER_STALL)
}
static USER_AGENT: std::sync::OnceLock<String> = std::sync::OnceLock::new();
pub fn set_app_version(version: &str) {
let _ = USER_AGENT.set(format!("Vector/{version}"));
}
pub fn user_agent() -> String {
USER_AGENT
.get()
.cloned()
.unwrap_or_else(|| format!("Vector/{}", env!("CARGO_PKG_VERSION")))
}
pub fn tor_http_timeout(clearnet: std::time::Duration) -> std::time::Duration {
crate::transport::budget(crate::transport::Op::HttpTotal, clearnet)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct ClientOptions {
timeout: Option<std::time::Duration>,
read_timeout: Option<std::time::Duration>,
follow_redirects: bool,
no_direct: bool,
}
#[derive(Clone, Debug)]
pub struct HttpClient {
owner: u64,
lane: Lane,
opts: ClientOptions,
}
pub fn build_http_client(timeout: std::time::Duration) -> Result<HttpClient, String> {
build_http_client_for(Lane::Shared, Some(timeout), None, true)
}
pub fn build_http_client_with_options(
timeout: Option<std::time::Duration>,
read_timeout: Option<std::time::Duration>,
follow_redirects: bool,
) -> Result<HttpClient, String> {
build_http_client_for(Lane::Shared, timeout, read_timeout, follow_redirects)
}
pub fn build_http_client_for(
lane: Lane,
timeout: Option<std::time::Duration>,
read_timeout: Option<std::time::Duration>,
follow_redirects: bool,
) -> Result<HttpClient, String> {
Ok(HttpClient {
owner: crate::db::current_session_id(),
lane,
opts: ClientOptions { timeout, read_timeout, follow_redirects, no_direct: false },
})
}
const DEFAULT_SHARED_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
pub fn shared_http_client() -> std::sync::Arc<HttpClient> {
std::sync::Arc::new(HttpClient {
owner: crate::db::current_session_id(),
lane: Lane::Shared,
opts: ClientOptions { timeout: Some(DEFAULT_SHARED_TIMEOUT), read_timeout: None, follow_redirects: true, no_direct: false },
})
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct PoolKey {
epoch: u64,
owner: u64,
lane: Lane,
opts: ClientOptions,
direct: bool,
}
type Pools = std::collections::HashMap<PoolKey, reqwest::Client>;
static POOLS: std::sync::Mutex<Option<Pools>> = std::sync::Mutex::new(None);
pub fn forget_clients() {
if let Ok(mut g) = POOLS.lock() {
*g = None;
}
}
pub fn rebuild_shared_http_client() -> Result<(), String> {
forget_clients();
Ok(())
}
fn template() -> &'static reqwest::Client {
static T: std::sync::OnceLock<reqwest::Client> = std::sync::OnceLock::new();
T.get_or_init(|| client_builder().build().expect("a plain reqwest client always builds"))
}
#[cfg(not(target_arch = "wasm32"))]
fn tls_config() -> rustls::ClientConfig {
let roots = rustls::RootCertStore { roots: webpki_roots::TLS_SERVER_ROOTS.to_vec() };
let mut cfg = rustls::ClientConfig::builder_with_provider(std::sync::Arc::new(rustls::crypto::ring::default_provider()))
.with_safe_default_protocol_versions()
.expect("ring supports TLS 1.2 and 1.3")
.with_root_certificates(roots)
.with_no_client_auth();
cfg.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
cfg
}
#[allow(clippy::disallowed_methods)]
fn client_builder() -> reqwest::ClientBuilder {
#[cfg(not(target_arch = "wasm32"))]
return reqwest::Client::builder().tls_backend_preconfigured(tls_config());
#[cfg(target_arch = "wasm32")]
return reqwest::Client::builder();
}
impl HttpClient {
pub fn request<U: reqwest::IntoUrl>(&self, method: reqwest::Method, url: U) -> Req {
Req { rb: template().request(method, url), client: self.clone() }
}
pub fn get<U: reqwest::IntoUrl>(&self, url: U) -> Req {
self.request(reqwest::Method::GET, url)
}
pub fn post<U: reqwest::IntoUrl>(&self, url: U) -> Req {
self.request(reqwest::Method::POST, url)
}
pub fn put<U: reqwest::IntoUrl>(&self, url: U) -> Req {
self.request(reqwest::Method::PUT, url)
}
pub fn head<U: reqwest::IntoUrl>(&self, url: U) -> Req {
self.request(reqwest::Method::HEAD, url)
}
pub fn delete<U: reqwest::IntoUrl>(&self, url: U) -> Req {
self.request(reqwest::Method::DELETE, url)
}
pub fn lane(&self) -> Lane {
self.lane
}
pub fn owner(&self) -> u64 {
self.owner
}
pub fn with_lane(&self, lane: Lane) -> HttpClient {
HttpClient { lane, ..self.clone() }
}
pub fn without_direct(&self) -> HttpClient {
HttpClient { opts: ClientOptions { no_direct: true, ..self.opts }, ..self.clone() }
}
}
#[cfg(not(target_arch = "wasm32"))]
fn build_pooled(opts: ClientOptions, proxy: Option<String>) -> Result<reqwest::Client, String> {
use crate::transport::{budget, Op};
let mut builder = client_builder()
.pool_max_idle_per_host(8)
.pool_idle_timeout(std::time::Duration::from_secs(90))
.tcp_keepalive(std::time::Duration::from_secs(30))
.http2_keep_alive_interval(std::time::Duration::from_secs(30))
.http2_keep_alive_while_idle(true)
.user_agent(user_agent())
.connect_timeout(budget(Op::HttpConnect, std::time::Duration::from_secs(15)));
if let Some(t) = opts.timeout {
builder = builder.timeout(budget(Op::HttpTotal, t));
}
if let Some(rt) = opts.read_timeout {
builder = builder.read_timeout(budget(Op::HttpRead, rt));
}
if !opts.follow_redirects {
builder = builder.redirect(reqwest::redirect::Policy::none());
} else {
builder = builder.redirect(reqwest::redirect::Policy::custom(|attempt| {
if attempt.previous().len() >= 10 {
return attempt.error("too many redirects");
}
if let Some(dest) = attempt.url().host_str().and_then(crate::transport::Dest::parse) {
let kind = crate::transport::preference().unwrap_or(crate::transport::Kind::Clearnet);
if let Some(r) = crate::transport::route::pre_route(kind, &dest) {
return attempt.error(r.text());
}
}
match validate_url_not_private(attempt.url().as_str()) {
Ok(()) => attempt.follow(),
Err(e) => attempt.error(e),
}
}));
}
if let Some(url) = proxy {
let proxy = reqwest::Proxy::all(url).map_err(|_| "Vector couldn't open its local proxy.".to_string())?;
builder = builder.proxy(proxy);
}
builder.build().map_err(|e| format!("Failed to build HTTP client: {}", e))
}
#[cfg(target_arch = "wasm32")]
fn build_pooled(_opts: ClientOptions, _proxy: Option<String>) -> Result<reqwest::Client, String> {
client_builder().build().map_err(|e| format!("HTTP client build failed: {e}"))
}
fn pooled(desc: &HttpClient, epoch: u64, egress: &crate::transport::Egress) -> Result<reqwest::Client, HttpError> {
let key = PoolKey {
epoch,
owner: desc.owner,
lane: desc.lane,
opts: desc.opts,
direct: matches!(egress, crate::transport::Egress::Direct),
};
if let Some(c) = POOLS.lock().ok().and_then(|g| g.as_ref().and_then(|m| m.get(&key).cloned())) {
return Ok(c);
}
let proxy = match egress {
#[cfg(not(target_arch = "wasm32"))]
crate::transport::Egress::Proxy(t) => Some(crate::transport::bridge::proxy_url(t).map_err(HttpError::Refused)?),
_ => None,
};
let client = build_pooled(desc.opts, proxy).map_err(|e| HttpError::Refused(crate::transport::ConnectError::Bridge(e)))?;
if crate::transport::epoch() == epoch {
if let Ok(mut g) = POOLS.lock() {
g.get_or_insert_with(Default::default).entry(key).or_insert_with(|| client.clone());
}
}
Ok(client)
}
#[cfg(target_arch = "wasm32")]
static HOST_ALIASES: std::sync::OnceLock<Vec<(String, reqwest::Url)>> = std::sync::OnceLock::new();
#[cfg(target_arch = "wasm32")]
pub fn set_host_aliases(aliases: &[(&str, &str)]) {
let parsed = aliases.iter().filter_map(|(host, base)| Some((host.to_string(), reqwest::Url::parse(base).ok()?))).collect();
let _ = HOST_ALIASES.set(parsed);
}
#[cfg(target_arch = "wasm32")]
fn apply_host_alias(url: &mut reqwest::Url) {
if let Some(next) = HOST_ALIASES.get().and_then(|aliases| aliased(url, aliases)) {
*url = next;
}
}
#[cfg(any(target_arch = "wasm32", test))]
fn aliased(url: &reqwest::Url, aliases: &[(String, reqwest::Url)]) -> Option<reqwest::Url> {
let host = url.host_str()?;
let (_, base) = aliases
.iter()
.find(|(alias, _)| host == alias || host.strip_suffix(alias.as_str()).is_some_and(|sub| sub.ends_with('.')))?;
let mut next = base.clone();
next.set_path(url.path());
next.set_query(url.query());
Some(next)
}
pub struct Req {
rb: reqwest::RequestBuilder,
client: HttpClient,
}
impl Req {
pub fn header<K, V>(mut self, key: K, value: V) -> Self
where
reqwest::header::HeaderName: TryFrom<K>,
<reqwest::header::HeaderName as TryFrom<K>>::Error: Into<http::Error>,
reqwest::header::HeaderValue: TryFrom<V>,
<reqwest::header::HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
self.rb = self.rb.header(key, value);
self
}
pub fn headers(mut self, headers: reqwest::header::HeaderMap) -> Self {
self.rb = self.rb.headers(headers);
self
}
pub fn body<T: Into<reqwest::Body>>(mut self, body: T) -> Self {
self.rb = self.rb.body(body);
self
}
pub fn json<T: serde::Serialize + ?Sized>(mut self, json: &T) -> Self {
self.rb = self.rb.json(json);
self
}
pub fn bearer_auth<T: std::fmt::Display>(mut self, token: T) -> Self {
self.rb = self.rb.bearer_auth(token);
self
}
pub fn basic_auth<U: std::fmt::Display, P: std::fmt::Display>(mut self, user: U, password: Option<P>) -> Self {
self.rb = self.rb.basic_auth(user, password);
self
}
pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
self.rb = self.rb.timeout(timeout);
self
}
#[cfg(target_arch = "wasm32")]
pub fn fetch_cache_no_store(mut self) -> Self {
self.rb = self.rb.fetch_cache_no_store();
self
}
#[allow(clippy::disallowed_methods)]
pub async fn send(self) -> Result<Resp, HttpError> {
let (_, req) = self.rb.build_split();
#[allow(unused_mut)]
let mut req = req.map_err(HttpError::from_reqwest)?;
let host = req.url().host_str().unwrap_or_default().to_string();
let port = req.url().port_or_known_default().unwrap_or(443);
let desc = self.client;
let (mut epoch, mut client) = (0, None);
for _ in 0..3 {
epoch = crate::transport::epoch();
let egress = crate::transport::egress(desc.owner, desc.lane, &host, port);
if let crate::transport::Egress::Refuse(e) = egress {
return Err(HttpError::Refused(e));
}
if desc.opts.no_direct && egress == crate::transport::Egress::Direct {
return Err(HttpError::Refused(crate::transport::ConnectError::Stale));
}
client = Some(pooled(&desc, epoch, &egress)?);
if crate::transport::epoch() == epoch {
break;
}
}
let client = client.expect("the loop ran");
#[cfg(target_arch = "wasm32")]
apply_host_alias(req.url_mut());
let changed = crate::transport::changed(epoch);
let run = client.execute(req);
futures_util::pin_mut!(changed, run);
match futures_util::future::select(changed, run).await {
futures_util::future::Either::Left(_) => Err(HttpError::NetworkChanged),
futures_util::future::Either::Right((Ok(inner), _)) => Ok(Resp { inner, epoch, owner: desc.owner }),
futures_util::future::Either::Right((Err(e), _)) => Err(HttpError::from_reqwest_for(e, &host)),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub type BodyStream = std::pin::Pin<Box<dyn futures_util::Stream<Item = Result<bytes::Bytes, HttpError>> + Send>>;
#[cfg(target_arch = "wasm32")]
pub type BodyStream = std::pin::Pin<Box<dyn futures_util::Stream<Item = Result<bytes::Bytes, HttpError>>>>;
pub struct Resp {
inner: reqwest::Response,
epoch: u64,
owner: u64,
}
impl Resp {
pub fn status(&self) -> reqwest::StatusCode {
self.inner.status()
}
pub fn headers(&self) -> &reqwest::header::HeaderMap {
self.inner.headers()
}
pub fn content_length(&self) -> Option<u64> {
self.inner.content_length()
}
pub fn url(&self) -> &reqwest::Url {
self.inner.url()
}
pub fn error_for_status(self) -> Result<Resp, HttpError> {
let (epoch, owner) = (self.epoch, self.owner);
self.inner.error_for_status().map(|inner| Resp { inner, epoch, owner }).map_err(HttpError::from_reqwest)
}
fn still_current(&self) -> bool {
crate::transport::epoch() == self.epoch && crate::db::live_session_id() == self.owner
}
#[cfg(not(target_arch = "wasm32"))]
#[allow(clippy::disallowed_methods)]
pub async fn chunk(&mut self) -> Result<Option<bytes::Bytes>, HttpError> {
if !self.still_current() {
return Err(HttpError::NetworkChanged);
}
let changed = crate::transport::changed(self.epoch);
let read = self.inner.chunk();
futures_util::pin_mut!(read, changed);
match futures_util::future::select(read, changed).await {
futures_util::future::Either::Left((r, _)) => r.map_err(HttpError::from_reqwest),
futures_util::future::Either::Right(_) => Err(HttpError::NetworkChanged),
}
}
#[allow(clippy::disallowed_methods)]
pub fn bytes_stream(self) -> BodyStream {
use futures_util::StreamExt;
let (epoch, owner) = (self.epoch, self.owner);
let inner = Box::pin(self.inner.bytes_stream());
Box::pin(futures_util::stream::unfold((inner, false), move |(mut inner, done)| async move {
if done {
return None;
}
if crate::transport::epoch() != epoch || crate::db::live_session_id() != owner {
return Some((Err(HttpError::NetworkChanged), (inner, true)));
}
let changed = crate::transport::changed(epoch);
futures_util::pin_mut!(changed);
match futures_util::future::select(inner.next(), changed).await {
futures_util::future::Either::Left((Some(item), _)) => Some((item.map_err(HttpError::from_reqwest), (inner, false))),
futures_util::future::Either::Left((None, _)) => None,
futures_util::future::Either::Right(_) => Some((Err(HttpError::NetworkChanged), (inner, true))),
}
}))
}
async fn whole<T, F: std::future::Future<Output = Result<T, reqwest::Error>>>(epoch: u64, read: F) -> Result<T, HttpError> {
let changed = crate::transport::changed(epoch);
futures_util::pin_mut!(read, changed);
match futures_util::future::select(read, changed).await {
futures_util::future::Either::Left((r, _)) => r.map_err(HttpError::from_reqwest),
futures_util::future::Either::Right(_) => Err(HttpError::NetworkChanged),
}
}
pub async fn bytes(self) -> Result<bytes::Bytes, HttpError> {
if !self.still_current() {
return Err(HttpError::NetworkChanged);
}
Self::whole(self.epoch, self.inner.bytes()).await
}
pub async fn text(self) -> Result<String, HttpError> {
if !self.still_current() {
return Err(HttpError::NetworkChanged);
}
Self::whole(self.epoch, self.inner.text()).await
}
pub async fn json<T: serde::de::DeserializeOwned>(self) -> Result<T, HttpError> {
if !self.still_current() {
return Err(HttpError::NetworkChanged);
}
Self::whole(self.epoch, self.inner.json::<T>()).await
}
}
#[derive(Debug)]
pub enum HttpError {
Refused(crate::transport::ConnectError),
NetworkChanged,
Http { err: reqwest::Error, text: String },
}
impl HttpError {
fn from_reqwest(err: reqwest::Error) -> Self {
HttpError::Http { text: err.to_string(), err }
}
fn from_reqwest_for(err: reqwest::Error, host: &str) -> Self {
let recorded = crate::transport::host::active().and_then(|a| {
let (at, e) = a.last_failure(host)?;
let fresh = at.elapsed() < std::time::Duration::from_secs(30);
let keep_reqwest = a.kind == crate::transport::Kind::Tor && matches!(e, crate::transport::ConnectError::Unreachable(_));
(fresh && !keep_reqwest).then(|| e.text())
});
HttpError::Http { text: recorded.unwrap_or_else(|| err.to_string()), err }
}
pub fn is_timeout(&self) -> bool {
matches!(self, HttpError::Http { err, .. } if err.is_timeout())
}
pub fn is_connect(&self) -> bool {
#[cfg(not(target_arch = "wasm32"))]
{
matches!(self, HttpError::Http { err, .. } if err.is_connect())
}
#[cfg(target_arch = "wasm32")]
{
false
}
}
pub fn is_request(&self) -> bool {
matches!(self, HttpError::Http { err, .. } if err.is_request())
}
pub fn status(&self) -> Option<reqwest::StatusCode> {
match self {
HttpError::Http { err, .. } => err.status(),
_ => None,
}
}
pub fn is_transient(&self) -> bool {
match self {
HttpError::NetworkChanged => true,
HttpError::Refused(e) => e.is_transient(),
HttpError::Http { .. } => false,
}
}
pub fn is_policy_refusal(&self) -> bool {
matches!(self, HttpError::Refused(crate::transport::ConnectError::Refused(_)))
}
}
impl std::fmt::Display for HttpError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HttpError::Refused(e) => f.write_str(&e.text()),
HttpError::NetworkChanged => f.write_str("The network changed."),
HttpError::Http { text, .. } => f.write_str(text),
}
}
}
impl std::error::Error for HttpError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
HttpError::Http { err, .. } => Some(err),
_ => None,
}
}
}
pub fn guard_body<S, E>(
body: S,
epoch: u64,
owner: u64,
) -> impl futures_util::Stream<Item = Result<bytes::Bytes, std::io::Error>> + Send + 'static
where
S: futures_util::Stream<Item = Result<bytes::Bytes, E>> + Send + 'static,
E: std::fmt::Display + Send + 'static,
{
crate::transport::guard::guard_body(body, epoch, owner)
}
pub async fn wait_after(e: &HttpError) -> Result<(), String> {
if !e.is_transient() {
return Err(e.to_string());
}
crate::transport::wait_ready(crate::transport::budget(crate::transport::Op::Startup, std::time::Duration::ZERO)).await
}
fn md_group_close(bytes: &[u8], start: usize, open: u8, close: u8) -> Option<usize> {
let mut depth = 1usize;
let mut escaped = false;
let mut j = start;
loop {
match bytes.get(j).copied() {
None => return None,
Some(b'\\') if !escaped => escaped = true,
Some(b) if b == open && !escaped => depth += 1,
Some(b) if b == close && !escaped => {
depth -= 1;
if depth == 0 {
return Some(j);
}
}
_ => escaped = false,
}
j += 1;
}
}
pub fn strip_md_link_claims(text: &str) -> String {
let bytes = text.as_bytes();
let mut out = String::with_capacity(text.len());
let mut last = 0;
let mut i = 0;
while i < bytes.len() {
if bytes[i] != b'[' || (i > 0 && bytes[i - 1] == b'!') {
i += 1;
continue;
}
let Some(close) = md_group_close(bytes, i + 1, b'[', b']') else { break };
match bytes.get(close + 1).copied() {
Some(b'(') => {
let Some(paren) = md_group_close(bytes, close + 2, b'(', b')') else {
i = close + 1;
continue;
};
out.push_str(&text[last..i]);
let href = text[close + 2..paren].trim();
if !(href.starts_with('<') && href.ends_with('>')) {
out.push(' ');
out.push_str(href);
out.push(' ');
}
i = paren + 1;
last = i;
}
Some(b'[') => {
let Some(ref_close) = md_group_close(bytes, close + 2, b'[', b']') else {
i = close + 1;
continue;
};
out.push_str(&text[last..i]);
i = ref_close + 1;
last = i;
}
_ => {
i = close + 1;
}
}
}
out.push_str(&text[last..]);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn md_link_claim_text_dropped_href_kept() {
let out = strip_md_link_claims("[https://your-bank.com](https://evil.io)");
assert!(!out.contains("your-bank.com"), "claimed text must not reach the scan: {out}");
assert!(out.contains("https://evil.io"), "real destination must reach the scan: {out}");
}
#[test]
fn md_no_preview_link_dropped_entirely() {
let out = strip_md_link_claims("see [docs](<https://vector.app/docs>) ok");
assert!(!out.contains("vector.app"), "no-preview href must not reach the scan: {out}");
assert!(out.contains("see ") && out.contains(" ok"));
}
#[test]
fn md_image_passes_through() {
let text = "";
assert_eq!(strip_md_link_claims(text), text);
}
#[test]
fn plain_text_and_bare_urls_untouched() {
let text = "check https://vector.app and [also] (spaced) brackets";
assert_eq!(strip_md_link_claims(text), text);
}
#[test]
fn multiple_links_keep_document_order() {
let out = strip_md_link_claims("[a](https://one.io) mid [b](https://two.io)");
let one = out.find("https://one.io").expect("first href kept");
let two = out.find("https://two.io").expect("second href kept");
assert!(one < two);
}
#[test]
fn nested_bracket_label_still_drops_claim() {
let out = strip_md_link_claims("[[https://trusted.com]](https://evil.io)");
assert!(!out.contains("trusted.com"), "nested-bracket claim must not reach the scan: {out}");
assert!(out.contains("https://evil.io"));
}
#[test]
fn escaped_bracket_label_still_drops_claim() {
let out = strip_md_link_claims(r"[https://trusted.com\]](https://evil.io)");
assert!(!out.contains("trusted.com"), "escaped-bracket claim must not reach the scan: {out}");
assert!(out.contains("https://evil.io"));
}
#[test]
fn paren_path_href_survives_whole() {
let out = strip_md_link_claims("[wiki](https://en.wikipedia.org/wiki/Foo_(bar))");
assert!(out.contains("https://en.wikipedia.org/wiki/Foo_(bar)"), "balanced-paren href kept intact: {out}");
}
#[test]
fn reference_link_label_dropped_definition_scanned() {
let out = strip_md_link_claims("[https://trusted.com][1]\n[1]: https://evil.io");
assert!(!out.contains("trusted.com"), "reflink claim must not reach the scan: {out}");
assert!(out.contains("https://evil.io"), "definition URL stays scannable: {out}");
}
#[test]
fn multibyte_label_no_panic() {
let out = strip_md_link_claims("[🔒 sécurisé — café](https://evil.io) 日本語");
assert!(out.contains("https://evil.io"));
assert!(out.contains("日本語"));
}
#[test]
fn valid_public_https_url_passes() {
assert!(validate_url_not_private("https://example.com/path").is_ok(),
"https://example.com should be allowed");
}
#[test]
fn valid_public_http_url_passes() {
assert!(validate_url_not_private("http://example.com").is_ok(),
"http://example.com should be allowed");
}
#[test]
fn valid_public_ip_8888_passes() {
assert!(validate_url_not_private("https://8.8.8.8/dns").is_ok(),
"8.8.8.8 (Google DNS) is a public IP and should be allowed");
}
#[test]
fn valid_public_ip_1111_passes() {
assert!(validate_url_not_private("https://1.1.1.1").is_ok(),
"1.1.1.1 (Cloudflare DNS) is a public IP and should be allowed");
}
#[test]
fn valid_url_with_port_passes() {
assert!(validate_url_not_private("https://example.com:8080/api").is_ok(),
"URL with port on public domain should be allowed");
}
#[test]
fn localhost_rejected() {
let result = validate_url_not_private("http://localhost/secret");
assert!(result.is_err(), "localhost should be rejected");
}
#[test]
fn ip_127_0_0_1_rejected() {
let result = validate_url_not_private("http://127.0.0.1/admin");
assert!(result.is_err(), "127.0.0.1 (loopback) should be rejected");
}
#[test]
fn ip_127_255_255_255_rejected() {
let result = validate_url_not_private("http://127.255.255.255");
assert!(result.is_err(), "127.255.255.255 (loopback range) should be rejected");
}
#[test]
fn private_class_a_10_rejected() {
let result = validate_url_not_private("http://10.0.0.1/internal");
assert!(result.is_err(), "10.0.0.1 (private class A) should be rejected");
}
#[test]
fn private_class_b_172_16_rejected() {
let result = validate_url_not_private("http://172.16.0.1/internal");
assert!(result.is_err(), "172.16.0.1 (private class B) should be rejected");
}
#[test]
fn private_class_b_172_31_rejected() {
let result = validate_url_not_private("http://172.31.255.255");
assert!(result.is_err(), "172.31.255.255 (private class B upper bound) should be rejected");
}
#[test]
fn private_class_c_192_168_rejected() {
let result = validate_url_not_private("http://192.168.1.1/router");
assert!(result.is_err(), "192.168.1.1 (private class C) should be rejected");
}
#[test]
fn link_local_169_254_rejected() {
let result = validate_url_not_private("http://169.254.1.1");
assert!(result.is_err(), "169.254.1.1 (link-local) should be rejected");
}
#[test]
fn cgn_100_64_rejected() {
let result = validate_url_not_private("http://100.64.0.1");
assert!(result.is_err(), "100.64.0.1 (CGN / shared address space) should be rejected");
}
#[test]
fn cgn_100_127_rejected() {
let result = validate_url_not_private("http://100.127.255.255");
assert!(result.is_err(), "100.127.255.255 (CGN upper bound) should be rejected");
}
#[test]
fn broadcast_255_rejected() {
let result = validate_url_not_private("http://255.255.255.255");
assert!(result.is_err(), "255.255.255.255 (broadcast) should be rejected");
}
#[test]
fn unspecified_0_0_0_0_rejected() {
let result = validate_url_not_private("http://0.0.0.0");
assert!(result.is_err(), "0.0.0.0 (unspecified) should be rejected");
}
#[test]
fn ipv6_loopback_rejected() {
let result = validate_url_not_private("http://[::1]/secret");
assert!(result.is_err(), "::1 (IPv6 loopback) should be rejected");
}
#[test]
fn ipv6_unique_local_fc00_rejected() {
let result = validate_url_not_private("http://[fc00::1]");
assert!(result.is_err(), "fc00::1 (IPv6 unique-local) should be rejected");
}
#[test]
fn ipv6_unique_local_fd00_rejected() {
let result = validate_url_not_private("http://[fd00::1]");
assert!(result.is_err(), "fd00::1 (IPv6 unique-local) should be rejected");
}
#[test]
fn ipv6_link_local_fe80_rejected() {
let result = validate_url_not_private("http://[fe80::1]");
assert!(result.is_err(), "fe80::1 (IPv6 link-local) should be rejected");
}
#[test]
fn ipv4_mapped_ipv6_loopback_rejected() {
let result = validate_url_not_private("http://[::ffff:127.0.0.1]");
assert!(result.is_err(), "::ffff:127.0.0.1 (IPv4-mapped loopback) should be rejected");
}
#[test]
fn ipv4_mapped_ipv6_private_rejected() {
let result = validate_url_not_private("http://[::ffff:192.168.1.1]");
assert!(result.is_err(), "::ffff:192.168.1.1 (IPv4-mapped private) should be rejected");
}
#[test]
fn dot_local_domain_rejected() {
let result = validate_url_not_private("http://mydevice.local/api");
assert!(result.is_err(), ".local domain should be rejected");
}
#[test]
fn dot_internal_domain_rejected() {
let result = validate_url_not_private("http://service.internal/health");
assert!(result.is_err(), ".internal domain should be rejected");
}
#[test]
fn ftp_scheme_rejected() {
let result = validate_url_not_private("ftp://example.com/file.txt");
assert!(result.is_err(), "ftp:// scheme should be rejected");
assert_eq!(result.unwrap_err(), "Only HTTP(S) URLs are allowed");
}
#[test]
fn file_scheme_rejected() {
let result = validate_url_not_private("file:///etc/passwd");
assert!(result.is_err(), "file:// scheme should be rejected");
assert_eq!(result.unwrap_err(), "Only HTTP(S) URLs are allowed");
}
#[test]
fn javascript_scheme_rejected() {
let result = validate_url_not_private("javascript:alert(1)");
assert!(result.is_err(), "javascript: scheme should be rejected");
}
#[test]
fn data_scheme_rejected() {
let result = validate_url_not_private("data:text/html,<h1>hi</h1>");
assert!(result.is_err(), "data: scheme should be rejected");
}
#[test]
fn no_host_rejected() {
let result = validate_url_not_private("http://");
assert!(result.is_err(), "URL with no host should be rejected");
}
#[test]
fn invalid_url_rejected() {
let result = validate_url_not_private("not a url at all");
assert!(result.is_err(), "invalid URL string should be rejected");
assert_eq!(result.unwrap_err(), "Invalid URL");
}
#[test]
fn empty_string_rejected() {
let result = validate_url_not_private("");
assert!(result.is_err(), "empty string should be rejected");
}
#[test]
fn cgn_100_63_not_rejected() {
assert!(validate_url_not_private("http://100.63.255.255").is_ok(),
"100.63.255.255 is outside CGN range and should be allowed");
}
#[test]
fn cgn_100_128_not_rejected() {
assert!(validate_url_not_private("http://100.128.0.1").is_ok(),
"100.128.0.1 is outside CGN range and should be allowed");
}
#[test]
fn private_172_15_not_rejected() {
assert!(validate_url_not_private("http://172.15.255.255").is_ok(),
"172.15.255.255 is outside private class B range and should be allowed");
}
#[test]
fn private_172_32_not_rejected() {
assert!(validate_url_not_private("http://172.32.0.1").is_ok(),
"172.32.0.1 is outside private class B range and should be allowed");
}
}
pub struct Egress {
pub url: String,
pub auth: Option<reqwest::header::HeaderValue>,
}
impl Egress {
pub fn proxied(&self) -> bool {
self.auth.is_some() || crate::proxy::proxy_server_of(&self.url).is_some()
}
}
pub async fn egress(url: &str) -> Egress {
match crate::proxy::proxied(url).await {
Some(via) => {
let auth = match crate::proxy::proxy_server_of(&via) {
Some(server) => crate::proxy::proxy_authorization(&server).await,
None => None,
};
Egress { url: via, auth }
}
None => Egress { url: url.to_string(), auth: None },
}
}
pub async fn proxied_request(client: &HttpClient, method: reqwest::Method, url: &str) -> Req {
let e = egress(url).await;
match e.auth {
Some(v) => client.with_lane(Lane::Account).request(method, &e.url).header(reqwest::header::AUTHORIZATION, v),
None => client.request(method, &e.url),
}
}
pub async fn remote_status(url: &str, timeout: std::time::Duration) -> Option<u16> {
let e = egress(url).await;
let client = build_http_client(timeout).ok()?;
let client = if e.auth.is_some() { client.with_lane(Lane::Account) } else { client };
let with = |req: Req| match &e.auth {
Some(v) => req.header(reqwest::header::AUTHORIZATION, v.clone()),
None => req,
};
if e.proxied() {
let resp = with(client.get(&e.url)).header(reqwest::header::RANGE, "bytes=0-0").send().await.ok()?;
if resp.status().is_success() {
return Some(200);
}
let body: serde_json::Value = resp.json().await.ok()?;
return body.get("source_status").and_then(|v| v.as_u64()).map(|v| v as u16);
}
let head = with(client.head(&e.url)).send().await.ok()?;
let status = head.status();
if status == reqwest::StatusCode::METHOD_NOT_ALLOWED || status == reqwest::StatusCode::NOT_IMPLEMENTED {
let r = with(client.get(&e.url)).header(reqwest::header::RANGE, "bytes=0-0").send().await.ok()?;
return Some(r.status().as_u16());
}
Some(status.as_u16())
}
pub async fn get_remote_file_size(url: &str) -> Option<u64> {
validate_url_not_private(url).ok()?;
let client = build_http_client(std::time::Duration::from_secs(8)).ok()?;
if let Ok(head_res) = proxied_request(&client, reqwest::Method::HEAD, url).await.send().await {
if let Some(length) = head_res.content_length() {
if length > 0 {
return Some(length);
}
}
}
if let Ok(partial_res) = proxied_request(&client, reqwest::Method::GET, url)
.await
.header("Range", "bytes=0-1")
.send()
.await
{
if let Some(content_range) = partial_res.headers().get("content-range") {
if let Ok(range_str) = content_range.to_str() {
if let Some(size_part) = range_str.split('/').nth(1) {
if let Ok(size) = size_part.parse::<u64>() {
return Some(size);
}
}
}
}
if let Some(length) = partial_res.content_length() {
if length > 100 {
return Some(length);
}
}
}
None
}
#[allow(clippy::unnecessary_cast)] pub fn raise_fd_limit() -> Option<u64> {
#[cfg(not(unix))]
{
None
}
#[cfg(unix)]
{
#[cfg(target_os = "macos")]
const CEILING: libc::rlim_t = 10240;
#[cfg(not(target_os = "macos"))]
const CEILING: libc::rlim_t = 1 << 20;
let mut lim = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut lim) } != 0 {
return None;
}
let target = lim.rlim_max.min(CEILING);
if target <= lim.rlim_cur {
return None;
}
lim.rlim_cur = target;
if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &lim) } != 0 {
return None;
}
Some(target as u64)
}
}
#[cfg(all(test, unix))]
mod fd_limit_tests {
#[test]
fn soft_limit_reaches_the_ceiling_and_is_never_lowered() {
let mut before = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
assert_eq!(unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut before) }, 0);
super::raise_fd_limit();
let mut after = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
assert_eq!(unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut after) }, 0);
#[cfg(target_os = "macos")]
let ceiling: libc::rlim_t = 10240;
#[cfg(not(target_os = "macos"))]
let ceiling: libc::rlim_t = 1 << 20;
assert!(after.rlim_cur >= before.rlim_cur);
assert!(after.rlim_cur >= after.rlim_max.min(ceiling));
}
}
#[cfg(test)]
mod alias_tests {
use super::aliased;
#[test]
fn a_host_and_its_subdomains_reach_the_alias_and_nothing_else_does() {
let onion = reqwest::Url::parse("http://abc.onion").unwrap();
let aliases = [("magnitude.vectorapp.io".to_string(), onion.clone()), ("magnitude.jskitty.com".to_string(), onion)];
let at = |u: &str| aliased(&reqwest::Url::parse(u).unwrap(), &aliases).map(|u| u.to_string());
assert_eq!(at("https://magnitude.vectorapp.io/ab.png").as_deref(), Some("http://abc.onion/ab.png"));
assert_eq!(at("https://us.magnitude.jskitty.com/proxy?url=x%2Fy").as_deref(), Some("http://abc.onion/proxy?url=x%2Fy"));
assert_eq!(at("https://evilmagnitude.vectorapp.io/ab.png"), None);
assert_eq!(at("https://magnitude.vectorapp.io.evil.com/ab.png"), None);
assert_eq!(at("https://blossom.primal.net/ab.png"), None);
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tls_tests {
use super::*;
#[test]
fn clients_build_on_ring_with_h2_offered_first() {
let cfg = tls_config();
assert_eq!(cfg.alpn_protocols, [b"h2".to_vec(), b"http/1.1".to_vec()]);
let opts = ClientOptions { timeout: None, read_timeout: None, follow_redirects: true, no_direct: false };
build_pooled(opts, None).expect("a pooled client builds");
template();
}
}
#[cfg(test)]
mod user_agent_tests {
use super::*;
#[test]
fn the_client_names_itself_as_vector() {
assert!(user_agent().starts_with("Vector/"), "{}", user_agent());
set_app_version("9.9.9-test");
assert!(user_agent().starts_with("Vector/"), "{}", user_agent());
}
}