Skip to content

Name a missing MemoryCache key 'string' in the Rust MemoryCache - #8584

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-18-audit-rjsg-memcache
Nov 19, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-18-audit-rjsg-memcache

Conversation

@littledivy

Copy link
Copy Markdown
Member

The Rust MemoryCache took the key of read() and delete() as a raw v8::Local<v8::Value>, so that it could keep lone surrogates as kj::String does, and checked that it was a string itself. When the key was missing, though, the #[jsg_method] arity check rejected the call first, and it names the parameter after its Rust type: env.CACHE.read() failed with "parameter 1 is not of type 'Value'." where the C++ MemoryCache says 'string'.

In this patch the key is a Key type whose FromJS accepts only a string, holds it as the same UTF-8 bytes, and is named 'string', as C++'s jsg::NonCoercible<kj::String> is. memory-cache-throws-test now calls read() and delete() without a key; it fails on the Rust MemoryCache without the fix.

@littledivy
littledivy requested review from a team as code owners November 18, 2026 05:28

@clanky-the-clanker clanky-the-clanker 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.

Superseded by the review of 58dd2c5a.

This review, of 24f1bb9f

✅ Approved — No issues found. The missing-key error now names 'string', and invalid keys are still thrown or rejected as before, depending on capture_async_api_throws.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

Key's FromJS gives 'string' as the type name, so a missing key and a non-string key both produce the C++-matching TypeError. The key bytes are the same as before (lone surrogates kept), OBJECT_CONVERSION_MAY_RUN_SCRIPT = false is right because only strings convert, and the removed key_bytes helper leaves no dead code behind. The test now covers calls with no argument for both read() and delete().


📝 Documentation: ✅ LGTM

The test's header comment, which says read() rejects under capture_async_api_throws and throws otherwise, is still accurate. For methods that return a promise, the method macro wraps argument conversion in jsg::capture_throws_as_rejection (src/rust/jsg-macros/lib.rs:570-580), so key conversion errors still reject under that flag. The doc comment on Key correctly describes how it relates to jsg::NonCoercible<kj::String> and how it holds the key as UTF-8.


Reviewed commit: 24f1bb9f

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I checked locally that memory-cache-rust-test fails without the fix and passes with it, and that C++ and Rust agree for undefined, null, new String('k'), Symbol() and object keys on both read() and delete(). Three non-blocking nits inline.

Comment thread src/workerd/api/memory-cache-api/lib.rs Resolved
Comment thread src/workerd/api/memory-cache-api/lib.rs Resolved
Comment thread src/workerd/api/memory-cache-api/lib.rs Resolved
The Rust MemoryCache took the key of read() and delete() as a raw `v8::Local<v8::Value>`, so that it could keep lone surrogates as `kj::String` does, and checked that it was a string itself. When the key was missing, though, the `#[jsg_method]` arity check rejected the call first, and it names the parameter after its Rust type: `env.CACHE.read()` failed with "parameter 1 is not of type 'Value'." where the C++ MemoryCache says 'string'.

In this patch the key is a `Key` type whose `FromJS` accepts only a string, holds it as the same UTF-8 bytes, and is named 'string', as C++'s `jsg::NonCoercible<kj::String>` is. memory-cache-throws-test now calls read() and delete() without a key; it fails on the Rust MemoryCache without the fix.
@littledivy
littledivy force-pushed the littledivy/2026-11-18-audit-rjsg-memcache branch from 24f1bb9 to 58dd2c5 Compare November 19, 2026 06:09

@clanky-the-clanker clanky-the-clanker 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.

✅ Approved — No issues found: a missing MemoryCache key now gives a TypeError naming 'string', as the C++ MemoryCache does, key bytes are unchanged and the test covers calls with no argument.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

Key implements FromJS completely and reports 'string' as its type name. It keeps the original UTF-8 key bytes. Moving it into the try_catch closure is valid because that closure is FnOnce. The updated test covers calling read() and delete() with no argument.


📝 Documentation: ✅ LGTM

The doc comment on Key and the test header comment are accurate.


Reviewed commit: 58dd2c5a

@littledivy
littledivy merged commit 42c6a20 into main Nov 19, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-18-audit-rjsg-memcache branch November 19, 2026 07:50
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