use axum::extract::Request;
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use http::StatusCode;
use std::path::{Path, PathBuf};
const WELL_KNOWN: &str = ".well-known";
#[cfg(windows)]
const SEPARATORS: &[char] = &['/', '\\'];
#[cfg(not(windows))]
const SEPARATORS: &[char] = &['/'];
pub(crate) async fn refuse_paths_outside(root: PathBuf, request: Request, next: Next) -> Response {
if !leads_inside(&root, request.uri().path()) {
return StatusCode::NOT_FOUND.into_response();
}
next.run(request).await
}
fn leads_inside(root: &Path, path: &str) -> bool {
let decoded = decode(path);
let relative = decoded.trim_start_matches(SEPARATORS);
match root.join(relative).canonicalize() {
Ok(resolved) => resolved.starts_with(root),
Err(_) => true,
}
}
pub(crate) async fn refuse_hidden_files(request: Request, next: Next) -> Response {
if names_a_hidden_file(request.uri().path()) {
return StatusCode::NOT_FOUND.into_response();
}
next.run(request).await
}
pub(crate) fn is_hidden(name: &str) -> bool {
name.starts_with('.') && name != WELL_KNOWN
}
fn names_a_hidden_file(path: &str) -> bool {
decode(path).split(SEPARATORS).any(is_hidden)
}
fn decode(path: &str) -> String {
let raw = path.as_bytes();
let mut decoded = Vec::with_capacity(raw.len());
let mut at = 0;
while at < raw.len() {
let pair = (raw.get(at + 1).and_then(hex), raw.get(at + 2).and_then(hex));
match (raw[at], pair) {
(b'%', (Some(high), Some(low))) => {
decoded.push(high << 4 | low);
at += 3;
}
_ => {
decoded.push(raw[at]);
at += 1;
}
}
}
String::from_utf8_lossy(&decoded).into_owned()
}
fn hex(digit: &u8) -> Option<u8> {
(*digit as char).to_digit(16).map(|value| value as u8)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hidden_addresses_are_refused() {
for path in [
"/.env",
"/.git/config",
"/%2e%65nv",
"/%2Eenv",
"/js/.hidden.js",
"/.well-known/.secret",
] {
assert!(names_a_hidden_file(path), "{path}");
}
}
#[test]
fn an_encoded_separator_does_not_hide_a_hidden_file() {
for path in ["/sub%2f.env", "/sub%2F.env", "/sub%2f.git%2fconfig"] {
assert!(names_a_hidden_file(path), "{path}");
}
}
#[cfg(not(windows))]
#[test]
fn a_backslash_is_an_ordinary_character_in_a_name_here() {
assert!(!names_a_hidden_file("/sub%5c.env"));
}
#[test]
fn ordinary_addresses_are_allowed() {
for path in ["/", "/index.html", "/a.b/c.js", "/.well-known/acme/token"] {
assert!(!names_a_hidden_file(path), "{path}");
}
}
}