use mime_guess::MimeGuess;
use std::path::{Component, Path, PathBuf};
pub fn resolve_static_pkg_path(
static_root: &Path,
requested_relative_path: &str,
) -> Option<PathBuf> {
let requested = Path::new(requested_relative_path);
if requested.is_absolute() {
return None;
}
if requested.components().any(|c| {
matches!(
c,
Component::ParentDir | Component::RootDir | Component::Prefix(_)
)
}) {
return None;
}
let canonical_root = std::fs::canonicalize(static_root).ok()?;
let candidate = canonical_root.join(requested);
let canonical_candidate = std::fs::canonicalize(candidate).ok()?;
if canonical_candidate.starts_with(&canonical_root) {
Some(canonical_candidate)
} else {
None
}
}
pub fn static_mime_for(path: &str) -> String {
MimeGuess::from_path(path)
.first_raw()
.map(normalize_static_mime)
.unwrap_or_else(|| "application/octet-stream".to_string())
}
pub fn normalize_static_mime(mime: &str) -> String {
if mime == "text/html" {
"text/html; charset=utf-8".to_string()
} else {
mime.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_static_pkg_path_rejects_parent_dir_traversal() {
let root = std::env::temp_dir().join("krab_core_static_root_reject");
std::fs::create_dir_all(&root).expect("failed to create static root");
assert!(resolve_static_pkg_path(&root, "../secret.txt").is_none());
}
#[test]
fn resolve_static_pkg_path_accepts_file_inside_root() {
let root = std::env::temp_dir().join("krab_core_static_root_accept");
std::fs::create_dir_all(&root).expect("failed to create static root");
let file = root.join("app.js");
std::fs::write(&file, "console.log('ok');").expect("failed to create static file");
let resolved = resolve_static_pkg_path(&root, "app.js").expect("expected in-root file");
let canonical_root = std::fs::canonicalize(&root).expect("failed to canonicalize root");
assert!(resolved.starts_with(&canonical_root));
}
#[test]
fn resolve_static_pkg_path_rejects_absolute_paths() {
let root = std::env::temp_dir().join("krab_core_static_root_absolute");
std::fs::create_dir_all(&root).expect("failed to create static root");
assert!(resolve_static_pkg_path(&root, "/etc/passwd").is_none());
assert!(resolve_static_pkg_path(&root, r"C:\Windows\system.ini").is_none());
}
#[test]
fn resolve_static_pkg_path_rejects_traversal_nested_mid_path() {
let root = std::env::temp_dir().join("krab_core_static_root_nested");
std::fs::create_dir_all(&root).expect("failed to create static root");
assert!(resolve_static_pkg_path(&root, "assets/../../secret.txt").is_none());
}
#[cfg(unix)]
fn link_file(target: &Path, link: &Path) -> std::io::Result<()> {
std::os::unix::fs::symlink(target, link)
}
#[cfg(windows)]
fn link_file(target: &Path, link: &Path) -> std::io::Result<()> {
std::os::windows::fs::symlink_file(target, link)
}
#[cfg(any(unix, windows))]
#[test]
fn resolve_static_pkg_path_rejects_symlink_escaping_root() {
let base = std::env::temp_dir().join("krab_core_static_root_symlink");
let root = base.join("root");
let outside = base.join("outside");
std::fs::create_dir_all(&root).expect("failed to create static root");
std::fs::create_dir_all(&outside).expect("failed to create outside dir");
let secret = outside.join("secret.txt");
std::fs::write(&secret, "do not serve me").expect("failed to create outside file");
let link = root.join("escape.txt");
let _ = std::fs::remove_file(&link);
if link_file(&secret, &link).is_err() {
if !cfg!(windows) {
panic!(
"symlink creation failed on a non-Windows host: this test asserted NOTHING and the traversal guard is unverified"
);
}
eprintln!(
"SKIPPED resolve_static_pkg_path_rejects_symlink_escaping_root: needs elevation on Windows; asserts on Linux CI"
);
return;
}
assert_eq!(
std::fs::canonicalize(&link).ok(),
std::fs::canonicalize(&secret).ok(),
"symlink did not resolve to the file outside the root"
);
assert!(resolve_static_pkg_path(&root, "escape.txt").is_none());
}
#[test]
fn resolve_static_pkg_path_rejects_a_missing_file() {
let root = std::env::temp_dir().join("krab_core_static_root_missing");
std::fs::create_dir_all(&root).expect("failed to create static root");
assert!(resolve_static_pkg_path(&root, "not-there.js").is_none());
}
#[test]
fn static_mime_for_uses_lookup_and_safe_default() {
assert_eq!(
static_mime_for("/pkg/app.js"),
"text/javascript".to_string()
);
assert_eq!(static_mime_for("/pkg/site.css"), "text/css".to_string());
assert_eq!(
static_mime_for("/pkg/index.html"),
"text/html; charset=utf-8".to_string()
);
assert_eq!(
static_mime_for("/pkg/module.wasm"),
"application/wasm".to_string()
);
assert_eq!(
static_mime_for("/pkg/blob.unknownext"),
"application/octet-stream".to_string()
);
}
#[test]
fn normalize_static_mime_only_touches_html() {
assert_eq!(
normalize_static_mime("text/html"),
"text/html; charset=utf-8"
);
assert_eq!(normalize_static_mime("text/css"), "text/css");
assert_eq!(
normalize_static_mime("application/wasm"),
"application/wasm"
);
}
}