Pier Angelo Vendrame pushed to branch tor-browser-156.0a1-17.0-2 at The Tor Project / Applications / Tor Browser Commits: 0b737404 by Pier Angelo Vendrame at 2026-11-11T20:25:11+01:00 fixup! TB 44806: Implement the tor integration in Rust. TB 44930: Implement the commands on the Rust control port Simplify how we handle compulsory group in regexes. - - - - - 8c10dd07 by Pier Angelo Vendrame at 2026-11-11T20:25:11+01:00 fixup! TB 44806: Implement the tor integration in Rust. TB 44930: Implement the commands on the Rust control port Implement Debug for Command to be able to use .unwrap_err() in tests. - - - - - 7788d5fa by Pier Angelo Vendrame at 2026-11-11T20:25:12+01:00 fixup! TB 44806: Implement the tor integration in Rust. TB 44930: Implement the commands on the Rust control port Added docstrings to code that was previously merged. - - - - - 136d1f61 by Elena at 2026-11-11T20:25:12+01:00 fixup! TB 44806: Implement the tor integration in Rust. TB 44930: Implement the commands on the Rust control port Implemented parsers for some GETINFO-based commands. - - - - - 938c4cb0 by Elena at 2026-11-11T20:25:12+01:00 fixup! TB 44806: Implement the tor integration in Rust. TB 44930: Implement the commands on the Rust control port Implemented GETINFO commands. - - - - - 64100cf5 by Elena at 2026-11-11T20:25:13+01:00 fixup! TB 44806: Implement the tor integration in Rust. TB 44930: Implement the commands on the Rust control port Implemented Onion auth parsers and commands. - - - - - 23 changed files: - Cargo.lock - toolkit/components/tor-integration/tor_provider/Cargo.toml - toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/command.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/get_info.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/mod.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/onion_auth_add.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/onion_auth_remove.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/onion_auth_view_keys.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/controller.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/error.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/bootstrap_status.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/bridge_line.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/circuit_status.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/client_transport_plugin.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/country_code.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/key_value_pairs.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/mod.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/node_address.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/onion_client_auth_view.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/socks_listeners.rs - + toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/stream_status.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/unescape.rs - toolkit/components/tor-integration/tor_provider/src/ctor/controller/types.rs Changes: ===================================== Cargo.lock ===================================== @@ -8124,6 +8124,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d4f6d1145dcb577acf783d4e601bc1d76a13337bb54e6233add580b07344c8b" dependencies = [ "displaydoc", + "serde", "zerovec", ] @@ -8262,6 +8263,7 @@ name = "tor_provider" version = "0.1.0" dependencies = [ "bytes", + "data-encoding", "hex", "itoa", "lazy_static", @@ -8269,7 +8271,10 @@ dependencies = [ "memchr", "regex", "serde", + "sha3", + "smallvec", "thiserror 2.0.12", + "tinystr", ] [[package]] ===================================== toolkit/components/tor-integration/tor_provider/Cargo.toml ===================================== @@ -6,6 +6,7 @@ edition = "2021" [dependencies] bytes = "1.4.0" +data-encoding = "2.3.3" hex = "0.4.3" itoa = "1.0.15" lazy_static = "1" @@ -13,4 +14,7 @@ log = "0.4" memchr = "2.7.4" regex = "1" serde = { version = "1.0", features = ["derive"] } +sha3 = "0.10.8" +smallvec = { version = "1.15.1", features = ["serde"] } thiserror = "2" +tinystr = { version = "0.8.1", features = ["serde"] } ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/command.rs ===================================== @@ -3,9 +3,66 @@ // <http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. +use std::fmt; + use crate::ctor::{controller::ControllerError, reply_parser::Reply}; pub struct Command<T> { pub command: String, pub handler: Box<dyn Fn(Reply) -> Result<T, ControllerError>>, } + +impl<T> fmt::Debug for Command<T> { + /// Custom formatter that redacts the arguments, as they might contain + /// sensitive information, such as passwords or keys. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut split = self.command.splitn(2, ' '); + let mut command = split.next().unwrap_or_default().to_string(); + if split.next().is_some() { + command.push_str(" ..."); + } + if command.is_empty() { + command = String::from("(empty)"); + } + + f.debug_struct("Command") + .field("command", &command) + .field("handler", &"<handler>") + .finish() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn command_fmt_is_redacted() { + { + let cmd = Command { + command: String::from("QUIT"), + handler: Box::new(|_| Ok(())), + }; + let cmd_fmt = format!("{:?}", cmd); + assert!(cmd_fmt.contains("command: \"QUIT\"")); + } + { + let password = "p4ssw0rd"; + let cmd = Command { + command: format!("SETCONF Socks5ProxyPassword={}", password), + handler: Box::new(|_| Ok(())), + }; + let cmd_fmt = format!("{:?}", cmd); + assert!(cmd_fmt.contains("command: \"SETCONF ...\"")); + assert!(!cmd_fmt.contains(password)); + } + { + let cmd = Command { + command: String::from(""), + handler: Box::new(|_| Ok(())), + }; + let cmd_fmt = format!("{:?}", cmd); + assert!(cmd_fmt.contains("command: \"(empty)\"")); + } + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/get_info.rs ===================================== @@ -0,0 +1,266 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use std::net::IpAddr; +use tinystr::TinyAsciiStr; + +use super::Command; +use crate::ctor::{ + controller::{parsers::*, types::*, ControllerError}, + reply_parser::{DetailReplyLine, Reply}, +}; + +pub fn bootstrap_phase() -> Result<Command<BootstrapStatus>, ControllerError> { + get_info_single( + String::from("status/bootstrap-phase"), + parse_bootstrap_status, + ) +} + +pub fn circuits() -> Result<Command<Vec<Circuit>>, ControllerError> { + get_info(String::from("circuit-status"), |lines| { + lines + .iter() + .filter_map(|line| parse_circuit_built(*line).transpose()) + .collect() + }) +} + +pub fn net_listeners_socks() -> Result<Command<Vec<SocksListener>>, ControllerError> { + get_info_single(String::from("net/listeners/socks"), parse_socks_listeners) +} + +pub fn node_addresses(node: NodeFingerprint) -> Result<Command<NodeAddresses>, ControllerError> { + get_info( + format!("ns/id/{}", hex::encode(node)), + addresses_from_status_info, + ) +} + +pub fn ip_to_country(ip: IpAddr) -> Result<Command<TinyAsciiStr<2>>, ControllerError> { + get_info_single(format!("ip-to-country/{}", ip), parse_country_code) +} + +fn get_info<F, T>(key: String, converter: F) -> Result<Command<T>, ControllerError> +where + F: Fn(&[&[u8]]) -> Result<T, ControllerError> + 'static, +{ + Ok(Command { + command: format!("GETINFO {}\r\n", key), + handler: Box::new(move |reply| parse_get_info(&key, &converter, reply)), + }) +} + +fn get_info_single<F, T>(key: String, converter: F) -> Result<Command<T>, ControllerError> +where + F: Fn(&[u8]) -> Result<T, ControllerError> + 'static, +{ + get_info(key, move |lines| { + if lines.len() != 1 { + Err(ControllerError::MalformedReply(String::from( + "Expected a single line on the reply.", + ))) + } else { + converter(lines[0]) + } + }) +} + +fn parse_get_info<S, F, T>(key: S, converter: &F, reply: Reply) -> Result<T, ControllerError> +where + S: AsRef<str>, + F: Fn(&[&[u8]]) -> Result<T, ControllerError> + 'static, +{ + if let Some(e) = ControllerError::from_reply(&reply) { + return Err(e); + } + + let k = key.as_ref().as_bytes(); + + for line in reply.details().iter() { + match line { + DetailReplyLine::MidReplyLine { code, line } => { + if *code != 250 { + continue; + } + if line.len() < k.len() + 1 { + continue; + } + if line[0..k.len()].eq_ignore_ascii_case(k) && line[k.len()] == b'=' { + return converter(&[&line[(k.len() + 1)..]]); + } + } + DetailReplyLine::DataReplyLine { code, lines } => { + if *code != 250 { + continue; + } + let first = &lines[0]; + if first.len() == k.len() + 1 + && first[0..k.len()].eq_ignore_ascii_case(k) + && first[k.len()] == b'=' + { + let lines: Vec<&[u8]> = lines[1..].iter().map(|b| &b[..]).collect(); + return converter(&lines); + } + } + } + } + + // We should never arrive here. Instead, tor should send 552. + Err(ControllerError::KeyNotFound(key.as_ref().into())) +} + +#[cfg(test)] +mod tests { + use std::assert_matches; + + use super::*; + use crate::ctor::reply_parser::make_reply; + + fn identity_converter(v: &[&[u8]]) -> Result<Vec<Vec<u8>>, ControllerError> { + Ok(v.iter().map(|s| Vec::from(*s)).collect()) + } + + #[test] + fn mid_line() { + assert_eq!( + parse_get_info( + "version", + &identity_converter, + make_reply(b"250-version=0.5.0.0-alpha-dev\r\n250 OK\r\n") + ) + .unwrap(), + &[b"0.5.0.0-alpha-dev"] + ); + } + + #[test] + fn data_line() { + assert_eq!( + parse_get_info( + "test", + &identity_converter, + make_reply(b"250+test=\r\n123\r\n456\r\n.\r\n250 OK\r\n") + ) + .unwrap(), + vec![b"123", b"456"] + ); + } + + #[test] + fn data_line_empty() { + assert_eq!( + parse_get_info( + "test", + &identity_converter, + make_reply(b"250+test=\r\n.\r\n250 OK\r\n") + ) + .unwrap(), + Vec::<&[u8]>::new() + ); + } + + #[test] + fn mid_line_prefix() { + assert_matches!( + parse_get_info( + "version", + &identity_converter, + make_reply(b"250-version__=0.5.0.0-alpha-dev\r\n250 OK\r\n") + ) + .unwrap_err(), + ControllerError::KeyNotFound(_) + ); + } + + #[test] + fn data_line_prefix() { + assert_matches!( + parse_get_info( + "test", + &identity_converter, + make_reply(b"250+test__=\r\n123\r\n456\r\n.\r\n250 OK\r\n") + ) + .unwrap_err(), + ControllerError::KeyNotFound(_) + ); + } + + #[test] + fn short_line_long_key() { + assert_matches!( + parse_get_info( + "averyverylongkey", + &identity_converter, + make_reply(b"250+short=\r\n123\r\n456\r\n.\r\n250 OK\r\n") + ) + .unwrap_err(), + ControllerError::KeyNotFound(_) + ); + assert_matches!( + parse_get_info( + "averyverylooooooooooongkey", + &identity_converter, + make_reply(b"250-short=\r\n250 OK\r\n") + ) + .unwrap_err(), + ControllerError::KeyNotFound(_) + ); + } + + #[test] + fn tor_error() { + assert_eq!( + parse_get_info( + "test", + &identity_converter, + make_reply(b"552 Unrecognized key \"test\"\r\n") + ) + .unwrap_err(), + ControllerError::TorError { + code: 552, + message: String::from("Unrecognized key \"test\"") + } + ); + } + + #[test] + fn error_in_success() { + // This case should not be possible in GETINFO. + // But we want to be sure we can handle it (as a key not found), just in + // case. + assert_matches!( + parse_get_info( + "test", + &identity_converter, + make_reply(b"552-test=0.5.0.0-alpha-dev\r\n250 OK\r\n") + ) + .unwrap_err(), + ControllerError::KeyNotFound(_) + ); + assert_matches!( + parse_get_info( + "test", + &identity_converter, + make_reply(b"552-test=\r\n250 OK\r\n") + ) + .unwrap_err(), + ControllerError::KeyNotFound(_) + ); + } + + #[test] + fn only_end_line() { + assert_matches!( + parse_get_info( + "test", + &identity_converter, + make_reply(b"250 OK\r\n") + ) + .unwrap_err(), + ControllerError::KeyNotFound(_) + ); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/mod.rs ===================================== @@ -6,6 +6,10 @@ mod authenticate; mod command; pub mod get_conf; +pub mod get_info; +mod onion_auth_add; +mod onion_auth_remove; +mod onion_auth_view_keys; mod reset_conf; mod save_conf; mod set_conf; @@ -15,6 +19,9 @@ mod take_ownership; pub use authenticate::authenticate; pub use command::Command; +pub use onion_auth_add::onion_auth_add; +pub use onion_auth_remove::onion_auth_remove; +pub use onion_auth_view_keys::onion_auth_view_keys; pub use reset_conf::reset_owning_controller_process; pub use save_conf::save_conf; pub use set_conf::set_conf; ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/onion_auth_add.rs ===================================== @@ -0,0 +1,106 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::bytes::Regex; + +use super::Command; +use crate::ctor::controller::{parsers::parse_ack, types::*, ControllerError}; + +lazy_static! { + // This is not a strict base64 regex, but we are intrested only in making + // sure user input does not allow command injection. + static ref BASE64ISH_REGEX: Regex = + Regex::new(r"^[A-Za-z0-9+/]*={0,3}$").expect("The regex is hardcoded, it should be good."); +} + +pub fn onion_auth_add( + address: HSAddress, + key: &[u8], + permanent: bool, +) -> Result<Command<u16>, ControllerError> { + if !BASE64ISH_REGEX.is_match(key) { + return Err(ControllerError::InvalidArgument(String::from( + "the key is not in base64", + ))); + } + let key = str::from_utf8(key).map_err(|e| ControllerError::InvalidArgument(e.to_string()))?; + let flags = if permanent { " Flags=Permanent" } else { "" }; + let command = format!( + "ONION_CLIENT_AUTH_ADD {} x25519:{}{}\r\n", + address.as_ref(), + key, + flags + ); + Ok(Command { + command, + handler: Box::new(parse_ack), + }) +} + +#[cfg(test)] +mod tests { + use std::assert_matches; + + use super::*; + + #[test] + fn permanent() { + let command = onion_auth_add( + HSAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd") + .unwrap(), + b"theKEYinbase64", + true, + ) + .unwrap(); + assert!(command.command.to_lowercase().contains("flags=permanent")); + } + + #[test] + fn not_permanent() { + let command = onion_auth_add( + HSAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd") + .unwrap(), + b"theKEYinbase64", + false, + ) + .unwrap(); + assert!(!command.command.to_lowercase().contains("permanent")); + } + + #[test] + fn pad_and_unpadded() { + // Tor is liberal about the necessity of padding, so make sure that + // padded strings are accepted by us even when not padded. + assert!(onion_auth_add( + HSAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd") + .unwrap(), + b"SGVsbG8sIHdvcmxkIQo=", + false, + ) + .is_ok()); + assert!(onion_auth_add( + HSAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd") + .unwrap(), + b"SGVsbG8sIHdvcmxkIQo", + false, + ) + .is_ok()); + } + + #[test] + fn reject_invalid_base64() { + assert_matches!( + onion_auth_add( + HSAddress::from_str("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd") + .unwrap(), + b"NotBase64!!!", + true, + ) + .unwrap_err(), + ControllerError::InvalidArgument(_) + ); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/onion_auth_remove.rs ===================================== @@ -0,0 +1,14 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use super::Command; +use crate::ctor::controller::{parsers::parse_ack, types::*, ControllerError}; + +pub fn onion_auth_remove(address: HSAddress) -> Result<Command<u16>, ControllerError> { + Ok(Command { + command: format!("ONION_CLIENT_AUTH_REMOVE {}\r\n", address.as_ref()), + handler: Box::new(parse_ack), + }) +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/commands/onion_auth_view_keys.rs ===================================== @@ -0,0 +1,14 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use super::Command; +use crate::ctor::controller::{parsers::parse_onion_client_auth_view, types::*, ControllerError}; + +pub fn onion_auth_view_keys() -> Result<Command<Vec<OnionClientAuth>>, ControllerError> { + Ok(Command { + command: String::from("ONION_CLIENT_AUTH_VIEW\r\n"), + handler: Box::new(parse_onion_client_auth_view), + }) +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/controller.rs ===================================== @@ -4,6 +4,8 @@ // copied, modified, or distributed except according to those terms. use bytes::Bytes; +use std::net::IpAddr; +use tinystr::TinyAsciiStr; use super::{ commands::{self, Command}, @@ -115,14 +117,73 @@ impl<CP: ControlPortInterface> TorController<CP> { self.send_command(commands::save_conf(), handler); } - pub fn get_bridges(&self, handler: Box<dyn FnOnce(Result<Vec<Bridge>, ControllerError>)>) { - self.send_command(commands::get_conf::bridges(), handler); + pub fn get_bootstrap_phase( + &self, + handler: Box<dyn FnOnce(Result<BootstrapStatus, ControllerError>)>, + ) { + self.send_command(commands::get_info::bootstrap_phase(), handler); + } + + pub fn get_socks_listeners( + &self, + handler: Box<dyn FnOnce(Result<Vec<SocksListener>, ControllerError>)>, + ) { + self.send_command(commands::get_info::net_listeners_socks(), handler); } // Circuit display + pub fn get_circuits(&self, handler: Box<dyn FnOnce(Result<Vec<Circuit>, ControllerError>)>) { + self.send_command(commands::get_info::circuits(), handler); + } + + pub fn get_node_addresses( + &self, + node: NodeFingerprint, + handler: Box<dyn FnOnce(Result<NodeAddresses, ControllerError>)>, + ) { + self.send_command(commands::get_info::node_addresses(node), handler); + } + + pub fn get_ip_country( + &self, + ip: IpAddr, + handler: Box<dyn FnOnce(Result<TinyAsciiStr<2>, ControllerError>)>, + ) { + self.send_command(commands::get_info::ip_to_country(ip), handler); + } + + pub fn get_bridges(&self, handler: Box<dyn FnOnce(Result<Vec<Bridge>, ControllerError>)>) { + self.send_command(commands::get_conf::bridges(), handler); + } + // Onion authentication + pub fn onion_auth_view_keys( + &self, + handler: Box<dyn FnOnce(Result<Vec<OnionClientAuth>, ControllerError>)>, + ) { + self.send_command(commands::onion_auth_view_keys(), handler); + } + + pub fn onion_auth_add( + &self, + address: HSAddress, + key: &[u8], + permanent: bool, + handler: Box<dyn FnOnce(Result<u16, ControllerError>)>, + ) { + self.send_command(commands::onion_auth_add(address, key, permanent), handler); + } + + pub fn onion_auth_remove( + &self, + address: HSAddress, + handler: Box<dyn FnOnce(Result<u16, ControllerError>)>, + ) { + self.send_command(commands::onion_auth_remove(address), handler); + } + // Miscellaneous pub fn get_pluggable_transports( ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/error.rs ===================================== @@ -5,6 +5,7 @@ use thiserror::Error; +use super::OnionValidationError; use crate::ctor::{ control_port::ControlPortError, reply_parser::{Reply, ReplyError}, @@ -34,6 +35,8 @@ pub enum ControllerError { MalformedReply(String), #[error("the argument passed to the command was not valid: {0}")] InvalidArgument(String), + #[error("invalid onion address: {0}")] + InvalidOnionAddress(#[from] OnionValidationError), } impl ControllerError { ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/bootstrap_status.rs ===================================== @@ -0,0 +1,267 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::bytes::{Regex, RegexBuilder}; + +use super::{parse_key_value_pairs, tor_unescape}; +use crate::ctor::controller::{types::*, ControllerError}; + +lazy_static! { + static ref STATUS_REGEX: Regex = RegexBuilder::new(r"^(NOTICE|WARN) BOOTSTRAP (.+)$") + .case_insensitive(true) + .build() + .expect("The regex is hardcoded, it should be good."); +} + +const STATUS_KEYWORDS: &[&'static str] = + &["PROGRESS", "TAG", "SUMMARY", "RECOMMENDATION", "REASON"]; + +/// Parse a STATUS_CLIENT event. +/// +/// We handle events that have this format: +/// NOTICE BOOTSTRAP PROGRESS=n TAG=keyword SUMMARY="Human readable string" REASON=keyword +/// WARN BOOTSTRAP PROGRESS=n TAG=keyword SUMMARY="Human readable string" REASON=keyword +/// WARN BOOTSTRAP PROGRESS=n TAG=keyword SUMMARY="Human readable string" REASON=keyword RECOMMENDATION=warn +pub fn parse_bootstrap_status(value: &[u8]) -> Result<BootstrapStatus, ControllerError> { + let groups = STATUS_REGEX + .captures(value) + .ok_or_else(|| ControllerError::WrongFormat(String::from_utf8_lossy(value).into_owned()))?; + + let severity = groups[1].to_ascii_uppercase(); + let severity = match severity.as_slice() { + b"NOTICE" => StatusSeverity::Notice, + b"WARN" => StatusSeverity::Warn, + _ => { + // Notice: the regex should make this case impossible... + return Err(ControllerError::MalformedReply(format!( + "unknown severity: {}", + String::from_utf8_lossy(&severity) + ))); + } + }; + + let arguments = parse_key_value_pairs( + groups + .get(2) + .ok_or_else(|| { + ControllerError::MalformedReply(String::from( + "the bootstrap status does not contain the compulsory values", + )) + })? + .as_bytes(), + STATUS_KEYWORDS, + )?; + + let progress = arguments + .get("PROGRESS") + .ok_or_else(|| ControllerError::KeyNotFound(String::from("progress"))) + .and_then(|v| str::from_utf8(v).map_err(|e| ControllerError::MalformedReply(e.to_string()))) + .and_then(|v| { + v.parse().map_err(|e| { + ControllerError::MalformedReply(format!("progress is not an integer: {}", e)) + }) + })?; + if progress > 100 { + return Err(ControllerError::MalformedReply(format!( + "progress exceeds 100: {}", + progress + ))); + } + + // For robustness, we do not check this value. + let tag = String::from_utf8_lossy( + arguments + .get("TAG") + .ok_or_else(|| ControllerError::KeyNotFound(String::from("tag")))?, + ) + .into_owned(); + let summary = String::from_utf8_lossy( + &tor_unescape( + arguments + .get("SUMMARY") + .ok_or_else(|| ControllerError::KeyNotFound(String::from("summary")))?, + ) + .map_err(|e| ControllerError::MalformedReply(e.to_string()))?, + ) + .into_owned(); + let reason = arguments + .get("REASON") + .map(|r| String::from_utf8_lossy(r).into_owned()); + let should_warn = arguments + .get("RECOMMENDATION") + // Recommendation can be either "warn" or "ignore". + // We chose to match warn as we want to be sure before issuing a warn, + // as connection assist will interrupt the bootstrap. + .map_or(false, |r| r.eq_ignore_ascii_case(b"warn")); + + Ok(BootstrapStatus { + severity, + progress, + tag, + summary, + reason, + should_warn, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::assert_matches; + + #[test] + fn without_recommendation() { + let line = b"NOTICE BOOTSTRAP PROGRESS=10 TAG=conn_done SUMMARY=\"Connected to a relay\""; + let status = parse_bootstrap_status(line).unwrap(); + assert_eq!(status.severity, StatusSeverity::Notice); + assert_eq!(status.progress, 10); + assert_eq!(status.tag, "conn_done"); + assert_eq!(status.summary, "Connected to a relay"); + assert!(status.reason.is_none()); + assert!(!status.should_warn); + } + + #[test] + fn with_recommendation() { + const PREFIX: &[u8] = + b"WARN BOOTSTRAP PROGRESS=14 TAG=handshake SUMMARY=\"Handshaking with a relay\" "; + { + let line = vec![PREFIX, b"RECOMMENDATION=warn"].concat(); + let status = parse_bootstrap_status(&line).unwrap(); + assert!(status.should_warn); + } + { + let line = vec![PREFIX, b"RECOMMENDATION=ignore"].concat(); + let status = parse_bootstrap_status(&line).unwrap(); + assert!(!status.should_warn); + } + { + let line = vec![PREFIX, b"RECOMMENDATION=invalid"].concat(); + let status = parse_bootstrap_status(&line).unwrap(); + assert!(!status.should_warn); + } + { + let line = vec![PREFIX, b"RECOMMENDATION="].concat(); + let status = parse_bootstrap_status(&line).unwrap(); + assert!(!status.should_warn); + } + } + + #[test] + fn with_reason() { + let line = b"WARN BOOTSTRAP PROGRESS=30 TAG=conn SUMMARY=\"Connecting to a relay\" REASON=CLOCK_SKEW"; + let status = parse_bootstrap_status(&line[..]).unwrap(); + assert_eq!(status.severity, StatusSeverity::Warn); + assert_eq!(status.progress, 30); + assert_eq!(status.tag, "conn"); + assert_eq!(status.summary, "Connecting to a relay"); + assert_eq!(status.reason.unwrap(), "CLOCK_SKEW"); + assert!(!status.should_warn); + } + + #[test] + fn case_insensitive() { + let line = b"wARn Bootstrap pRoGrEsS=10 tAg=conn_DONE sUmmaRY=\"connected TO a ReLaY\" reCOmmeNdATIOn=waRn"; + let status = parse_bootstrap_status(line).unwrap(); + assert_eq!(status.severity, StatusSeverity::Warn); + assert_eq!(status.progress, 10); + assert_eq!(status.tag, "conn_DONE"); + assert_eq!(status.summary, "connected TO a ReLaY"); + assert!(status.should_warn); + } + + #[test] + fn invalid_severity() { + let line = b"info bootstrap PROGRESS=10 TAG=test SUMMARY=\"Test\""; + assert_matches!( + parse_bootstrap_status(line).unwrap_err(), + ControllerError::WrongFormat(_) + ); + } + + #[test] + fn invalid_progress() { + // Sanity tests. + assert!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=0 TAG=test SUMMARY=\"Test\"") + .is_ok() + ); + assert!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=1 TAG=test SUMMARY=\"Test\"") + .is_ok() + ); + assert!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=10 TAG=test SUMMARY=\"Test\"") + .is_ok() + ); + assert!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=99 TAG=test SUMMARY=\"Test\"") + .is_ok() + ); + assert!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=100 TAG=test SUMMARY=\"Test\"") + .is_ok() + ); + + // Invalid lines. + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=aaa TAG=test SUMMARY=\"Test\"") + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=101 TAG=test SUMMARY=\"Test\"") + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + } + + #[test] + fn missing_args() { + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap TAG=test SUMMARY=\"Test\"").unwrap_err(), + ControllerError::KeyNotFound(_) + ); + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=3 SUMMARY=\"Test\"").unwrap_err(), + ControllerError::KeyNotFound(_) + ); + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=3 TAG=test").unwrap_err(), + ControllerError::KeyNotFound(_) + ); + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap PROGRESS=3").unwrap_err(), + ControllerError::KeyNotFound(_) + ); + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap TAG=test").unwrap_err(), + ControllerError::KeyNotFound(_) + ); + assert_matches!( + parse_bootstrap_status(b"NOTICE bootstrap SUMMARY=\"Test\"").unwrap_err(), + ControllerError::KeyNotFound(_) + ); + } + + #[test] + fn reject_garbage() { + assert_matches!( + parse_bootstrap_status(b"UNALLOWED_PREFIX NOTICE BOOTSTRAP PROGRESS=10 TAG=conn_done SUMMARY=\"Connected to a relay\"").unwrap_err(), + ControllerError::WrongFormat(_) + ); + assert!(parse_bootstrap_status(b"NOTICE BOOTSTRAP PROGRESS=10 TAG=conn_done SUMMARY=\"Connected to a relay\" UNKNOWN_ARG=\"value\"").is_ok()); + assert!(parse_bootstrap_status(b"NOTICE BOOTSTRAP PROGRESS=10 UNKNOWN_ARG=\"value\" TAG=conn_done SUMMARY=\"Connected to a relay\"").is_ok()); + assert!(parse_bootstrap_status(b"NOTICE BOOTSTRAP PROGRESS=10 TAG=conn_done SUMMARY=\"Connected to a relay\" UNKNOWN_ARG=").is_ok()); + assert!(parse_bootstrap_status(b"NOTICE BOOTSTRAP PROGRESS=10 UNKNOWN_ARG= TAG=conn_done SUMMARY=\"Connected to a relay\"").is_ok()); + // Arguments that are not key=value pair (e.g., they are missing the + // equal sign) are rejected. + assert!(parse_bootstrap_status(b"NOTICE BOOTSTRAP INVALID_ARG PROGRESS=10 TAG=conn_done SUMMARY=\"Connected to a relay\"").is_err()); + assert!(parse_bootstrap_status(b"NOTICE BOOTSTRAP PROGRESS=10 INVALID_ARG TAG=conn_done SUMMARY=\"Connected to a relay\"").is_err()); + assert!(parse_bootstrap_status(b"NOTICE BOOTSTRAP PROGRESS=10 TAG=conn_done SUMMARY=\"Connected to a relay\" INVALID_ARG").is_err()); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/bridge_line.rs ===================================== @@ -33,6 +33,9 @@ lazy_static! { .expect("The regex is hardcoded, it should be good."); } +/// Errors we might encounter when parsing the bridge line. +/// We have a specific type in case we want to re-use this parser for other +/// purposes. #[derive(Error, Debug, Clone, PartialEq, Eq)] pub enum BridgeParseError { #[error("the bridge line has a wrong format and could not be parsed")] @@ -47,6 +50,19 @@ pub enum BridgeParseError { ArgsWithoutTransport, } +/// Parse a bridge line. +/// +/// Supported lines are: +/// - <address> [<fingerprint>] +/// - transport <address> [<fingerprint>] [<optional-arguments>] +/// +/// There is not a spec for bridge lines. +/// The closest one is on the man page, but checking src/app/config/config.c of +/// c-tor reveals more information such as: +/// - "vanilla" bridges cannot have optional arguments (as they do not have a +/// pluggable transport) +/// - transports must be C identifiers (i.e., they can contain letters, digits +/// and '_', but cannot start with a number). pub fn parse_bridge_line(value: &[u8]) -> Result<Bridge, BridgeParseError> { let caps = BRIDGE_REGEX .captures(value) ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/circuit_status.rs ===================================== @@ -0,0 +1,327 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::bytes::{Regex, RegexBuilder}; +use smallvec::SmallVec; + +use super::parse_key_value_pairs; +use crate::ctor::controller::{types::*, ControllerError}; + +lazy_static! { + // https://spec.torproject.org/control-spec/replies.html#CIRC + static ref CIRCUIT_BUILT_REGEX: Regex = RegexBuilder::new( + "^(?<CircuitID>[a-zA-Z0-9]{1,16}) BUILT (?<Path>[A-Za-z0-9,$~]+)(?: (?<Args>.*))?$", + ) + .case_insensitive(true) + .build() + .expect("The regex is hardcoded, it should be good."); + static ref NODE_REGEX: Regex = + Regex::new("^\\$(?<Fingerprint>[0-9a-fA-F]{40})(?:~[a-zA-Z0-9]+)?$") + .expect("The regex is hardcoded, it should be good."); +} + +/// The keywords we are interested in parsing for circuit statuses. +const CIRC_KEYWORDS: &[&'static str] = &["CONFLUX_ID"]; + +/// Parse CIRCUIT BUILT events. +/// We parse lines that look like `<circuitID> BUILT <path> <args>`. +/// We ignore circuits that do not match it, so that it can be used also to +/// parse the output of `GETINFO circuit-status`, as long as every line is +/// passed individually. +/// However, if a CIRCUIT looks like a BUILT but has some errors, we might +/// return an error instead (e.g., if one of the nodes in the path has a wrong +/// fingerprint length, or if the keyword-value pairs are encoded in a wrong +/// format). +/// +/// The only keyword argument we parse is the conflux ID. +pub fn parse_circuit_built(line: &[u8]) -> Result<Option<Circuit>, ControllerError> { + let Some(circuit) = CIRCUIT_BUILT_REGEX.captures(line) else { + // We might parse the status of all circuits together. + // Therefore we do not want to error when we are dealing with a status + // that is not "BUILT" but something else. + // Also, our regex forces a path to be available, even though the spec + // does not explicitly require it. However, we are already returning + // None, so we do not need to handle that case separately. + return Ok(None); + }; + + let id = CircuitID::new(&circuit["CircuitID"]).ok_or_else(|| { + ControllerError::MalformedReply(format!( + "invalid circuit id {}", + String::from_utf8_lossy(&circuit["CircuitID"]) + )) + })?; + + let path = circuit["Path"] + .split(|b| *b == b',') + .map(|node| { + let node = NODE_REGEX.captures(node).ok_or_else(|| { + ControllerError::MalformedReply(format!( + "Invalid path item: {}", + String::from_utf8_lossy(node) + )) + })?; + let mut fingerprint = NodeFingerprint::default(); + hex::decode_to_slice(&node["Fingerprint"], &mut fingerprint) + .map_err(|e| ControllerError::MalformedReply(e.to_string()))?; + Ok(fingerprint) + }) + .collect::<Result<SmallVec<_>, ControllerError>>()?; + debug_assert!( + !path.is_empty(), + "The path in the regex was not empty, if we arrived here we must have a path item." + ); + + let conflux_id = circuit + .name("Args") + .map(|args| parse_key_value_pairs(args.as_bytes(), CIRC_KEYWORDS)) + .transpose()? + .and_then(|args| { + let mut id = ConfluxID::default(); + args.get("CONFLUX_ID") + .and_then(|s| hex::decode_to_slice(s, &mut id).ok())?; + Some(id) + }); + + Ok(Some(Circuit { + id, + path, + conflux_id, + })) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn fingerprint_from_hex(hex: &str) -> NodeFingerprint { + let mut fp = NodeFingerprint::default(); + hex::decode_to_slice(hex, &mut fp).unwrap(); + fp + } + + fn conflux_id_from_hex(hex: &str) -> ConfluxID { + let mut id = ConfluxID::default(); + hex::decode_to_slice(hex, &mut id).unwrap(); + id + } + + #[test] + fn parse_single_node_circuit() { + let input = b"1 BUILT $0000000000000000000000000000000000000001"; + let circuit = parse_circuit_built(input).unwrap().unwrap(); + + assert_eq!(circuit.id.as_str(), "1"); + assert_eq!(circuit.path.len(), 1); + assert_eq!( + circuit.path[0], + fingerprint_from_hex("0000000000000000000000000000000000000001") + ); + assert_eq!(circuit.conflux_id, None); + } + + #[test] + fn parse_three_node_circuit() { + let input = b"abc123 BUILT $aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,$bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,$cccccccccccccccccccccccccccccccccccccccc"; + let circuit = parse_circuit_built(input).unwrap().unwrap(); + + assert_eq!(circuit.id.as_str(), "abc123"); + assert_eq!(circuit.path.len(), 3); + assert_eq!( + circuit.path[0], + fingerprint_from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") + ); + assert_eq!( + circuit.path[1], + fingerprint_from_hex("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb") + ); + assert_eq!( + circuit.path[2], + fingerprint_from_hex("cccccccccccccccccccccccccccccccccccccccc") + ); + } + + #[test] + fn parse_with_conflux_id() { + let input = b"qwerty999 BUILT $1111111111111111111111111111111111111111 CONFLUX_ID=0102030405060708090a0b0c0d0e0f10"; + let circuit = parse_circuit_built(input).unwrap().unwrap(); + + assert_eq!(circuit.id.as_str(), "qwerty999"); + assert_eq!(circuit.path.len(), 1); + assert_eq!( + circuit.conflux_id.unwrap(), + conflux_id_from_hex("0102030405060708090a0b0c0d0e0f10") + ); + } + + #[test] + fn parse_with_nicknames() { + let input = b"1234 BUILT $1111111111111111111111111111111111111111~relay1,$2222222222222222222222222222222222222222~relay2,$3333333333333333333333333333333333333333~relay3"; + let circuit = parse_circuit_built(input).unwrap().unwrap(); + + assert_eq!(circuit.id.as_str(), "1234"); + assert_eq!(circuit.path.len(), 3); + assert_eq!( + circuit.path[0], + fingerprint_from_hex("1111111111111111111111111111111111111111") + ); + assert_eq!( + circuit.path[1], + fingerprint_from_hex("2222222222222222222222222222222222222222") + ); + assert_eq!( + circuit.path[2], + fingerprint_from_hex("3333333333333333333333333333333333333333") + ); + } + + #[test] + fn parse_mixed_case() { + let input = b"test BuiLt $AaBbCcDdEeFf0123456789AaBbCcDdEeFf012345 conflux_ID=0102030405060708090a0b0c0d0E0F10"; + let circuit = parse_circuit_built(input).unwrap().unwrap(); + + assert_eq!(circuit.path.len(), 1); + assert_eq!( + circuit.path[0], + fingerprint_from_hex("aabbccddeeff0123456789aabbccddeeff012345") + ); + assert_eq!( + circuit.conflux_id.unwrap(), + conflux_id_from_hex("0102030405060708090a0b0c0d0e0f10") + ); + } + + #[test] + fn parse_multiple_args() { + let input = b"ID1 built $1111111111111111111111111111111111111111 PURPOSE=GENERAL CONFLUX_ID=aabbccddeeff00112233445566778899"; + let circuit = parse_circuit_built(input).unwrap().unwrap(); + + assert_eq!(circuit.id.as_str(), "ID1"); + assert_eq!( + circuit.conflux_id.unwrap(), + conflux_id_from_hex("aabbccddeeff00112233445566778899") + ); + } + + #[test] + fn long_circuit() { + let input = b"id1 BUILT $1111111111111111111111111111111111111111,$2222222222222222222222222222222222222222,$3333333333333333333333333333333333333333,$4444444444444444444444444444444444444444"; + let circuit = parse_circuit_built(input).unwrap().unwrap(); + assert_eq!(circuit.path.len(), 4); + } + + #[test] + fn not_built() { + { + let input = b"1000 EXTENDED $1111111111111111111111111111111111111111,$2222222222222222222222222222222222222222"; + assert!(parse_circuit_built(input).unwrap().is_none()); + } + { + let input = b"2000 CLOSED"; + assert!(parse_circuit_built(input).unwrap().is_none()); + } + { + let input = b"3000 GUARD_WAIT"; + assert!(parse_circuit_built(input).unwrap().is_none()); + } + } + + #[test] + fn missing_path() { + let input = b"circuit1 BUILT"; + assert!(parse_circuit_built(input).unwrap().is_none()); + } + + #[test] + fn missing_circuit_id() { + assert!( + parse_circuit_built(b"BUILT $1111111111111111111111111111111111111111") + .unwrap() + .is_none() + ); + assert!( + parse_circuit_built(b" BUILT $1111111111111111111111111111111111111111") + .unwrap() + .is_none() + ); + } + + #[test] + fn missing_status_keyword() { + let input = b"circuit1 $1111111111111111111111111111111111111111"; + assert!(parse_circuit_built(input).unwrap().is_none()); + } + + #[test] + fn invalid_circuit_id() { + { + // CircuitID must be alphanumeric only + let input = b"circuit+1 BUILT $1111111111111111111111111111111111111111"; + assert!(parse_circuit_built(input).unwrap().is_none()); + } + { + // CircuitID max length is 16 + let input = b"12345678901234567 BUILT $1111111111111111111111111111111111111111"; + assert!(parse_circuit_built(input).unwrap().is_none()); + } + } + + #[test] + fn invalid_fingerprint() { + assert!(parse_circuit_built(b"circuit1 BUILT $123456").is_err()); + assert!(parse_circuit_built( + b"circuit1 BUILT $11111111111111111111111111111111111111112222" + ) + .is_err()); + assert!( + parse_circuit_built(b"circuit1 BUILT $ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ") + .is_err() + ); + } + + #[test] + fn partially_invalid_path() { + assert!(parse_circuit_built(b"1 BUILT $1111111111111111111111111111111111111111,$222222222222222222222222222222222222222").is_err()); + assert!(parse_circuit_built(b"2 BUILT $1111111111111111111111111111111111111111,junk,$2222222222222222222222222222222222222222").is_err()); + assert!(parse_circuit_built(b"3 BUILT BUILT $1111111111111111111111111111111111111111$2222222222222222222222222222222222222222").is_err()); + assert!( + parse_circuit_built(b"4 BUILT $1111111111111111111111111111111111111111,").is_err() + ); + } + + #[test] + fn invalid_conflux_id() { + // Wrong conflux ID is not a fatal error. + { + let input = + b"circuit1 BUILT $1111111111111111111111111111111111111111 CONFLUX_ID=GGGGGGGGGGGGGGGG"; + let result = parse_circuit_built(input).unwrap().unwrap(); + assert!(result.conflux_id.is_none()); + } + { + let input = b"circuit1 BUILT $1111111111111111111111111111111111111111 CONFLUX_ID=0102"; + let result = parse_circuit_built(input).unwrap().unwrap(); + assert!(result.conflux_id.is_none()); + } + { + let input = b"circuit1 BUILT $1111111111111111111111111111111111111111 CONFLUX_ID=000000001111111122222222"; + let result = parse_circuit_built(input).unwrap().unwrap(); + assert!(result.conflux_id.is_none()); + } + } + + #[test] + fn invalid_path() { + let input = b"circuit1 BUILT $"; + assert!(parse_circuit_built(input).is_err()); + } + + #[test] + fn missing_dollar_sign() { + let input = b"circuit1 BUILT 1111111111111111111111111111111111111111"; + assert!(parse_circuit_built(input).is_err()); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/client_transport_plugin.rs ===================================== @@ -19,6 +19,20 @@ lazy_static! { .expect("The regex is hardcoded, it should be good."); } +/// Parse the definition of a pluggable transport. +/// We handle these formats (from tor's man page): +/// - <transports> socks4 <ip:port> +/// - <transports> socks5 <ip:port> +/// - <transports> exec <path> [<options>] +/// +/// Note: +/// - each line can handle multiple transports (they are comma separated) +/// - IPv4 and IPv6 addresses are supported. Hostnames and Unix sockets are not +/// - we accept path with encoded strings, but tor might now. Also, the +/// conversion between bytes and Windows paths (UTF-16ish) is not well defined +/// - options can have spaces +/// - tor allows to configure multiple transport plugins. This function must be +/// called with one defition at the time pub fn parse_client_transport_plugin(line: &[u8]) -> Result<PluggableTransport, ControllerError> { if let Some(socks_line) = SOCKS_REGEX.captures(line) { parse_socks(&socks_line) @@ -31,11 +45,9 @@ pub fn parse_client_transport_plugin(line: &[u8]) -> Result<PluggableTransport, } } +/// Check and split the transports fn get_transports(c: &Captures) -> Result<Vec<String>, ControllerError> { - let transports = c - .name("transport") - .ok_or(ControllerError::KeyNotFound(String::from("transport")))? - .as_bytes() + let transports = c["transport"] .split(|b| *b == b',') .map(|t| String::from_utf8_lossy(t).into_owned()) .collect::<Vec<_>>(); @@ -47,21 +59,13 @@ fn get_transports(c: &Captures) -> Result<Vec<String>, ControllerError> { Ok(transports) } +/// Parse the socks4/socks5 cases fn parse_socks(socks_line: &Captures) -> Result<PluggableTransport, ControllerError> { - let address = str::from_utf8( - socks_line - .name("address") - .ok_or(ControllerError::KeyNotFound(String::from("address")))? - .as_bytes(), - ) - .expect("The regex matches only ASCII characters (hence valid UTF-8)") - .parse() - .map_err(|e| ControllerError::MalformedReply(format!("{}", e)))?; - let plugin = match socks_line - .name("protocol") - .ok_or(ControllerError::KeyNotFound(String::from("protocol")))? - .as_bytes() - { + let address = str::from_utf8(&socks_line["address"]) + .expect("The regex matches only ASCII characters (hence valid UTF-8)") + .parse() + .map_err(|e| ControllerError::MalformedReply(format!("{}", e)))?; + let plugin = match &socks_line["protocol"] { b"socks4" => ClientTransportPlugin::Socks4(address), b"socks5" => ClientTransportPlugin::Socks5(address), _ => unreachable!("We validated this with the regex"), @@ -72,19 +76,15 @@ fn parse_socks(socks_line: &Captures) -> Result<PluggableTransport, ControllerEr }) } +/// Parse the exec cases fn parse_exec(exec_line: &Captures) -> Result<PluggableTransport, ControllerError> { let options = exec_line .name("options") .map(|o| o.as_bytes()) .and_then(|s| if s.is_empty() { None } else { Some(s.into()) }); - let path = tor_unescape( - exec_line - .name("path") - .ok_or(ControllerError::KeyNotFound(String::from("path")))? - .as_bytes(), - ) - .map_err(|e| ControllerError::MalformedReply(e.to_string()))? - .into_owned(); + let path = tor_unescape(&exec_line["path"]) + .map_err(|e| ControllerError::MalformedReply(e.to_string()))? + .into_owned(); if path.is_empty() { return Err(ControllerError::MalformedReply(String::from("empty path"))); } ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/country_code.rs ===================================== @@ -0,0 +1,110 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use tinystr::TinyAsciiStr; + +use crate::ctor::ControllerError; + +/// Parse the country codes as passed by the +/// `GETINFO ip-to-country/<ip-address>` command. +/// The function checks that the country code is composed by two letters, or it +/// is ??. Anything else will be rejected. +/// Cases will be preserved. +pub fn parse_country_code(line: &[u8]) -> Result<TinyAsciiStr<2>, ControllerError> { + if line.len() != 2 { + return Err(ControllerError::MalformedReply(format!( + "expected two bytes (got {})", + line.len(), + ))); + } + let code = TinyAsciiStr::try_from_utf8(line) + .map_err(|e| ControllerError::MalformedReply(e.to_string()))?; + if code != "??" && !code.is_ascii_alphabetic() { + return Err(ControllerError::MalformedReply(String::from( + "country codes must be 2 letters or be ??", + ))); + } + Ok(code) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::assert_matches; + + #[test] + fn country_code() { + assert_eq!(parse_country_code(b"IT").unwrap(), "IT"); + } + + #[test] + fn unknown() { + assert_eq!(parse_country_code(b"??").unwrap(), "??"); + } + + #[test] + fn case_preserved() { + assert_eq!(parse_country_code(b"aa").unwrap(), "aa"); + assert_eq!(parse_country_code(b"aB").unwrap(), "aB"); + assert_eq!(parse_country_code(b"Ba").unwrap(), "Ba"); + assert_eq!(parse_country_code(b"BB").unwrap(), "BB"); + } + + #[test] + fn too_short() { + assert_matches!( + parse_country_code(b"I").unwrap_err(), + ControllerError::MalformedReply(_) + ); + } + + #[test] + fn too_long() { + assert_matches!( + parse_country_code(b"ITA").unwrap_err(), + ControllerError::MalformedReply(_) + ); + } + + #[test] + fn not_ascii() { + assert_matches!( + parse_country_code(b"\xF0\xF0").unwrap_err(), + ControllerError::MalformedReply(_) + ); + } + + #[test] + fn empty() { + assert_matches!( + parse_country_code(b"").unwrap_err(), + ControllerError::MalformedReply(_) + ); + } + + #[test] + fn not_alphabetic() { + assert_matches!( + parse_country_code(b"12").unwrap_err(), + ControllerError::MalformedReply(_) + ); + assert_matches!( + parse_country_code(b"A2").unwrap_err(), + ControllerError::MalformedReply(_) + ); + assert_matches!( + parse_country_code(b"1B").unwrap_err(), + ControllerError::MalformedReply(_) + ); + assert_matches!( + parse_country_code(b"?c").unwrap_err(), + ControllerError::MalformedReply(_) + ); + assert_matches!( + parse_country_code(b"d?").unwrap_err(), + ControllerError::MalformedReply(_) + ); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/key_value_pairs.rs ===================================== @@ -0,0 +1,189 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::bytes::Regex; +use std::collections::HashMap; + +use crate::ctor::controller::ControllerError; + +lazy_static! { + // Matching the final group will assure we reject quotes in any unquoted + // string. + static ref KEY_VALUE_PAIR_REGEX: Regex = Regex::new(r#"^ *([A-Za-z0-9_]+)=("(?:\\.|[^"])*"|[^ "]*)(?: +|$)"#) + .expect("The regex is hardcoded, it should be good."); +} + +/// Get multiple key-value pairs from a line. +/// +/// Often the commands expect values to follow a certain format for the values. +/// Validation needs to be performed by the caller. +/// +/// Also, usually keywords are defined with double quotes, therefore they should +/// be treated as case-insensitive. +/// This function compares the keyword it finds with the keywords from the list +/// in a case-insensitive fashion, and uses the list's string as a key. +/// +/// All arguments are processed regardless of them being in the list, and the +/// order in allowed_keys does not influence how this function works. +/// In other words, this function does not validate the order in which the +/// key/value pairs appear in the line. +/// +/// Repeated entries are silently tolerated, and the last matching element will +/// be assigned to the returned map. However, callers should not depend on this +/// behavior. +pub(super) fn parse_key_value_pairs<'a>( + line: &'a [u8], + allowed_keys: &[&'static str], +) -> Result<HashMap<&'static str, &'a [u8]>, ControllerError> { + let mut values = HashMap::new(); + let mut pos = 0; + while pos < line.len() { + let Some(kv) = KEY_VALUE_PAIR_REGEX.captures(&line[pos..]) else { + return Err(ControllerError::MalformedReply(format!( + "invalid key/value pair: {}", + String::from_utf8_lossy(&line[pos..]) + ))); + }; + pos += kv[0].len(); + + let key = match allowed_keys + .iter() + .find(|k| kv[1].eq_ignore_ascii_case(k.as_bytes())) + { + Some(k) => *k, + None => { + continue; + } + }; + // Notice that kv[2] does not work here as it has explicitly the same + // lifetime as the Captures, rather than the original buffer's. + let value = kv.get(2).expect("Group 2 is compulsory.").as_bytes(); + values.insert(key, value); + } + Ok(values) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn single_pair() { + { + let values = parse_key_value_pairs(b"KEY=Value", &["KEY"]).unwrap(); + assert_eq!(values.len(), 1); + assert_eq!(values.get("KEY").unwrap(), b"Value"); + } + { + let values = parse_key_value_pairs(b"KEY=\"Value with quotes\"", &["KEY"]).unwrap(); + assert_eq!(values.len(), 1); + assert_eq!(values.get("KEY").unwrap(), b"\"Value with quotes\""); + } + { + let values = parse_key_value_pairs(b"KEY=\"Value\"", &["KEY"]).unwrap(); + assert_eq!(values.len(), 1); + assert_eq!(values.get("KEY").unwrap(), b"\"Value\""); + } + } + + #[test] + fn empty_value() { + let values = parse_key_value_pairs(b"KEY=", &["KEY"]).unwrap(); + assert_eq!(values.len(), 1); + assert_eq!(values.get("KEY").unwrap(), b""); + } + + #[test] + fn quotes() { + let values = parse_key_value_pairs(b"KEY=\"Value with \\\" esccape\"", &["KEY"]).unwrap(); + assert_eq!(values.len(), 1); + assert_eq!(values.get("KEY").unwrap(), b"\"Value with \\\" esccape\""); + } + + #[test] + fn test_multiple() { + let values = parse_key_value_pairs( + b"GOOD=yes QUOTED=\"Quoted value\" ANOTHER=1", + &["GOOD", "NOT_FOUND", "QUOTED", "ANOTHER"], + ) + .unwrap(); + assert_eq!(values.len(), 3); + assert_eq!(values.get("GOOD").unwrap(), b"yes"); + assert_eq!(values.get("QUOTED").unwrap(), b"\"Quoted value\""); + assert_eq!(values.get("ANOTHER").unwrap(), b"1"); + assert!(values.get("NOT_FOUND").is_none()); + } + + #[test] + fn ignore_if_unallowed() { + let values = parse_key_value_pairs(b"A=1 B=2 C=\"Quoted\"", &["A", "C"]).unwrap(); + assert_eq!(values.len(), 2); + assert_eq!(values.get("A").unwrap(), b"1"); + assert!(values.get("B").is_none()); + assert_eq!(values.get("C").unwrap(), b"\"Quoted\""); + } + + #[test] + fn only_in_allow_list() { + let values = parse_key_value_pairs(b"A=1 C=\"Quoted\"", &["A", "B", "C"]).unwrap(); + assert_eq!(values.len(), 2); + assert_eq!(values.get("A").unwrap(), b"1"); + assert!(values.get("B").is_none()); + assert_eq!(values.get("C").unwrap(), b"\"Quoted\""); + } + + #[test] + fn case_insensitive_keys() { + let values = + parse_key_value_pairs(b"A=123 tESt=value KEY=val", &["a", "TesT", "kEy"]).unwrap(); + assert_eq!(values.len(), 3); + assert_eq!(values.get("a").unwrap(), b"123"); + assert_eq!(values.get("TesT").unwrap(), b"value"); + assert_eq!(values.get("kEy").unwrap(), b"val"); + } + + #[test] + fn missing_equal_sign() { + assert!(parse_key_value_pairs(b"KEY", &["KEY"]).is_err()); + assert!(parse_key_value_pairs( + b"VALUE=something MISSING COUNT=3 QUOTED=\"Quoted value\"", + &["VALUE", "MISSING", "COUNT", "QUOTED"] + ) + .is_err()); + assert!(parse_key_value_pairs( + b"MISSING VALUE=something COUNT=3 QUOTED=\"Quoted value\"", + &["VALUE", "MISSING", "COUNT", "QUOTED"] + ) + .is_err()); + assert!(parse_key_value_pairs( + b"VALUE=something COUNT=3 QUOTED=\"Quoted value\" MISSING", + &["VALUE", "MISSING", "COUNT", "QUOTED"] + ) + .is_err()); + } + + #[test] + fn problematic_quotes() { + assert!(parse_key_value_pairs(b"KEY=\"Unbalanced quote", &["KEY"]).is_err()); + assert!(parse_key_value_pairs(b"KEY=value\"", &["KEY"]).is_err()); + assert!(parse_key_value_pairs(b"KEY=val\"ue", &["KEY"]).is_err()); + assert!(parse_key_value_pairs( + b"GOOD=yes INVALID=\"Unbalanced QUOTED=\"Quoted value\" ANOTHER=1", + &["GOOD", "INVALID", "QUOTED", "ANOTHER"] + ) + .is_err()); + assert!(parse_key_value_pairs( + b"GOOD=yes QUOTED=\"Quoted value\" INVALID=\"Unbalanced ANOTHER=1", + &["GOOD", "INVALID", "QUOTED", "ANOTHER"] + ) + .is_err()); + assert!(parse_key_value_pairs( + b"GOOD=yes QUOTED=\"Quoted value\" ANOTHER=1 INVALID=\"Unbalanced ", + &["GOOD", "INVALID", "QUOTED", "ANOTHER"] + ) + .is_err()); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/mod.rs ===================================== @@ -4,16 +4,32 @@ // copied, modified, or distributed except according to those terms. mod ack; +mod bootstrap_status; mod bridge_line; +mod circuit_status; mod client_transport_plugin; +mod country_code; mod escape; +mod key_value_pairs; +mod node_address; +mod onion_client_auth_view; +mod socks_listeners; +mod stream_status; mod unescape; #[cfg(test)] mod tests; pub use ack::parse_ack; +pub use bootstrap_status::parse_bootstrap_status; pub use bridge_line::parse_bridge_line; +pub use circuit_status::parse_circuit_built; pub use client_transport_plugin::parse_client_transport_plugin; +pub use country_code::parse_country_code; pub use escape::tor_escape_into; +use key_value_pairs::parse_key_value_pairs; +pub use node_address::addresses_from_status_info; +pub use onion_client_auth_view::parse_onion_client_auth_view; +pub use socks_listeners::parse_socks_listeners; +pub use stream_status::parse_stream_sentconnect; pub use unescape::tor_unescape; ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/node_address.rs ===================================== @@ -0,0 +1,403 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::bytes::Regex; +use std::net::{Ipv4Addr, SocketAddrV6}; + +use crate::ctor::controller::{types::*, ControllerError}; + +lazy_static! { + // First line is always: + // `r nickname identity digest PubDate PubTime IP ORPort DirPort` + // https://spec.torproject.org/dir-spec/consensus-formats.html#section:router-s... + static ref R_LINE_REGEX: Regex = Regex::new( + r"^r [A-Za-z0-9]+ [A-Za-z0-9+/]+ [A-Za-z0-9+/=]+ \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ([0-9\.]{7,15}) \d{1,5} \d{1,5}$", + ) + .expect("The regex is hardcoded, it should be good."); +} + +/// Parse a router status and return its IPv4 (and IPv6, if available). +/// We follow the specs from: +/// https://spec.torproject.org/dir-spec/consensus-formats.html#section:router-s... +/// +/// We expect the first line to have this format: +/// `r nickname identity digest PubDate PubTime IP ORPort DirPort` +/// We only validate the allowed characters, we do not validate all the fields. +/// +/// We ignore any other lines, except the ones for IPv6 addresses, that have +/// this format: `a [<IPv6-address>]:port`. +/// Only the first valid "a" line will be picked up. +/// +/// This function parses one router at the time. +pub fn addresses_from_status_info(lines: &[&[u8]]) -> Result<NodeAddresses, ControllerError> { + if lines.is_empty() { + return Err(ControllerError::MalformedReply(String::from( + "empty status info", + ))); + } + + let Some(r_line) = R_LINE_REGEX.captures(lines[0]) else { + return Err(ControllerError::MalformedReply(format!( + "the first line cannot be parsed: {}", + String::from_utf8_lossy(lines[0]) + ))); + }; + let ipv4: Ipv4Addr = str::from_utf8(&r_line[1]) + .map_err(|e| ControllerError::MalformedReply(e.to_string()))? + .parse() + .map_err(|e| ControllerError::MalformedReply(format!("{}", e)))?; + + let ipv6 = lines[1..].iter().find_map(|line| { + if !line.starts_with(b"a ") { + return None; + } + let s = str::from_utf8(&line[2..]).ok()?; + let addrv6: SocketAddrV6 = s.parse().ok()?; + Some(*addrv6.ip()) + }); + + Ok(NodeAddresses { ipv4, ipv6 }) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{assert_matches, net::Ipv6Addr}; + + #[test] + fn happy_both() { + assert_eq!( + addresses_from_status_info(&[ + b"r nickname abAB123+/xyzXYZaaaaaaaaaaaa bbbbbbbbabAB123+/xyzXYZbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a [fdc8:08d6:f860:d6c::3100]:9001", + b"s Fast Guard HSDir Running Stable V2Dir Valid", + b"w Bandwidth=5000", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: Some(Ipv6Addr::new( + 0xfdc8, 0x08d6, 0xf860, 0xd6c, 0, 0, 0, 0x3100 + )), + } + ); + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"s Fast Guard HSDir Running Stable V2Dir Valid", + b"w Bandwidth=5000", + b"a [fdc8:08d6:f860:d6c::3100]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: Some(Ipv6Addr::new( + 0xfdc8, 0x08d6, 0xf860, 0xd6c, 0, 0, 0, 0x3100 + )), + } + ); + } + + #[test] + fn only_v4() { + assert_eq!( + addresses_from_status_info(&[b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0"]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: None + } + ); + } + + #[test] + fn invalid_v6_ignored() { + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a [zzzz:08d6:f860:d6c::3100]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: None + } + ); + + // Missing the port is also rejected for IPv6 + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a [fdc8:08d6:f860:d6c::3100]", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: None + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: None + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a ", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: None + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"abc [fdc8:dead:beef:d6c::3100]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: None + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a 127.0.0.1", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: None + } + ); + } + + #[test] + fn multiple_a() { + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a [cafe:dead:beef:0123::3100]:9001", + b"a [cafe:dead:beef:0123::3200]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: Some(Ipv6Addr::new( + 0xcafe, 0xdead, 0xbeef, 0x0123, 0, 0, 0, 0x3100 + )) + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a [cafe:dead:beef:0123::3100]:9001", + b"a [cafe:dead:xxxx:0123::3200]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: Some(Ipv6Addr::new( + 0xcafe, 0xdead, 0xbeef, 0x0123, 0, 0, 0, 0x3100 + )) + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a [cafe:dead:xxxx:0123::3200]:9001", + b"a [cafe:dead:beef:0123::3100]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: Some(Ipv6Addr::new( + 0xcafe, 0xdead, 0xbeef, 0x0123, 0, 0, 0, 0x3100 + )) + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a ", + b"a [cafe:dead:beef:0123::3100]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: Some(Ipv6Addr::new( + 0xcafe, 0xdead, 0xbeef, 0x0123, 0, 0, 0, 0x3100 + )) + } + ); + + assert_eq!( + addresses_from_status_info(&[ + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + b"a", + b"a [cafe:dead:beef:0123::3100]:9001", + ]).unwrap(), + NodeAddresses { + ipv4: Ipv4Addr::new(192, 168, 1, 3), + ipv6: Some(Ipv6Addr::new( + 0xcafe, 0xdead, 0xbeef, 0x0123, 0, 0, 0, 0x3100 + )) + } + ); + } + + #[test] + fn invalid_r() { + assert_matches!( + addresses_from_status_info(&[]).unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[b"does not start with r"]).unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"r nick$$name aaaa bbbb 2038-01-01 00:00:00 127.0.0.1 9001 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"r nickname aa!@aa bbbb 2038-01-01 00:00:00 127.0.0.1 9001 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2038-1-01 00:00:00 127.0.0.1 9001 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2038-01-01 00:00:00 127.0.0.1000 9001 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2038-01-01 00:00:00 ....123 9001 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + // Bad IPv4 (an octet is 3 digits but higher than 255) + assert_matches!( + addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2038-01-01 00:00:00 127.0.0.300 9001 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2000-06-08 15:10:00 1.2.3.4 100000 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"rrr nickname aaaa bbbb 2000-06-08 15:10:00 1.2.3.4 1000 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"garbage r nickname aaaa bbbb 2000-06-08 15:10:00 1.2.3.4 9000 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2000-06-08 15:10:00 1.2.3.4 443 0 garbage" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + assert_matches!( + addresses_from_status_info(&[ + b"R nickname aaaa bbbb 2000-06-08 15:10:00 1.2.3.4 443 0" + ]) + .unwrap_err(), + ControllerError::MalformedReply(_) + ); + + // Only IPv6 is not valid + assert_matches!( + addresses_from_status_info(&[b"a [fdc8:08d6:f860:d6c::3100]:9001"]).unwrap_err(), + ControllerError::MalformedReply(_) + ); + + // r must come first + assert_matches!( + addresses_from_status_info(&[ + b"a [fdc8:08d6:f860:d6c::3100]:9001", + b"r nickname aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb 2038-01-01 00:00:00 192.168.1.3 9001 0", + ]).unwrap_err(), + ControllerError::MalformedReply(_) + ); + } + + #[test] + fn fields_not_validated() { + // Some fiels are not validated on purposes. + // We might change this in the future, but for now we accept them, so + // we create these tests just to remind us about this. + + // Digest is in base64. tor's convention is to use unpadded base64, but + // the specs are not explicit about it (while they are for identity). + // Let's check we accept a padded base64 for it. + assert!(addresses_from_status_info(&[ + b"r nickname aaaa bbbb= 2026-11-09 10:34:58 1.2.3.4 7777 8888" + ]) + .is_ok()); + + // No validation on date and time, except for the format. + assert!(addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2038-25-50 00:00:00 1.2.3.4 7777 8888" + ]) + .is_ok()); + assert!(addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2000-04-12 32:80:99 1.2.3.4 7777 8888" + ]) + .is_ok()); + + // Ports are not validated either, except for length. + assert!(addresses_from_status_info(&[ + b"r nickname aaaa bbbb 2000-10-15 12:34:56 1.2.3.4 99999 67890" + ]) + .is_ok()); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/onion_client_auth_view.rs ===================================== @@ -0,0 +1,502 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::Regex; + +use super::parse_key_value_pairs; +use crate::ctor::{ + controller::{types::*, ControllerError}, + reply_parser::{DetailReplyLine, Reply}, +}; + +lazy_static! { + static ref CLIENT_REGEX: Regex = Regex::new( + r"^(?i)CLIENT (?<HSAddress>[a-zA-Z2-7]{56}) (?<KeyType>[A-Za-z0-9]+):(?<PrivateKeyBlob>[A-Za-z0-9+/]+={0,3})(?: (?<Args>.+))?$" + ) + .expect("The regex is hardcoded, it should be good."); +} + +const AUTH_VIEW_HEADER: &[u8] = b"ONION_CLIENT_AUTH_VIEW"; + +/// Check the first line of the reply to ONION_CLIENT_AUTH_VIEW. +fn check_auth_view(line: Option<&DetailReplyLine>) -> Result<(), ControllerError> { + if let Some(DetailReplyLine::MidReplyLine { code, line }) = line { + if *code != 250 { + // The first thing we check is that we end with "250 OK", so this + // error code is okay. + return Err(ControllerError::MixedCodes); + } + if line.eq_ignore_ascii_case(AUTH_VIEW_HEADER) { + return Ok(()); + } + } + Err(ControllerError::MalformedReply(String::from( + "the first line must be 250-ONION_CLIENT_AUTH_VIEW.", + ))) +} + +/// Parse an individual 250-CLIENT line, which follows this format: +/// `250-CLIENT <hs-address> <key-type>:<key-blob> <kw-args>`. +/// +/// Note: +/// - hs-address is expected not to have the .onion suffix +/// - we will accept any key type, even though tor only supports x25519 +/// - key-blob is expected to be base64 (but we perform a light validation and +/// return the key as we received it) +/// - we only parse the Flags keyword argument, and only the permanent flag +/// (in a case-insensitive manner) +fn parse_client_line(data: &[u8]) -> Result<OnionClientAuth, ControllerError> { + // The reply for this command should not contain escaped strings, but only + // ASCII data. So, it must also be valid UTF-8. + let data = str::from_utf8(data).map_err(|e| ControllerError::MalformedReply(e.to_string()))?; + let groups = CLIENT_REGEX + .captures(data) + .ok_or_else(|| ControllerError::WrongFormat(data.to_string()))?; + + let address = + HSAddress::from_str(&groups["HSAddress"]).map_err(|e| ControllerError::from(e))?; + + let key_type = groups["KeyType"].into(); + let key_blob = groups["PrivateKeyBlob"].into(); + + let is_permanent = groups + .name("Args") + .map(|a| parse_key_value_pairs(a.as_str().as_bytes(), &["Flags"])) + .transpose()? + .and_then(|args| { + args.get("Flags").map(|flags| { + flags + .split(|b| *b == b',') + .any(|flag| flag.eq_ignore_ascii_case(b"Permanent")) + }) + }) + .unwrap_or(false); + + Ok(OnionClientAuth { + address, + key_type, + key_blob, + is_permanent, + }) +} + +/// Parse the full reply to a ONION_CLIENT_AUTH_VIEW command. +pub fn parse_onion_client_auth_view(reply: Reply) -> Result<Vec<OnionClientAuth>, ControllerError> { + if let Some(e) = ControllerError::from_reply(&reply) { + return Err(e); + } + if reply.end_line().code != 250 { + return Err(ControllerError::MalformedReply(format!( + "expected the end line to be a '250 OK', found '{}'", + String::from_utf8_lossy(reply.end_line().line.as_ref()) + ))); + } + + check_auth_view(reply.details().first())?; + + reply.details()[1..] + .iter() + .map(|l| match l { + DetailReplyLine::MidReplyLine { code, line } => { + if *code == 250 { + parse_client_line(line.as_ref()) + } else { + Err(ControllerError::MixedCodes) + } + } + DetailReplyLine::DataReplyLine { .. } => Err(ControllerError::MalformedReply( + String::from("Datalines are not allowed in ONION_CLIENT_AUTH_VIEW."), + )), + }) + .collect() +} + +#[cfg(test)] +mod tests { + use std::assert_matches; + + use super::*; + use crate::ctor::reply_parser::make_reply; + + #[test] + fn single_no_permanent() { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/==\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert_eq!( + result, + vec![OnionClientAuth { + address: HSAddress::new( + b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd" + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("abcdef1234567890+/=="), + is_permanent: false, + }] + ) + } + + #[test] + fn single_permanent() { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== Flags=Permanent\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert_eq!( + result, + vec![OnionClientAuth { + address: HSAddress::new( + b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd" + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("abcdef1234567890+/=="), + is_permanent: true, + }] + ) + } + + #[test] + fn empty() { + let reply = make_reply(b"250-ONION_CLIENT_AUTH_VIEW\r\n250 OK\r\n"); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert_eq!(result, vec![]); + } + + #[test] + fn multiple() { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== Flags=Permanent\r\n", + "250-CLIENT sp3k262uwy4r2k3ycr5awluarykdpag6a7y33jxop4cs2lu5uz5sseqd x25519:thekeyhere\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert_eq!( + result, + vec![ + OnionClientAuth { + address: HSAddress::new( + b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd" + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("abcdef1234567890+/=="), + is_permanent: true, + }, + OnionClientAuth { + address: HSAddress::new( + b"sp3k262uwy4r2k3ycr5awluarykdpag6a7y33jxop4cs2lu5uz5sseqd" + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("thekeyhere"), + is_permanent: false, + }, + ] + ) + } + + #[test] + fn case_insensitive() { + let raw = concat!( + "250-onioN_CLIENt_autH_VIEW\r\n", + "250-CLieNt Pg6MmjiYjMcRssLvyKFWNntLARu7P5SVn6y2YMMJu6NUbxNdf4PscRYD x25519:abcdef1234567890+/== fLaGs=permAnEnt\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert_eq!( + result, + vec![OnionClientAuth { + address: HSAddress::new( + b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd" + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("abcdef1234567890+/=="), + is_permanent: true, + }] + ) + } + + #[test] + fn unknown_flags() { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== Flags=SomeFlag,AnotherFlag\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert_eq!( + result, + vec![OnionClientAuth { + address: HSAddress::new( + b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd" + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("abcdef1234567890+/=="), + is_permanent: false, + }] + ) + } + + #[test] + fn multiple_flags() { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== Flags=SomeFlag,Permanent,AnotherFlag\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert_eq!( + result, + vec![OnionClientAuth { + address: HSAddress::new( + b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd" + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("abcdef1234567890+/=="), + is_permanent: true, + }] + ) + } + + #[test] + fn client_name_ignored() { + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== ClientName=test\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + assert!(parse_onion_client_auth_view(reply).is_ok()); + } + + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== ClientName=test Flags=Permanent\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert!(result[0].is_permanent); + } + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== ClientName=test Flags=\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert!(!result[0].is_permanent); + } + + // In the specs, Flags is listed after ClientName, but we accept any + // order. + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== Flags=Permanent ClientName=test\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert!(result[0].is_permanent); + } + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/== Flags= ClientName=test\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply).unwrap(); + assert!(!result[0].is_permanent); + } + } + + #[test] + fn unsuccessful_end_code() { + { + let reply = make_reply(b"512 Syntax error\r\n"); + let result = parse_onion_client_auth_view(reply); + assert_matches!( + result.unwrap_err(), + ControllerError::TorError { code: 512, .. } + ); + } + { + let reply = make_reply(b"250-ONION_CLIENT_AUTH_VIEW\r\n512 Syntax error\r\n"); + let result = parse_onion_client_auth_view(reply); + assert_matches!( + result.unwrap_err(), + ControllerError::TorError { code: 512, .. } + ); + } + } + + #[test] + fn bad_header() { + { + let reply = make_reply(b"250 OK\r\n"); + let result = parse_onion_client_auth_view(reply); + assert_matches!(result.unwrap_err(), ControllerError::MalformedReply(_)); + } + { + let reply = make_reply(b"254-BAD HEADER\r\n250 OK\r\n"); + let result = parse_onion_client_auth_view(reply); + assert_eq!(result.unwrap_err(), ControllerError::MixedCodes); + } + { + let reply = make_reply(b"250-BAD HEADER\r\n250 OK\r\n"); + let result = parse_onion_client_auth_view(reply); + assert_matches!(result.unwrap_err(), ControllerError::MalformedReply(_)); + } + { + let reply = make_reply(b"250+DataReply Header\r\n.\r\n250 OK\r\n"); + let result = parse_onion_client_auth_view(reply); + assert_matches!(result.unwrap_err(), ControllerError::MalformedReply(_)); + } + } + + #[test] + fn datalines() { + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250+CLIENT\r\n.\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply); + assert_matches!(result.unwrap_err(), ControllerError::MalformedReply(_)); + } + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/==\r\n", + "250+CLIENT\r\n.\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply); + assert_matches!(result.unwrap_err(), ControllerError::MalformedReply(_)); + } + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250+CLIENT\r\n.\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/==\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply); + assert_matches!(result.unwrap_err(), ControllerError::MalformedReply(_)); + } + } + + #[test] + fn no_250_codes() { + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "251-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/==\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply); + assert_eq!(result.unwrap_err(), ControllerError::MixedCodes); + } + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/==\r\n", + "251-CLIENT sp3k262uwy4r2k3ycr5awluarykdpag6a7y33jxop4cs2lu5uz5sseqd x25519:abcdef1234567890+/==\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply); + assert_eq!(result.unwrap_err(), ControllerError::MixedCodes); + } + { + let raw = concat!( + "250-ONION_CLIENT_AUTH_VIEW\r\n", + "251-CLIENT sp3k262uwy4r2k3ycr5awluarykdpag6a7y33jxop4cs2lu5uz5sseqd x25519:abcdef1234567890+/==\r\n", + "250-CLIENT pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd x25519:abcdef1234567890+/==\r\n", + "250 OK\r\n" + ); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply); + assert_eq!(result.unwrap_err(), ControllerError::MixedCodes); + } + { + let raw = concat!("251-ONION_CLIENT_AUTH_VIEW\r\n", "250 OK\r\n"); + let reply = make_reply(raw.as_bytes()); + let result = parse_onion_client_auth_view(reply); + assert_eq!(result.unwrap_err(), ControllerError::MixedCodes); + } + { + let raw = concat!("251-ONION_CLIENT_AUTH_VIEW\r\n", "251 OK\r\n"); + let reply = make_reply(raw.as_bytes()); + assert!(parse_onion_client_auth_view(reply).is_err()); + } + { + let raw = concat!("250-ONION_CLIENT_AUTH_VIEW\r\n", "251 OK\r\n"); + let reply = make_reply(raw.as_bytes()); + assert!(parse_onion_client_auth_view(reply).is_err()); + } + } + + #[test] + fn malformed_client_line() { + let make_error = + |r: &'static str| parse_onion_client_auth_view(make_reply(r.as_bytes())).unwrap_err(); + + assert_matches!( + make_error("250-ONION_CLIENT_AUTH_VIEW\r\n250-aaaaaaaaa\r\n250 OK\r\n"), + ControllerError::WrongFormat(_) + ); + assert_matches!( + make_error("250-ONION_CLIENT_AUTH_VIEW\r\n250-client aaaaaaaaa\r\n250 OK\r\n"), + ControllerError::WrongFormat(_) + ); + assert_matches!( + make_error( + "250-ONION_CLIENT_AUTH_VIEW\r\n250-client aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa x25519:aaaa\r\n250 OK\r\n" + ), + ControllerError::InvalidOnionAddress(_) + ); + assert_matches!( + make_error( + "250-ONION_CLIENT_AUTH_VIEW\r\n250-CLIENT sp3k262uwy4r2k3ycr5awluarykdpag6a7y33jxop4cs2lu5uz5sseqd x25519:not_base_64!!\r\n250 OK\r\n" + ), + ControllerError::WrongFormat(_) + ); + assert_matches!( + make_error( + "250-ONION_CLIENT_AUTH_VIEW\r\n250-CLIENT sp3k262uwy4r2k3ycr5awluarykdpag6a7y33jxop4cs2lu5uz5sseqd x25519:aaaa KeyWithoutEqual\r\n250 OK\r\n" + ), + ControllerError::MalformedReply(_) + ); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/socks_listeners.rs ===================================== @@ -0,0 +1,215 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::bytes::Regex; +use std::path::PathBuf; + +use super::tor_unescape; +use crate::ctor::controller::{types::*, ControllerError}; + +lazy_static! { + // This regular expression does not include $ on purpose, as we are looping + // on all values: we get the first quoted value and consume any spaces at + // end. Then we feed the rest of the string to the same regex, and if it + // fails it means there was garbage and we should reject the entire reply. + static ref LISTENERS_REGEX: Regex = Regex::new(r#"^ *("(?:[^"\\]|\\.)+")( +|$)"#) + .expect("The regex is hardcoded, it should be good."); +} + +/// Parse a Unix-domain socket path on Windows. +/// Since they are not supported on Windows, we return None. +#[cfg(target_os = "windows")] +fn make_path(_buf: &[u8]) -> Option<PathBuf> { + None +} + +/// Parse a Unix-domain socket on a non-Windows platform. +/// This function always returns a Some(<path>), even when the path is empty. +#[cfg(not(target_os = "windows"))] +fn make_path(buf: &[u8]) -> Option<PathBuf> { + use std::{ffi::OsStr, os::unix::ffi::OsStrExt}; + Some(PathBuf::from(OsStr::from_bytes(buf))) +} + +/// Parse a single SOCKS listener. +fn parse_listener(listener: &[u8]) -> Option<Result<SocksListener, ControllerError>> { + let addr = match tor_unescape(listener) { + Ok(a) => a, + Err(e) => { + return Some(Err(ControllerError::MalformedReply(e.to_string()))); + } + }; + if addr.starts_with(b"unix:/") { + make_path(&addr[5..]).map(|p| Ok(SocksListener::UnixSocket(p))) + } else { + Some( + str::from_utf8(&*addr) + .map_err(|e| ControllerError::MalformedReply(format!("{}", e))) + .and_then(|s| { + s.parse() + .map_err(|e| ControllerError::MalformedReply(format!("{}", e))) + }) + .map(|addr| SocksListener::Tcp(addr)), + ) + } +} + +/// Parse the output of a `GETINFO net/listeners/socks` command. +/// The reply is expected to be a single line which can contain multiple +/// listeners (each one quoted and separated by space). +/// +/// A reply without listeners is valid (it will return an empty list) and +/// expected before the bootstrap. +/// +/// We support IPv4 and IPv6 listeners on all platforms. +/// On non-Windows platforms, we support also Unix-domain sockets. +/// On Windows we parse them but we do not add them to the list. +pub fn parse_socks_listeners(line: &[u8]) -> Result<Vec<SocksListener>, ControllerError> { + let mut listeners = Vec::new(); + + // We use a manual loop because we want to make sure that items are head to + // tail and we are not simply ignoring invalid content. + let mut pos = 0; + while pos < line.len() { + let remaining = &line[pos..]; + match LISTENERS_REGEX.captures(remaining) { + Some(cap) => { + if let Some(l) = parse_listener(&cap[1]) { + listeners.push(l?); + } + pos += cap[0].len(); + } + None => { + return Err(ControllerError::WrongFormat( + String::from_utf8_lossy(line).into_owned(), + )); + } + } + } + + Ok(listeners) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::net::{Ipv4Addr, SocketAddrV4, SocketAddrV6}; + + #[test] + fn single_ipv4() { + assert_eq!( + parse_socks_listeners(b"\"127.0.0.1:9150\"").unwrap(), + vec![SocksListener::Tcp( + SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 9150).into() + )] + ); + } + + #[test] + fn single_ipv6() { + let addr_v6: SocketAddrV6 = "[2001:0db8:7000:0000:0000:dead:beef:1234]:9150" + .parse() + .unwrap(); + assert_eq!( + parse_socks_listeners(b"\"[2001:0db8:7000:0000:0000:dead:beef:1234]:9150\"").unwrap(), + vec![SocksListener::Tcp(addr_v6.into())] + ); + } + + #[test] + fn single_unix() { + let line = b"\"unix:/tmp/test.sock\""; + if cfg!(target_os = "windows") { + assert_eq!(parse_socks_listeners(line).unwrap(), vec![]); + } else { + assert_eq!( + parse_socks_listeners(line).unwrap(), + vec![SocksListener::UnixSocket(PathBuf::from("/tmp/test.sock"))] + ); + } + } + + #[test] + fn no_listeners() { + assert_eq!(parse_socks_listeners(b"").unwrap(), vec![]); + } + + #[test] + fn multiple_listeners() { + let mut listeners = vec![ + SocksListener::Tcp(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 9050).into()), + SocksListener::Tcp(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 9150).into()), + ]; + if cfg!(not(target_os = "windows")) { + listeners.push(SocksListener::UnixSocket(PathBuf::from("/tmp/test.sock"))); + } + assert_eq!( + parse_socks_listeners(b"\"127.0.0.1:9050\" \"127.0.0.1:9150\" \"unix:/tmp/test.sock\"") + .unwrap(), + listeners + ); + } + + #[test] + fn unbalanced_quotes() { + assert!(parse_socks_listeners(b"\"127.0.0.1:9050").is_err()); + assert!(parse_socks_listeners(b"127.0.0.1:9050\"").is_err()); + assert!(parse_socks_listeners(b"\"127.0.0.1:9050\" \"127.0.0.1:9051").is_err()); + assert!( + parse_socks_listeners(b"\"127.0.0.1:9050\" \"127.0.0.1:9051 \"127.0.0.1:9052\"") + .is_err() + ); + } + + #[test] + fn noise_in_reply() { + assert!(parse_socks_listeners(b"garbage \"127.0.0.1:9050\"").is_err()); + assert!(parse_socks_listeners(b"\"127.0.0.1:9050\" garbage").is_err()); + assert!(parse_socks_listeners(b"garbage \"127.0.0.1:9050\" garbage").is_err()); + assert!(parse_socks_listeners(b"\"127.0.0.1:9050\" garbage \"127.0.0.1:9051\"").is_err()); + assert!( + parse_socks_listeners(b"garbage \"127.0.0.1:9050\" garbage \"127.0.0.1:9051\"") + .is_err() + ); + assert!( + parse_socks_listeners(b"\"127.0.0.1:9050\" garbage \"127.0.0.1:9051\" garbage") + .is_err() + ); + assert!(parse_socks_listeners( + b"garbage \"127.0.0.1:9050\" garbage \"127.0.0.1:9051\" garbage" + ) + .is_err()); + assert!(parse_socks_listeners(b"\"127.0.0.1:9050\" 127.0.0.1:9150").is_err()); + } + + #[test] + fn spaces() { + assert!(parse_socks_listeners(b"\"127.0.0.1:9050\"\"127.0.0.1:9150\"").is_err()); + assert_eq!( + parse_socks_listeners(b"\"127.0.0.1:9050\" \"127.0.0.1:9150\"").unwrap(), + vec![ + SocksListener::Tcp(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 9050).into()), + SocksListener::Tcp(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 9150).into()) + ] + ); + assert_eq!( + parse_socks_listeners(b" \"127.0.0.1:9050\" \"127.0.0.1:9150\" ").unwrap(), + vec![ + SocksListener::Tcp(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 9050).into()), + SocksListener::Tcp(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 9150).into()) + ] + ); + } + + #[test] + fn invalid() { + assert!(parse_socks_listeners(b"\"127.0.0.1:9050\" \"127.0.0.1:100000\"").is_err()); + assert!(parse_socks_listeners(b"\"127.0.0.1:100000\" \"127.0.0.1:9050\"").is_err()); + assert!(parse_socks_listeners(b"\"127.0.0.1\"").is_err()); + assert!(parse_socks_listeners(b"\"localhost:9050\"").is_err()); + assert!(parse_socks_listeners(b"\"\"").is_err()); + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/stream_status.rs ===================================== @@ -0,0 +1,296 @@ +// Licensed under the Apache License, Version 2.0, +// <http://apache.org/licenses/LICENSE-2.0> or the MIT license +// <http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. + +use lazy_static::lazy_static; +use regex::bytes::Regex; + +use super::{parse_key_value_pairs, tor_unescape}; +use crate::ctor::controller::{CircuitID, ControllerError, Stream, StreamID}; + +const STREAM_EVENT: &[u8] = b"SENTCONNECT"; + +lazy_static! { + // https://spec.torproject.org/control-spec/replies.html#STREAM + static ref STATUS_REGEX: Regex = Regex::new( + "^(?<StreamID>[a-zA-Z0-9]{1,16}) (?<StreamStatus>[A-Za-z_]+) (?<CircuitID>[a-zA-Z0-9]{1,16}) (?:[^ ]+)(?: (?<Args>.*))?$" + ) + .expect("The regex is hardcoded, it should be good."); +} + +const USERNAME_KEYWORD: &'static str = "SOCKS_USERNAME"; +const PASSWORD_KEYWORD: &'static str = "SOCKS_PASSWORD"; +const STREAM_KEYWORDS: &[&'static str] = &[USERNAME_KEYWORD, PASSWORD_KEYWORD]; + +/// Parse a stream update event. +/// Only the SENTCONNECT events will be parsed, the other ones will be ignored. +pub fn parse_stream_sentconnect(line: &[u8]) -> Result<Option<Stream>, ControllerError> { + let stream = STATUS_REGEX + .captures(line) + .ok_or_else(|| ControllerError::WrongFormat(String::from_utf8_lossy(line).into_owned()))?; + + if !stream["StreamStatus"].eq_ignore_ascii_case(STREAM_EVENT) { + return Ok(None); + } + + let stream_id = StreamID::new(&stream["StreamID"]).ok_or_else(|| { + ControllerError::MalformedReply(format!( + "invalid stream ID '{}'", + String::from_utf8_lossy(&stream["StreamID"]) + )) + })?; + let circuit_id = CircuitID::new(&stream["CircuitID"]).ok_or_else(|| { + ControllerError::MalformedReply(format!( + "invalid circuit ID '{}'", + String::from_utf8_lossy(&stream["CircuitID"]) + )) + })?; + + let args = stream + .name("Args") + .map(|a| parse_key_value_pairs(a.as_bytes(), STREAM_KEYWORDS)) + .transpose()? + .unwrap_or_default(); + let get_credential = |name| { + args.get(name) + .map(|c| tor_unescape(c).map(|c| c.into_owned())) + .transpose() + .map_err(|e| ControllerError::MalformedReply(e.to_string())) + }; + let socks_username = get_credential(USERNAME_KEYWORD)?; + let socks_password = get_credential(PASSWORD_KEYWORD)?; + + Ok(Some(Stream { + stream_id, + circuit_id, + socks_username, + socks_password, + })) +} + +#[cfg(test)] +mod tests { + use std::assert_matches; + + use super::*; + + #[test] + fn simple() { + let line = b"1 SENTCONNECT 0 1.2.3.4:443"; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"1").unwrap()); + assert_eq!(stream.circuit_id, CircuitID::new(b"0").unwrap()); + assert_eq!(stream.socks_username, None); + assert_eq!(stream.socks_password, None); + } + + #[test] + fn with_socks_credentials() { + let line = + b"42 SENTCONNECT 100 (Tor_internal):80 SOCKS_USERNAME=\"user\" SOCKS_PASSWORD=\"pass\""; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"42").unwrap()); + assert_eq!(stream.circuit_id, CircuitID::new(b"100").unwrap()); + assert_eq!(stream.socks_username, Some(Vec::from("user"))); + assert_eq!(stream.socks_password, Some(Vec::from("pass"))); + } + + #[test] + fn only_username() { + let line = b"5 SENTCONNECT 10 10.20.30.40:443 SOCKS_USERNAME=\"alice\""; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"5").unwrap()); + assert_eq!(stream.circuit_id, CircuitID::new(b"10").unwrap()); + assert_eq!(stream.socks_username, Some(Vec::from("alice"))); + assert_eq!(stream.socks_password, None); + } + + #[test] + fn only_password() { + let line = b"7 SENTCONNECT 20 192.168.1.2:22 SOCKS_PASSWORD=\"secret123\""; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"7").unwrap()); + assert_eq!(stream.circuit_id, CircuitID::new(b"20").unwrap()); + assert_eq!(stream.socks_username, None); + assert_eq!(stream.socks_password, Some(Vec::from("secret123"))); + } + + #[test] + fn valid_but_other_status() { + let line = b"1 DETACHED 0 172.16.10.2:8080"; + let result = parse_stream_sentconnect(line); + assert!(result.unwrap().is_none()); + } + + #[test] + fn status_case_insensitive() { + let line = b"1 sentconnect 0 (Tor_internal):22 socks_username=USER socks_password=PASS"; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"1").unwrap()); + assert_eq!(stream.circuit_id, CircuitID::new(b"0").unwrap()); + assert_eq!(stream.socks_username.unwrap(), b"USER"); + assert_eq!(stream.socks_password.unwrap(), b"PASS"); + } + + #[test] + fn status_mixed_case() { + let line = b"1 SentConnect 0 192.168.4.5:22 SOcKs_USeRNAMe=User soCKS_paSSWoRd=paSs"; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"1").unwrap()); + assert_eq!(stream.circuit_id, CircuitID::new(b"0").unwrap()); + assert_eq!(stream.socks_username.unwrap(), b"User"); + assert_eq!(stream.socks_password.unwrap(), b"paSs"); + } + + #[test] + fn status_unescape_error() { + assert!(parse_stream_sentconnect( + b"5 SENTCONNECT 10 10.20.30.40:443 SOCKS_USERNAME=\"alice\\xyz\"" + ) + .is_err()); + } + + #[test] + fn large_ids() { + { + let line = b"9999999999999999 SENTCONNECT 1 5.6.7.8:80"; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!( + stream.stream_id, + StreamID::new(b"9999999999999999").unwrap() + ); + assert_eq!(stream.circuit_id, CircuitID::new(b"1").unwrap()); + } + { + let line = b"2 SENTCONNECT 1234567890123456 5.6.7.8:80"; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"2").unwrap()); + assert_eq!( + stream.circuit_id, + CircuitID::new(b"1234567890123456").unwrap() + ); + } + } + + #[test] + fn escaped_credentials() { + let line = + b"1 SENTCONNECT 0 (Tor_internal):80 SOCKS_USERNAME=\"user\\x20example\" SOCKS_PASSWORD=\"pass\\167ord\""; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.socks_username.unwrap(), b"user example"); + assert_eq!(stream.socks_password.unwrap(), b"password"); + } + + #[test] + fn alphanumeric_ids() { + let line = b"abcX1y2Z3 SENTCONNECT ABCxyz 10.10.10.10:80"; + let result = parse_stream_sentconnect(line); + let stream = result.unwrap().unwrap(); + assert_eq!(stream.stream_id, StreamID::new(b"abcX1y2Z3").unwrap()); + assert_eq!(stream.circuit_id, CircuitID::new(b"ABCxyz").unwrap()); + } + + #[test] + fn stream_targets() { + // Valid: IPv4 + assert!( + parse_stream_sentconnect(b"1234 SENTCONNECT 5678 10.10.10.10:80") + .unwrap() + .is_some() + ); + // Valid: IPv6 + assert!( + parse_stream_sentconnect(b"1 SENTCONNECT 2 [cafe:dead:beef:0123::3200]:443") + .unwrap() + .is_some() + ); + // Valid: hostname + assert!(parse_stream_sentconnect(b"1 SENTCONNECT 2 example.org:443") + .unwrap() + .is_some()); + // Valid: (Tor_internal) literal + assert!( + parse_stream_sentconnect(b"1 SENTCONNECT 2 (Tor_internal):22") + .unwrap() + .is_some() + ); + + // Invalid (no port) but accepted anyway for now + assert!( + parse_stream_sentconnect(b"1234 SENTCONNECT 5678 12.34.56.78") + .unwrap() + .is_some() + ); + assert!( + parse_stream_sentconnect(b"1 SENTCONNECT 2 [cafe:dead:beef:0123::3200]") + .unwrap() + .is_some() + ); + assert!(parse_stream_sentconnect(b"1 SENTCONNECT 2 example.net") + .unwrap() + .is_some()); + assert!(parse_stream_sentconnect(b"1 SENTCONNECT 2 (Tor_internal)") + .unwrap() + .is_some()); + } + + #[test] + fn empty() { + let line = b""; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + + #[test] + fn invalid() { + let line = b"invalid input"; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + + #[test] + fn missing_status() { + let line = b"1 0 10.0.0.1:80"; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + + #[test] + fn invalid_ids() { + { + let line = b"123+ SENTCONNECT 0 10.0.0.1:80"; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + { + let line = b"+++ SENTCONNECT 0 10.0.0.1:80"; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + { + let line = b"12345678901234567 SENTCONNECT 0 10.0.0.1:80"; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + { + let line = b"1 SENTCONNECT 123+-+ 10.0.0.1:80"; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + { + let line = b"1 SENTCONNECT +-+ 10.0.0.1:80"; + let result = parse_stream_sentconnect(line); + assert_matches!(result.unwrap_err(), ControllerError::WrongFormat(_)); + } + } +} ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/parsers/unescape.rs ===================================== @@ -21,7 +21,8 @@ pub enum UnescapeError { } /// Adaptation of tor's unescape_string (src/lib/encoding/cstring.c). -/// The main difference is that we allow unquoted strings. +/// The main difference is that we allow unquoted strings, as long as they do +/// not contain spaces. pub fn tor_unescape<'a>(buf: &'a [u8]) -> Result<Cow<'a, [u8]>, UnescapeError> { match buf.first() { Some(b'"') => { ===================================== toolkit/components/tor-integration/tor_provider/src/ctor/controller/types.rs ===================================== @@ -3,9 +3,49 @@ // <http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. -use serde::Serialize; -use std::net::SocketAddr; +use data_encoding::{DecodeError, BASE32_NOPAD}; +use serde::{Serialize, Serializer}; +use sha3::{Digest, Sha3_256}; +use smallvec::SmallVec; +use std::{ + fmt, + net::{Ipv4Addr, Ipv6Addr, SocketAddr}, + path::PathBuf, +}; +use thiserror::Error; +use tinystr::TinyAsciiStr; +/// The severity of STATUS_CLIENT updates. It is the first token: +/// +/// NOTICE BOOTSTRAP PROGRESS=n ... +/// WARN BOOTSTRAP PROGRESS=n ... +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +pub enum StatusSeverity { + Notice, + Warn, +} + +impl ToString for StatusSeverity { + fn to_string(&self) -> String { + match self { + StatusSeverity::Notice => String::from("NOTICE"), + StatusSeverity::Warn => String::from("WARN"), + } + } +} + +/// Data structure with the information associated to a STATUS_CLIENT event. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct BootstrapStatus { + pub severity: StatusSeverity, + pub progress: u8, + pub tag: String, + pub summary: String, + pub reason: Option<String>, + pub should_warn: bool, +} + +/// Information extracted from a bridge line. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct Bridge { pub transport: Option<String>, @@ -14,6 +54,39 @@ pub struct Bridge { pub args: Option<Vec<u8>>, } +/// A circuit ID. It can be an alphanumeric string up to 16 characters. +/// At the moment, comparison between them is case-sensitive, but tor uses only +/// digits. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)] +pub struct CircuitID(TinyAsciiStr<16>); + +impl CircuitID { + pub fn new(data: &[u8]) -> Option<Self> { + if data.is_empty() || data.iter().any(|c| !c.is_ascii_alphanumeric()) { + return None; + } + let s = str::from_utf8(data).ok()?.parse().ok()?; + Some(Self(s)) + } + + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + +/// A conflux cookie, used to group conflux circuits. +pub type ConfluxID = [u8; 16]; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Circuit { + pub id: CircuitID, + pub path: SmallVec<[NodeFingerprint; 3]>, + pub conflux_id: Option<ConfluxID>, +} + +/// The definition of a handler for a pluggable transport. +/// The conversion between 8-bit characters and 16-bit characters on Windows is +/// not well defined. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub enum ClientTransportPlugin { Executable { @@ -24,6 +97,10 @@ pub enum ClientTransportPlugin { Socks5(SocketAddr), } +/// A configuration value to be used with the SETCONF command. +/// Boolean will be converted to 1 and 0. +/// Null can be used to reset a certain value. When building the SETCONF +/// command, we will add `<key>`, rather than `<key>=<value>` or `<key>=`. #[derive(Debug, Clone)] pub enum ConfValue<'a> { Bool(bool), @@ -33,8 +110,243 @@ pub enum ConfValue<'a> { Null, } +/// Onion service address (in the canonical base32 representation, without the +/// .onion suffix). +/// The address is validated on construction, to make sure it adheres to the +/// spec: https://spec.torproject.org/rend-spec/encoding-onion-addresses.html. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct HSAddress([u8; 56]); + +impl HSAddress { + pub fn new(base32_address: &[u8; 56]) -> Result<Self, OnionValidationError> { + validate_onion_address(base32_address).map(Self) + } + + pub fn from_slice(base32_address: &[u8]) -> Result<Self, OnionValidationError> { + Self::new( + base32_address + .try_into() + .map_err(|_| OnionValidationError::InvalidLength)?, + ) + } + + pub fn from_str(base32_address: &str) -> Result<Self, OnionValidationError> { + Self::from_slice(base32_address.as_bytes()) + } +} + +impl AsRef<str> for HSAddress { + fn as_ref(&self) -> &str { + str::from_utf8(&self.0).expect( + "We validated the address on the constructor, therefore it must be valid UTF-8.", + ) + } +} + +impl Serialize for HSAddress { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(self.as_ref()) + } +} + +/// Possible errors that can happen when validating an onion address. +#[derive(Error, Debug, Clone, PartialEq, Eq)] +pub enum OnionValidationError { + #[error("Invalid length")] + InvalidLength, + #[error("cannot decode the address")] + CannotDecode(#[from] DecodeError), + #[error("unsupported version: {0}")] + UnsupportedVersion(u8), + #[error("bad checksum")] + BadChecksum, +} + +/// Check the validity of an onion address. +/// +/// https://spec.torproject.org/rend-spec/encoding-onion-addresses.html +fn validate_onion_address(base32_address: &[u8; 56]) -> Result<[u8; 56], OnionValidationError> { + let mut uppercased = *base32_address; + uppercased.make_ascii_uppercase(); + let decoded = BASE32_NOPAD + .decode(&uppercased) + .map_err(|e| OnionValidationError::from(e))?; + debug_assert_eq!( + decoded.len(), + 35, + "We are using fixed size, how can the length be wrong?!" + ); + + let pubkey = &decoded[0..32]; + let stored_checksum = &decoded[32..34]; + let version = decoded[34]; + + if version != 3 { + return Err(OnionValidationError::UnsupportedVersion(version)); + } + + let hash = { + let mut hasher = Sha3_256::new(); + hasher.update(b".onion checksum"); + hasher.update(pubkey); + hasher.update(&[version]); + hasher.finalize() + }; + + let calculated_checksum = &hash[0..2]; + if stored_checksum != calculated_checksum { + return Err(OnionValidationError::BadChecksum); + } + + Ok(uppercased) +} + +/// The information about the IP addresses of a relay. +/// At the moment, all realys must have an IPv4 address, and tor will use at +/// most one IPv6 address (which is optional). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +pub struct NodeAddresses { + pub ipv4: Ipv4Addr, + pub ipv6: Option<Ipv6Addr>, +} + +/// The fingerprint of a node (as bytes, i.e., decoded from the 40 hex digits +/// format). +pub type NodeFingerprint = [u8; 20]; + +/// A private key for onion authentication. +/// The key_blob is a base64 string, rather than a binary blob. +#[derive(Clone, PartialEq, Eq, Serialize)] +pub struct OnionClientAuth { + pub address: HSAddress, + pub key_type: String, + pub key_blob: String, + pub is_permanent: bool, +} + +impl fmt::Debug for OnionClientAuth { + /// Explicit implementation of fmt::Debug for OnionClientAuth that redacts + /// the private key. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("OnionClientAuth") + .field("address", &self.address) + .field("key_type", &self.key_type) + .field("key_blob", &"[redacted]") + .field("is_permanent", &self.is_permanent) + .finish() + } +} + +/// A pluggable transport, with its handler and the transport it can manages. #[derive(Debug, Clone, PartialEq, Eq, Serialize)] pub struct PluggableTransport { pub transports: Vec<String>, pub plugin: ClientTransportPlugin, } + +/// A listener for the SOCKS port. +/// On Windows, only TCP listeners are supported. +#[derive(Debug, PartialEq, Eq, Serialize)] +pub enum SocksListener { + Tcp(SocketAddr), + UnixSocket(PathBuf), +} + +/// The bare minimum information we need about a stream to associate some +/// isolation credentials to a circuit. +/// +/// Notice: since tor uses SOCKS passwords as isolation parameters rather than +/// as real passwords, we do not redact them, and we use the default Debug +/// implementation. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct Stream { + pub stream_id: StreamID, + pub circuit_id: CircuitID, + pub socks_username: Option<Vec<u8>>, + pub socks_password: Option<Vec<u8>>, +} + +/// A stream ID. It can be an alphanumeric string up to 16 characters. +/// At the moment, comparison between them is case-sensitive, but tor uses only +/// digits. +/// Circuit IDs are defined in the same way, but we do not define them as alias +/// to prevent exchanging them by mistake. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)] +pub struct StreamID(TinyAsciiStr<16>); + +impl StreamID { + pub fn new(data: &[u8]) -> Option<Self> { + if data.is_empty() || data.iter().any(|c| !c.is_ascii_alphanumeric()) { + return None; + } + let s = str::from_utf8(data).ok()?.parse().ok()?; + Some(Self(s)) + } + + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_onion_validation() { + assert!(validate_onion_address( + b"PG6MMJIYJMCRSSLVYKFWNNTLARU7P5SVN6Y2YMMJU6NUBXNDF4PSCRYD" + ) + .is_ok()); + assert!(validate_onion_address( + b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd" + ) + .is_ok()); + assert!(validate_onion_address( + b"pG6mMjIyJmCrSsLvYkFwNnTlArU7p5SvN6y2YmMjU6nuBxNdF4pScRyD" + ) + .is_ok()); + + assert!(matches!( + validate_onion_address(b"+-6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd") + .unwrap_err(), + OnionValidationError::CannotDecode(_) + )); + + assert_eq!( + validate_onion_address(b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscrye") + .unwrap_err(), + OnionValidationError::UnsupportedVersion(4) + ); + + assert_eq!( + validate_onion_address(b"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsyd") + .unwrap_err(), + OnionValidationError::BadChecksum + ); + assert_eq!( + validate_onion_address(b"aaammjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsyd") + .unwrap_err(), + OnionValidationError::BadChecksum + ); + } + + #[test] + fn onion_private_key_redacted() { + let auth = OnionClientAuth { + address: HSAddress::from_str( + "PG6MMJIYJMCRSSLVYKFWNNTLARU7P5SVN6Y2YMMJU6NUBXNDF4PSCRYD", + ) + .unwrap(), + key_type: String::from("x25519"), + key_blob: String::from("abase64key"), + is_permanent: false, + }; + let auth_fmt = format!("{:?}", auth); + assert!(!auth_fmt.contains(&auth.key_blob)); + assert!(auth_fmt.contains("[redacted]")); + } +} View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/compare/7adda3d... -- View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/compare/7adda3d... You're receiving this email because of your account on gitlab.torproject.org. Manage all notifications: https://gitlab.torproject.org/-/profile/notifications | Help: https://gitlab.torproject.org/help
participants (1)
-
Pier Angelo Vendrame (@pierov)