use super::{temp_path, verify_sha256, FetchError, FetchRequest, Fetched, Fetcher, ProgressFn};
use async_trait::async_trait;
use sha2::{Digest, Sha256};
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
const FILE_SCHEME: &str = "file";
const READ_CHUNK: usize = 64 * 1024;
pub struct FileFetcher {
schemes: Vec<String>,
allowed_roots: Option<Vec<PathBuf>>,
}
impl FileFetcher {
pub fn new() -> Self {
Self {
schemes: vec![FILE_SCHEME.to_owned()],
allowed_roots: None,
}
}
pub fn with_allowed_roots(roots: impl IntoIterator<Item = PathBuf>) -> Self {
Self {
schemes: vec![FILE_SCHEME.to_owned()],
allowed_roots: Some(roots.into_iter().collect()),
}
}
pub fn allowed_roots(&self) -> Option<&[PathBuf]> {
self.allowed_roots.as_deref()
}
pub fn resolve(&self, uri: &str) -> Result<PathBuf, FetchError> {
let rest = uri
.strip_prefix("file://")
.ok_or_else(|| FetchError::InvalidAddress(format!("not a file URI: `{uri}`")))?;
let (authority, path) = match rest.find('/') {
Some(index) => (&rest[..index], &rest[index..]),
None => (rest, ""),
};
if !authority.is_empty() && !authority.eq_ignore_ascii_case("localhost") {
return Err(FetchError::InvalidAddress(format!(
"file URI authority `{authority}` is not local: `{uri}`"
)));
}
if path.is_empty() {
return Err(FetchError::InvalidAddress(format!(
"file URI has no path: `{uri}`"
)));
}
Ok(PathBuf::from(percent_decode(path)?))
}
async fn authorize(&self, source: PathBuf) -> Result<PathBuf, FetchError> {
let Some(roots) = &self.allowed_roots else {
return Ok(source);
};
let canonical = tokio::fs::canonicalize(&source).await?;
for root in roots {
if let Ok(root) = tokio::fs::canonicalize(root).await {
if canonical.starts_with(&root) {
return Ok(canonical);
}
}
}
Err(FetchError::AccessDenied(format!(
"{} is outside the configured file roots",
source.display()
)))
}
}
impl Default for FileFetcher {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Fetcher for FileFetcher {
fn name(&self) -> &str {
"file"
}
fn schemes(&self) -> &[String] {
&self.schemes
}
async fn fetch(&self, request: &FetchRequest) -> Result<Fetched, FetchError> {
self.fetch_with_progress(request, &|_: u64| {}).await
}
async fn fetch_with_progress(
&self,
request: &FetchRequest,
progress: &ProgressFn<'_>,
) -> Result<Fetched, FetchError> {
let source = self.authorize(self.resolve(&request.address.uri)?).await?;
let metadata = tokio::fs::metadata(&source).await?;
if !metadata.is_file() {
return Err(FetchError::Io(std::io::Error::new(
ErrorKind::InvalidInput,
format!("not a regular file: {}", source.display()),
)));
}
let temp = temp_path(&request.destination);
match copy(&source, &temp, request, progress).await {
Ok(fetched) => Ok(fetched),
Err(error) => {
let _ = tokio::fs::remove_file(&temp).await;
Err(error)
}
}
}
}
async fn copy(
source: &Path,
temp: &Path,
request: &FetchRequest,
progress: &ProgressFn<'_>,
) -> Result<Fetched, FetchError> {
if let Some(parent) = temp.parent() {
tokio::fs::create_dir_all(parent).await?;
}
let mut reader = tokio::fs::File::open(source).await?;
let mut writer = tokio::fs::File::create(temp).await?;
let mut hasher = Sha256::new();
let mut buffer = vec![0u8; READ_CHUNK];
let mut total = 0u64;
loop {
let read = reader.read(&mut buffer).await?;
if read == 0 {
break;
}
hasher.update(&buffer[..read]);
writer.write_all(&buffer[..read]).await?;
total += read as u64;
progress(total);
}
writer.flush().await?;
let digest = hex::encode(hasher.finalize());
verify_sha256(request.expected_sha256.as_deref(), &digest)?;
tokio::fs::rename(temp, &request.destination).await?;
Ok(Fetched {
path: request.destination.clone(),
bytes: total,
sha256: Some(digest),
address: request.address.clone(),
})
}
fn percent_decode(input: &str) -> Result<String, FetchError> {
let bytes = input.as_bytes();
let mut decoded = Vec::with_capacity(bytes.len());
let mut index = 0;
while index < bytes.len() {
if bytes[index] == b'%' {
if index + 2 >= bytes.len() {
return Err(FetchError::InvalidAddress(format!(
"truncated percent escape in file URI: `{input}`"
)));
}
let high = hex_digit(bytes[index + 1], input)?;
let low = hex_digit(bytes[index + 2], input)?;
decoded.push((high << 4) | low);
index += 3;
} else {
decoded.push(bytes[index]);
index += 1;
}
}
String::from_utf8(decoded)
.map_err(|_| FetchError::InvalidAddress(format!("file URI is not valid UTF-8: `{input}`")))
}
fn hex_digit(byte: u8, input: &str) -> Result<u8, FetchError> {
match byte {
b'0'..=b'9' => Ok(byte - b'0'),
b'a'..=b'f' => Ok(byte - b'a' + 10),
b'A'..=b'F' => Ok(byte - b'A' + 10),
_ => Err(FetchError::InvalidAddress(format!(
"invalid percent escape in file URI: `{input}`"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolves_absolute_paths_and_localhost() {
let fetcher = FileFetcher::new();
assert_eq!(
fetcher.resolve("file:///data/report.pdf").expect("resolve"),
PathBuf::from("/data/report.pdf")
);
assert_eq!(
fetcher
.resolve("file://localhost/data/report.pdf")
.expect("resolve"),
PathBuf::from("/data/report.pdf")
);
assert_eq!(
fetcher
.resolve("file://LOCALHOST/data/report.pdf")
.expect("resolve"),
PathBuf::from("/data/report.pdf")
);
}
#[test]
fn decodes_percent_escapes() {
let fetcher = FileFetcher::new();
assert_eq!(
fetcher
.resolve("file:///tmp/a%20b%2Fc.txt")
.expect("resolve"),
PathBuf::from("/tmp/a b/c.txt")
);
}
#[test]
fn rejects_non_local_authority_and_missing_path() {
let fetcher = FileFetcher::new();
for uri in [
"file://server/share/file",
"file://relative",
"file://",
"https://example.org/file",
] {
assert!(
matches!(fetcher.resolve(uri), Err(FetchError::InvalidAddress(_))),
"`{uri}` should be rejected"
);
}
}
#[test]
fn rejects_malformed_percent_escapes() {
let fetcher = FileFetcher::new();
for uri in [
"file:///tmp/a%2.txt",
"file:///tmp/a%zz.txt",
"file:///tmp/a%",
] {
assert!(
matches!(fetcher.resolve(uri), Err(FetchError::InvalidAddress(_))),
"`{uri}` should be rejected"
);
}
}
}