Validate connection text and identity arguments before dispatch
This commit is contained in:
1 parent
c12cf2123f
commit
f41e064ea6
5 files changed
+74
-16
No files matched your search
+40
-15
@@ -203,20 +203,25 @@ impl ValidatedCommand {
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ConnectArgs {
|
||||
pub address: String,
|
||||
#[serde(default)]
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
pub server_password: Option<String>,
|
||||
#[serde(default)]
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
pub channel: Option<String>,
|
||||
#[serde(default)]
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
pub channel_password: Option<String>,
|
||||
#[serde(default)]
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
pub identity_id: Option<String>,
|
||||
/// Nickname for this connection only (bookmark override); absent: the identity's
|
||||
/// nickname/name. Validated by `Store::open_for_connect`; the identity is never changed.
|
||||
#[serde(default)]
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
pub nickname: Option<String>,
|
||||
}
|
||||
|
||||
// Omission selects the default. An explicitly supplied null is not a string.
|
||||
fn present_string<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Option<String>, D::Error> {
|
||||
String::deserialize(deserializer).map(Some)
|
||||
}
|
||||
|
||||
/// One parsed stdin line.
|
||||
#[derive(Debug)]
|
||||
pub enum Input {
|
||||
@@ -259,7 +264,7 @@ struct NicknameArgs {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct DisconnectArgs {
|
||||
#[serde(default)]
|
||||
#[serde(default, deserialize_with = "present_string")]
|
||||
message: Option<String>,
|
||||
}
|
||||
|
||||
@@ -417,7 +422,7 @@ fn check_stream_id(id: &str) -> Result<(), ErrorObj> {
|
||||
}
|
||||
|
||||
// The original bridge measured string limits in UTF-16 units. Only SDP descriptions are multiline.
|
||||
fn stream_text(text: &str, min: usize, max: usize, multiline: bool) -> bool {
|
||||
fn bounded_text(text: &str, min: usize, max: usize, multiline: bool) -> bool {
|
||||
(min..=max).contains(&text.encode_utf16().count()) && !text.bytes().any(|b| {
|
||||
(b < 0x20 || b == 0x7f) && !(multiline && matches!(b, b'\n' | b'\r' | b'\t'))
|
||||
})
|
||||
@@ -426,7 +431,7 @@ fn stream_text(text: &str, min: usize, max: usize, multiline: bool) -> bool {
|
||||
fn check_signal(value: &Value) -> Result<(), ErrorObj> {
|
||||
let envelope = value.as_object().filter(|v| v.len() == 2).ok_or_else(|| bad("json must contain only cmd and args"))?;
|
||||
let args = envelope.get("args").and_then(Value::as_object).ok_or_else(|| bad("json.args must be an object"))?;
|
||||
let text = |key: &str, min, max, multiline| args.get(key).and_then(Value::as_str).is_some_and(|s| stream_text(s, min, max, multiline));
|
||||
let text = |key: &str, min, max, multiline| args.get(key).and_then(Value::as_str).is_some_and(|s| bounded_text(s, min, max, multiline));
|
||||
let valid = match envelope.get("cmd").and_then(Value::as_str) {
|
||||
Some("answer") => args.len() == 1 && text("answer", 1, 60_000, true),
|
||||
Some("reconnectOffer") => args.len() == 1 && text("offer", 1, 60_000, true),
|
||||
@@ -607,13 +612,20 @@ pub fn validate_command(mut command: Command) -> Result<ValidatedCommand, ErrorO
|
||||
let update = matches!(&command, Command::StreamUpdate(_));
|
||||
match &mut command {
|
||||
Command::Connect(a) => {
|
||||
check_text("address", &a.address, 255, false)?;
|
||||
check_opt("server_password", &a.server_password)?;
|
||||
check_opt("channel", &a.channel)?;
|
||||
check_opt("channel_password", &a.channel_password)?;
|
||||
check_opt("identity_id", &a.identity_id)?;
|
||||
for (field, value, min, max) in [
|
||||
("address", Some(a.address.as_str()), 1, 255),
|
||||
("server_password", a.server_password.as_deref(), 0, 255),
|
||||
("channel", a.channel.as_deref(), 1, 512),
|
||||
("channel_password", a.channel_password.as_deref(), 0, 255),
|
||||
("nickname", a.nickname.as_deref(), 1, 64),
|
||||
] {
|
||||
if value.is_some_and(|s| !bounded_text(s, min, max, false)) { return Err(bad(format!("invalid {field}"))); }
|
||||
}
|
||||
if a.identity_id.as_deref().is_some_and(|uid| !crate::profile::valid_uid(uid)) { return Err(bad("invalid identity_id")); }
|
||||
}
|
||||
Command::Disconnect { message } => {
|
||||
if message.as_deref().is_some_and(|s| !bounded_text(s, 0, 80, false)) { return Err(bad("invalid disconnect message")); }
|
||||
}
|
||||
Command::Disconnect { message } => check_opt("message", message)?,
|
||||
Command::JoinChannel { channel_id, password } => {
|
||||
if *channel_id == 0 { return Err(bad("channel_id must be positive")); }
|
||||
check_opt("password", password)?;
|
||||
@@ -643,7 +655,7 @@ pub fn validate_command(mut command: Command) -> Result<ValidatedCommand, ErrorO
|
||||
}
|
||||
Command::StreamJoin { stream_id, message, .. } => {
|
||||
check_stream_id(stream_id)?;
|
||||
if message.as_deref().is_some_and(|s| !stream_text(s, 0, 200, false)) { return Err(bad("invalid stream join message")); }
|
||||
if message.as_deref().is_some_and(|s| !bounded_text(s, 0, 200, false)) { return Err(bad("invalid stream join message")); }
|
||||
}
|
||||
Command::StreamSignal { stream_id, json, .. } => {
|
||||
check_stream_id(stream_id)?;
|
||||
@@ -783,6 +795,19 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_connect_accepts_the_reference_field_limits() {
|
||||
let pair = "\u{10000}";
|
||||
let mut args = ConnectArgs {
|
||||
address: pair.repeat(127) + "x", channel: Some(pair.repeat(256)),
|
||||
identity_id: Some("test/9PZ9vww/Bpf5vJxtJhpz80=".into()), nickname: Some(pair.repeat(32)),
|
||||
server_password: Some("x".repeat(255)), channel_password: Some(String::new()),
|
||||
};
|
||||
assert!(validate_command(Command::Connect(args.clone())).is_ok());
|
||||
args.channel.as_mut().unwrap().push('x');
|
||||
assert_bad(Command::Connect(args));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connect_rejects_non_string_nickname_overrides() {
|
||||
let parse = |args: Value| parse_line(&serde_json::json!({"id":"c","cmd":"connect","args":args}).to_string());
|
||||
|
||||
@@ -290,6 +290,33 @@ fn client_menu_commands_are_validated_and_never_fake_success_offline() {
|
||||
core.shutdown();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connect_and_disconnect_validate_arguments_before_network_io() {
|
||||
let dir = TempDir::new("connection-args");
|
||||
let mut core = Core::start(&dir.0);
|
||||
for (field, value) in [
|
||||
("address", json!("x".repeat(256))), ("address", json!("localhost\n")),
|
||||
("channel", json!("")), ("channel", json!("x".repeat(513))),
|
||||
("nickname", json!("x".repeat(65))), ("identity_id", Value::Null), ("identity_id", json!("other")),
|
||||
("server_password", json!("x".repeat(256))), ("channel_password", json!("bad\tpassword")),
|
||||
] {
|
||||
let mut args = json!({"address":"127.0.0.1"}); args[field] = value;
|
||||
let (reply, events) = core.request("invalid-connect", "connect", args);
|
||||
assert_eq!(reply["error"]["code"], "bad_request", "{field}: {reply}");
|
||||
assert!(!events.iter().any(|e| e["state"] == "connecting"));
|
||||
}
|
||||
let limit = "\u{10000}".repeat(40);
|
||||
for args in [json!({"message":format!("{limit}x")}), json!({"message":"\r"}), json!({"message":null})] {
|
||||
let (reply, _) = core.request("invalid-disconnect", "disconnect", args);
|
||||
assert_eq!(reply["error"]["code"], "bad_request", "{reply}");
|
||||
}
|
||||
for valid in [json!({}), json!({"message":""}), json!({"message":limit})] {
|
||||
let (reply, _) = core.request("valid-disconnect", "disconnect", valid);
|
||||
assert_eq!(reply["error"]["code"], "not_connected", "{reply}");
|
||||
}
|
||||
core.shutdown();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn channel_queries_validate_arguments_before_connection_lookup() {
|
||||
let dir = TempDir::new("channel-query-args");
|
||||
|
||||
Reference in new issue
Block a user