0.34.13_cleaup_code

This commit is contained in:
Mateusz Gruszczyński committed 2026-10-26 14:14:41 +01:00
1 parent ec46a96a29
commit 5c2e50ec06
87 files changed
+4890 -1338

No files matched your search

+30 -10
View File
@@ -1,11 +1,11 @@
use super::{CompiledService, SymlinkPolicy};
#[cfg(unix)]
use std::{ffi::CString, sync::Arc};
use std::{
fs::File,
io,
path::{Path, PathBuf},
};
#[cfg(unix)]
use std::{ffi::CString, sync::Arc};
pub(super) struct FilesystemRoot {
root: PathBuf,
@@ -63,12 +63,18 @@ pub(super) fn open_root_directory(path: &Path) -> io::Result<File> {
pub(super) fn path_components(path: &str) -> io::Result<Vec<String>> {
if !path.starts_with('/') {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "path is not absolute"));
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"path is not absolute",
));
}
let mut result = Vec::new();
for part in path.split('/').filter(|part| !part.is_empty()) {
if part == "." || part == ".." || part.contains('\\') || part.contains('\0') {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "ambiguous path"));
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"ambiguous path",
));
}
result.push(part.to_owned());
}
@@ -93,7 +99,10 @@ pub(super) fn secure_open_sync(root: &FilesystemRoot, components: &[String]) ->
}
let current = unsafe { File::from_raw_fd(fd) };
let metadata = current.metadata()?;
return Ok(Opened { file: current, metadata });
return Ok(Opened {
file: current,
metadata,
});
}
let mut current: Option<File> = None;
for (index, component) in components.iter().enumerate() {
@@ -135,7 +144,10 @@ pub(super) fn secure_open_sync(root: &FilesystemRoot, components: &[String]) ->
"Web Server serves only regular files and directories",
));
}
Ok(Opened { file: current, metadata })
Ok(Opened {
file: current,
metadata,
})
}
#[cfg(target_os = "linux")]
@@ -192,7 +204,10 @@ pub(super) fn list_directory_sync(
let opened = secure_open_sync(root, components)?;
if !opened.metadata.is_dir() {
return Err(io::Error::new(io::ErrorKind::NotADirectory, "not a directory"));
return Err(io::Error::new(
io::ErrorKind::NotADirectory,
"not a directory",
));
}
let raw = opened.file.try_clone()?.into_raw_fd();
let dir = unsafe { libc::fdopendir(raw) };
@@ -241,7 +256,10 @@ pub(super) fn list_directory_sync(
) -> io::Result<Vec<DirectoryEntry>> {
let opened = secure_open_sync(root, components)?;
if !opened.metadata.is_dir() {
return Err(io::Error::new(io::ErrorKind::NotADirectory, "not a directory"));
return Err(io::Error::new(
io::ErrorKind::NotADirectory,
"not a directory",
));
}
let mut path = root.root.clone();
for component in components {
@@ -249,7 +267,10 @@ pub(super) fn list_directory_sync(
}
let canonical = std::fs::canonicalize(path)?;
if !canonical.starts_with(&root.root) {
return Err(io::Error::new(io::ErrorKind::PermissionDenied, "path escaped document root"));
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"path escaped document root",
));
}
let mut entries = Vec::new();
for entry in std::fs::read_dir(canonical)? {
@@ -275,4 +296,3 @@ pub(super) fn list_directory_sync(
});
Ok(entries)
}