Repository navigation
Expand file tree
/
Copy pathtest_process_creation_sandbox.py
More file actions
175 lines (144 loc) · 5.17 KB
/
Copy pathtest_process_creation_sandbox.py
File metadata and controls
175 lines (144 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import pytest
import mock
from codemodder.dependency import Security
from core_codemods.process_creation_sandbox import ProcessSandbox
from tests.codemods.base_codemod_test import BaseSemgrepCodemodTest
@mock.patch("codemodder.codemods.api.FileContext.add_dependency")
class TestProcessCreationSandbox(BaseSemgrepCodemodTest):
codemod = ProcessSandbox
def test_name(self, _):
assert self.codemod.name == "sandbox-process-creation"
def test_import_subprocess(self, adds_dependency, tmpdir):
input_code = """
import subprocess
subprocess.run("echo 'hi'", shell=True)
var = "hello"
"""
expected = """
import subprocess
from security import safe_command
safe_command.run(subprocess.run, "echo 'hi'", shell=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)
adds_dependency.assert_called_once_with(Security)
def test_import_alias(self, adds_dependency, tmpdir):
input_code = """
import subprocess as sub
sub.run("echo 'hi'", shell=True)
var = "hello"
"""
expected = """
import subprocess as sub
from security import safe_command
safe_command.run(sub.run, "echo 'hi'", shell=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)
adds_dependency.assert_called_once_with(Security)
def test_from_subprocess(self, adds_dependency, tmpdir):
input_code = """
from subprocess import run
run("echo 'hi'", shell=True)
var = "hello"
"""
expected = """
from subprocess import run
from security import safe_command
safe_command.run(run, "echo 'hi'", shell=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)
adds_dependency.assert_called_once_with(Security)
def test_subprocess_nameerror(self, _, tmpdir):
input_code = """
subprocess.run("echo 'hi'", shell=True)
import subprocess
"""
expected = input_code
self.run_and_assert(tmpdir, input_code, expected)
@pytest.mark.parametrize(
"input_code,expected",
[
(
"""
import subprocess
import csv
subprocess.run("echo 'hi'", shell=True)
csv.excel
""",
"""
import subprocess
import csv
from security import safe_command
safe_command.run(subprocess.run, "echo 'hi'", shell=True)
csv.excel
""",
),
(
"""
import subprocess
from csv import excel
subprocess.run("echo 'hi'", shell=True)
excel
""",
"""
import subprocess
from csv import excel
from security import safe_command
safe_command.run(subprocess.run, "echo 'hi'", shell=True)
excel
""",
),
],
)
def test_other_import_untouched(
self, adds_dependency, tmpdir, input_code, expected
):
self.run_and_assert(tmpdir, input_code, expected)
adds_dependency.assert_called_once_with(Security)
def test_multifunctions(self, adds_dependency, tmpdir):
# Test that subprocess methods that aren't part of the codemod are not changed.
# If we add the function as one of
# our codemods, this test would change.
input_code = """
import subprocess
subprocess.run("echo 'hi'", shell=True)
subprocess.check_output(["ls", "-l"])"""
expected = """
import subprocess
from security import safe_command
safe_command.run(subprocess.run, "echo 'hi'", shell=True)
subprocess.check_output(["ls", "-l"])"""
self.run_and_assert(tmpdir, input_code, expected)
adds_dependency.assert_called_once_with(Security)
def test_custom_run(self, _, tmpdir):
input_code = """
from app_funcs import run
run("echo 'hi'", shell=True)"""
expected = input_code
self.run_and_assert(tmpdir, input_code, expected)
def test_subprocess_call(self, adds_dependency, tmpdir):
input_code = """
import subprocess
subprocess.call(["ls", "-l"])
"""
expected = """
import subprocess
from security import safe_command
safe_command.run(subprocess.call, ["ls", "-l"])
"""
self.run_and_assert(tmpdir, input_code, expected)
adds_dependency.assert_called_once_with(Security)
def test_subprocess_popen(self, adds_dependency, tmpdir):
input_code = """
import subprocess
subprocess.Popen(["ls", "-l"])
"""
expected = """
import subprocess
from security import safe_command
safe_command.run(subprocess.Popen, ["ls", "-l"])
"""
self.run_and_assert(tmpdir, input_code, expected)
adds_dependency.assert_called_once_with(Security)