When to use Shell=True for Python subprocess module -


this question has answer here:

it seems whenever try use python's subprocess module, find still don't understand things. currently, trying join 3 mp4 files within python module.

when tried

z ='mp4box -cat test_0.mp4 -cat test_1.mp4 -cat test_2.mp4 -new test_012d.mp4' subprocess.popen(z,shell=true) 

everything worked.

when tried

z = ['mp4box', '-cat test_0.mp4', '-cat test_1.mp4', '-cat test_2.mp4', '-new test_012d.mp4'] subprocess.popen(z,shell=false) 

i got following error:

option -cat test_0.mp4 unknown. please check usage 

i thought shell=false needed supply list first element executable wanted run , each succeeding element argument executable. mistaken in belief, or there correct way create command wanted use?

also, there rules using shell=true in subprocess.popen? far, know(?) "don't - can expose code shell injection attacks". why shell=false avoid problem? there ever actual advantage using 'shell=true`?

if shell true, specified command executed through shell. can useful if using python enhanced control flow offers on system shells , still want convenient access other shell features such shell pipes, filename wildcards, environment variable expansion, , expansion of ~ user’s home directory.

when shell=true dangerous?

if execute shell commands might include unsanitized input untrusted source, make program vulnerable shell injection, serious security flaw can result in arbitrary command execution. reason, use of shell=true discouraged in cases command string constructed external input

eg. (taken docs)

>>> subprocess import call >>> filename = input("what file display?\n") file display? non_existent; rm -rf / # >>> call("cat " + filename, shell=true) # uh-oh. end badly.. 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -