~ubuntu-branches/ubuntu/wily/bandit/wily-proposed

« back to all changes in this revision

Viewing changes to docs/exec.md

  • Committer: Package Import Robot
  • Author(s): Dave Walker (Daviey)
  • Date: 2015-07-22 09:01:39 UTC
  • Revision ID: package-import@ubuntu.com-20150722090139-fl0nluy0x8m9ctx4
Tags: upstream-0.12.0
ImportĀ upstreamĀ versionĀ 0.12.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
exec()
 
2
=====================
 
3
The [python docs](https://docs.python.org/2.0/ref/exec.html) succinctly describe why ```exec()``` is bad:
 
4
* "This statement supports dynamic execution of Python code."
 
5
 
 
6
### Correct
 
7
Look for alternate solutions than ```exec```; often times you can find other modules or builtins to complete the task securely.
 
8
 
 
9
If ```exec``` is absolutely necessary, extreme care must be taken to ensure no untrusted input is included in the expression that ```exec``` evaluates.
 
10
 
 
11
### Incorrect
 
12
A common use case is to to read a file and then exec the content to execute Python within your currently running script, e.g:
 
13
```python
 
14
exec( open('setup.py','rb').read() )
 
15
```
 
16
 
 
17
That is obviously scary because you are executing the Python code in setup.py.  Another example that is even more scary is a practice similar to:
 
18
```python
 
19
exec 'from ' + mod_name + ' import test'
 
20
```
 
21
 
 
22
If we set mod_name to ```unittest```, everything works normally. However, if we set mod_name to ```unittest import test; import ast #``` we've successfully imported a module that the developer did not intend.
 
23
 
 
24
## Consequences
 
25
* Unintended code execution
 
26
 
 
27
## References
 
28
* [0] https://docs.python.org/2.0/ref/exec.html