Skip to content

valkey - fix: clear() only removes keys in the exact namespace - #2147

Merged
jaredwray merged 2 commits into
mainfrom
claude/tender-gauss-jdemw2
Sep 17, 2026
Merged

jaredwray merged 2 commits into
mainfrom
claude/tender-gauss-jdemw2

Conversation

@jaredwray

@jaredwray jaredwray commented Sep 17, 2026 •

Copy link
Copy Markdown
Owner

Please check if the PR fulfills these requirements

  • Followed the Contributing and Code of Conduct guidelines.
  • Tests for the changes have been added (for bug fixes/features) with 100% code coverage.

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Bug fix.

Problem

When useSets is false (the default), clear() built its KEYS pattern as ${prefix}*, which expands to namespace:<ns>*. That pattern also matches every namespace that merely starts with the same characters, so clearing namespace users also deleted every key under users-archive, users2, and so on.

A second, related gap (raised by the Codex review on this PR): a namespace containing a glob metacharacter (*, ?, [, ], \) was treated as pattern syntax by both clear() and iterator(), so tenant* could clear or iterate tenant-prod.

@keyv/redis is unaffected because it builds its pattern with the separator.

Fix

  • storage/valkey/src/index.ts: new private getKeyPattern() builds the KEYS / SCAN MATCH pattern as namespace:<ns>:* with glob metacharacters in the prefix escaped, so only the adapter's own namespace matches literally. clear() and iterator() both use it. The useSets: true path (SMEMBERS of the tracking set) is unchanged.
  • storage/valkey/test/namespace.test.ts and test/iterator.test.ts: regression tests that write into namespace X and a sibling X<suffix> and assert the sibling survives clear(), for both useSets modes, plus tests for clear() and iterator() with a namespace containing [a-z]?*\. The useSets: false sibling test and both glob tests fail on main and pass with the fix.
  • storage/valkey/README.md and the clear() / iterator() / getKeyPattern() JSDoc: document the exact pattern, that prefix-sharing namespaces are isolated, and one limitation that a pattern cannot remove: because : is the key separator and keys may contain : too, a namespace that extends another with : (users:archive under users) is not distinguishable from keys containing :. The docs point to useSets: true for that separation. Encoding or rejecting : would change the stored key layout for existing users, so it is out of scope for this fix.

Verification

Ran pnpm test:ci in storage/valkey against a local standalone server plus a 3-node cluster: lint clean, 150 tests passing (146 existing + 4 new), 100% line coverage. pnpm build for the package also passes. CI on the head commit is green on Node 22, 24 and 26, codecov (project and patch at 100%), bun, browser, and the security scans.

Related

  • The v5 branch has the same pattern in packages/valkey/src/index.ts (${this._getNamespace()}*) and will need a separate fix against v5.
  • The proposed @keyv/valkey-glide adapter in valkey-glide - feat: Add Valkey GLIDE storage adapter #2146 copies the same ${prefix}* pattern and should get the same fix.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Jt8UZWqPE62M8xJrbeSGws

The KEYS pattern used by clear() when useSets is false was
`namespace:<ns>*`, which also matched every namespace that starts with
the same characters, so clearing `users` wiped `users-archive` too.
Include the key separator in the pattern (`namespace:<ns>:*`) so only
the adapter's own namespace matches, mirroring what iterator() already
does. Adds regression tests for both useSets modes and documents the
exact pattern in the README.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jt8UZWqPE62M8xJrbeSGws
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-17T19:25:14.784129Z e5b819d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e5b819d5bc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread storage/valkey/src/index.ts Outdated
Comment thread storage/valkey/src/index.ts Outdated
@codecov

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a3404d3) to head (110926c).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2147   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           55        55           
  Lines         5281      5284    +3     
  Branches       857       855    -2     
=========================================
+ Hits          5281      5284    +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Codex review on #2147 pointed out that a namespace containing `*`, `?`,
`[`, `]` or `\` was still treated as glob syntax by clear(), and the same
line in iterator(), so `tenant*` could clear or iterate `tenant-prod`.
Both now build their pattern through getKeyPattern(), which escapes those
characters before appending the `:*` separator, with regression tests
for both methods.

A namespace that extends another with `:` (`users:archive` under `users`)
is inherently ambiguous in the `namespace:<ns>:<key>` layout because keys
may contain `:` too, so the README and JSDoc now state that limitation
and point to `useSets: true` instead of claiming exact isolation.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jt8UZWqPE62M8xJrbeSGws
@jaredwray
jaredwray merged commit 4d369be into main Sep 17, 2026
16 checks passed
@jaredwray
jaredwray deleted the claude/tender-gauss-jdemw2 branch September 17, 2026 19:35
EmilBuszylo pushed a commit to EmilBuszylo/keyv that referenced this pull request Sep 23, 2026
clear() and iterator() build their SCAN MATCH pattern from the raw
namespace, so a namespace containing *, ?, [, ], or \ was treated as
glob syntax and could match a different namespace (e.g. "tenant*"
would also match "tenant-prod"). Route both through a new
getKeyPattern() that escapes those characters before appending the :*
separator, mirroring the fix already shipped for @keyv/valkey (jaredwray#2147).

Also documents that a namespace extending another with ":" (e.g.
"users:archive" under "users") still can't be told apart from a key
containing ":" — use useSets: true for that separation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
EmilBuszylo pushed a commit to EmilBuszylo/keyv that referenced this pull request Oct 1, 2026
clear() and iterator() build their SCAN MATCH pattern from the raw
namespace, so a namespace containing *, ?, [, ], or \ was treated as
glob syntax and could match a different namespace (e.g. "tenant*"
would also match "tenant-prod"). Route both through a new
getKeyPattern() that escapes those characters before appending the :*
separator, mirroring the fix already shipped for @keyv/valkey (jaredwray#2147).

Also documents that a namespace extending another with ":" (e.g.
"users:archive" under "users") still can't be told apart from a key
containing ":" — use useSets: true for that separation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
jaredwray added a commit that referenced this pull request Oct 3, 2026
)

clear() and iterator() passed `<namespace><keyPrefixSeparator>*` to
SCAN MATCH without escaping it, so `*`, `?`, `[`, `]` and `\` in the
namespace or separator were read as glob syntax. clear() on namespace
`tenant*` deleted namespace `tenant-prod`'s keys and iterator() returned
them, while a namespace such as `t[12]` or `a\b` never matched its own
keys. v5 built the same pattern.

Both now build the pattern through getKeyPattern(), which escapes those
characters in the namespace and separator, as @keyv/valkey does since
#2147. filterScannedKeys() also checks each SCAN result
against the literal prefix, so clear() can't delete another namespace's
keys whatever glob syntax the server supports.

Adds regression tests for clear(), iterator() and a separator with glob
characters, documents the literal matching and the separator limitation
in the README, and notes the change in the v5-to-v6 migration guide and
the keyv-migrate skill references.


Claude-Session: https://claude.ai/code/session_01Wm1wtCGzQJEyYZdekgNVge

Co-authored-by: Claude <noreply@anthropic.com>
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.

2 participants