Path validation

This commit is contained in:
Matt Brubeck 2020-05-17 12:25:39 -07:00
parent 0e6163dcd9
commit 7eeba3959e

View file

@ -5,14 +5,12 @@ use {
task, task,
}, },
async_tls::TlsAcceptor, async_tls::TlsAcceptor,
rustls::{ rustls::internal::pemfile::{certs, rsa_private_keys},
internal::pemfile::{certs, rsa_private_keys},
},
std::{ std::{
error::Error, error::Error,
fs::File, fs::File,
io::BufReader, io::BufReader,
path::Path, path::{Path, PathBuf},
sync::Arc, sync::Arc,
}, },
url::Url, url::Url,
@ -59,6 +57,14 @@ async fn connection(acceptor: TlsAcceptor, stream: TcpStream) -> Result {
let url = Url::parse(request.trim())?; let url = Url::parse(request.trim())?;
eprintln!("Got request: {:?}", url); eprintln!("Got request: {:?}", url);
let path: PathBuf = url.path_segments().unwrap().collect();
eprintln!("Path: {:?}", path);
let path = Path::new(".").join(path).canonicalize()?;
eprintln!("Path: {:?}", path);
// TODO: Return a not found error
assert!(path.starts_with(std::env::current_dir()?));
let mut stream = stream.into_inner(); let mut stream = stream.into_inner();
stream.write_all(b"20 text/gemini\r\n").await?; stream.write_all(b"20 text/gemini\r\n").await?;
stream.write_all(b"=> ").await?; stream.write_all(b"=> ").await?;