Hide ptth_server.toml with 403 Forbidden

main
_ 2020-11-08 15:01:15 +00:00
parent f42068db89
commit 345fa64ad0
6 changed files with 30 additions and 10 deletions

View File

@ -69,7 +69,8 @@ async fn handle_all (req: Request <Body>, state: Arc <ServerState <'static>>)
file_server_root, file_server_root,
ptth_req.method, ptth_req.method,
&ptth_req.uri, &ptth_req.uri,
&ptth_req.headers &ptth_req.headers,
None
).await; ).await;
let mut resp = Response::builder () let mut resp = Response::builder ()

View File

@ -14,10 +14,12 @@ struct Opt {
#[tokio::main] #[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> { async fn main () -> Result <(), Box <dyn Error>> {
tracing_subscriber::fmt::init (); tracing_subscriber::fmt::init ();
let config_file = ptth::load_toml::load ("config/ptth_server.toml"); let path = PathBuf::from ("./config/ptth_server.toml");
let config_file = ptth::load_toml::load (&path);
ptth::server::run_server ( ptth::server::run_server (
config_file, config_file,
ptth::graceful_shutdown::init () ptth::graceful_shutdown::init (),
Some (path)
).await ).await
} }

View File

@ -132,7 +132,7 @@ mod tests {
let (stop_server_tx, stop_server_rx) = oneshot::channel (); let (stop_server_tx, stop_server_rx) = oneshot::channel ();
let task_server = { let task_server = {
spawn (async move { spawn (async move {
server::run_server (config_file, stop_server_rx).await.unwrap (); server::run_server (config_file, stop_server_rx, None).await.unwrap ();
}) })
}; };

View File

@ -248,7 +248,8 @@ pub async fn serve_all (
root: &Path, root: &Path,
method: http_serde::Method, method: http_serde::Method,
uri: &str, uri: &str,
headers: &HashMap <String, Vec <u8>> headers: &HashMap <String, Vec <u8>>,
hidden_path: Option <&Path>
) )
-> http_serde::Response -> http_serde::Response
{ {
@ -285,6 +286,14 @@ pub async fn serve_all (
let mut full_path = PathBuf::from (root); let mut full_path = PathBuf::from (root);
full_path.push (path); full_path.push (path);
debug! ("full_path = {:?}", full_path);
if let Some (hidden_path) = hidden_path {
if full_path == hidden_path {
return serve_error (http_serde::StatusCode::Forbidden, "403 Forbidden".into ()).await;
}
}
if let Ok (dir) = read_dir (&full_path).await { if let Ok (dir) = read_dir (&full_path).await {
serve_dir ( serve_dir (
handlebars, handlebars,

View File

@ -29,6 +29,7 @@ struct ServerState {
config: Config, config: Config,
handlebars: Handlebars <'static>, handlebars: Handlebars <'static>,
client: Client, client: Client,
hidden_path: Option <PathBuf>,
} }
fn status_reply (c: http_serde::StatusCode, body: &str) -> http_serde::Response fn status_reply (c: http_serde::StatusCode, body: &str) -> http_serde::Response
@ -76,7 +77,8 @@ async fn handle_req_resp <'a> (
file_server_root, file_server_root,
parts.method, parts.method,
uri, uri,
&parts.headers &parts.headers,
state.hidden_path.as_ref ().map (|p| p.as_path ())
).await ).await
} }
else { else {
@ -138,7 +140,8 @@ pub struct Config {
pub async fn run_server ( pub async fn run_server (
config_file: ConfigFile, config_file: ConfigFile,
shutdown_oneshot: oneshot::Receiver <()> shutdown_oneshot: oneshot::Receiver <()>,
hidden_path: Option <PathBuf>
) )
-> Result <(), Box <dyn Error>> -> Result <(), Box <dyn Error>>
{ {
@ -168,6 +171,7 @@ pub async fn run_server (
}, },
handlebars, handlebars,
client, client,
hidden_path,
}); });
let mut backoff_delay = 0; let mut backoff_delay = 0;

10
todo.md
View File

@ -1,16 +1,16 @@
- Not working behind Nginx (Works okay behind Caddy) - Not working behind Nginx (Works okay behind Caddy)
- Still getting the slow request turtle in FF - 500-900 ms wait time - Reduce idle memory use?
- Folder icons in dir list
- ".." from server to server list is broken - ".." from server to server list is broken
- Redirect to add trailing slashes - Redirect to add trailing slashes
- Add file size in directory listing - Add file size in directory listing
- Allow spaces in server names - Allow spaces in server names
- Make file_server_root mandatory
- Deny unused HTTP methods for endpoints - Deny unused HTTP methods for endpoints
- Hide ptth_server.toml from file server
- ETag cache based on mtime - ETag cache based on mtime
- Server-side hash? - Server-side hash?
- Log / audit log? - Log / audit log?
- Add "Last check-in time" to server list
- Prevent directory traversal attacks in file_server.rs - Prevent directory traversal attacks in file_server.rs
- Error handling - Error handling
@ -27,3 +27,7 @@ Relay can't shut down gracefully if Firefox is connected to it, e.g. if Firefox
kept a connection open while watching a video. kept a connection open while watching a video.
I'm pretty sure this is a bug in Hyper, so for now I've worked around it with a I'm pretty sure this is a bug in Hyper, so for now I've worked around it with a
forced shutdown timer. forced shutdown timer.
Sometimes I get the turtle icon in Firefox's network debugger. But this happens
even with Caddy running a static file server, so I can't prove that it's on my
side. The VPS is cheap, and the datacenter is far away.