Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not crash in ServerConfig::from_url() on unknown method #1762

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/shadowsocks/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,13 @@ impl ServerConfig {
}
};

let method = method.parse().expect("method");
let method = match method.parse::<CipherKind>() {
Ok(m) => m,
Err(err) => {
error!("failed to parse \"{}\" to CipherKind, err: {:?}", method, err);
return Err(UrlParseError::InvalidMethod);
}
};
let mut svrconfig = ServerConfig::new(addr, pwd, method);

if let Some(q) = parsed.query() {
Expand Down Expand Up @@ -959,6 +965,7 @@ impl ServerConfig {
pub enum UrlParseError {
ParseError(url::ParseError),
InvalidScheme,
InvalidMethod,
InvalidUserInfo,
MissingHost,
InvalidAuthInfo,
Expand All @@ -977,6 +984,7 @@ impl fmt::Display for UrlParseError {
match *self {
UrlParseError::ParseError(ref err) => fmt::Display::fmt(err, f),
UrlParseError::InvalidScheme => write!(f, "URL must have \"ss://\" scheme"),
UrlParseError::InvalidMethod => write!(f, "unknown encryption method"),
UrlParseError::InvalidUserInfo => write!(f, "invalid user info"),
UrlParseError::MissingHost => write!(f, "missing host"),
UrlParseError::InvalidAuthInfo => write!(f, "invalid authentication info"),
Expand All @@ -991,6 +999,7 @@ impl error::Error for UrlParseError {
match *self {
UrlParseError::ParseError(ref err) => Some(err as &dyn error::Error),
UrlParseError::InvalidScheme => None,
UrlParseError::InvalidMethod => None,
UrlParseError::InvalidUserInfo => None,
UrlParseError::MissingHost => None,
UrlParseError::InvalidAuthInfo => None,
Expand Down Expand Up @@ -1405,3 +1414,14 @@ impl FromStr for ReplayAttackPolicy {
}
}
}

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

#[test]
fn test_server_config_from_url() {
let server_config = ServerConfig::from_url("ss://foo:bar@127.0.0.1:9999");
assert!(matches!(server_config, Err(UrlParseError::InvalidMethod)));
}
}
Loading