Skip to content

Python: Add shlex.quote as command injection sanitizer#23

Open
mrigankpawagi wants to merge 1 commit into
mainfrom
improve-py-cmd-injection-shlex
Open

Python: Add shlex.quote as command injection sanitizer#23
mrigankpawagi wants to merge 1 commit into
mainfrom
improve-py-cmd-injection-shlex

Conversation

@mrigankpawagi

Copy link
Copy Markdown
Owner

Fix

Adds shlex.quote() and pipes.quote() (deprecated alias) as sanitizers for the py/command-line-injection query.

Problem

shlex.quote() is the standard Python function for escaping shell metacharacters in command arguments. It wraps the string in single quotes and escapes any existing single quotes. Code that uses shlex.quote() before passing user input to shell commands is safe from command injection, but the query currently flags it anyway.

Validation

Python docs: https://docs.python.org/3/library/shlex.html#shlex.quote - "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."

shlex.quote (Python 3) and pipes.quote (Python 2) properly escape
shell metacharacters, preventing command injection when user input
is incorporated into shell commands. These were already sanitizers
for py/shell-command-constructed-from-input but not for
py/command-line-injection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants