//! The sole HTTP boundary. Redirects are refused so they cannot escape the allowlist.
use crate::Result;
use std::io::Read;
use std::time::Duration;
/// An owned, unbuffered response. HTTP failures are distinct from retryable IO failures.
pub struct StreamResponse {
pub status: u16,
pub total: Option<u64>,
pub reader: Box<dyn Read + Send>,
}
#[derive(Debug)]
pub struct StreamFailure {
pub retryable: bool,
error: crate::Error,
}
impl StreamFailure {
pub fn network(error: impl Into<crate::Error>) -> Self {
Self {
retryable: true,
error: error.into(),
}
}
pub fn rejected(error: impl Into<crate::Error>) -> Self {
Self {
retryable: false,
error: error.into(),
}
}
}
impl std::fmt::Display for StreamFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.error.fmt(f)
}
}
impl std::error::Error for StreamFailure {}
pub trait Stream: Send + Sync {
fn get(&self, url: &str) -> std::result::Result<StreamResponse, StreamFailure>;
}
pub const USER_AGENT: &str =
"FactorioModManager/0.1 (personal read-only mod browser; werry/factoriomodmanager)";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Method {
Get,
Post,
}
pub trait Fetch: Send + Sync {
fn fetch(&self, method: Method, url: &str, body: &[u8]) -> Result<Vec<u8>>;
}
pub fn validate_url(url: &str) -> Result<()> {
let rest = url
.strip_prefix("https://")
.ok_or("Only HTTPS requests are allowed")?;
let (host, path) = rest.split_once('/').unwrap_or((rest, ""));
if !matches!(
host,
"mods.factorio.com" | "assets-mod.factorio.com" | "mods-storage.re146.dev" | "re146.dev"
) || url.contains(['\\', '\r', '\n', '#'])
|| (host == "re146.dev" && path.split('?').next() != Some("factorio/mods/modinfo"))
{
return Err("Network host/path is not allowed".into());
}
// Credentials are never sent, including token query parameters accidentally copied from a URL.
if path.split(['?', '&']).any(|part| {
matches!(
part.split('=').next(),
Some("username" | "token" | "password")
)
}) {
return Err("Credential parameters are forbidden".into());
}
Ok(())
}
#[derive(Clone)]
pub struct Http {
agent: ureq::Agent,
}
impl Default for Http {
fn default() -> Self {
Self {
agent: ureq::Agent::config_builder()
.timeout_connect(Some(Duration::from_secs(10)))
.timeout_recv_response(Some(Duration::from_secs(60)))
.timeout_recv_body(Some(Duration::from_secs(60)))
.timeout_global(Some(Duration::from_secs(75)))
.max_redirects(0)
.user_agent(USER_AGENT)
.build()
.into(),
}
}
}
impl Fetch for Http {
fn fetch(&self, method: Method, url: &str, body: &[u8]) -> Result<Vec<u8>> {
request(&self.agent, method, url, body)
}
}
impl Stream for Http {
fn get(&self, url: &str) -> std::result::Result<StreamResponse, StreamFailure> {
validate_url(url).map_err(StreamFailure::rejected)?;
let response = self
.agent
.get(url)
.config()
.http_status_as_error(false)
.build()
.call()
.map_err(|error| {
// Decision 2: policy, TLS configuration and redirects are not network retries.
if matches!(
&error,
ureq::Error::Io(_)
| ureq::Error::Timeout(_)
| ureq::Error::HostNotFound
| ureq::Error::ConnectionFailed
| ureq::Error::Protocol(_)
) {
StreamFailure::network(error)
} else {
StreamFailure::rejected(error)
}
})?;
let status = response.status().as_u16();
let total = response
.headers()
.get("content-length")
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse().ok());
Ok(StreamResponse {
status,
total,
reader: Box::new(response.into_body().into_reader()),
})
}
}
fn request(agent: &ureq::Agent, method: Method, url: &str, body: &[u8]) -> Result<Vec<u8>> {
validate_url(url)?;
let mut response = match method {
Method::Get => agent.get(url).call()?,
Method::Post => {
if !url.starts_with("https://mods.factorio.com/api/mods?")
&& url != "https://mods.factorio.com/api/search"
{
return Err("POST is limited to read-only portal queries".into());
}
agent
.post(url)
.header("Content-Type", "application/json")
.send(body)?
}
};
if !response.status().is_success() {
return Err(format!("HTTP {}", response.status()).into());
}
Ok(response
.body_mut()
.with_config()
.limit(32 * 1024 * 1024)
.read_to_vec()?)
}