diff --git a/cmd/crates/soroban-test/tests/it/main.rs b/cmd/crates/soroban-test/tests/it/main.rs index 40d702eaad..d066d71f7a 100644 --- a/cmd/crates/soroban-test/tests/it/main.rs +++ b/cmd/crates/soroban-test/tests/it/main.rs @@ -11,6 +11,7 @@ mod init; mod integration; mod log; mod message; +mod network_metadata_sanitize; mod plugin; mod rpc_provider; mod skill; diff --git a/cmd/crates/soroban-test/tests/it/network_metadata_sanitize.rs b/cmd/crates/soroban-test/tests/it/network_metadata_sanitize.rs new file mode 100644 index 0000000000..fca2377f1c --- /dev/null +++ b/cmd/crates/soroban-test/tests/it/network_metadata_sanitize.rs @@ -0,0 +1,130 @@ +use httpmock::{prelude::*, Mock}; +use serde_json::json; +use soroban_rpc::{GetNetworkResponse, GetVersionInfoResponse}; +use soroban_test::{AssertExt, TestEnv}; + +// A hostile RPC / history-archive peer must not be able to smuggle terminal +// escape sequences through metadata the CLI renders. `network info`, `doctor`, +// and `snapshot create` display strings taken straight from a peer response, so +// those strings are run through `soroban_spec_tools::sanitize` before printing. + +const ANSI: &str = "\u{1b}[2J\u{1b}[1;1H***PWNED***"; + +// The injected clear-screen escape must never reach the terminal raw, while the +// harmless `***PWNED***` text survives as proof the peer field was rendered at +// all (so the assertion can't pass vacuously). `\u{1b}[2J` is distinct from the +// CLI's own cursor-control escapes, so matching it won't flag benign output. +fn assert_sanitized(context: &str, stderr: &str) { + assert!( + stderr.contains("***PWNED***"), + "{context} did not render the peer-controlled field:\n{stderr:?}" + ); + assert!( + !stderr.contains("\u{1b}[2J"), + "{context} leaked a raw terminal-escape sequence:\n{stderr:?}" + ); +} + +fn mock_get_network(server: &MockServer) -> Mock<'_> { + server.mock(|when, then| { + when.method(POST).path("/").body_contains("getNetwork"); + then.status(200).json_body(json!({ + "jsonrpc": "2.0", + "id": 0, + "result": GetNetworkResponse { + friendbot_url: None, + passphrase: format!("Test SDF Network{ANSI}"), + protocol_version: 22, + } + })); + }) +} + +fn mock_get_version_info(server: &MockServer, id: u32) -> Mock<'_> { + server.mock(|when, then| { + when.method(POST).path("/").body_contains("getVersionInfo"); + then.status(200).json_body(json!({ + "jsonrpc": "2.0", + "id": id, + "result": GetVersionInfoResponse { + version: format!("24.1.0{ANSI}"), + commmit_hash: format!("deadbeef{ANSI}"), + build_timestamp: "2026-10-19T00:00:00Z".to_string(), + captive_core_version: format!("stellar-core 23.0{ANSI}"), + protocol_version: 23, + } + })); + }) +} + +#[test] +fn network_info_sanitizes_rpc_metadata() { + let server = MockServer::start(); + let get_network = mock_get_network(&server); + // `network info` reuses one client: getNetwork is request id 0, getVersionInfo id 1. + let get_version_info = mock_get_version_info(&server, 1); + let sandbox = TestEnv::with_rpc_provider(&server.url(""), vec![]); + + let stderr = sandbox + .new_assert_cmd("network") + .arg("info") + .assert() + .success() + .stderr_as_str(); + + get_network.assert(); + get_version_info.assert(); + + assert_sanitized("network info", &stderr); +} + +#[test] +fn doctor_sanitizes_rpc_version() { + let server = MockServer::start(); + // `doctor` uses a fresh client per network, so getVersionInfo is request id 0. + let get_version_info = mock_get_version_info(&server, 0); + let sandbox = TestEnv::default(); + + sandbox + .new_assert_cmd("network") + .args([ + "add", + "evilnet", + "--rpc-url", + &server.url(""), + "--network-passphrase", + "Test SDF Network ; September 2015", + ]) + .assert() + .success(); + + let stderr = sandbox.new_assert_cmd("doctor").assert().stderr_as_str(); + + get_version_info.assert(); + + assert_sanitized("doctor", &stderr); +} + +#[test] +fn snapshot_create_sanitizes_archive_passphrase() { + let server = MockServer::start(); + let history = server.mock(|when, then| { + when.method(GET).path("/.well-known/stellar-history.json"); + then.status(200).json_body(json!({ + "currentLedger": 100, + "currentBuckets": [], + "networkPassphrase": format!("Test SDF Network{ANSI}"), + })); + }); + let sandbox = TestEnv::default(); + + let stderr = sandbox + .new_assert_cmd("snapshot") + .args(["create", "--archive-url", &server.url("")]) + .assert() + .stderr_as_str(); + + history.assert(); + + assert_sanitized("snapshot create", &stderr); +} diff --git a/cmd/soroban-cli/src/commands/doctor.rs b/cmd/soroban-cli/src/commands/doctor.rs index b2d2ea989d..4e0c1d697b 100644 --- a/cmd/soroban-cli/src/commands/doctor.rs +++ b/cmd/soroban-cli/src/commands/doctor.rs @@ -1,6 +1,7 @@ use clap::Parser; use rustc_version::version; use semver::Version; +use soroban_spec_tools::sanitize; use std::fmt::Debug; use std::process::Command; @@ -105,7 +106,9 @@ async fn print_network( redact_url(&network.rpc_url) )); print.blankln(format!("protocol {}", version_info.protocol_version)); - print.blankln(format!("rpc {}", version_info.version)); + // `version` is a free-form string from the RPC response; sanitize it so a + // hostile peer can't smuggle terminal-escape sequences into the output. + print.blankln(format!("rpc {}", sanitize(&version_info.version))); Ok(()) } diff --git a/cmd/soroban-cli/src/commands/network/info.rs b/cmd/soroban-cli/src/commands/network/info.rs index c23dd1e308..9d16b4570b 100644 --- a/cmd/soroban-cli/src/commands/network/info.rs +++ b/cmd/soroban-cli/src/commands/network/info.rs @@ -1,4 +1,5 @@ use sha2::{Digest, Sha256}; +use soroban_spec_tools::sanitize; use crate::commands::global; use crate::config::network; @@ -63,18 +64,25 @@ struct Info { impl Info { fn print_text(&self, print: &print::Print) { + // Every field other than `id` and `protocol_version` is a free-form + // string taken straight from the RPC `getVersionInfo`/`getNetwork` + // response, so a hostile peer must not be able to smuggle + // terminal-escape sequences through them. print.infoln(format!("Network Id: {}", self.id)); - print.infoln(format!("Version: {}", self.version)); - print.infoln(format!("Commit Hash: {}", self.commit_hash)); - print.infoln(format!("Build Timestamp: {}", self.build_timestamp)); + print.infoln(format!("Version: {}", sanitize(&self.version))); + print.infoln(format!("Commit Hash: {}", sanitize(&self.commit_hash))); + print.infoln(format!( + "Build Timestamp: {}", + sanitize(&self.build_timestamp) + )); print.infoln(format!( "Captive Core Version: {}", - self.captive_core_version + sanitize(&self.captive_core_version) )); print.infoln(format!("Protocol Version: {}", self.protocol_version)); - print.infoln(format!("Passphrase: {}", self.passphrase)); + print.infoln(format!("Passphrase: {}", sanitize(&self.passphrase))); if let Some(friendbot_url) = &self.friendbot_url { - print.infoln(format!("Friendbot Url: {friendbot_url}")); + print.infoln(format!("Friendbot Url: {}", sanitize(friendbot_url))); } } } diff --git a/cmd/soroban-cli/src/commands/snapshot/create.rs b/cmd/soroban-cli/src/commands/snapshot/create.rs index ca7dcd18af..ff89ea3619 100644 --- a/cmd/soroban-cli/src/commands/snapshot/create.rs +++ b/cmd/soroban-cli/src/commands/snapshot/create.rs @@ -6,6 +6,7 @@ use humantime::format_duration; use itertools::{Either, Itertools}; use sha2::{Digest, Sha256}; use soroban_ledger_snapshot::LedgerSnapshot; +use soroban_spec_tools::sanitize; use std::{ collections::HashSet, fs, @@ -203,7 +204,12 @@ impl Cmd { let network_id = Sha256::digest(network_passphrase); print.infoln(format!("Ledger: {ledger}")); - print.infoln(format!("Network Passphrase: {network_passphrase}")); + // The passphrase comes from the history archive JSON, so sanitize it to + // keep a hostile archive from smuggling terminal-escape sequences. + print.infoln(format!( + "Network Passphrase: {}", + sanitize(network_passphrase) + )); print.infoln(format!("Network id: {}", hex::encode(network_id))); // Get ledger close time and base reserve from archive