Skip to content

Commit 21cae9f

Browse files
committed
Safely invoke commands over SSH
It is common misunderstanding that SSH accepts CMD [ARGS...]. Actually it joins all positional arguments and passes the resulting single string to sh -c.
1 parent 14404f4 commit 21cae9f

3 files changed

Lines changed: 6 additions & 7 deletions

File tree

src/img/tester.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#~ along with this program. If not, see <http://www.gnu.org/licenses/>.
1414

1515
import os, sys
16+
import pipes
1617
import hashlib
1718
import subprocess as sub
1819
from multiprocessing import Process, TimeoutError
@@ -194,7 +195,7 @@ def ssh(self, command, user="root", ignore_error=False):
194195
ssh_comm = copy(self.sshbase)
195196
ssh_comm.extend(["-l%s" % user])
196197
ssh_comm.extend(self.sopts)
197-
ssh_comm.extend(command)
198+
ssh_comm.extend([pipes.quote(arg) for arg in command])
198199
try:
199200
self.run(ssh_comm)
200201
except sub.CalledProcessError:
@@ -588,7 +589,7 @@ def run_tests(self):
588589
else:
589590
print "trying to get any test results"
590591
self.commands.scpfrom("/tmp/results/*", self.results_dir)
591-
self.commands.ssh(['rm', '-rf', '/tmp/results/*'])
592+
self.commands.ssh(['sh', '-c', 'rm -rf /tmp/results/*'])
592593
except:
593594
pass
594595

src/img/worker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""MIC2 mic-image-creator wrapper"""
1717

1818
import os
19+
import pipes
1920
import subprocess as sub
2021
from multiprocessing import Process, TimeoutError
2122
import time, datetime
@@ -250,7 +251,7 @@ def ssh(self, command):
250251
"""
251252
ssh_comm = copy(self.sshbase)
252253
ssh_comm.extend(self.sopts)
253-
ssh_comm.extend(command)
254+
ssh_comm.extend([pipes.quote(arg) for arg in command])
254255
self.run(ssh_comm)
255256

256257
def is_lvm(self, img):
@@ -351,7 +352,7 @@ def runmic(self, ssh=False, job_args=None):
351352
mic_comm.append('--config=%s' % job_args.ksfile_name)
352353
elif self.ict == "mic":
353354
mic_comm.append('%s' % job_args.image_type)
354-
mic_comm.append('"%s"' % job_args.ksfile_name)
355+
mic_comm.append('%s' % job_args.ksfile_name)
355356

356357
mic_comm.append('--arch=%s' % job_args.arch)
357358
mic_comm.append('--outdir=%s' % job_args.outdir)

src/img_web/app/views.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ def submit(request):
140140
else:
141141
tokenmap["RELEASEPATTERN"] = ":/%s" % tokenvalue
142142

143-
if " " in tokenvalue:
144-
tokenvalue = '"%s"' % tokenvalue
145-
146143
tokenmap[token.name] = tokenvalue
147144

148145
archtoken = jobdata['architecture']

0 commit comments

Comments
 (0)