Skip to content

Commit

Permalink
Error when extraneous parameters are included in string to parse
Browse files Browse the repository at this point in the history
In 0.9, we're using UnsupportedModifier to not break the API, but 0.10
will return a new more specific error variant.
  • Loading branch information
mcginty committed Jan 26, 2024
1 parent dbdcc48 commit f280991
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ impl FromStr for NoiseParams {
#[cfg(not(feature = "hfs"))]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut split = s.split('_');
Ok(NoiseParams::new(
let params = NoiseParams::new(
s.to_owned(),
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
))
);
if split.next().is_some() {
return Err(PatternProblem::UnsupportedModifier.into());
}
Ok(params)
}

#[cfg(feature = "hfs")]
Expand All @@ -218,6 +222,9 @@ impl FromStr for NoiseParams {
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
);
if split.next().is_some() {
return Err(PatternProblem::UnsupportedModifier.into());
}

// Validate that a KEM is specified iff the hfs modifier is present
if p.handshake.is_hfs() != p.kem.is_some() {
Expand Down Expand Up @@ -332,4 +339,12 @@ mod tests {
HandshakeTokens::try_from(&p.handshake).unwrap_err()
);
}

#[test]
fn test_extraneous_string_data() {
assert_eq!(
Error::Pattern(PatternProblem::UnsupportedModifier),
"Noise_XXpsk0_25519_AESGCM_SHA256_HackThePlanet".parse::<NoiseParams>().unwrap_err()
);
}
}

0 comments on commit f280991

Please # to comment.