feat: add option --allow-hash to allow/disallow file hashing (#657)

This commit is contained in:
sigoden
2026-01-09 16:43:18 +08:00
committed by GitHub
parent ca18df1a36
commit 2b2c7bd5f7
4 changed files with 28 additions and 2 deletions

View File

@@ -67,6 +67,7 @@ Options:
--allow-search Allow search files/folders --allow-search Allow search files/folders
--allow-symlink Allow symlink to files/folders outside root directory --allow-symlink Allow symlink to files/folders outside root directory
--allow-archive Allow download folders as archive file --allow-archive Allow download folders as archive file
--allow-hash Allow ?hash query to get file sha256 hash
--enable-cors Enable CORS, sets `Access-Control-Allow-Origin: *` --enable-cors Enable CORS, sets `Access-Control-Allow-Origin: *`
--render-index Serve index.html when requesting a directory, returns 404 if not found index.html --render-index Serve index.html when requesting a directory, returns 404 if not found index.html
--render-try-index Serve index.html when requesting a directory, returns directory listing if not found index.html --render-try-index Serve index.html when requesting a directory, returns directory listing if not found index.html
@@ -346,6 +347,7 @@ All options can be set using environment variables prefixed with `DUFS_`.
--allow-search DUFS_ALLOW_SEARCH=true --allow-search DUFS_ALLOW_SEARCH=true
--allow-symlink DUFS_ALLOW_SYMLINK=true --allow-symlink DUFS_ALLOW_SYMLINK=true
--allow-archive DUFS_ALLOW_ARCHIVE=true --allow-archive DUFS_ALLOW_ARCHIVE=true
--allow-hash DUFS_ALLOW_HASH=true
--enable-cors DUFS_ENABLE_CORS=true --enable-cors DUFS_ENABLE_CORS=true
--render-index DUFS_RENDER_INDEX=true --render-index DUFS_RENDER_INDEX=true
--render-try-index DUFS_RENDER_TRY_INDEX=true --render-try-index DUFS_RENDER_TRY_INDEX=true
@@ -383,6 +385,7 @@ allow-delete: true
allow-search: true allow-search: true
allow-symlink: true allow-symlink: true
allow-archive: true allow-archive: true
allow-hash: true
enable-cors: true enable-cors: true
render-index: true render-index: true
render-try-index: true render-try-index: true

View File

@@ -148,6 +148,14 @@ pub fn build_cli() -> Command {
.action(ArgAction::SetTrue) .action(ArgAction::SetTrue)
.help("Allow download folders as archive file"), .help("Allow download folders as archive file"),
) )
.arg(
Arg::new("allow-hash")
.env("DUFS_ALLOW_HASH")
.hide_env(true)
.long("allow-hash")
.action(ArgAction::SetTrue)
.help("Allow ?hash query to get file sha256 hash"),
)
.arg( .arg(
Arg::new("enable-cors") Arg::new("enable-cors")
.env("DUFS_ENABLE_CORS") .env("DUFS_ENABLE_CORS")
@@ -281,6 +289,7 @@ pub struct Args {
pub allow_search: bool, pub allow_search: bool,
pub allow_symlink: bool, pub allow_symlink: bool,
pub allow_archive: bool, pub allow_archive: bool,
pub allow_hash: bool,
pub render_index: bool, pub render_index: bool,
pub render_spa: bool, pub render_spa: bool,
pub render_try_index: bool, pub render_try_index: bool,
@@ -375,6 +384,9 @@ impl Args {
if !args.allow_symlink { if !args.allow_symlink {
args.allow_symlink = allow_all || matches.get_flag("allow-symlink"); args.allow_symlink = allow_all || matches.get_flag("allow-symlink");
} }
if !args.allow_hash {
args.allow_hash = allow_all || matches.get_flag("allow-hash");
}
if !args.allow_archive { if !args.allow_archive {
args.allow_archive = allow_all || matches.get_flag("allow-archive"); args.allow_archive = allow_all || matches.get_flag("allow-archive");
} }

View File

@@ -358,7 +358,11 @@ impl Server {
self.handle_edit_file(path, DataKind::View, head_only, user, &mut res) self.handle_edit_file(path, DataKind::View, head_only, user, &mut res)
.await?; .await?;
} else if has_query_flag(&query_params, "hash") { } else if has_query_flag(&query_params, "hash") {
if self.args.allow_hash {
self.handle_hash_file(path, head_only, &mut res).await?; self.handle_hash_file(path, head_only, &mut res).await?;
} else {
status_forbid(&mut res);
}
} else { } else {
self.handle_send_file(path, headers, head_only, &mut res) self.handle_send_file(path, headers, head_only, &mut res)
.await?; .await?;

View File

@@ -203,7 +203,7 @@ fn head_file(server: TestServer) -> Result<(), Error> {
} }
#[rstest] #[rstest]
fn hash_file(server: TestServer) -> Result<(), Error> { fn hash_file(#[with(&["--allow-hash"])] server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(format!("{}index.html?hash", server.url()))?; let resp = reqwest::blocking::get(format!("{}index.html?hash", server.url()))?;
assert_eq!( assert_eq!(
resp.headers().get("content-type").unwrap(), resp.headers().get("content-type").unwrap(),
@@ -217,6 +217,13 @@ fn hash_file(server: TestServer) -> Result<(), Error> {
Ok(()) Ok(())
} }
#[rstest]
fn no_hash_file(server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(format!("{}index.html?hash", server.url()))?;
assert_eq!(resp.status(), 403);
Ok(())
}
#[rstest] #[rstest]
fn get_file_404(server: TestServer) -> Result<(), Error> { fn get_file_404(server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(format!("{}404", server.url()))?; let resp = reqwest::blocking::get(format!("{}404", server.url()))?;