-
Notifications
You must be signed in to change notification settings - Fork 0
π‘οΈ Sentinel: [HIGH] Harden Unit Test Gate and Fix Telemetry #319
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 | ||||||||||||||
|
|
||||||||||||||
| # ============================================================================= | ||||||||||||||
|
|
@@ -56,9 +57,9 @@ | |||||||||||||
| # TUNABLE: Adjust regex for different code formats | ||||||||||||||
| CODE_BLOCK_PATTERNS = [ | ||||||||||||||
| # Markdown code blocks: ```python ... ``` | ||||||||||||||
| r"```python\n(.*?)```", | ||||||||||||||
| r"```python\s*(.*?)\s*```", | ||||||||||||||
| # Markdown code blocks without language: ``` ... ``` | ||||||||||||||
| r"```\n(.*?)```", | ||||||||||||||
| r"```\s*(.*?)\s*```", | ||||||||||||||
|
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. The current generic code block pattern will also match blocks that start with
Suggested change
|
||||||||||||||
| # Inline code markers | ||||||||||||||
| r"`([^`\n]+)`", | ||||||||||||||
| ] | ||||||||||||||
|
|
@@ -84,6 +85,10 @@ | |||||||||||||
| r"\bshelve\.open\b", | ||||||||||||||
| # File operations (specifically writing/appending) | ||||||||||||||
| r"\bopen\s*\([^)]*,\s*(mode\s*=\s*)?['\"][^'\"r]*[wa+x]", | ||||||||||||||
| # Sandbox escapes | ||||||||||||||
| r"__subclasses__", | ||||||||||||||
| r"__globals__", | ||||||||||||||
| r"__builtins__", | ||||||||||||||
|
Comment on lines
+89
to
+91
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. Adding word boundaries (
Suggested change
|
||||||||||||||
| ] | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
|
@@ -213,6 +218,9 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu | |||||||||||||
| # Write code to temp file | ||||||||||||||
| test_file = os.path.join(temp_dir, "test_code.py") | ||||||||||||||
|
|
||||||||||||||
| # Indent the code to fit into the try block | ||||||||||||||
| indented_code = textwrap.indent(code, " ") | ||||||||||||||
|
Comment on lines
+221
to
+222
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. |
||||||||||||||
|
|
||||||||||||||
| # Wrap code to capture output safely | ||||||||||||||
| wrapped_code = f""" | ||||||||||||||
| import sys | ||||||||||||||
|
|
@@ -229,7 +237,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} | ||||||||||||||
| {indented_code} | ||||||||||||||
|
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. The current implementation executes untrusted code by directly injecting it into the wrapper script's source. This causes namespace leakage, allowing the generated code to access and potentially manipulate the wrapper's internal variables like A more secure approach is to execute the code in a restricted global namespace using
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| sys.stdout = original_stdout | ||||||||||||||
| sys.stderr = original_stderr | ||||||||||||||
|
|
@@ -255,6 +263,13 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu | |||||||||||||
| except SyntaxError as e: | ||||||||||||||
| return False, "", f"Syntax error: {e}" | ||||||||||||||
|
|
||||||||||||||
| # Filter environment to prevent secret leakage | ||||||||||||||
| allowed_env_vars = ["PATH", "PYTHONPATH", "LANG", "PYTHONIOENCODING"] | ||||||||||||||
| filtered_env = {k: v for k, v in os.environ.items() if k in allowed_env_vars} | ||||||||||||||
| filtered_env["PYTHONPATH"] = os.path.pathsep.join( | ||||||||||||||
| [temp_dir, filtered_env.get("PYTHONPATH", "")] | ||||||||||||||
| ).strip(os.path.pathsep) | ||||||||||||||
|
|
||||||||||||||
| # Try to execute with timeout | ||||||||||||||
| try: | ||||||||||||||
| result = subprocess.run( | ||||||||||||||
|
|
@@ -263,7 +278,7 @@ def test_python_code(code: str, temp_dir: str, execution_timeout: int = 5) -> Tu | |||||||||||||
| text=True, | ||||||||||||||
| timeout=execution_timeout, | ||||||||||||||
| cwd=temp_dir, | ||||||||||||||
| env={**os.environ, "PYTHONPATH": temp_dir}, | ||||||||||||||
| env=filtered_env, | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| stdout = result.stdout | ||||||||||||||
|
|
@@ -367,7 +382,9 @@ def load_jsonl(path: str) -> List[Dict[str, Any]]: | |||||||||||||
|
|
||||||||||||||
| def save_jsonl(samples: List[Dict[str, Any]], path: str) -> None: | ||||||||||||||
| """Save samples to JSONL file.""" | ||||||||||||||
| os.makedirs(os.path.dirname(path), exist_ok=True) | ||||||||||||||
| dirname = os.path.dirname(path) | ||||||||||||||
| if dirname: | ||||||||||||||
| os.makedirs(dirname, exist_ok=True) | ||||||||||||||
|
|
||||||||||||||
| with open(path, "w") as f: | ||||||||||||||
| for sample in samples: | ||||||||||||||
|
|
||||||||||||||
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.
This regex can be improved to handle optional whitespace between the backticks and the
pythonidentifier, making it more robust against different markdown formatting styles.