dash/ash
dash/ash
Posted Oct 9, 2014 11:30 UTC (Thu) by ssokolow (guest, #94568)In reply to: dash/ash by ibukanov
Parent article: Bash gets shellshocked
That sort of thing is why I encourage friends who are launching child processes to do their scripting in Python using the subprocess module.
They really did a great job on designing its API... especially when paired with various other modules already part of stdlib.
- It's guaranteed to execvp() the requested binary directly without shell indirection unless you explicitly use
shell=True - Any necessary argument parsing and expansion can be done without arbitrary code execution by using
shlex.split(),os.path.expanduser(),fnmatch.filter(),glob.glob()modules from the Python standard library. - Quoted strings can still be handled safely by using shlex to explicitly perform argument splitting without code execution before using subprocess.
- The
envargument makes it easy to call a subprocess with a sanitized environment. - The
cwdargument avoids the need forcding inos.system()or doing anos.getcwd() os.chdir()dance.
Apparently someone's also ported it to ruby though, unfortunately, it's not part of stdlib there and I don't know whether shlex is also available.
Plus, of course, convenience functions like subprocess.call(), subprocess.check_call(), and subprocess.check_output() integrate nicely with the mix of try/except/finally and os.walk() I already recommend for that sort of scripting.
