From 2b029e95570312de19b039c74f55298a439d6ad2 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 28 Jan 2024 19:51:06 -0500 Subject: add `--disable-indexing` cli flag to completely disable directory indexing --- src/args.rs | 6 ++++++ src/config.rs | 4 ++++ src/listing.rs | 9 +++++++++ src/renderer.rs | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index e42d2ff..7502d9f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -294,6 +294,12 @@ pub struct CliArgs { /// Enable README.md rendering in directories #[arg(long, env = "MINISERVE_README")] pub readme: bool, + + /// Disable indexing + /// This will prevent miniserve from generating directory listings + /// and will return a 404 instead. + #[arg(long, env = "MINISERVE_DISABLE_INDEXING")] + pub disable_indexing: bool, } /// Checks whether an interface is valid, i.e. it can be parsed into an IP address diff --git a/src/config.rs b/src/config.rs index 43414b2..3643502 100644 --- a/src/config.rs +++ b/src/config.rs @@ -146,6 +146,9 @@ pub struct MiniserveConfig { /// If enabled, render the readme from the current directory pub readme: bool, + /// If enabled, indexing is disabled. + pub disable_indexing: bool, + /// If set, use provided rustls config for TLS #[cfg(feature = "tls")] pub tls_rustls_config: Option, @@ -311,6 +314,7 @@ impl MiniserveConfig { hide_theme_selector: args.hide_theme_selector, show_wget_footer: args.show_wget_footer, readme: args.readme, + disable_indexing: args.disable_indexing, tls_rustls_config: tls_rustls_server_config, compress_response: args.compress_response, }) diff --git a/src/listing.rs b/src/listing.rs index faa0918..a8668e3 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -376,6 +376,15 @@ pub fn directory_listing( .body(actix_web::body::BodyStream::new(rx)), )) } else { + if conf.disable_indexing { + return Ok(ServiceResponse::new( + req.clone(), + HttpResponse::NotFound() + .content_type(mime::TEXT_PLAIN_UTF_8) + .body("File not found."), + )); + } + Ok(ServiceResponse::new( req.clone(), HttpResponse::Ok().content_type(mime::TEXT_HTML_UTF_8).body( diff --git a/src/renderer.rs b/src/renderer.rs index 3b62704..b691bca 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -687,7 +687,7 @@ pub fn render_error( p { (error) } } // WARN don't expose random route! - @if conf.route_prefix.is_empty() { + @if conf.route_prefix.is_empty() && !conf.disable_indexing { div.error-nav { a.error-back href=(return_address) { "Go back to file listing" -- cgit v1.2.3