Skip to content

fix(plugins): parse Redis transcript buffer with ast.literal_eval, not eval (#13663) - #13713

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/issue-13663-eval-to-literal-eval
Sep 13, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-13663-eval-to-literal-eval

Conversation

@kodjima33

@kodjima33 kodjima33 commented Sep 13, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

get_upsert_segment_to_transcript_plugin in plugins/db.py read the per-session
transcript buffer from Redis and parsed it with eval(). eval() executes
arbitrary Python, so any value stored 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 Redis instance, corruption — ran as code inside
the plugin process, which holds the OpenAI and Redis credentials. A merely
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.

Tested

plugins/test_db_transcript_segments.py stubs redis and models (no live
Redis needed) and exercises the real function:

  • round trip through two calls (write then read back via ast.literal_eval)
  • a malicious payload (__import__('os').system(...)) — confirmed this test
    fails against the pre-fix code: the payload executes (writes a sentinel
    file) before the old code crashes on the non-list eval() result — and
    passes after the fix
  • a non-list literal resets the buffer to empty instead of crashing

Registered as plugin-db-transcript-segments-tests in
.github/checks-manifest.yaml. Ran scripts/backend-python-format --write
on the changed files.

Failure-Class: new

fixes #13663

🤖 automated by hourly watchdog — tested and merged

Review in cubic

…, 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

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files

Confidence score: 4/5

  • In plugins/test_db_transcript_segments.py, load_db_module leaves fake models, redis, and db modules in sys.modules and its added sys.path entry 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

@kodjima33
kodjima33 merged commit c632e66 into main Sep 13, 2026
52 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-13663-eval-to-literal-eval branch September 13, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plugins/db.py parses the session transcript buffer from Redis with eval()

1 participant