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 From ad06f034bd991e931c4dcf5eea78362fd0f440b2 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Mon, 29 Jan 2024 19:25:03 -0500 Subject: add short version of `--disable-indexing` cli arg --- src/args.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index 7502d9f..3124d96 100644 --- a/src/args.rs +++ b/src/args.rs @@ -296,9 +296,10 @@ pub struct CliArgs { 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")] + #[arg(short = 'I', long, env = "MINISERVE_DISABLE_INDEXING")] pub disable_indexing: bool, } -- cgit v1.2.3 From 7b5b1a0e7fec2c0577eb0dc2a93e9fde48228a67 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Tue, 30 Jan 2024 09:39:43 -0500 Subject: completely disable the listing renderer when indexing is disabled --- src/main.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ce21260..63b26da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::net::{IpAddr, SocketAddr, TcpListener}; use std::thread; use std::time::Duration; -use actix_files::NamedFile; +use actix_files::{Directory, NamedFile}; use actix_web::{ dev::{fn_service, ServiceRequest, ServiceResponse}, http::header::ContentType, @@ -354,9 +354,21 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { let base_path = conf.path.clone(); let no_symlinks = conf.no_symlinks; + if !conf.disable_indexing { + files = files + .show_files_listing() + .files_listing_renderer(listing::directory_listing); + } else { + // If indexing is disabled, we return a 404 for any directory request so we don't + // leak the directory structure. + files = files.show_files_listing().files_listing_renderer( + |_dir: &Directory, req: &HttpRequest| { + let res = HttpResponse::NotFound().body("File not found."); + Ok(ServiceResponse::new(req.clone(), res)) + }, + ); + } files - .show_files_listing() - .files_listing_renderer(listing::directory_listing) .prefer_utf8(true) .redirect_to_slash_directory() .path_filter(move |path, _| { -- cgit v1.2.3 From 20b9126f2b3747686d9198661c227e43a30cd739 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Tue, 30 Jan 2024 09:45:15 -0500 Subject: Revert "completely disable the listing renderer when indexing is disabled" This reverts commit 7b5b1a0e7fec2c0577eb0dc2a93e9fde48228a67. --- src/main.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 63b26da..ce21260 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::net::{IpAddr, SocketAddr, TcpListener}; use std::thread; use std::time::Duration; -use actix_files::{Directory, NamedFile}; +use actix_files::NamedFile; use actix_web::{ dev::{fn_service, ServiceRequest, ServiceResponse}, http::header::ContentType, @@ -354,21 +354,9 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { let base_path = conf.path.clone(); let no_symlinks = conf.no_symlinks; - if !conf.disable_indexing { - files = files - .show_files_listing() - .files_listing_renderer(listing::directory_listing); - } else { - // If indexing is disabled, we return a 404 for any directory request so we don't - // leak the directory structure. - files = files.show_files_listing().files_listing_renderer( - |_dir: &Directory, req: &HttpRequest| { - let res = HttpResponse::NotFound().body("File not found."); - Ok(ServiceResponse::new(req.clone(), res)) - }, - ); - } files + .show_files_listing() + .files_listing_renderer(listing::directory_listing) .prefer_utf8(true) .redirect_to_slash_directory() .path_filter(move |path, _| { -- cgit v1.2.3 From cf46ce687c84fc831b1c7edd50fbc68619792275 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Tue, 30 Jan 2024 09:45:56 -0500 Subject: move check for disable_indexing --- src/listing.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/listing.rs b/src/listing.rs index a8668e3..c837576 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -161,6 +161,14 @@ pub fn directory_listing( let current_user: Option<&CurrentUser> = extensions.get::(); let conf = req.app_data::().unwrap(); + if conf.disable_indexing { + return Ok(ServiceResponse::new( + req.clone(), + HttpResponse::NotFound() + .content_type(mime::TEXT_PLAIN_UTF_8) + .body("File not found."), + )); + } let serve_path = req.path(); let base = Path::new(serve_path); @@ -376,15 +384,6 @@ 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( -- cgit v1.2.3 From a85e128fb4722f6d6260a1034fd853a0bf9a146f Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Wed, 31 Jan 2024 21:53:12 -0500 Subject: tweak doc comment --- src/args.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index 3124d96..e44f27b 100644 --- a/src/args.rs +++ b/src/args.rs @@ -297,8 +297,8 @@ pub struct CliArgs { /// Disable indexing /// - /// This will prevent miniserve from generating directory listings - /// and will return a 404 instead. + /// This will prevent directory listings from being generated + /// and return an error instead. #[arg(short = 'I', long, env = "MINISERVE_DISABLE_INDEXING")] pub disable_indexing: bool, } -- cgit v1.2.3