Skip to content

Commit 2b2c7bd

Browse files
authored
feat: add option --allow-hash to allow/disallow file hashing (#657)
1 parent ca18df1 commit 2b2c7bd

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Options:
6767
--allow-search Allow search files/folders
6868
--allow-symlink Allow symlink to files/folders outside root directory
6969
--allow-archive Allow download folders as archive file
70+
--allow-hash Allow ?hash query to get file sha256 hash
7071
--enable-cors Enable CORS, sets `Access-Control-Allow-Origin: *`
7172
--render-index Serve index.html when requesting a directory, returns 404 if not found index.html
7273
--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_`.
346347
--allow-search DUFS_ALLOW_SEARCH=true
347348
--allow-symlink DUFS_ALLOW_SYMLINK=true
348349
--allow-archive DUFS_ALLOW_ARCHIVE=true
350+
--allow-hash DUFS_ALLOW_HASH=true
349351
--enable-cors DUFS_ENABLE_CORS=true
350352
--render-index DUFS_RENDER_INDEX=true
351353
--render-try-index DUFS_RENDER_TRY_INDEX=true
@@ -383,6 +385,7 @@ allow-delete: true
383385
allow-search: true
384386
allow-symlink: true
385387
allow-archive: true
388+
allow-hash: true
386389
enable-cors: true
387390
render-index: true
388391
render-try-index: true

src/args.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ pub fn build_cli() -> Command {
148148
.action(ArgAction::SetTrue)
149149
.help("Allow download folders as archive file"),
150150
)
151+
.arg(
152+
Arg::new("allow-hash")
153+
.env("DUFS_ALLOW_HASH")
154+
.hide_env(true)
155+
.long("allow-hash")
156+
.action(ArgAction::SetTrue)
157+
.help("Allow ?hash query to get file sha256 hash"),
158+
)
151159
.arg(
152160
Arg::new("enable-cors")
153161
.env("DUFS_ENABLE_CORS")
@@ -281,6 +289,7 @@ pub struct Args {
281289
pub allow_search: bool,
282290
pub allow_symlink: bool,
283291
pub allow_archive: bool,
292+
pub allow_hash: bool,
284293
pub render_index: bool,
285294
pub render_spa: bool,
286295
pub render_try_index: bool,
@@ -375,6 +384,9 @@ impl Args {
375384
if !args.allow_symlink {
376385
args.allow_symlink = allow_all || matches.get_flag("allow-symlink");
377386
}
387+
if !args.allow_hash {
388+
args.allow_hash = allow_all || matches.get_flag("allow-hash");
389+
}
378390
if !args.allow_archive {
379391
args.allow_archive = allow_all || matches.get_flag("allow-archive");
380392
}

src/server.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,11 @@ impl Server {
358358
self.handle_edit_file(path, DataKind::View, head_only, user, &mut res)
359359
.await?;
360360
} else if has_query_flag(&query_params, "hash") {
361-
self.handle_hash_file(path, head_only, &mut res).await?;
361+
if self.args.allow_hash {
362+
self.handle_hash_file(path, head_only, &mut res).await?;
363+
} else {
364+
status_forbid(&mut res);
365+
}
362366
} else {
363367
self.handle_send_file(path, headers, head_only, &mut res)
364368
.await?;

tests/http.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn head_file(server: TestServer) -> Result<(), Error> {
203203
}
204204

205205
#[rstest]
206-
fn hash_file(server: TestServer) -> Result<(), Error> {
206+
fn hash_file(#[with(&["--allow-hash"])] server: TestServer) -> Result<(), Error> {
207207
let resp = reqwest::blocking::get(format!("{}index.html?hash", server.url()))?;
208208
assert_eq!(
209209
resp.headers().get("content-type").unwrap(),
@@ -217,6 +217,13 @@ fn hash_file(server: TestServer) -> Result<(), Error> {
217217
Ok(())
218218
}
219219

220+
#[rstest]
221+
fn no_hash_file(server: TestServer) -> Result<(), Error> {
222+
let resp = reqwest::blocking::get(format!("{}index.html?hash", server.url()))?;
223+
assert_eq!(resp.status(), 403);
224+
Ok(())
225+
}
226+
220227
#[rstest]
221228
fn get_file_404(server: TestServer) -> Result<(), Error> {
222229
let resp = reqwest::blocking::get(format!("{}404", server.url()))?;

0 commit comments

Comments
 (0)