Correct handle potential errors in `get_path`

This commit is contained in:
Jake Howard 2024-02-25 14:30:24 +00:00
parent fa4836f761
commit 0f83176b5f
No known key found for this signature in database
GPG Key ID: 57AFB45680EDD477
4 changed files with 50 additions and 18 deletions

View File

@ -71,7 +71,11 @@ fn setup(config_folder: &Path) -> IoResult<(Data<RwLock<Config>>, ServerConfig,
// Create necessary directories.
fs::create_dir_all(&server_config.upload_path)?;
for paste_type in &[PasteType::Url, PasteType::Oneshot, PasteType::OneshotUrl] {
fs::create_dir_all(paste_type.get_path(&server_config.upload_path))?;
fs::create_dir_all(
paste_type
.get_path(&server_config.upload_path)
.expect("failed to get path for paste type"),
)?;
}
// Set up a watcher for the configuration file changes.

View File

@ -58,12 +58,12 @@ impl PasteType {
}
/// Returns the given path with [`directory`](Self::get_dir) adjoined.
pub fn get_path(&self, path: &Path) -> PathBuf {
pub fn get_path(&self, path: &Path) -> Option<PathBuf> {
let dir = self.get_dir();
if dir.is_empty() {
path.to_path_buf()
Some(path.to_path_buf())
} else {
util::safe_path_join(path, Path::new(&dir)).unwrap()
util::safe_path_join(path, Path::new(&dir))
}
}
@ -122,12 +122,20 @@ impl Paste {
if let Some(handle_spaces_config) = config.server.handle_spaces {
file_name = handle_spaces_config.process_filename(&file_name);
}
let mut path =
util::safe_path_join(self.type_.get_path(&config.server.upload_path), &file_name)
let mut path = util::safe_path_join(
self.type_
.get_path(&config.server.upload_path)
.ok_or(IoError::new(
IoErrorKind::Other,
String::from("invalid filename"),
))?;
))?,
&file_name,
)
.ok_or(IoError::new(
IoErrorKind::Other,
String::from("invalid filename"),
))?;
let mut parts: Vec<&str> = file_name.split('.').collect();
let mut dotfile = false;
let mut lower_bound = 1;
@ -263,12 +271,19 @@ impl Paste {
file_name = random_text;
}
}
let mut path =
util::safe_path_join(self.type_.get_path(&config.server.upload_path), &file_name)
let mut path = util::safe_path_join(
self.type_
.get_path(&config.server.upload_path)
.ok_or(IoError::new(
IoErrorKind::Other,
String::from("invalid filename"),
))?;
))?,
&file_name,
)
.ok_or(IoError::new(
IoErrorKind::Other,
String::from("invalid filename"),
))?;
if let Some(timestamp) = expiry_date {
path.set_file_name(format!("{file_name}.{timestamp}"));
}
@ -434,7 +449,7 @@ mod tests {
fs::remove_file(file_name)?;
for paste_type in &[PasteType::Url, PasteType::Oneshot] {
fs::create_dir_all(paste_type.get_path(&config.server.upload_path))?;
fs::create_dir_all(paste_type.get_path(&config.server.upload_path).unwrap())?;
}
config.paste.random_url = None;
@ -446,6 +461,7 @@ mod tests {
let file_name = paste.store_file("test.file", Some(expiry_date), None, &config)?;
let file_path = PasteType::Oneshot
.get_path(&config.server.upload_path)
.unwrap()
.join(format!("{file_name}.{expiry_date}"));
assert_eq!("test", fs::read_to_string(&file_path)?);
fs::remove_file(file_path)?;
@ -462,6 +478,7 @@ mod tests {
let file_name = paste.store_url(None, &config)?;
let file_path = PasteType::Url
.get_path(&config.server.upload_path)
.unwrap()
.join(&file_name);
assert_eq!(url, fs::read_to_string(&file_path)?);
fs::remove_file(file_path)?;
@ -489,6 +506,7 @@ mod tests {
.await?;
let file_path = PasteType::RemoteFile
.get_path(&config.server.upload_path)
.unwrap()
.join(file_name);
assert_eq!(
"8c712905b799905357b8202d0cb7a244cefeeccf7aa5eb79896645ac50158ffa",
@ -497,7 +515,7 @@ mod tests {
fs::remove_file(file_path)?;
for paste_type in &[PasteType::Url, PasteType::Oneshot] {
fs::remove_dir(paste_type.get_path(&config.server.upload_path))?;
fs::remove_dir(paste_type.get_path(&config.server.upload_path).unwrap())?;
}
Ok(())

View File

@ -95,8 +95,13 @@ async fn serve(
let mut paste_type = PasteType::File;
if !path.exists() || path.is_dir() {
for type_ in &[PasteType::Url, PasteType::Oneshot, PasteType::OneshotUrl] {
let alt_path = safe_path_join(type_.get_path(&config.server.upload_path), &*file)
.ok_or(error::ErrorInternalServerError("Invalid filename"))?;
let alt_path = safe_path_join(
type_
.get_path(&config.server.upload_path)
.ok_or(error::ErrorInternalServerError("invalid filename"))?,
&*file,
)
.ok_or(error::ErrorInternalServerError("invalid filename"))?;
let alt_path = util::glob_match_file(alt_path)?;
if alt_path.exists()
|| path.file_name().and_then(|v| v.to_str()) == Some(&type_.get_dir())
@ -1090,7 +1095,7 @@ mod tests {
)
.await;
let url_upload_path = PasteType::Url.get_path(&config.server.upload_path);
let url_upload_path = PasteType::Url.get_path(&config.server.upload_path).unwrap();
fs::create_dir_all(&url_upload_path)?;
let response = test::call_service(
@ -1128,7 +1133,9 @@ mod tests {
)
.await;
let oneshot_upload_path = PasteType::Oneshot.get_path(&config.server.upload_path);
let oneshot_upload_path = PasteType::Oneshot
.get_path(&config.server.upload_path)
.unwrap();
fs::create_dir_all(&oneshot_upload_path)?;
let file_name = "oneshot.txt";
@ -1188,7 +1195,9 @@ mod tests {
)
.await;
let url_upload_path = PasteType::OneshotUrl.get_path(&config.server.upload_path);
let url_upload_path = PasteType::OneshotUrl
.get_path(&config.server.upload_path)
.unwrap();
fs::create_dir_all(&url_upload_path)?;
let response = test::call_service(

View File

@ -65,7 +65,8 @@ pub fn get_expired_files(base_path: &Path) -> Vec<PathBuf> {
PasteType::OneshotUrl,
]
.into_iter()
.filter_map(|v| glob(&v.get_path(base_path).join("*.[0-9]*").to_string_lossy()).ok())
.filter_map(|v| v.get_path(base_path))
.filter_map(|v| glob(&v.join("*.[0-9]*").to_string_lossy()).ok())
.flat_map(|glob| glob.filter_map(|v| v.ok()).collect::<Vec<PathBuf>>())
.filter(|path| {
if let Some(extension) = path