host: self-hosted Google Fonts (gfonts) and the font cache (fonts)
gfonts (pure): CSS2 API URLs, the fonts.googleapis.com/fonts.gstatic.com allowlist, CSS parsing and face selection, woff2 checks, the sha256 manifest cache format and the page script, whose origin guard and runtime global now come from the SiteSpec. fonts (net feature): the download client (allowlist re-checked per request, no redirects, size caps), the family/preview caches and install/remove/faces/preview for the app's shell-only commands. Tests and CSS fixtures moved with them.
This commit is contained in:
parent
caef87775f
commit
7307bbed22
6 changed files with 1176 additions and 0 deletions
750
crates/waybetter-host/src/gfonts.rs
Normal file
750
crates/waybetter-host/src/gfonts.rs
Normal file
|
|
@ -0,0 +1,750 @@
|
|||
//! Google Fonts, self-hosted: a site's CSP usually blocks remote `@font-face` fetches, so the app
|
||||
//! downloads woff2 files natively (`fonts.googleapis.com`/`fonts.gstatic.com` only) and hands the
|
||||
//! raw bytes to site pages as `FontFace(ArrayBuffer)`.
|
||||
//!
|
||||
//! Pure module (no network, no Tauri): CSS parsing, URL/byte validation, the on-disk cache format
|
||||
//! and the page script. [`crate::fonts`] (`net` feature) downloads and caches with it.
|
||||
|
||||
use crate::site::SiteSpec;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::path::{Path, PathBuf};
|
||||
use url::Url;
|
||||
|
||||
pub const CSS_ORIGIN: &str = "https://fonts.googleapis.com";
|
||||
pub const FILE_HOST: &str = "fonts.gstatic.com";
|
||||
pub const MAX_CSS_BYTES: usize = 256 * 1024;
|
||||
pub const MAX_FILE_BYTES: usize = 1_500_000;
|
||||
pub const MAX_FAMILY_BYTES: usize = 4_000_000;
|
||||
pub const MAX_CACHE_BYTES: usize = 80_000_000;
|
||||
pub const MAX_FAMILIES: usize = 64;
|
||||
pub const MAX_PREVIEW_TEXT: usize = 120;
|
||||
pub const MAX_PREVIEW_BYTES: usize = 300_000;
|
||||
pub const KEEP_SUBSETS: [&str; 2] = ["latin", "latin-ext"];
|
||||
pub const FONTS_DIR: &str = "fonts";
|
||||
pub const MANIFEST: &str = "manifest.json";
|
||||
/// Google only serves woff2 to modern desktop browsers; a stale/unknown UA gets ttf/eot instead.
|
||||
pub const USER_AGENT: &str =
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36";
|
||||
|
||||
/// Google family names: letters, digits and single interior spaces (`"Noto Sans JP"`, `"Lato"`).
|
||||
pub fn valid_family(name: &str) -> bool {
|
||||
if name.is_empty() || name.len() > 48 {
|
||||
return false;
|
||||
}
|
||||
let bytes = name.as_bytes();
|
||||
if bytes[0] == b' ' || bytes[bytes.len() - 1] == b' ' {
|
||||
return false;
|
||||
}
|
||||
if name.contains(" ") {
|
||||
return false;
|
||||
}
|
||||
bytes.iter().all(|b| b.is_ascii_alphanumeric() || *b == b' ')
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", tag = "kind")]
|
||||
pub enum Weights {
|
||||
Range { min: u16, max: u16 },
|
||||
List { values: Vec<u16> },
|
||||
}
|
||||
|
||||
impl Weights {
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
match self {
|
||||
Weights::Range { min, max } => {
|
||||
if !(100..=1000).contains(min) || !(100..=1000).contains(max) || min > max {
|
||||
return Err("VALIDATION: invalid weight range".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Weights::List { values } => {
|
||||
if values.is_empty() || values.len() > 9 {
|
||||
return Err("VALIDATION: 1-9 weights".into());
|
||||
}
|
||||
if !values.iter().all(|w| (100..=1000).contains(w)) {
|
||||
return Err("VALIDATION: invalid weight".into());
|
||||
}
|
||||
let mut sorted = values.clone();
|
||||
sorted.sort_unstable();
|
||||
sorted.dedup();
|
||||
if sorted.len() != values.len() {
|
||||
return Err("VALIDATION: duplicate weight".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The css2 `wght@...` axis query, e.g. `"wght@100..900"` or `"wght@400;700"`.
|
||||
pub fn query(&self) -> String {
|
||||
match self {
|
||||
Weights::Range { min, max } => format!("wght@{min}..{max}"),
|
||||
Weights::List { values } => {
|
||||
let mut sorted = values.clone();
|
||||
sorted.sort_unstable();
|
||||
let parts: Vec<String> = sorted.iter().map(|w| w.to_string()).collect();
|
||||
format!("wght@{}", parts.join(";"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds `https://fonts.googleapis.com/css2?family=<family>:<weights>&display=swap[&text=...]`.
|
||||
pub fn css_url(family: &str, weights: &Weights, text: Option<&str>) -> Result<Url, String> {
|
||||
if !valid_family(family) {
|
||||
return Err("VALIDATION: invalid family name".into());
|
||||
}
|
||||
weights.validate()?;
|
||||
if let Some(text) = text {
|
||||
let chars = text.chars().count();
|
||||
if chars == 0 || chars > MAX_PREVIEW_TEXT {
|
||||
return Err("VALIDATION: preview text length".into());
|
||||
}
|
||||
if text.chars().any(|c| c.is_control()) {
|
||||
return Err("VALIDATION: preview text contains control characters".into());
|
||||
}
|
||||
}
|
||||
let mut url = Url::parse(&format!("{CSS_ORIGIN}/css2")).map_err(|_| "VALIDATION: unreachable")?;
|
||||
{
|
||||
let mut pairs = url.query_pairs_mut();
|
||||
pairs.append_pair("family", &format!("{family}:{}", weights.query()));
|
||||
pairs.append_pair("display", "swap");
|
||||
if let Some(text) = text {
|
||||
pairs.append_pair("text", text);
|
||||
}
|
||||
}
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
/// `https://fonts.googleapis.com/css2?...` only, no userinfo, no explicit port.
|
||||
pub fn allowed_css_url(u: &Url) -> bool {
|
||||
u.scheme() == "https"
|
||||
&& u.host_str() == Some("fonts.googleapis.com")
|
||||
&& u.path() == "/css2"
|
||||
&& u.username().is_empty()
|
||||
&& u.password().is_none()
|
||||
&& u.port().is_none()
|
||||
}
|
||||
|
||||
/// `https://fonts.gstatic.com/s/...` (static) or `/l/font...` (`&text=` subset preview) only.
|
||||
pub fn allowed_file_url(u: &Url) -> bool {
|
||||
u.scheme() == "https"
|
||||
&& u.host_str() == Some(FILE_HOST)
|
||||
&& u.username().is_empty()
|
||||
&& u.password().is_none()
|
||||
&& u.port().is_none()
|
||||
&& (u.path().starts_with("/s/") || u.path().starts_with("/l/font"))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Face {
|
||||
pub subset: Option<String>,
|
||||
pub style: String,
|
||||
pub weight: (u16, u16),
|
||||
pub url: String,
|
||||
pub unicode_range: Option<String>,
|
||||
}
|
||||
|
||||
fn slice_between<'a>(text: &'a str, start: &str, terminators: &[char]) -> Option<&'a str> {
|
||||
let idx = text.find(start)? + start.len();
|
||||
let rest = &text[idx..];
|
||||
let end = rest.find(|c| terminators.contains(&c)).unwrap_or(rest.len());
|
||||
Some(rest[..end].trim())
|
||||
}
|
||||
|
||||
/// Parses `font-weight: 400;` or `font-weight: 100 900;` into `(min, max)`.
|
||||
fn parse_weight(value: &str) -> Option<(u16, u16)> {
|
||||
let mut parts = value.split_whitespace();
|
||||
let a: u16 = parts.next()?.parse().ok()?;
|
||||
let b: u16 = match parts.next() {
|
||||
Some(s) => s.parse().ok()?,
|
||||
None => a,
|
||||
};
|
||||
if parts.next().is_some() {
|
||||
return None;
|
||||
}
|
||||
if !(1..=1000).contains(&a) || !(1..=1000).contains(&b) {
|
||||
return None;
|
||||
}
|
||||
Some((a.min(b), a.max(b)))
|
||||
}
|
||||
|
||||
/// Extracts the first `url(...) format('woff2')` from a `src:` declaration value.
|
||||
fn woff2_url(src: &str) -> Option<String> {
|
||||
let mut rest = src;
|
||||
loop {
|
||||
let url_idx = rest.find("url(")?;
|
||||
let after_url = &rest[url_idx + 4..];
|
||||
let close = after_url.find(')')?;
|
||||
let raw = after_url[..close].trim();
|
||||
let raw = raw.trim_matches(|c| c == '\'' || c == '"');
|
||||
let after_close = &after_url[close + 1..];
|
||||
let format_ok = after_close
|
||||
.find("format(")
|
||||
.map(|fi| {
|
||||
let fafter = &after_close[fi + 7..];
|
||||
let fclose = fafter.find(')').unwrap_or(0);
|
||||
let fmt = fafter[..fclose].trim().trim_matches(|c| c == '\'' || c == '"');
|
||||
fmt.eq_ignore_ascii_case("woff2")
|
||||
})
|
||||
.unwrap_or(false);
|
||||
// Stop scanning this declaration once we pass the next comma boundary (a src: list entry).
|
||||
let boundary = after_close.find(',').unwrap_or(after_close.len());
|
||||
if format_ok {
|
||||
return Some(raw.to_string());
|
||||
}
|
||||
rest = &after_close[boundary..];
|
||||
if !rest.contains("url(") {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Hand-written `@font-face` block parser (no regex crate): tracks the `/* subset */` comment
|
||||
/// that precedes each block, and pulls `font-style`, `font-weight`, `src` (woff2 only) and
|
||||
/// `unicode-range` out of it.
|
||||
pub fn parse_css(css: &str) -> Result<Vec<Face>, String> {
|
||||
let trimmed = css.trim_start();
|
||||
if trimmed.starts_with('<') {
|
||||
return Err("FONTS: unknown family".into());
|
||||
}
|
||||
let mut faces = Vec::new();
|
||||
let mut pos = 0usize;
|
||||
let mut pending_subset: Option<String> = None;
|
||||
while let Some(rel) = {
|
||||
let comment_rel = css[pos..].find("/*");
|
||||
let face_rel = css[pos..].find("@font-face");
|
||||
match (comment_rel, face_rel) {
|
||||
(Some(c), Some(f)) => Some(c.min(f)),
|
||||
(Some(c), None) => Some(c),
|
||||
(None, Some(f)) => Some(f),
|
||||
(None, None) => None,
|
||||
}
|
||||
} {
|
||||
let abs = pos + rel;
|
||||
if css[abs..].starts_with("/*") {
|
||||
let after = abs + 2;
|
||||
let Some(end_rel) = css[after..].find("*/") else { break };
|
||||
let comment = css[after..after + end_rel].trim();
|
||||
if !comment.is_empty() && comment.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') {
|
||||
pending_subset = Some(comment.to_string());
|
||||
}
|
||||
pos = after + end_rel + 2;
|
||||
continue;
|
||||
}
|
||||
// "@font-face"
|
||||
let Some(brace_rel) = css[abs..].find('{') else { break };
|
||||
let brace_abs = abs + brace_rel;
|
||||
let Some(close_rel) = css[brace_abs..].find('}') else { break };
|
||||
let block = &css[brace_abs + 1..brace_abs + close_rel];
|
||||
pos = brace_abs + close_rel + 1;
|
||||
|
||||
let style = slice_between(block, "font-style:", &[';']).unwrap_or("normal").to_string();
|
||||
let weight_raw = slice_between(block, "font-weight:", &[';']);
|
||||
let src_raw = slice_between(block, "src:", &[';']);
|
||||
let unicode_range = slice_between(block, "unicode-range:", &[';']).map(|s| s.to_string());
|
||||
|
||||
let (Some(weight_raw), Some(src_raw)) = (weight_raw, src_raw) else {
|
||||
pending_subset = None;
|
||||
continue;
|
||||
};
|
||||
let Some(weight) = parse_weight(weight_raw) else {
|
||||
pending_subset = None;
|
||||
continue;
|
||||
};
|
||||
let Some(url_str) = woff2_url(src_raw) else {
|
||||
pending_subset = None;
|
||||
continue;
|
||||
};
|
||||
let Ok(parsed_url) = Url::parse(&url_str) else {
|
||||
pending_subset = None;
|
||||
continue;
|
||||
};
|
||||
if !allowed_file_url(&parsed_url) {
|
||||
pending_subset = None;
|
||||
continue;
|
||||
}
|
||||
|
||||
faces.push(Face { subset: pending_subset.take(), style, weight, url: url_str, unicode_range });
|
||||
}
|
||||
if faces.is_empty() {
|
||||
return Err("FONTS: no usable font faces".into());
|
||||
}
|
||||
Ok(faces)
|
||||
}
|
||||
|
||||
/// Keeps normal-style faces; if any carry a subset label, keeps only `latin`/`latin-ext`
|
||||
/// (unlabeled faces, e.g. a `&text=` preview, are all kept). Capped at 24 faces.
|
||||
pub fn select_faces(faces: Vec<Face>) -> Vec<Face> {
|
||||
let normal: Vec<Face> = faces.into_iter().filter(|f| f.style == "normal").collect();
|
||||
let has_labels = normal.iter().any(|f| f.subset.is_some());
|
||||
let mut kept: Vec<Face> = if has_labels {
|
||||
normal.into_iter().filter(|f| f.subset.as_deref().is_some_and(|s| KEEP_SUBSETS.contains(&s))).collect()
|
||||
} else {
|
||||
normal
|
||||
};
|
||||
kept.truncate(24);
|
||||
kept
|
||||
}
|
||||
|
||||
/// WOFF2 header: signature(4) flavor(4) length(4) numTables(2) reserved(2) totalSfntSize(4)
|
||||
/// totalCompressedSize(4) majorVersion(2) minorVersion(2) metaOffset(4) metaLength(4)
|
||||
/// metaOrigLength(4) privOffset(4) privLength(4) = 48 bytes.
|
||||
pub fn is_woff2(bytes: &[u8]) -> bool {
|
||||
if bytes.len() < 48 || &bytes[0..4] != b"wOF2" {
|
||||
return false;
|
||||
}
|
||||
let length = u32::from_be_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]) as usize;
|
||||
length == bytes.len()
|
||||
}
|
||||
|
||||
pub fn slug(family: &str) -> String {
|
||||
family.to_lowercase().replace(' ', "-")
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct CachedFace {
|
||||
/// `"0.woff2"`, `"1.woff2"`, ...
|
||||
pub file: String,
|
||||
pub weight: [u16; 2],
|
||||
pub unicode_range: Option<String>,
|
||||
pub subset: Option<String>,
|
||||
pub bytes: usize,
|
||||
/// Lowercase hex.
|
||||
pub sha256: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Manifest {
|
||||
pub family: String,
|
||||
pub weights: Weights,
|
||||
pub faces: Vec<CachedFace>,
|
||||
pub bytes: usize,
|
||||
/// RFC3339.
|
||||
pub installed_at: String,
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
fn valid_face_file(file: &str) -> Option<usize> {
|
||||
let n = file.strip_suffix(".woff2")?;
|
||||
if n.is_empty() || !n.bytes().all(|b| b.is_ascii_digit()) || (n.len() > 1 && n.starts_with('0')) {
|
||||
return None;
|
||||
}
|
||||
let n: usize = n.parse().ok()?;
|
||||
if n >= 24 {
|
||||
return None;
|
||||
}
|
||||
Some(n)
|
||||
}
|
||||
|
||||
impl Manifest {
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
if !valid_family(&self.family) {
|
||||
return Err("VALIDATION: invalid family name".into());
|
||||
}
|
||||
self.weights.validate()?;
|
||||
if self.faces.is_empty() || self.faces.len() > 24 {
|
||||
return Err("VALIDATION: invalid face count".into());
|
||||
}
|
||||
let mut total = 0usize;
|
||||
for face in &self.faces {
|
||||
if valid_face_file(&face.file).is_none() {
|
||||
return Err("VALIDATION: invalid cached face file name".into());
|
||||
}
|
||||
if face.sha256.len() != 64 || !face.sha256.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
return Err("VALIDATION: invalid sha256".into());
|
||||
}
|
||||
if face.weight[0] > face.weight[1] || face.weight[0] < 1 || face.weight[1] > 1000 {
|
||||
return Err("VALIDATION: invalid cached face weight".into());
|
||||
}
|
||||
total += face.bytes;
|
||||
}
|
||||
if total != self.bytes || total > MAX_FAMILY_BYTES {
|
||||
return Err("VALIDATION: family size mismatch".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn sha256_hex(bytes: &[u8]) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
hasher.finalize().iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
/// Reads and verifies every cached face file under `dir` against `manifest`; returns their bytes
|
||||
/// in manifest order. Any size/hash/magic mismatch is treated as a damaged cache, not a partial one.
|
||||
pub fn verify_cached(dir: &Path, manifest: &Manifest) -> Result<Vec<Vec<u8>>, String> {
|
||||
manifest.validate()?;
|
||||
let mut out = Vec::with_capacity(manifest.faces.len());
|
||||
for face in &manifest.faces {
|
||||
let bytes = std::fs::read(dir.join(&face.file)).map_err(|_| "FONTS: cache damaged")?;
|
||||
if bytes.len() != face.bytes || !is_woff2(&bytes) || sha256_hex(&bytes) != face.sha256 {
|
||||
return Err("FONTS: cache damaged".into());
|
||||
}
|
||||
out.push(bytes);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// `fonts_root/<slug(family)>`, or `None` when `family` is not a valid Google Fonts name.
|
||||
pub fn family_dir(fonts_root: &Path, family: &str) -> Option<PathBuf> {
|
||||
if !valid_family(family) {
|
||||
return None;
|
||||
}
|
||||
Some(fonts_root.join(slug(family)))
|
||||
}
|
||||
|
||||
pub fn read_manifest(fonts_root: &Path, family: &str) -> Option<Manifest> {
|
||||
let dir = family_dir(fonts_root, family)?;
|
||||
let text = std::fs::read_to_string(dir.join(MANIFEST)).ok()?;
|
||||
let manifest: Manifest = serde_json::from_str(&text).ok()?;
|
||||
manifest.validate().ok()?;
|
||||
if manifest.family != family {
|
||||
return None;
|
||||
}
|
||||
Some(manifest)
|
||||
}
|
||||
|
||||
pub fn list_installed(fonts_root: &Path) -> Vec<Manifest> {
|
||||
let Ok(entries) = std::fs::read_dir(fonts_root) else { return Vec::new() };
|
||||
let mut out = Vec::new();
|
||||
for entry in entries.flatten() {
|
||||
if out.len() >= MAX_FAMILIES {
|
||||
break;
|
||||
}
|
||||
let Ok(file_type) = entry.file_type() else { continue };
|
||||
if !file_type.is_dir() {
|
||||
continue;
|
||||
}
|
||||
let text = std::fs::read_to_string(entry.path().join(MANIFEST)).ok();
|
||||
let Some(text) = text else { continue };
|
||||
let Ok(manifest) = serde_json::from_str::<Manifest>(&text) else { continue };
|
||||
if manifest.validate().is_err() {
|
||||
continue;
|
||||
}
|
||||
out.push(manifest);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn cache_bytes(fonts_root: &Path) -> usize {
|
||||
list_installed(fonts_root).iter().map(|m| m.bytes).sum()
|
||||
}
|
||||
|
||||
/// Validates every buffer, writes them into a fresh temp sibling directory, then swaps it in for
|
||||
/// the family directory (best-effort atomic: remove old, rename temp into place).
|
||||
pub fn write_family(
|
||||
fonts_root: &Path,
|
||||
family: &str,
|
||||
weights: &Weights,
|
||||
faces: &[(Face, Vec<u8>)],
|
||||
installed_at: String,
|
||||
) -> Result<Manifest, String> {
|
||||
if !valid_family(family) {
|
||||
return Err("VALIDATION: invalid family name".into());
|
||||
}
|
||||
weights.validate()?;
|
||||
if faces.is_empty() || faces.len() > 24 {
|
||||
return Err("VALIDATION: invalid face count".into());
|
||||
}
|
||||
let mut total = 0usize;
|
||||
for (_, bytes) in faces {
|
||||
if bytes.len() > MAX_FILE_BYTES || !is_woff2(bytes) {
|
||||
return Err("FONTS: invalid font file".into());
|
||||
}
|
||||
total += bytes.len();
|
||||
}
|
||||
if total > MAX_FAMILY_BYTES {
|
||||
return Err("FONTS: family too large".into());
|
||||
}
|
||||
|
||||
let final_dir = family_dir(fonts_root, family).ok_or("VALIDATION: invalid family name")?;
|
||||
let unique = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0);
|
||||
let tmp_dir = fonts_root.join(format!(".tmp-{}-{}-{unique}", slug(family), std::process::id()));
|
||||
std::fs::create_dir_all(&tmp_dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||
|
||||
let mut cached_faces = Vec::with_capacity(faces.len());
|
||||
for (index, (face, bytes)) in faces.iter().enumerate() {
|
||||
let file = format!("{index}.woff2");
|
||||
std::fs::write(tmp_dir.join(&file), bytes).map_err(|error| format!("STORAGE: {error}"))?;
|
||||
cached_faces.push(CachedFace {
|
||||
file,
|
||||
weight: [face.weight.0, face.weight.1],
|
||||
unicode_range: face.unicode_range.clone(),
|
||||
subset: face.subset.clone(),
|
||||
bytes: bytes.len(),
|
||||
sha256: sha256_hex(bytes),
|
||||
});
|
||||
}
|
||||
|
||||
let manifest = Manifest {
|
||||
family: family.to_string(),
|
||||
weights: weights.clone(),
|
||||
faces: cached_faces,
|
||||
bytes: total,
|
||||
installed_at,
|
||||
source: "google".into(),
|
||||
};
|
||||
manifest.validate()?;
|
||||
let json = serde_json::to_string_pretty(&manifest).map_err(|_| "STORAGE: serialization failed")?;
|
||||
std::fs::write(tmp_dir.join(MANIFEST), json).map_err(|error| format!("STORAGE: {error}"))?;
|
||||
|
||||
if final_dir.exists() {
|
||||
std::fs::remove_dir_all(&final_dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||
}
|
||||
std::fs::rename(&tmp_dir, &final_dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||
Ok(manifest)
|
||||
}
|
||||
|
||||
pub fn remove_family(fonts_root: &Path, family: &str) -> Result<(), String> {
|
||||
let dir = family_dir(fonts_root, family).ok_or("VALIDATION: invalid family name")?;
|
||||
if dir.exists() {
|
||||
std::fs::remove_dir_all(&dir).map_err(|error| format!("STORAGE: {error}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `{ "family": ..., "faces": [ { "weight": [min,max], "unicodeRange": ..., "data": "<base64>" } ] }`
|
||||
pub fn faces_json(manifest: &Manifest, bytes: &[Vec<u8>]) -> serde_json::Value {
|
||||
use base64::Engine;
|
||||
let faces: Vec<serde_json::Value> = manifest
|
||||
.faces
|
||||
.iter()
|
||||
.zip(bytes)
|
||||
.map(|(face, data)| {
|
||||
serde_json::json!({
|
||||
"weight": face.weight,
|
||||
"unicodeRange": face.unicode_range,
|
||||
"data": base64::engine::general_purpose::STANDARD.encode(data),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
serde_json::json!({ "family": manifest.family, "faces": faces })
|
||||
}
|
||||
|
||||
/// The script evaluated in every site tab so the page runtime (`window.<runtime_global>.fonts`) can
|
||||
/// hand cached font bytes to `document.fonts` without the site's CSP ever seeing a remote font URL.
|
||||
/// It does nothing outside the top frame on the site's primary origin.
|
||||
pub fn page_script(site: &SiteSpec, fonts: &[serde_json::Value]) -> Option<String> {
|
||||
if fonts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let json = serde_json::to_string(fonts).ok()?;
|
||||
let json = json.replace("</", "<\\/").replace('\u{2028}', "\\u2028").replace('\u{2029}', "\\u2029");
|
||||
let (origin, global) = (site.primary_origin(), site.runtime_global);
|
||||
Some(format!(
|
||||
"(()=>{{if(window.top!==window||location.origin!=='{origin}')return;const f={json};const BT=window.{global};if(BT&&BT.fonts)BT.fonts.provide(f);else(window.__wbFonts=window.__wbFonts||[]).push(...f);}})();"
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::site::testing::SITE;
|
||||
|
||||
const INTER_CSS: &str = include_str!("../tests/fixtures/gfonts/inter.css");
|
||||
const LATO_CSS: &str = include_str!("../tests/fixtures/gfonts/lato.css");
|
||||
const LATO_TEXT_CSS: &str = include_str!("../tests/fixtures/gfonts/lato-text.css");
|
||||
|
||||
#[test]
|
||||
fn parses_inter_variable_font() {
|
||||
let faces = parse_css(INTER_CSS).unwrap();
|
||||
assert_eq!(faces.len(), 7);
|
||||
let subsets: Vec<&str> = faces.iter().map(|f| f.subset.as_deref().unwrap()).collect();
|
||||
assert_eq!(subsets, ["cyrillic-ext", "cyrillic", "greek-ext", "greek", "vietnamese", "latin-ext", "latin"]);
|
||||
assert!(faces.iter().all(|f| f.weight == (100, 900)));
|
||||
assert!(faces.iter().all(|f| f.style == "normal"));
|
||||
|
||||
let selected = select_faces(faces);
|
||||
assert_eq!(selected.len(), 2);
|
||||
let mut subsets: Vec<&str> = selected.iter().map(|f| f.subset.as_deref().unwrap()).collect();
|
||||
subsets.sort_unstable();
|
||||
assert_eq!(subsets, ["latin", "latin-ext"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_lato_static_weights() {
|
||||
let faces = parse_css(LATO_CSS).unwrap();
|
||||
assert_eq!(faces.len(), 4);
|
||||
assert!(faces.iter().all(|f| f.weight == (400, 400) || f.weight == (700, 700)));
|
||||
let selected = select_faces(faces);
|
||||
assert_eq!(selected.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_lato_text_preview_without_subset_labels() {
|
||||
let faces = parse_css(LATO_TEXT_CSS).unwrap();
|
||||
assert_eq!(faces.len(), 2);
|
||||
assert!(faces.iter().all(|f| f.subset.is_none()));
|
||||
assert!(faces.iter().all(|f| f.url.contains("/l/font")));
|
||||
let selected = select_faces(faces);
|
||||
assert_eq!(selected.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_response_is_unknown_family() {
|
||||
let err = parse_css("<!DOCTYPE html><html></html>").unwrap_err();
|
||||
assert_eq!(err, "FONTS: unknown family");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_css_has_no_usable_faces() {
|
||||
let err = parse_css("/* nothing here */").unwrap_err();
|
||||
assert_eq!(err, "FONTS: no usable font faces");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn css_url_allowlist_rejects_bad_hosts_and_paths() {
|
||||
assert!(allowed_css_url(&Url::parse("https://fonts.googleapis.com/css2?family=Lato").unwrap()));
|
||||
assert!(!allowed_css_url(&Url::parse("http://fonts.googleapis.com/css2?family=Lato").unwrap()));
|
||||
assert!(!allowed_css_url(&Url::parse("https://fonts.googleapis.com/css3?family=Lato").unwrap()));
|
||||
assert!(!allowed_css_url(&Url::parse("https://evil.test/fonts.googleapis.com/css2").unwrap()));
|
||||
assert!(!allowed_css_url(&Url::parse("https://user@fonts.googleapis.com/css2").unwrap()));
|
||||
assert!(!allowed_css_url(&Url::parse("https://fonts.googleapis.com:8443/css2").unwrap()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_url_allowlist_rejects_lookalike_hosts() {
|
||||
assert!(allowed_file_url(&Url::parse("https://fonts.gstatic.com/s/lato/v25/x.woff2").unwrap()));
|
||||
assert!(allowed_file_url(&Url::parse("https://fonts.gstatic.com/l/font?kit=abc").unwrap()));
|
||||
assert!(!allowed_file_url(&Url::parse("https://fonts.gstatic.com.evil.test/s/lato/v25/x.woff2").unwrap()));
|
||||
assert!(!allowed_file_url(&Url::parse("https://evil.test/fonts.gstatic.com/s/lato/v25/x.woff2").unwrap()));
|
||||
assert!(!allowed_file_url(&Url::parse("http://fonts.gstatic.com/s/lato/v25/x.woff2").unwrap()));
|
||||
assert!(!allowed_file_url(&Url::parse("https://fonts.gstatic.com/other/x.woff2").unwrap()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn css_url_encoding_and_validation() {
|
||||
let url = css_url("Noto Sans JP", &Weights::List { values: vec![700, 400] }, None).unwrap();
|
||||
assert!(url.as_str().starts_with("https://fonts.googleapis.com/css2?"));
|
||||
assert!(url.as_str().contains("family=Noto+Sans+JP%3Awght%40400%3B700") || url.query().unwrap().contains("family=Noto+Sans+JP:wght@400;700"));
|
||||
assert!(url.query().unwrap().contains("display=swap"));
|
||||
|
||||
let with_text = css_url("Lato", &Weights::Range { min: 400, max: 700 }, Some("Hi")).unwrap();
|
||||
assert!(with_text.query().unwrap().contains("text=Hi"));
|
||||
|
||||
assert!(css_url("../x", &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||
assert!(css_url("a\u{0}", &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||
assert!(css_url("", &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||
assert!(css_url(&"a".repeat(49), &Weights::Range { min: 400, max: 400 }, None).is_err());
|
||||
assert!(css_url("Lato", &Weights::Range { min: 50, max: 400 }, None).is_err());
|
||||
assert!(css_url("Lato", &Weights::Range { min: 400, max: 400 }, Some(&"a".repeat(121))).is_err());
|
||||
assert!(css_url("Lato", &Weights::Range { min: 400, max: 400 }, Some("")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_woff2_checks_magic_and_length_field() {
|
||||
let mut good = vec![0u8; 48];
|
||||
good[0..4].copy_from_slice(b"wOF2");
|
||||
good[8..12].copy_from_slice(&48u32.to_be_bytes());
|
||||
assert!(is_woff2(&good));
|
||||
|
||||
let mut bad_magic = good.clone();
|
||||
bad_magic[0..4].copy_from_slice(b"XXXX");
|
||||
assert!(!is_woff2(&bad_magic));
|
||||
|
||||
let mut bad_length = good.clone();
|
||||
bad_length[8..12].copy_from_slice(&999u32.to_be_bytes());
|
||||
assert!(!is_woff2(&bad_length));
|
||||
|
||||
assert!(!is_woff2(&[0u8; 10]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_family_read_manifest_verify_round_trip() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut face_bytes = vec![0u8; 100];
|
||||
face_bytes[0..4].copy_from_slice(b"wOF2");
|
||||
face_bytes[8..12].copy_from_slice(&100u32.to_be_bytes());
|
||||
let face = Face { subset: Some("latin".into()), style: "normal".into(), weight: (400, 400), url: "https://fonts.gstatic.com/s/x.woff2".into(), unicode_range: Some("U+00-FF".into()) };
|
||||
let weights = Weights::List { values: vec![400] };
|
||||
let manifest = write_family(dir.path(), "Lato", &weights, &[(face, face_bytes.clone())], "2026-01-01T00:00:00Z".into()).unwrap();
|
||||
assert_eq!(manifest.bytes, 100);
|
||||
|
||||
let read = read_manifest(dir.path(), "Lato").unwrap();
|
||||
assert_eq!(read, manifest);
|
||||
|
||||
let family_dir_path = family_dir(dir.path(), "Lato").unwrap();
|
||||
let verified = verify_cached(&family_dir_path, &read).unwrap();
|
||||
assert_eq!(verified, vec![face_bytes]);
|
||||
|
||||
assert_eq!(cache_bytes(dir.path()), 100);
|
||||
assert_eq!(list_installed(dir.path()).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tampered_cache_file_fails_verification() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut face_bytes = vec![0u8; 60];
|
||||
face_bytes[0..4].copy_from_slice(b"wOF2");
|
||||
face_bytes[8..12].copy_from_slice(&60u32.to_be_bytes());
|
||||
let face = Face { subset: None, style: "normal".into(), weight: (400, 400), url: "https://fonts.gstatic.com/s/x.woff2".into(), unicode_range: None };
|
||||
let weights = Weights::List { values: vec![400] };
|
||||
let manifest = write_family(dir.path(), "Lato", &weights, &[(face, face_bytes)], "2026-01-01T00:00:00Z".into()).unwrap();
|
||||
|
||||
let family_dir_path = family_dir(dir.path(), "Lato").unwrap();
|
||||
std::fs::write(family_dir_path.join(&manifest.faces[0].file), vec![1u8; 60]).unwrap();
|
||||
let err = verify_cached(&family_dir_path, &manifest).unwrap_err();
|
||||
assert_eq!(err, "FONTS: cache damaged");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_buffer_is_rejected() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut face_bytes = vec![0u8; 48];
|
||||
face_bytes[0..4].copy_from_slice(b"wOF2");
|
||||
face_bytes[8..12].copy_from_slice(&(MAX_FILE_BYTES as u32 + 1).to_be_bytes());
|
||||
face_bytes.resize(MAX_FILE_BYTES + 1, 0);
|
||||
let face = Face { subset: None, style: "normal".into(), weight: (400, 400), url: "https://fonts.gstatic.com/s/x.woff2".into(), unicode_range: None };
|
||||
let weights = Weights::List { values: vec![400] };
|
||||
let err = write_family(dir.path(), "Lato", &weights, &[(face, face_bytes)], "2026-01-01T00:00:00Z".into()).unwrap_err();
|
||||
assert_eq!(err, "FONTS: invalid font file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manifest_with_path_traversal_file_name_is_rejected() {
|
||||
let manifest = Manifest {
|
||||
family: "Lato".into(),
|
||||
weights: Weights::List { values: vec![400] },
|
||||
faces: vec![CachedFace { file: "../evil.woff2".into(), weight: [400, 400], unicode_range: None, subset: None, bytes: 48, sha256: "0".repeat(64) }],
|
||||
bytes: 48,
|
||||
installed_at: "2026-01-01T00:00:00Z".into(),
|
||||
source: "google".into(),
|
||||
};
|
||||
assert!(manifest.validate().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_script_escapes_closing_script_tags() {
|
||||
let fonts = vec![serde_json::json!({ "family": "</script><script>alert(1)</script>", "faces": [] })];
|
||||
let script = page_script(&SITE, &fonts).unwrap();
|
||||
assert!(script.starts_with("(()=>{if(window.top!==window||location.origin!=='https://x.com')return;"));
|
||||
assert!(script.contains("const BT=window.BetterTwttr;"));
|
||||
assert!(!script.contains("</script>"));
|
||||
assert!(script.contains("<\\/script>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn page_script_is_none_for_empty_fonts() {
|
||||
assert!(page_script(&SITE, &[]).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_family_rejects_bad_names() {
|
||||
assert!(valid_family("Lato"));
|
||||
assert!(valid_family("Noto Sans JP"));
|
||||
assert!(!valid_family(""));
|
||||
assert!(!valid_family(" Lato"));
|
||||
assert!(!valid_family("Lato "));
|
||||
assert!(!valid_family("Lato Bold"));
|
||||
assert!(!valid_family("../x"));
|
||||
assert!(!valid_family(&"a".repeat(49)));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue