Skip to content

Commit a680e7a

Browse files
committed
Allow getenv
1 parent 9302ce1 commit a680e7a

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

detectors/built_in/custom_detectors_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def static_code_analysis(module_path, forbidden_imports=None, forbidden_calls=No
136136
if isinstance(node, ast.ImportFrom):
137137
if node.module and node.module.split(".")[0] in forbidden_imports:
138138
# Allow specific exception: from os import environ
139-
if node.module == "os" and len(node.names) == 1 and node.names[0].name == "environ":
139+
if node.module == "os" and len(node.names) == 1 and node.names[0].name in {"environ", "getenv"}:
140140
continue
141141
issues.append(f"- Forbidden import: {node.module} (line {node.lineno})")
142142

tests/detectors/builtIn/test_custom.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def test_unsafe_code(self, client):
144144
assert "Unsafe code detected" in str(excinfo.value)
145145
assert "Forbidden import: os" in str(excinfo.value) or "os.system" in str(excinfo.value)
146146

147+
147148
def test_unsafe_code_import_from(self, client):
148149
write_code_to_custom_detectors(UNSAFE_CODE_IMPORT_FROM)
149150
from detectors.built_in.custom_detectors_wrapper import CustomDetectorRegistry
@@ -152,7 +153,9 @@ def test_unsafe_code_import_from(self, client):
152153
assert "Unsafe code detected" in str(excinfo.value)
153154
assert "Forbidden import: sys" in str(excinfo.value) or "sys.path" in str(excinfo.value)
154155

155-
def test_unsafe_code_import_from_environ(self, client):
156+
157+
def test_safe_code_import_from_environ(self, client):
158+
# from os import environ <- should not trigger the unsafe import error
156159
write_code_to_custom_detectors(SAFE_CODE_IMPORT_FROM_ENVIRON)
157160
from detectors.built_in.custom_detectors_wrapper import CustomDetectorRegistry
158161
CustomDetectorRegistry()

0 commit comments

Comments
 (0)