//! Bounded description-only HTTP and decoding. Portal requests retain their own allow-list.
use crate::{
Result,
cache::{Cache, url_key},
net::USER_AGENT,
};
use image::{AnimationDecoder, ImageDecoder, ImageFormat, ImageReader, RgbaImage};
use std::{
io::{Cursor, Read},
path::PathBuf,
time::{Duration, Instant},
};
pub const FETCH_TIMEOUT: Duration = Duration::from_secs(15);
pub const FRAME_CAP: usize = 500;
pub const MEMORY_CAP: usize = 64 * 1024 * 1024;
pub const APP_MEMORY_CAP: usize = 256 * 1024 * 1024;
pub fn validate_url(url: &str) -> Result<()> {
if !crate::markdown::browser_url(url) || !url.starts_with("https://") {
return Err("Description images require an HTTPS DNS URL".into());
}
let rest = &url[8..];
let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
let host = if let Some((host, port)) = authority.split_once(':') {
if port != "443" {
return Err("Description image port must be 443".into());
}
host
} else {
authority
};
let host = host.strip_suffix('.').unwrap_or(host).to_ascii_lowercase();
let labels: Vec<_> = host.split('.').collect();
if labels.len() < 2
|| host.len() > 253
|| labels.iter().any(|l| {
l.is_empty()
|| l.len() > 63
|| l.starts_with('-')
|| l.ends_with('-')
|| !l.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-')
})
|| labels
.last()
.is_none_or(|l| l.bytes().all(|b| b.is_ascii_digit()) || l.starts_with("0x"))
|| ["localhost", "local", "internal"]
.iter()
.any(|suffix| host == *suffix || host.ends_with(&format!(".{suffix}")))
{
return Err("Description image host must be a public DNS name".into());
}
// Decode parameter names so escaping cannot sneak credential fields into a request.
for parameter in rest.split(['?', '&']).skip(1) {
let name = parameter.split('=').next().unwrap_or_default();
let mut decoded = String::new();
let mut chars = name.bytes();
while let Some(b) = chars.next() {
if b == b'%' {
let a = chars.next().and_then(|b| (b as char).to_digit(16));
let b = chars.next().and_then(|b| (b as char).to_digit(16));
if let (Some(a), Some(b)) = (a, b) {
decoded.push((a * 16 + b) as u8 as char);
}
} else {
decoded.push(b as char);
}
}
if matches!(
decoded.to_ascii_lowercase().as_str(),
"username" | "token" | "password"
) {
return Err("Credential parameters are forbidden".into());
}
}
Ok(())
}
pub struct Response {
pub status: u16,
pub length: Option<u64>,
pub location: Option<String>,
pub reader: Box<dyn Read + Send>,
}
pub trait Transport: Send + Sync {
fn get(&self, url: &str, remaining: Duration) -> Result<Response>;
}
#[derive(Default)]
pub struct Http;
impl Transport for Http {
fn get(&self, url: &str, remaining: Duration) -> Result<Response> {
validate_url(url)?;
// A dedicated agent has no cookie/auth state, automatic redirects or environment proxy.
let agent: ureq::Agent = ureq::Agent::config_builder()
.proxy(None)
.max_redirects(0)
.user_agent(USER_AGENT)
.timeout_connect(Some(Duration::from_secs(10)))
.timeout_global(Some(remaining))
.http_status_as_error(false)
.build()
.into();
let response = agent.get(url).call()?;
let headers = response.headers();
Ok(Response {
status: response.status().as_u16(),
length: headers
.get("content-length")
.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse().ok()),
location: headers
.get("location")
.and_then(|h| h.to_str().ok())
.map(str::to_owned),
reader: Box::new(response.into_body().into_reader()),
})
}
}
fn resolve(base: &str, location: &str) -> Result<String> {
if location
.chars()
.any(|c| c.is_whitespace() || c.is_control() || c == '\\')
{
return Err("Invalid image redirect".into());
}
if location.contains("://") {
return Ok(location.into());
}
if location.starts_with("//") {
return Ok(format!("https:{location}"));
}
let uri: ureq::http::Uri = base.parse()?;
let origin = format!(
"https://{}",
uri.authority().ok_or("Missing redirect authority")?
);
if location.starts_with('?') {
return Ok(format!("{origin}{}{location}", uri.path()));
}
if location.starts_with('#') {
return Ok(format!("{}{location}", base.split('#').next().unwrap()));
}
// A colon in a first relative segment denotes a scheme, never a relative path.
if location.split('/').next().is_some_and(|s| s.contains(':')) {
return Err("Invalid redirect scheme".into());
}
let path = if location.starts_with('/') {
location.to_owned()
} else {
format!(
"{}{location}",
uri.path()
.rsplit_once('/')
.map_or("/", |(s, _)| &uri.path()[..s.len() + 1])
)
};
let (path, suffix) = path
.find(['?', '#'])
.map_or((path.as_str(), ""), |i| (&path[..i], &path[i..]));
let mut parts = Vec::new();
for part in path.split('/') {
match part {
"." => (),
".." => {
parts.pop();
}
"" if parts.is_empty() => (),
_ => parts.push(part),
}
}
Ok(format!("{origin}/{}{suffix}", parts.join("/")))
}
pub fn extension(bytes: &[u8]) -> Option<&'static str> {
match image::guess_format(bytes).ok()? {
ImageFormat::Png => Some("png"),
ImageFormat::Jpeg => Some("jpg"),
ImageFormat::Gif => Some("gif"),
ImageFormat::WebP => Some("webp"),
_ => None,
}
}
fn folder(url: &str) -> &'static str {
if url.starts_with("https://assets-mod.factorio.com/") {
"portal-thumbs"
} else {
"description-images"
}
}
pub fn cached_path(cache: &Cache, url: &str) -> Result<PathBuf> {
validate_url(url)?;
for ext in ["png", "jpg", "gif", "webp"] {
let path = cache.path(&format!("{}/{}.{ext}", folder(url), url_key(url)))?;
if path.is_file() {
return Ok(path);
}
}
cache.path(&format!("{}/{}.png", folder(url), url_key(url)))
}
/// No download size cap. The injected clock and production reader enforce the total deadline.
pub fn fetch_with_clock(
transport: &impl Transport,
cache: &Cache,
url: &str,
elapsed: impl Fn() -> Duration,
) -> Result<PathBuf> {
validate_url(url)?;
let path = cached_path(cache, url)?;
if path.is_file() {
return Ok(path);
}
let remaining = || {
FETCH_TIMEOUT
.checked_sub(elapsed())
.filter(|d| !d.is_zero())
.ok_or_else(|| crate::Error::from("Description image timed out"))
};
let mut current = url.to_owned();
for hop in 0..=3 {
validate_url(¤t)?;
let mut response = transport.get(current.split('#').next().unwrap(), remaining()?)?;
remaining()?;
if matches!(response.status, 301 | 302 | 303 | 307 | 308) {
if hop == 3 {
return Err("Too many description image redirects".into());
}
current = resolve(
¤t,
response
.location
.as_deref()
.ok_or("Missing redirect location")?,
)?;
continue;
}
if !(200..300).contains(&response.status) {
return Err(format!("Image HTTP {}", response.status).into());
}
let mut bytes = Vec::new();
let mut chunk = [0; 32 * 1024];
loop {
remaining()?;
let n = response.reader.read(&mut chunk)?;
remaining()?;
if n == 0 {
break;
}
bytes.extend_from_slice(&chunk[..n]);
}
let ext = extension(&bytes).ok_or("Description image is not PNG, JPEG, GIF or WebP")?;
// Validate before caching: broken files and oversized dimensions leave no disk entry.
decode(&bytes)?;
remaining()?;
return cache.write(&format!("{}/{}.{ext}", folder(url), url_key(url)), &bytes);
}
unreachable!()
}
pub struct Decoded {
pub frames: Vec<RgbaImage>,
pub delays: Vec<i32>,
}
impl Decoded {
pub fn bytes(&self) -> usize {
self.frames.iter().map(|f| f.as_raw().len()).sum()
}
}
fn dimensions(w: u32, h: u32) -> Result<()> {
if w == 0 || h == 0 || w > 4096 || h > 4096 || u64::from(w) * u64::from(h) > 16_000_000 {
return Err("Description image exceeds 4096 px or 16 megapixels".into());
}
Ok(())
}
pub fn decode(bytes: &[u8]) -> Result<Decoded> {
decode_capped(bytes, FRAME_CAP, MEMORY_CAP)
}
fn decode_capped(bytes: &[u8], frame_cap: usize, memory_cap: usize) -> Result<Decoded> {
let format = image::guess_format(bytes)?;
extension(bytes).ok_or("Unsupported description image format")?;
let reader = ImageReader::with_format(Cursor::new(bytes), format);
let (w, h) = reader.into_dimensions()?;
dimensions(w, h)?;
let mut limits = image::Limits::default();
limits.max_image_width = Some(4096);
limits.max_image_height = Some(4096);
// Decoder disposal/compositing scratch is separate from retained RGBA frames.
limits.max_alloc = Some((MEMORY_CAP * 4) as u64);
let cursor = Cursor::new(bytes);
let frames = match format {
ImageFormat::Gif => {
let mut d = image::codecs::gif::GifDecoder::new(cursor)?;
d.set_limits(limits.clone())?;
Some(d.into_frames())
}
ImageFormat::WebP => {
let mut d = image::codecs::webp::WebPDecoder::new(cursor)?;
d.set_limits(limits.clone())?;
Some(d.into_frames())
}
ImageFormat::Png => {
let mut d = image::codecs::png::PngDecoder::new(cursor)?;
d.set_limits(limits.clone())?;
if d.is_apng()? {
Some(d.apng()?.into_frames())
} else {
None
}
}
_ => None,
};
if let Some(frames) = frames {
let mut result = Decoded {
frames: Vec::new(),
delays: Vec::new(),
};
let frame_bytes = w as usize * h as usize * 4;
for frame in frames {
// Probe one more frame before deciding the image is over the cap.
let frame = frame?;
if (result.frames.len() == frame_cap
|| (result.frames.len() + 1) * frame_bytes > memory_cap)
&& !result.frames.is_empty()
{
result.frames.truncate(1);
result.delays.truncate(1);
return Ok(result);
}
let (num, den) = frame.delay().numer_denom_ms();
let delay = if u64::from(num) <= u64::from(den) * 10 {
100
} else {
num.div_ceil(den).min(i32::MAX as u32) as i32
};
let buffer = frame.into_buffer();
dimensions(buffer.width(), buffer.height())?;
result.frames.push(buffer);
result.delays.push(delay);
}
if !result.frames.is_empty() {
return Ok(result);
}
}
let mut reader = ImageReader::with_format(Cursor::new(bytes), format);
reader.limits(limits);
Ok(Decoded {
frames: vec![reader.decode()?.into_rgba8()],
delays: vec![100],
})
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Position {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
pub fn layout(sizes: &[(f32, f32)], width: f32) -> (Vec<Position>, f32) {
let width = width.max(1.0);
let (mut x, mut y, mut line_height) = (0.0, 0.0, 0.0f32);
let mut positions: Vec<Position> = Vec::new();
let mut line_start = 0;
for &(w, h) in sizes {
let scale = (width / w.max(1.0)).min(1.0);
let (w, h) = (w * scale, h * scale);
if x > 0.0 && x + w > width {
for position in &mut positions[line_start..] {
position.y += line_height - position.height;
}
line_start = positions.len();
y += line_height + 4.0;
x = 0.0;
line_height = 0.0;
}
positions.push(Position {
x,
y,
width: w,
height: h,
});
x += w + 4.0;
line_height = line_height.max(h);
}
for position in &mut positions[line_start..] {
position.y += line_height - position.height;
}
(positions, y + line_height)
}
pub fn fetch(transport: &impl Transport, cache: &Cache, url: &str) -> Result<PathBuf> {
let start = Instant::now();
fetch_with_clock(transport, cache, url, || start.elapsed())
}
#[cfg(test)]
mod tests {
use super::*;
use image::{Delay, Frame, Rgba, codecs::gif::GifEncoder};
fn gif(count: usize) -> Vec<u8> {
let mut bytes = Vec::new();
{
let mut encoder = GifEncoder::new(&mut bytes);
for i in 0..count {
encoder
.encode_frame(Frame::from_parts(
RgbaImage::from_pixel(2, 2, Rgba([i as u8, 0, 0, 255])),
0,
0,
Delay::from_numer_denom_ms(30, 1),
))
.unwrap();
}
}
bytes
}
#[test]
fn frame_and_decoded_memory_caps_keep_first_frame() {
for (count, frames, memory) in [(501, FRAME_CAP, MEMORY_CAP), (3, 500, 32)] {
let decoded = decode_capped(&gif(count), frames, memory).unwrap();
assert_eq!(decoded.frames.len(), 1);
assert_eq!(decoded.frames[0].get_pixel(0, 0).0, [0, 0, 0, 255]);
}
assert_eq!(decode(&gif(500)).unwrap().frames.len(), 500);
}
#[test]
fn gif_transparency_and_background_disposal_are_composited() {
let mut bytes = Vec::new();
{
let mut encoder = GifEncoder::new(&mut bytes);
let red = RgbaImage::from_pixel(2, 2, Rgba([255, 0, 0, 255]));
let mut blue = RgbaImage::new(2, 2);
blue.put_pixel(0, 0, Rgba([0, 0, 255, 255]));
let mut green = RgbaImage::new(2, 2);
green.put_pixel(1, 1, Rgba([0, 255, 0, 255]));
for f in [red, blue, green] {
encoder.encode_frame(Frame::new(f)).unwrap();
}
}
// Set frame zero to Keep; frame one remains Background as encoded by image.
let gce = bytes.windows(3).position(|s| s == [0x21, 0xf9, 4]).unwrap();
bytes[gce + 3] = (bytes[gce + 3] & !0x1c) | 4;
let decoded = decode(&bytes).unwrap();
assert_eq!(decoded.frames[1].get_pixel(0, 0).0, [0, 0, 255, 255]);
assert_eq!(decoded.frames[1].get_pixel(1, 1).0, [255, 0, 0, 255]);
assert_eq!(decoded.frames[2].get_pixel(0, 0).0[3], 0);
assert_eq!(decoded.frames[2].get_pixel(1, 1).0, [0, 255, 0, 255]);
}
#[test]
fn relative_redirects_follow_uri_path_rules() {
for (location, expected) in [
("../c.png", "https://example.com/c.png"),
("/new", "https://example.com/new"),
("?new=1", "https://example.com/a/b?new=1"),
("//other.example/a", "https://other.example/a"),
] {
assert_eq!(
resolve("https://example.com/a/b", location).unwrap(),
expected
);
}
}
}