-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [security enhancement] Harden unit test gate and fix telemetry bug #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/bootstrap-scaffold
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |||||
| import subprocess | ||||||
| import sys | ||||||
| import tempfile | ||||||
| import textwrap | ||||||
| from typing import Any, Dict, List, Tuple | ||||||
|
|
||||||
| # ============================================================================= | ||||||
|
|
@@ -82,6 +83,10 @@ | |||||
| r"\bshutil\.(rmtree|move|copy|copy2|copyfile|copymode|copystat|chown)\b", | ||||||
| r"\bpickle\.(load|loads)\b", | ||||||
| r"\bshelve\.open\b", | ||||||
| # Internal attributes for sandbox escape | ||||||
| r"__subclasses__", | ||||||
| r"__globals__", | ||||||
| r"__builtins__", | ||||||
| # File operations (specifically writing/appending) | ||||||
| r"\bopen\s*\([^)]*,\s*(mode\s*=\s*)?['\"][^'\"r]*[wa+x]", | ||||||
| ] | ||||||
|
|
@@ -229,7 +234,7 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu | |||||
| sys.stderr = stderr_capture | ||||||
|
|
||||||
| # Execute the user's code | ||||||
| {code} | ||||||
| {textwrap.indent(code, ' ')} | ||||||
|
|
||||||
| sys.stdout = original_stdout | ||||||
| sys.stderr = original_stderr | ||||||
|
|
@@ -257,13 +262,19 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu | |||||
|
|
||||||
| # Try to execute with timeout | ||||||
| try: | ||||||
| # BOLT SECURITY: Filter environment to prevent leakage of sensitive keys (e.g. OPENAI_API_KEY) | ||||||
| # to the generated code being tested. | ||||||
| allowed_env_keys = {"PATH", "PYTHONPATH", "LANG", "PYTHONIOENCODING"} | ||||||
| safe_env = {k: v for k, v in os.environ.items() if k in allowed_env_keys} | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Environment variable keys can be case-insensitive on some platforms (like Windows). The current set lookup is case-sensitive, which might cause essential variables like
Suggested change
|
||||||
| safe_env["PYTHONPATH"] = temp_dir # Ensure isolation and local imports | ||||||
|
|
||||||
| result = subprocess.run( | ||||||
| [sys.executable, test_file], | ||||||
| capture_output=True, | ||||||
| text=True, | ||||||
| timeout=execution_timeout, | ||||||
| cwd=temp_dir, | ||||||
| env={**os.environ, "PYTHONPATH": temp_dir}, | ||||||
| env=safe_env, | ||||||
| ) | ||||||
|
|
||||||
| stdout = result.stdout | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
PYTHONPATHkey inallowed_env_keysis redundant because it is explicitly overwritten on line 269. Removing it from the allowlist improves clarity and ensures that the host'sPYTHONPATHis intentionally ignored in favor of the isolatedtemp_dir. Additionally, consider movingallowed_env_keysto a module-level constant to avoid recreating the set on every function call.