Skip to content

fix(python): escape glob characters in Redis session key scan (AI-4255) - #2675

Merged
aliev merged 2 commits into
mainfrom
fix/redis-session-keys-glob
Nov 27, 2026
Merged

aliev merged 2 commits into
mainfrom
fix/redis-session-keys-glob

Conversation

@aliev

@aliev aliev commented Nov 27, 2026

Copy link
Copy Markdown
Member

AI-4255

Why

RedisSessionKVStore.keys() put the key prefix in the Redis SCAN MATCH pattern without an escape. The prefix holds the call_id from the HTTP routes of Runner.serve(). A call_id with *, ?, [ or ] matched the session keys of other calls. Then get_for_call() returned sessions of other calls, and the max_sessions_per_call check counted them.

Changes

  • keys() escapes \, *, ?, [ and ] in the prefix. The pattern now matches the prefix as literal text, the same as InMemorySessionKVStore.

Validation

  • New tests in test_redis_store.py (real Redis container): a prefix with *, ?, [ab] or \ returns only keys that start with that literal text. All 5 tests failed on main before the fix: each returned keys of other calls.
  • uv run --no-sync pytest tests/test_agents/test_session_registry/: 62 passed.
  • ruff check, ruff format --check and mypy on the changed files: clean.

@aliev
aliev requested a review from kanat November 27, 2026 13:35

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

VERDICT: GO

Reviewed at 601aceb, merged onto current main in a scratch tree. The fix is correct: keys() is the only SCAN/KEYS/MATCH call in the SDK (grep of scan_iter and .keys( over sdks, plugins, examples), and its only caller is SessionRegistry.get_for_call. The escape set (\, *, ?, [, ]) is the full set of Redis glob specials. Ids without those characters give the same pattern as before: the escape returns the string unchanged for vision_agents:sessions/call-123/, test: and similar. The Redis container tests pass (83 passed in the session_registry dir), ruff, ruff format and mypy are clean on the touched files, and the PR's CI checks are green.

Mutation checks against a real Redis: with no escape (the base behaviour) the 5 new tests fail, and with only * escaped 2 fail. With \ dropped from the escape class everything still passes, which is the one follow-up (R1.1, inline). It does not block: the code is right, only the test is loose.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

VERDICT: GO

[security R1] The escape is correct and complete. keys() is the only pattern scan in the session registry, and its result is never used to delete keys. The escape covers every Redis glob character: once [ is escaped no character class can open, so ^, - and ] stay literal. Ids without glob characters match exactly as before. I checked this against a real Redis with the PR merged onto current main, at the registry level as well as the store level, and on the base the same checks fail.

One follow-up that does not block: the backslash escape has no test that fails without it (S1.1, inline).

What I checked
Check Result
Other SCAN, KEYS or pattern DEL in session_registry/ or the SDK none
Delete driven by scan results none; delete() and remove() take exact keys
Mutations on the escape class *, ?, [ caught; ] is equivalent (literal in Redis); \ survives
CI all checks pass

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving on the reviewers' GO (main and security GO; the backslash-escape test row is a follow-up; text review clear).

RedisSessionKVStore.keys() put the prefix in the SCAN MATCH pattern as is.
A call_id with *, ?, [ or ] matched the session keys of other calls.
The store now escapes these characters and the backslash.
@aliev
aliev force-pushed the fix/redis-session-keys-glob branch from 601aceb to 994ad72 Compare November 27, 2026 19:44
@aliev
aliev merged commit cfe761b into main Nov 27, 2026
4 of 7 checks passed
@aliev
aliev deleted the fix/redis-session-keys-glob branch November 27, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants