Shell escape a string to safely use it as a token in a shell command (backport of cPython shlex.quote for Python versions 2.x & < 3.3)
Project description
shellescape
Description
The shellescape Python module defines the shellescape.quote()
function that returns a shell-escaped version of a Python string. This is a backport of the shlex.quote()
function from Python 3.8 that makes it accessible to users of Python 3 versions < 3.3 and all Python 2.x versions.
quote(s)
From the Python documentation:
Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.
This idiom would be unsafe:
>>> filename = 'somefile; rm -rf ~'
>>> command = 'ls -l {}'.format(filename)
>>> print(command) # executed by a shell: boom!
ls -l somefile; rm -rf ~
quote()
lets you plug the security hole:
>>> command = 'ls -l {}'.format(quote(filename))
>>> print(command)
ls -l 'somefile; rm -rf ~'
>>> remote_command = 'ssh home {}'.format(quote(command))
>>> print(remote_command)
ssh home 'ls -l '"'"'somefile; rm -rf ~'"'"''
The quoting is compatible with UNIX shells and with shlex.split()
:
>>> remote_command = split(remote_command)
>>> remote_command
['ssh', 'home', "ls -l 'somefile; rm -rf ~'"]
>>> command = split(remote_command[-1])
>>> command
['ls', '-l', 'somefile; rm -rf ~']
Usage
Include shellescape
in your project setup.py file install_requires
dependency definition list:
setup(
...
install_requires=['shellescape'],
...
)
Then import the quote
function into your module(s) and use it as needed:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from shellescape import quote
filename = "somefile; rm -rf ~"
escaped_shell_command = 'ls -l {}'.format(quote(filename))
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file shellescape-3.8.1.tar.gz
.
File metadata
- Download URL: shellescape-3.8.1.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40b310b30479be771bf3ab28bd8d40753778488bd46ea0969ba0b35038c3ec26 |
|
MD5 | 82e0074ef39471286f37d701e9efce7e |
|
BLAKE2b-256 | 194013b9e84bf04774365830cbed1bd95a989d5324a99d207bcb1619a6c517f2 |
File details
Details for the file shellescape-3.8.1-py2.py3-none-any.whl
.
File metadata
- Download URL: shellescape-3.8.1-py2.py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f17127e390fa3f9aaa80c69c16ea73615fd9b5318fd8309c1dca6168ae7d85bf |
|
MD5 | 26e30843610330a890d96f35aa22859b |
|
BLAKE2b-256 | d0f40081137fceff5779cd4205c1e96657e41cc2d2d56c940dc8eeb6111780f7 |