Skip to content

Commit 453c391

Browse files
committed
Python: Add CodeExecution tests for stdlib
1 parent 0af86cb commit 453c391

File tree

16 files changed

+109
-0
lines changed

16 files changed

+109
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# exec statement is Python 2 specific
2+
exec "print(42)" # $getCode="print(42)"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| CodeExecution.py:2:19:2:40 | Comment # $getCode="print(42)" | Missing result:getCode="print(42)" |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import python
2+
import experimental.meta.ConceptsTest
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=1 --lang=2
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import builtins
2+
3+
# exec being part of builtins is Python 3 only
4+
builtins.exec("print(42)") # $getCode="print(42)"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| CodeExecution.py:4:29:4:50 | Comment # $getCode="print(42)" | Missing result:getCode="print(42)" |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import python
2+
import experimental.meta.ConceptsTest
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=1 --lang=3
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# without this, `eval("print(42)")` becomes invalid syntax in Python 2, since print is a
2+
# statement
3+
from __future__ import print_function
4+
5+
import sys
6+
7+
if sys.version_info[0] == 3:
8+
import builtins
9+
if sys.version_info[0] == 2:
10+
import __builtin__ as builtins
11+
12+
exec("print(42)") # $getCode="print(42)"
13+
eval("print(42)") # $getCode="print(42)"
14+
15+
builtins.eval("print(42)") # $getCode="print(42)"
16+
17+
cmd = compile("print(42)", "<filename>", "exec")
18+
exec(cmd) # $getCode=cmd
19+
20+
cmd = builtins.compile("print(42)", "<filename>", "exec")
21+
exec(cmd) # $getCode=cmd
22+
23+
# ------------------------------------------------------------------------------
24+
# taint related
25+
26+
27+
def test_additional_taint():
28+
src = TAINTED_STRING
29+
30+
cmd1 = compile(src, "<filename>", "exec")
31+
cmd2 = compile(source=src, filename="<filename>", mode="exec")
32+
cmd3 = builtins.compile(src, "<filename>", "exec")
33+
34+
ensure_tainted(
35+
src,
36+
cmd1,
37+
cmd2,
38+
cmd3,
39+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# without this, `eval("print(42)")` becomes invalid syntax in Python 2, since print is a
2+
# statement
3+
from __future__ import print_function
4+
5+
6+
def eval(*args, **kwargs):
7+
raise Exception("no eval")
8+
9+
10+
# This function call might be marked as a code execution, but it actually isn't.
11+
eval("print(42)")

0 commit comments

Comments
 (0)