π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Escape terminal controls in paste consume
Manav Rathie90d793c8ba8
diff --git a/rust/apps/cli/src/cli/paste.rs b/rust/apps/cli/src/cli/paste.rsindex 36567a553c..d86b6fa340 100644--- a/rust/apps/cli/src/cli/paste.rs+++ b/rust/apps/cli/src/cli/paste.rs@@ -32,12 +32,18 @@ pub enum PasteSubcommands {password: bool,},- /// Consume a one-time encrypted paste+ /// Consume a one-time encrypted paste.+ ///+ /// Control characters are escaped when stdout is a terminal. Use --raw to print verbatim.#[command(alias = "read")]Consume {/// Paste URL or access tokenlink_or_token: String,+ /// Print paste text verbatim even when stdout is a terminal.+ #[arg(long)]+ raw: bool,+/// Fragment key when passing only an access token./// Password-protected keys include the `p-` prefix.#[arg(long)]diff --git a/rust/apps/cli/src/commands/paste.rs b/rust/apps/cli/src/commands/paste.rsindex 9ffcde6576..00d43c59f8 100644--- a/rust/apps/cli/src/commands/paste.rs+++ b/rust/apps/cli/src/commands/paste.rs@@ -43,17 +43,44 @@ pub async fn handle_paste_command(cmd: PasteCommand) -> Result<()> {}PasteSubcommands::Consume {link_or_token,+ raw,key,endpoint,} => {let (access_token, paste_key) = parse_paste_reference(&link_or_token, key)?;let text = consume_paste(&endpoint, &access_token, &paste_key).await?;- print!("{text}");+ print_consumed_paste(&text, raw);Ok(())}}}+fn print_consumed_paste(text: &str, raw: bool) {+ if !raw && io::stdout().is_terminal() {+ print!("{}", terminal_safe_paste_text(text));+ } else {+ print!("{text}");+ }+}++fn terminal_safe_paste_text(text: &str) -> String {+ let mut output = String::with_capacity(text.len());+ let mut chars = text.chars().peekable();+ while let Some(ch) = chars.next() {+ match ch {+ '\n' | '\t' => output.push(ch),+ '\r' if chars.peek() == Some(&'\n') => {+ chars.next();+ output.push('\r');+ output.push('\n');+ }+ _ if ch.is_control() => output.extend(ch.escape_default()),+ _ => output.push(ch),+ }+ }+ output+}+fn read_paste_text(text: Option<String>, file: Option<PathBuf>) -> Result<String> {let text = match (text, file) {(Some(text), None) => text,@@ -710,6 +737,28 @@ mod tests {assert!(matches!(error, Error::AuthenticationFailed(_)));}+ #[test]+ fn default_tty_output_escapes_controls() {+ let text = "ok\n\x1b]52;c;AAAA\x07\rhidden\tend\u{85}";++ assert_eq!(+ terminal_safe_paste_text(text),+ "ok\n\\u{1b}]52;c;AAAA\\u{7}\\rhidden\tend\\u{85}",+ );+ }++ #[test]+ fn terminal_safe_paste_text_preserves_crlf() {+ assert_eq!(terminal_safe_paste_text("one\r\ntwo\r\n"), "one\r\ntwo\r\n");+ }++ #[test]+ fn terminal_safe_paste_text_preserves_printable_unicode() {+ let text = "api key: fran\u{e7}ais \u{1f510}";++ assert_eq!(terminal_safe_paste_text(text), text);+ }+#[test]fn prompted_password_retry_can_recover() {let (paste_key, payload) =