From 2ddbeae138dc2964c84b78fdedc43f7d44c331c9 Mon Sep 17 00:00:00 2001 From: "bors[bot]" <26634292+bors[bot]@users.noreply.github.com> Date: Wed, 21 Jun 2023 18:12:52 +0000 Subject: [PATCH] Merge #3135 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 3135: Quote '\n' in strings which go through SSH. r=georgeliao,sharder996.townsend2010 a=luis4a0 When we need to pass a string through SSH, we escape some characters. But we forgot about newlines, which are eliminated unless quoted. This PR just put newlines in double quotes, and leave escaping the rest of the characters untouched. Fixes https://github.com/canonical/multipass/issues/3116. Co-authored-by: Luis PeƱaranda --- src/utils/utils.cpp | 5 +++-- tests/test_utils.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index c8b3cc1536..e2133f3974 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -263,8 +263,9 @@ std::string mp::utils::escape_for_shell(const std::string& in) { if (0xa == c) // newline { - *ret_insert++ = 0x5c; // backslash - *ret_insert++ = 0x20; // space + *ret_insert++ = 0x22; // double quotes + *ret_insert++ = 0xa; // newline + *ret_insert++ = 0x22; // double quotes } else { diff --git a/tests/test_utils.cpp b/tests/test_utils.cpp index 583090b406..c9e8c79b3a 100644 --- a/tests/test_utils.cpp +++ b/tests/test_utils.cpp @@ -426,11 +426,11 @@ TEST(Utils, escape_for_shell_actually_escapes) EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\\ \\\"quotes\\\"")); } -TEST(Utils, escape_for_shell_replaces_newlines_with_spaces) +TEST(Utils, escape_for_shell_quotes_newlines) { std::string s{"I've got\nnewlines"}; auto res = mp::utils::escape_for_shell(s); - EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\\ newlines")); + EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\"\n\"newlines")); } TEST(Utils, escape_for_shell_quotes_empty_string)