Repository navigation
fix(plugins): parse Redis transcript buffer with ast.literal_eval, not eval (#13663) - #13713
Merged
Merged
Conversation
…, not eval (#13663) get_upsert_segment_to_transcript_plugin() in plugins/db.py read the per-session transcript buffer from Redis and ran it through eval(). eval() executes arbitrary Python, so any value under plugin:{plugin_id}:session:{session_id}:transcript_segments that is not the plain str(list-of-dicts) this function writes -- a manual redis-cli edit, a sibling writer on the shared instance, corruption -- ran as code inside the plugin process, which holds the OpenAI and Redis credentials. A malformed value also crashed the /mentor webhook until the key's 5-minute TTL expired. Fix: ast.literal_eval() parses the identical format str(segments) writes (TranscriptSegment fields are all str/int/float/bool/None, so repr output is always a valid literal) while rejecting anything executable. A stored value that fails to parse, or parses to something other than a list of dicts, resets the buffer to empty instead of crashing. Added plugins/test_db_transcript_segments.py (stubs redis and models, no live services needed) and wired it into .github/checks-manifest.yaml. Confirmed the malicious-payload test fails against the pre-fix code -- the payload executes (it writes a sentinel file) before the old code crashes on the non-list result -- and passes after the fix. Ran `scripts/backend-python-format --write` on the changed files. Failure-Class: new fixes #13663
There was a problem hiding this comment.
1 issue found across 4 files
Confidence score: 4/5
- In
plugins/test_db_transcript_segments.py,load_db_moduleleaves fakemodels,redis, anddbmodules insys.modulesand its addedsys.pathentry in place, which can contaminate later tests and cause order-dependent failures; restore the original module and path state after loading.
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_transcript_segments.py">
<violation number="1" location="plugins/test_db_transcript_segments.py:42">
P3: `load_db_module` permanently overwrites `sys.modules['models']`, `['redis']`, and `['db']` and inserts into `sys.path` but never restores them, leaving the fakes installed for every later test in the process. The existing plugins convention (e.g. `plugins/omi-slack-app/test_segments.py` setUp) snapshots `sys.modules` and restores originals in a `finally`/cleanup, so any shared-pytest run that collects this file after other plugin tests will hand those tests a fake `models`/`redis`. Snapshot the originals once and restore them via `addCleanup`/`tearDown`.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| def load_db_module(): | ||
| fake_models = types.ModuleType('models') | ||
| fake_models.TranscriptSegment = FakeTranscriptSegment | ||
| sys.modules['models'] = fake_models |
There was a problem hiding this comment.
P3: load_db_module permanently overwrites sys.modules['models'], ['redis'], and ['db'] and inserts into sys.path but never restores them, leaving the fakes installed for every later test in the process. The existing plugins convention (e.g. plugins/omi-slack-app/test_segments.py setUp) snapshots sys.modules and restores originals in a finally/cleanup, so any shared-pytest run that collects this file after other plugin tests will hand those tests a fake models/redis. Snapshot the originals once and restore them via addCleanup/tearDown.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At plugins/test_db_transcript_segments.py, line 42:
<comment>`load_db_module` permanently overwrites `sys.modules['models']`, `['redis']`, and `['db']` and inserts into `sys.path` but never restores them, leaving the fakes installed for every later test in the process. The existing plugins convention (e.g. `plugins/omi-slack-app/test_segments.py` setUp) snapshots `sys.modules` and restores originals in a `finally`/cleanup, so any shared-pytest run that collects this file after other plugin tests will hand those tests a fake `models`/`redis`. Snapshot the originals once and restore them via `addCleanup`/`tearDown`.</comment>
<file context>
@@ -0,0 +1,98 @@
+def load_db_module():
+ fake_models = types.ModuleType('models')
+ fake_models.TranscriptSegment = FakeTranscriptSegment
+ sys.modules['models'] = fake_models
+
+ fake_redis_module = types.ModuleType('redis')
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
get_upsert_segment_to_transcript_plugininplugins/db.pyread the per-sessiontranscript buffer from Redis and parsed it with
eval().eval()executesarbitrary Python, so any value stored under
plugin:{plugin_id}:session:{session_id}:transcript_segmentsthat is not theplain
str(list-of-dicts)this function writes — a manualredis-cliedit, asibling writer on the shared Redis instance, corruption — ran as code inside
the plugin process, which holds the OpenAI and Redis credentials. A merely
malformed value also crashed the
/mentorwebhook until the key's 5-minuteTTL expired.
Fix
ast.literal_eval()parses the identical formatstr(segments)writes(
TranscriptSegmentfields are allstr/int/float/bool/None, sorepr()output is always a valid literal) while rejecting anythingexecutable. A stored value that fails to parse, or parses to something other
than a list of dicts, resets the buffer to empty instead of crashing.
Tested
plugins/test_db_transcript_segments.pystubsredisandmodels(no liveRedis needed) and exercises the real function:
ast.literal_eval)__import__('os').system(...)) — confirmed this testfails against the pre-fix code: the payload executes (writes a sentinel
file) before the old code crashes on the non-list
eval()result — andpasses after the fix
Registered as
plugin-db-transcript-segments-testsin.github/checks-manifest.yaml. Ranscripts/backend-python-format --writeon the changed files.
Failure-Class: new
fixes #13663
🤖 automated by hourly watchdog — tested and merged