Skip to content

Commit

Permalink
feat: validate the provided semantic version str
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Mar 18, 2024
1 parent a2861dd commit 3e824bd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum Error {
DateTimeParseError(#[from] chrono::ParseError),
#[error("Could not convert API response header links to string")]
HeaderLinksToStrError,
#[error("The version string is invalid: {0}")]
InvalidVersionFormat(String),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Expand All @@ -29,6 +31,8 @@ pub enum Error {
LatestReleaseNotFound(String),
#[error("{0}")]
PlatformNotSupported(String),
#[error("Could not compile the regex statement")]
RegexError,
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
#[error("Release binary {0} was not found")]
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ impl SafeReleaseRepositoryInterface for SafeReleaseRepository {
dest_path: &Path,
callback: &ProgressCallback,
) -> Result<PathBuf> {
// parse version str.
let version_pattern =
regex::Regex::new(r"^\d+\.\d+\.\d+$").map_err(|_| Error::RegexError)?;
if !version_pattern.is_match(version) {
return Err(Error::InvalidVersionFormat(version.to_string()));
}

let archive_ext = archive_type.to_string();
let url = format!(
"{}/{}-{}-{}.{}",
Expand Down
8 changes: 3 additions & 5 deletions tests/test_download_from_s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn download_and_extract(
}

#[tokio::test]
async fn should_fail_when_trying_to_download_invalid_combination() {
async fn should_fail_when_trying_to_download_with_invalid_version() {
let dest_dir = assert_fs::TempDir::new().unwrap();
let download_dir = dest_dir.child("download_to");
download_dir.create_dir_all().unwrap();
Expand All @@ -91,10 +91,8 @@ async fn should_fail_when_trying_to_download_invalid_combination() {
match result {
Ok(_) => panic!("This test should result in a failure"),
Err(e) => match e {
Error::ReleaseBinaryNotFound(url) => {
assert_eq!(url, "https://sn-cli.s3.eu-west-2.amazonaws.com/safe-x.y.z-x86_64-unknown-linux-musl.tar.gz");
}
_ => panic!("The error type should be ReleaseBinaryNotFound"),
Error::InvalidVersionFormat(_) => {}
_ => panic!("The error type should be InvalidVersionFormat. Got {e:?}"),
},
}
}
Expand Down

0 comments on commit 3e824bd

Please sign in to comment.