Skip to content

Commit

Permalink
Don't parse an empty string as a token
Browse files Browse the repository at this point in the history
Bare minimum validation :)

Signed-off-by: Christopher Maier <cmaier@chef.io>
  • Loading branch information
christophermaier authored and Gina Peers committed Apr 17, 2019
1 parent 91b558d commit 8aa96fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions components/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum Error {
GossipFileRelativePath(String),
HabitatCore(hcore::Error),
InstallHookFailed(PackageIdent),
InvalidEventStreamToken(String),
InvalidInstallHookMode(String),
/// Occurs when making lower level IO calls.
IO(io::Error),
Expand Down Expand Up @@ -121,6 +122,9 @@ impl fmt::Display for Error {
Error::InstallHookFailed(ref ident) => {
format!("Install hook exited unsuccessfully: {}", ident)
}
Error::InvalidEventStreamToken(ref s) => {
format!("Invalid event stream token provided: '{}'", s)
}
Error::InvalidInstallHookMode(ref e) => {
format!("Invalid InstallHookMode conversion from {}", e)
}
Expand Down Expand Up @@ -185,6 +189,7 @@ impl error::Error for Error {
}
Error::HabitatCore(ref err) => err.description(),
Error::InstallHookFailed(_) => "Install hook exited unsuccessfully",
Error::InvalidEventStreamToken(_) => "Invalid event stream token provided",
Error::InvalidInstallHookMode(_) => "Invalid InstallHookMode",
Error::IO(ref err) => err.description(),
Error::JoinPathsError(ref err) => err.description(),
Expand Down
22 changes: 20 additions & 2 deletions components/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,31 @@ impl AutomateAuthToken {
}

impl FromStr for AutomateAuthToken {
type Err = ();
type Err = Error;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(AutomateAuthToken(s.to_string()))
if s.is_empty() {
Err(Error::InvalidEventStreamToken(s.to_string()))
} else {
Ok(AutomateAuthToken(s.to_string()))
}
}
}

impl fmt::Display for AutomateAuthToken {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) }
}

#[cfg(test)]
mod test {
use super::*;

mod auth_token {
use super::*;

#[test]
fn cannot_parse_from_empty_string() { assert!("".parse::<AutomateAuthToken>().is_err()) }

}

}

0 comments on commit 8aa96fc

Please sign in to comment.