From 04f2c037de0304a6924911551fcadac6e8216b85 Mon Sep 17 00:00:00 2001 From: beeb Date: Sat, 11 Feb 2023 14:44:00 +0100 Subject: [PATCH 1/2] fix: sanitize default filename The default filename includes the name of directory being backed up, so it needs to be sanitized --- src/aws.rs | 7 +++++-- src/config.rs | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/aws.rs b/src/aws.rs index 073fdb0..9c6a323 100644 --- a/src/aws.rs +++ b/src/aws.rs @@ -14,7 +14,10 @@ use aws_sdk_s3::{ use aws_smithy_http::byte_stream::Length; use log::*; -use crate::{backup::Archive, config::Params}; +use crate::{ + backup::Archive, + config::{sanitize_filename, Params}, +}; /// In bytes, minimum chunk size of 5MB. Increase CHUNK_SIZE to send larger chunks. const CHUNK_SIZE: u64 = 1024 * 1024 * 5; @@ -52,7 +55,7 @@ pub(crate) async fn upload_file(archive: Archive, params: &Params) -> Result<()> params .folder .file_name() - .map(|f| f.to_string_lossy().to_string()) + .map(|f| sanitize_filename(f.to_string_lossy().to_string())) .unwrap_or("backup".to_string()) ) }); diff --git a/src/config.rs b/src/config.rs index 22fb82a..8eaab62 100644 --- a/src/config.rs +++ b/src/config.rs @@ -118,7 +118,7 @@ pub(crate) async fn parse_config() -> Result { } /// Only keep recommended chars for S3 object keys and truncate to 1000 chars -fn sanitize_filename(filename: impl Into) -> String { +pub(crate) fn sanitize_filename(filename: impl Into) -> String { let mut filename: String = filename.into(); // remove invalid characters filename.retain(|c| c.is_ascii_alphanumeric() || VALID_FILENAME_CHARS.contains(c)); From 8500a3a013934370ffc037dd8c69df74de93f43f Mon Sep 17 00:00:00 2001 From: beeb Date: Sat, 11 Feb 2023 14:55:26 +0100 Subject: [PATCH 2/2] refactor: folder name sanitation --- src/aws.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/aws.rs b/src/aws.rs index 9c6a323..032b4c3 100644 --- a/src/aws.rs +++ b/src/aws.rs @@ -50,14 +50,12 @@ pub(crate) async fn upload_file(archive: Archive, params: &Params) -> Result<()> }) .unwrap_or_else(|| { // default filename is awsbck_ + the folder name + .tar.gz - format!( - "awsbck_{}.tar.gz", - params - .folder - .file_name() - .map(|f| sanitize_filename(f.to_string_lossy().to_string())) - .unwrap_or("backup".to_string()) - ) + let sanitized_folder_name = params + .folder + .file_name() + .map(|f| sanitize_filename(f.to_string_lossy().to_string())) + .unwrap_or("backup".to_string()); + format!("awsbck_{sanitized_folder_name}.tar.gz") }); let multipart_upload_res: CreateMultipartUploadOutput = client .create_multipart_upload()