Skip to content

Commit 7c852c7

Browse files
committed
Stop using base64 for escaping bash
Signed-off-by: Aleksei Stepanov <penguinolog@gmail.com>
1 parent 4794806 commit 7c852c7

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

exec_helpers/_ssh_client_base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
__all__ = ("SSHClientBase", "SshExecuteAsyncResult")
2020

2121
# Standard Library
22-
import base64
2322
import concurrent.futures
2423
import copy
2524
import datetime
2625
import logging
26+
import shlex
2727
import stat
2828
import time
2929
import typing
@@ -479,8 +479,7 @@ def _execute_async( # pylint: disable=arguments-differ
479479

480480
started = datetime.datetime.utcnow()
481481
if self.sudo_mode:
482-
encoded_cmd = base64.b64encode(cmd.encode("utf-8")).decode("utf-8")
483-
cmd = f'sudo -S bash -c \'eval "$(base64 -d <(echo "{encoded_cmd}"))"\''
482+
cmd = f'sudo -S bash -c "eval {shlex.quote(cmd)}"'
484483
chan.exec_command(cmd) # nosec # Sanitize on caller side
485484
if stdout.channel.closed is False:
486485
# noinspection PyTypeChecker

test/test_ssh_client_execute_async_special.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# under the License.
1414

1515
# Standard Library
16-
import base64
16+
import shlex
1717
import typing
1818
from unittest import mock
1919

@@ -55,10 +55,10 @@ def read_stream(stream: FakeFileStream) -> typing.Tuple[bytes, ...]:
5555
password = "pass"
5656

5757
command = "ls ~\nline 2\nline 3\nline с кирилицей"
58+
cmd_execute = f"{command}\n"
5859
command_log = f"Executing command:\n{command.rstrip()!r}\n"
5960
stdout_src = (b" \n", b"2\n", b"3\n", b" \n")
6061
stderr_src = (b" \n", b"0\n", b"1\n", b" \n")
61-
encoded_cmd = base64.b64encode(f"{command}\n".encode("utf-8")).decode("utf-8")
6262

6363
print_stdin = 'read line; echo "$line"'
6464
default_timeout = 60 * 60 # 1 hour
@@ -120,7 +120,7 @@ def test_001_execute_async_sudo(ssh, ssh_transport_channel):
120120
ssh_transport_channel.assert_has_calls(
121121
(
122122
mock.call.makefile_stderr("rb"),
123-
mock.call.exec_command(f'sudo -S bash -c \'eval "$(base64 -d <(echo "{encoded_cmd}"))"\''),
123+
mock.call.exec_command(f'sudo -S bash -c \"eval {shlex.quote(cmd_execute)}\"'),
124124
)
125125
)
126126

0 commit comments

Comments
 (0)