From 7ab5244bf5c993ec07764ac9963e64aecf05f7ab Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 9 Jul 2019 17:48:43 +0530 Subject: [PATCH] ssh kitten: Make argument parsing more like ssh Fixes #1787 --- docs/changelog.rst | 2 ++ kittens/ssh/main.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 4f235cf37..41383f592 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -46,6 +46,8 @@ To update |kitty|, :doc:`follow the instructions `. - Linux: Fix a regression in 0.14.0 that caused the event loop to tick continuously, wasting CPU even when idle (:iss:`1782`) +- ssh kitten: Make argument parsing more like ssh (:iss:`1787`) + 0.14.2 [2019-06-09] --------------------- diff --git a/kittens/ssh/main.py b/kittens/ssh/main.py index 6e7a27149..ff37048e0 100644 --- a/kittens/ssh/main.py +++ b/kittens/ssh/main.py @@ -4,6 +4,7 @@ import os import re +import shlex import subprocess import sys @@ -93,6 +94,18 @@ def parse_ssh_args(args): return ssh_args, server_args, passthrough +def quote(x): + # we have to escape unbalanced quotes and other unparseable + # args as they will break the shell script + # But we do not want to quote things like * or 'echo hello' + # See https://github.com/kovidgoyal/kitty/issues/1787 + try: + shlex.split(x) + except ValueError: + x = shlex.quote(x) + return x + + def main(args): ssh_args, server_args, passthrough = parse_ssh_args(args[1:]) if passthrough: @@ -101,7 +114,7 @@ def main(args): terminfo = subprocess.check_output(['infocmp']).decode('utf-8') sh_script = SHELL_SCRIPT.replace('TERMINFO', terminfo, 1) if len(server_args) > 1: - command_to_execute = ["'{}'".format(c.replace("'", """'"'"'""")) for c in server_args[1:]] + command_to_execute = [quote(c) for c in server_args[1:]] command_to_execute = 'exec ' + ' '.join(command_to_execute) else: command_to_execute = ''