Consider quotes when splitting command arguments

This commit is contained in:
mfridge 2023-01-06 16:07:33 +01:00
parent 8bdbed3331
commit 848c0ef3ab

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.