Merge pull request #2239 from mfridge/action-command-split

Consider quotes when splitting command arguments
This commit is contained in:
Nicolas Hennion 2023-01-08 14:50:59 +01:00 committed by GitHub
commit 1e7d058dae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@
from glances.compat import nativestr
from subprocess import Popen, PIPE
import re
def secure_popen(cmd):
@ -48,8 +49,8 @@ def __secure_popen(cmd):
p_last = None
# Split by pipe '|'
for sub_cmd in cmd.split('|'):
# Split by space ' '
sub_cmd_split = [i for i in sub_cmd.split(' ') if i]
# Split by space character, but do no split spaces within quotes
sub_cmd_split = [_ for _ in list(filter(None, re.split(r'(\s+)|(".*?"+?)|(\'.*?\'+?)', sub_cmd))) if _ != ' ']
p = Popen(sub_cmd_split, shell=False, stdin=sub_cmd_stdin, stdout=PIPE, stderr=PIPE)
if p_last is not None:
# Allow p_last to receive a SIGPIPE if p exits.