fix(upload): prevent path traversal on upload directory

closes #2
This commit is contained in:
orhun 2021-07-28 00:13:14 +03:00
parent 60c25e2fbc
commit 315585db36
No known key found for this signature in database
GPG key ID: F83424824B3E4B90

View file

@ -1,6 +1,7 @@
use crate::config::Config; use crate::config::Config;
use std::fs::File; use std::fs::File;
use std::io::{Result as IoResult, Write}; use std::io::{Result as IoResult, Write};
use std::path::PathBuf;
/// Writes the bytes to a file in upload directory. /// Writes the bytes to a file in upload directory.
/// ///
@ -10,10 +11,16 @@ use std::io::{Result as IoResult, Write};
/// ///
/// [`default_extension`]: crate::config::PasteConfig::default_extension /// [`default_extension`]: crate::config::PasteConfig::default_extension
/// [`random_url.enabled`]: crate::random::RandomURLConfig::enabled /// [`random_url.enabled`]: crate::random::RandomURLConfig::enabled
pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<String> { pub fn save(file_name: &str, bytes: &[u8], config: &Config) -> IoResult<String> {
if file_name == "-" { let file_name = match PathBuf::from(file_name)
file_name = "stdin"; .file_name()
} .map(|v| v.to_str())
.flatten()
{
Some("-") => String::from("stdin"),
Some(v) => v.to_string(),
None => String::from("file"),
};
let mut path = config.server.upload_path.join(file_name); let mut path = config.server.upload_path.join(file_name);
match path.clone().extension() { match path.clone().extension() {
Some(extension) => { Some(extension) => {