Skip to content

Command injection in IPython #12023

@mschwager

Description

@mschwager

Hi IPython,

First off, I'd like to say how much I enjoy using IPython - I use it every day. Recently I've begun work on a static analysis tool for Python code. I ran a subset of its rules against the IPython code base and received some interesting results. In particular, Dlint's checks for shell=True subprocess calls:

$ python3 -m flake8 --select=DUO116 ipython/IPython/
ipython/IPython/core/interactiveshell.py:2482:22: DUO116 use of "shell=True" is insecure in "subprocess" module
ipython/IPython/core/hooks.py:80:12: DUO116 use of "shell=True" is insecure in "subprocess" module
ipython/IPython/core/page.py:214:24: DUO116 use of "shell=True" is insecure in "subprocess" module
ipython/IPython/lib/editorhooks.py:55:16: DUO116 use of "shell=True" is insecure in "subprocess" module
ipython/IPython/utils/_process_common.py:79:9: DUO116 use of "shell=True" is insecure in "subprocess" module
ipython/IPython/utils/sysinfo.py:58:12: DUO116 use of "shell=True" is insecure in "subprocess" module

I manually investigated each finding with the following results:

  • interactiveshell.py: Unclear if vulnerable, unclear if shell=True is necessary
  • hooks.py: Vulnerable via $EDITOR, shell=True unnecessary
  • page.py: Vulnerable via $PAGER, shell=True unnecessary
  • editorhooks.py: Unclear if vulnerable, unclear if shell=True is necessary
  • _process_commend.py: Unclear if vulnerable, seems shell=True is necessary
  • sysinfo.py: Not vulnerable, shell=True unnecessary

The following demonstrates the vulnerabilities in hooks and page:

$ PAGER='echo "pager" > /tmp/pager' ipython -c "open??"
$ cat /tmp/pager
pager
$ EDITOR='echo "editor" > /tmp/editor' ipython -c "%edit"
$ cat /tmp/editor
editor

This issue is highlighted in the Python subprocess docs: Subprocess Security Considerations. This issue falls under CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection'). I would recommend avoiding shell=True whenever possible and investigating the other findings and ensuring they do not have the same issue. It appears that most of the calls do not need shell functionality anyway.

Let me know if you need any additional information!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions