Skip to content

Commit 7b3675a

Browse files
authored
Escape control characters in message sign preview. (#2517)
1 parent 946e839 commit 7b3675a

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

cmd/crates/soroban-test/tests/it/message.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,8 @@ fn message_sign_escapes_control_characters_in_preview() {
212212
!stderr.contains('\x1b'),
213213
"stderr should not contain raw ESC bytes, got: {stderr:?}"
214214
);
215+
assert!(
216+
stderr.contains("\\x1b"),
217+
"stderr should contain escaped ESC as \\x1b, got: {stderr:?}"
218+
);
215219
}

cmd/soroban-cli/src/commands/message/sign.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
config::{locator, secret},
1010
print::Print,
1111
signer::{self, Signer},
12-
utils::strip_control_escapes,
12+
utils::escape_control_characters,
1313
};
1414

1515
use super::SEP53_PREFIX;
@@ -93,7 +93,7 @@ impl Cmd {
9393
};
9494
print.infoln(format!(
9595
"Message: {}",
96-
strip_control_escapes(&message_display)
96+
escape_control_characters(&message_display)
9797
));
9898
println!("{signature_base64}");
9999
Ok(())

cmd/soroban-cli/src/utils.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,20 @@ pub fn is_hex_string(s: &str) -> bool {
171171
s.chars().all(|s| s.is_ascii_hexdigit())
172172
}
173173

174-
pub fn strip_control_escapes(s: &str) -> String {
175-
s.chars().filter(|c| !c.is_control()).collect()
174+
pub fn escape_control_characters(s: &str) -> String {
175+
use std::fmt::Write as _;
176+
let mut result = String::with_capacity(s.len());
177+
for c in s.chars() {
178+
if c.is_control() {
179+
let mut buf = [0u8; 4];
180+
for &byte in c.encode_utf8(&mut buf).as_bytes() {
181+
write!(result, "\\x{byte:02x}").unwrap();
182+
}
183+
} else {
184+
result.push(c);
185+
}
186+
}
187+
result
176188
}
177189

178190
pub fn contract_id_hash_from_asset(

0 commit comments

Comments
 (0)