mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 21:23:58 +01:00
Merge remote-tracking branch 'onionk/rust-protokeyword1-035'
This commit is contained in:
commit
84e3ada71b
4
changes/bug27687
Normal file
4
changes/bug27687
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
o Minor bugfixes (rust):
|
||||||
|
- protover parsed and accepted unknown protocol names containing invalid
|
||||||
|
characters outside the range [A-Za-z0-9-]. Fixes bug 27687; bugfix on
|
||||||
|
0.3.3.1-alpha.
|
@ -18,6 +18,7 @@ pub enum ProtoverError {
|
|||||||
ExceedsExpansionLimit,
|
ExceedsExpansionLimit,
|
||||||
UnknownProtocol,
|
UnknownProtocol,
|
||||||
ExceedsNameLimit,
|
ExceedsNameLimit,
|
||||||
|
InvalidProtocol,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Descriptive error messages for `ProtoverError` variants.
|
/// Descriptive error messages for `ProtoverError` variants.
|
||||||
@ -48,6 +49,9 @@ impl Display for ProtoverError {
|
|||||||
ProtoverError::ExceedsNameLimit => {
|
ProtoverError::ExceedsNameLimit => {
|
||||||
write!(f, "An unrecognised protocol name was too long.")
|
write!(f, "An unrecognised protocol name was too long.")
|
||||||
}
|
}
|
||||||
|
ProtoverError::InvalidProtocol => {
|
||||||
|
write!(f, "A protocol name includes invalid characters.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,11 +89,17 @@ impl fmt::Display for UnknownProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_valid_proto(s: &str) -> bool {
|
||||||
|
s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
|
||||||
|
}
|
||||||
|
|
||||||
impl FromStr for UnknownProtocol {
|
impl FromStr for UnknownProtocol {
|
||||||
type Err = ProtoverError;
|
type Err = ProtoverError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
if s.len() <= MAX_PROTOCOL_NAME_LENGTH {
|
if !is_valid_proto(s) {
|
||||||
|
Err(ProtoverError::InvalidProtocol)
|
||||||
|
} else if s.len() <= MAX_PROTOCOL_NAME_LENGTH {
|
||||||
Ok(UnknownProtocol(s.to_string()))
|
Ok(UnknownProtocol(s.to_string()))
|
||||||
} else {
|
} else {
|
||||||
Err(ProtoverError::ExceedsNameLimit)
|
Err(ProtoverError::ExceedsNameLimit)
|
||||||
@ -105,6 +111,9 @@ impl UnknownProtocol {
|
|||||||
/// Create an `UnknownProtocol`, ignoring whether or not it
|
/// Create an `UnknownProtocol`, ignoring whether or not it
|
||||||
/// exceeds MAX_PROTOCOL_NAME_LENGTH.
|
/// exceeds MAX_PROTOCOL_NAME_LENGTH.
|
||||||
fn from_str_any_len(s: &str) -> Result<Self, ProtoverError> {
|
fn from_str_any_len(s: &str) -> Result<Self, ProtoverError> {
|
||||||
|
if !is_valid_proto(s) {
|
||||||
|
return Err(ProtoverError::InvalidProtocol);
|
||||||
|
}
|
||||||
Ok(UnknownProtocol(s.to_string()))
|
Ok(UnknownProtocol(s.to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -772,6 +781,29 @@ mod test {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
macro_rules! parse_proto {
|
||||||
|
($e:expr) => {{
|
||||||
|
let proto: Result<UnknownProtocol, _> = $e.parse();
|
||||||
|
let proto2 = UnknownProtocol::from_str_any_len($e);
|
||||||
|
assert_eq!(proto, proto2);
|
||||||
|
proto
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_protocol_from_str() {
|
||||||
|
assert!(parse_proto!("Cons").is_ok());
|
||||||
|
assert!(parse_proto!("123").is_ok());
|
||||||
|
assert!(parse_proto!("1-2-3").is_ok());
|
||||||
|
|
||||||
|
let err = Err(ProtoverError::InvalidProtocol);
|
||||||
|
assert_eq!(err, parse_proto!("a_b_c"));
|
||||||
|
assert_eq!(err, parse_proto!("a b"));
|
||||||
|
assert_eq!(err, parse_proto!("a,"));
|
||||||
|
assert_eq!(err, parse_proto!("b."));
|
||||||
|
assert_eq!(err, parse_proto!("é"));
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! assert_protoentry_is_parseable {
|
macro_rules! assert_protoentry_is_parseable {
|
||||||
($e:expr) => {
|
($e:expr) => {
|
||||||
let protoentry: Result<ProtoEntry, ProtoverError> = $e.parse();
|
let protoentry: Result<ProtoEntry, ProtoverError> = $e.parse();
|
||||||
|
Loading…
Reference in New Issue
Block a user