Repository navigation
fix(plugins): replace unsafe eval with ast.literal_eval in db.py (#13663) - #13694
Conversation
There was a problem hiding this comment.
2 issues found across 2 files
Confidence score: 3/5
plugins/test_db.pyis omitted from.github/checks-manifest.yaml, so the new security regression test will not run in CI or preflight lanes and the eval-to-literal_eval fix could regress unnoticed—register the test in the manifest.plugins/test_db.pypermanently replacessys.modules["redis"]andsys.modules["models"]at module import, which can contaminate later tests in a shared pytest session—restore the original modules or scope the stubs with test isolation.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="plugins/test_db.py">
<violation number="1" location="plugins/test_db.py:40">
P3: The module-scope `sys.modules["redis"] = redis_stub` / `sys.modules["models"] = models_stub` override installs permanently at import and is never restored, so every later import in a shared pytest session silently resolves to these hand-rolled stub objects (e.g. a non-Pydantic `TranscriptSegment`). This can mask real model behavior and bleed into co-run test files; it also duplicates the repo's sanctioned `backend/testing/import_isolation.stub_modules` helper. Prefer that helper (or defer imports into a per-test context) instead of permanently mutating `sys.modules`.</violation>
<violation number="2" location="plugins/test_db.py:46">
P2: The new hermetic regression test for the eval->literal_eval security fix is not registered in `.github/checks-manifest.yaml`, so no CI or preflight lane ever runs `plugins/test_db.py` (no trigger covers `plugins/db.py` changes). The test only executes when a developer manually runs pytest, which silently leaves the security regression unguarded. Register a check with `command: ["pytest", "plugins/test_db.py"]`, `triggers: ["plugins/db.py", "plugins/test_db.py", "plugins/models.py"]`, and `lanes: ["local", "ci"]`, matching the existing per-app plugin test entries.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| from models import TranscriptSegment | ||
|
|
||
|
|
||
| class TestPluginDb(unittest.TestCase): |
There was a problem hiding this comment.
P2: The new hermetic regression test for the eval->literal_eval security fix is not registered in .github/checks-manifest.yaml, so no CI or preflight lane ever runs plugins/test_db.py (no trigger covers plugins/db.py changes). The test only executes when a developer manually runs pytest, which silently leaves the security regression unguarded. Register a check with command: ["pytest", "plugins/test_db.py"], triggers: ["plugins/db.py", "plugins/test_db.py", "plugins/models.py"], and lanes: ["local", "ci"], matching the existing per-app plugin test entries.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At plugins/test_db.py, line 46:
<comment>The new hermetic regression test for the eval->literal_eval security fix is not registered in `.github/checks-manifest.yaml`, so no CI or preflight lane ever runs `plugins/test_db.py` (no trigger covers `plugins/db.py` changes). The test only executes when a developer manually runs pytest, which silently leaves the security regression unguarded. Register a check with `command: ["pytest", "plugins/test_db.py"]`, `triggers: ["plugins/db.py", "plugins/test_db.py", "plugins/models.py"]`, and `lanes: ["local", "ci"]`, matching the existing per-app plugin test entries.</comment>
<file context>
@@ -0,0 +1,111 @@
+from models import TranscriptSegment
+
+
+class TestPluginDb(unittest.TestCase):
+
+ @mock.patch.object(db, 'r')
</file context>
|
|
||
| models_stub = types.ModuleType("models") | ||
| models_stub.TranscriptSegment = TranscriptSegment | ||
| sys.modules["models"] = models_stub |
There was a problem hiding this comment.
P3: The module-scope sys.modules["redis"] = redis_stub / sys.modules["models"] = models_stub override installs permanently at import and is never restored, so every later import in a shared pytest session silently resolves to these hand-rolled stub objects (e.g. a non-Pydantic TranscriptSegment). This can mask real model behavior and bleed into co-run test files; it also duplicates the repo's sanctioned backend/testing/import_isolation.stub_modules helper. Prefer that helper (or defer imports into a per-test context) instead of permanently mutating sys.modules.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At plugins/test_db.py, line 40:
<comment>The module-scope `sys.modules["redis"] = redis_stub` / `sys.modules["models"] = models_stub` override installs permanently at import and is never restored, so every later import in a shared pytest session silently resolves to these hand-rolled stub objects (e.g. a non-Pydantic `TranscriptSegment`). This can mask real model behavior and bleed into co-run test files; it also duplicates the repo's sanctioned `backend/testing/import_isolation.stub_modules` helper. Prefer that helper (or defer imports into a per-test context) instead of permanently mutating `sys.modules`.</comment>
<file context>
@@ -0,0 +1,111 @@
+
+ models_stub = types.ModuleType("models")
+ models_stub.TranscriptSegment = TranscriptSegment
+ sys.modules["models"] = models_stub
+
+import db
</file context>
… in checks-manifest
|
Addressed all 3 review items in commit
Verified with |
|
Verified at head af4a9d5 — this is a correct fix for the
Two non-blocking notes:
Heads-up for maintainers: #13664 (by the issue reporter) fixes the same bug with slightly different trade-offs (RecursionError/MemoryError handling, atomic by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
|
Keeping this open on DIRTY eval→ast.literal_eval in plugins/db.py + tests; rebase (cancelled Hygiene/Formatting). I am not approving or merging this SHA. |
kodjima33
left a comment
There was a problem hiding this comment.
Legitimate security fix removing eval() (arbitrary code execution via Redis-stored transcript buffer) in favor of ast.literal_eval with dict/key validation, matches the explicit non-abuse carve-out for PRs that remove existing eval() calls. Hermetic test proves both safe parsing and that a malicious import payload no longer executes. CI fully green.
Closes #13663
Summary
Fixes a security vulnerability and potential crash in
get_upsert_segment_to_transcript_plugininplugins/db.pywhere the Redis transcript buffer was parsed with Python'seval().Changes Made
• Replaced
eval(segments)with safeast.literal_eval(segments_raw).• Added bytes decoding handling for Redis byte string responses.
• Added element dictionary validation: filters parsed elements to ensure only valid dictionary objects containing required keys (
text,speaker,is_user,start,end) are accepted.• Added hermetic unit tests in
plugins/test_db.pywithout permanentsys.modulesmutation.• Registered test in
.github/checks-manifest.yaml.Failure-Class: none