DEV Community

Cover image for Next.js use cache Reused the Same Cache Entry for Different Files Before 16.4.5

Next.js use cache Reused the Same Cache Entry for Different Files Before 16.4.5

Shubhra Pokhariya on November 18, 2026

The Next.js docs tell you that the serializable arguments you pass to a use cache function become part of its cache key. They even list which types...
Collapse
 
dafili profile image
Renhua Wei •

GHSA-rhh4-g77r-r9qj aside, the 250KB stack blowup is the tell: if that threshold moves when you rerun the same array under node --stack-size=4000, it's arity spread on the bytes, not a heap limit, and your CI box with a bigger stack will quietly pass what a laptop throws on. Until you can be on 16.4.5+, hash the argument yourself — crypto.createHash('sha256').update(buf).digest('hex') — the key stays honest and stays small.

Picked as gem
Collapse
 
shubhradev profile image
Shubhra Pokhariya •

I haven't run "--stack-size=4000" myself. On a second machine, the limit roughly halved at 500 and roughly doubled at 2000, and "String.fromCodePoint(...new Uint16Array(n))" fails in plain Node at about 125,235 elements. That points to the argument spread, as you suggested.

The machine-to-machine difference showed up too. My limit was about 250,000 bytes, while the second machine's was about 247,000.

One thing on the upgrade. 16.4.5 fixes the key collision, not this stack overflow. I got the same size limit on 16.4.4, 16.4.5 and 16.4.6. Hashing only helps if the cached function gets the bytes another way, such as reading them from storage using the hash. If you still pass a large "Uint8Array" as the argument, it's still in the key and still hits the limit. Also, on 16.4.4, a content hash alone won't distinguish files with the same bytes but different names or types, so pass those separately if the function reads them.

Collapse
 
dafili profile image
Renhua Wei •

True on the upgrade: 16.4.5 is the collision fix, the spread ceiling stays. If the key has to hold the bytes rather than a hash of them, the stack-free path is Buffer.from(u8).toString('latin1') - one pass, no argument list, no dependence on --stack-size, and it round-trips through Buffer.from(str, 'latin1'). Worth pinning with a 20 MB buffer on both of your machines before trusting it on 16.4.6.

Thread Thread
 
shubhradev profile image
Shubhra Pokhariya •

It would avoid the spread, but I haven't tried it with "use cache". The catch is that it still passes the entire payload through argument serialization and key computation, just as a string.

In my tests, a string argument failed at about 999,930 characters with "Maximum array nesting exceeded". A 20 MB buffer converted to Latin-1 would be 20 million characters, so I'd expect it to hit that limit instead.

Thread Thread
 
dafili profile image
Renhua Wei •

I can't tell you what 20 MB does here — I never put one through a "use cache" key, so my latin1 line is unmeasured past your ceiling. What decides it is which side throws: bisect with for (const n of [1e6,4e6,1.6e7]) { try { ck("a".repeat(n)) } catch (e) { console.log(n, e.name, e.message) } }, where ck is the cached function. If "Maximum array nesting exceeded" shows up on the plain string at every size, it is your key builder recursing into the value, and the fix belongs there — hand it a flat hash list instead of the payload — not in the payload type.

Collapse
 
mudassirworks profile image
Mudassir Khan •

the odd length byte array matching the same array plus one trailing zero is the finding that stuck with me. a cache key that drops information is basically a tenancy bug waiting for the wrong request. we had a shared fetch wrapper keyed on url only and two orgs ended up reading each other data in staging, caught only because a test used two users in different orgs. did you try putting the org id explicitly into the args, or do you prefer keeping tenant scope in a separate cache tag?

Picked as gem
Collapse
 
shubhradev profile image
Shubhra Pokhariya •

That staging bug is exactly why I'd want the tenant as an explicit argument. I haven't tested org IDs specifically, but in my 54 input pairs, different strings and numbers always got separate entries, so that's where I'd start.

Tags are a different job. They let you invalidate entries for a tenant, but they don't put the tenant into the key. I'd also take the org ID from the server-side session, not from anything the client sends.

Collapse
 
webdeveloperhyper profile image
Web Developer Hyper •

Wow! You did such a deep analysis to find the bug in Next.js. Glad to hear it seems to be already fixed. Nice post!👍

Collapse
 
shubhradev profile image
Shubhra Pokhariya •

Thank you so much! 😊 I spent quite a bit of time testing this across versions and digging into the cache-key implementation. Glad you enjoyed the post!

Collapse
 
eye_java_420f1faa10ae8b86 profile image
Nan •

Fifty-four hand-picked pairs is a good harness, but it's still a list someone has to remember to extend. The property underneath all of them is simpler: at the byte level, the key builder should be injective. Different serialized bytes should mean a different key, with no exceptions table to maintain. I'd feed random byte strings (plus random File-ish metadata) straight into the key builder and assert key(a) != key(b) whenever a != b. No HTTP and no cache handler in the loop, just the serializer.

A fuzz run would surface the odd-length/trailing-zero case in minutes. That one was an equality rule throwing away information before hashing, and it only looked like a file problem. The stack blowup would show up in the same run as its own failure class instead of sitting behind a machine-dependent threshold.

Collapse
 
shubhradev profile image
Shubhra Pokhariya • • Edited

Fair point. A list only covers what I thought to put in it, and fuzzing the key builder directly is a better way to test the general property.

I haven't tried it in isolation. I got the real keys by wrapping Next's cache handler to log them, and the "encodeFormData" copy in the post is simplified, so I'd need to extract the real function from the build.

Plain "key(a) != key(b)" isn't quite the right check on its own. In my tests, an array hole and "undefined" shared a cache entry without changing the result. But leaving a field out of the key can also give a wrong result, as my "File.lastModified" test shows. That's why my harness computes what B should have received without the cache. A fuzzer needs that same check.

For the trailing zero I'd build related inputs on purpose, like taking an array and appending a zero, rather than relying on random pairs to land on it. I'd keep the stack blowup as its own check, since the threshold moves between machines and with warm-up.

Collapse
 
paullovelace profile image
Paul Lovelace •

Bob getting Alice's file is the kind of bug that makes you stare at the screen for a minute. The trailing-zero-byte collision is the one that got me — [61,62,63] and [61,62,63,0] producing the same key is so quietly wrong. The 'pass ids, not files' rule is the takeaway I'll actually remember. Have you checked whether the same key-building shows up in unstable_cache, or is that a separate rabbit hole?

Collapse
 
shubhradev profile image
Shubhra Pokhariya •

Thanks, Paul! The trailing-zero-byte case was one of the more interesting findings. In my 16.4.4 test, [61,62,63] and [61,62,63,0] ended up sharing the same cache entry. The 16.4.5 fix added the byte length, along with the file name and type, to the key.

On unstable_cache, I haven't traced its key-building path. The November security release also includes a separate advisory involving unstable_cache and use cache, but that issue is about a cross-request deduplication race involving root params, not the binary-argument collision I tested. So I can't say whether they share the same key-building code. That would be interesting to investigate separately.

Collapse
 
shubhradev profile image
Shubhra Pokhariya •

If you're passing a File to a use cache function, consider passing an ID instead and reading the file inside the cached function.

The ID needs to change when the file content changes. A content hash or version number can work for this. Otherwise, the cache may return a result based on the old file content.

Collapse
 
hemapriya_kanagala profile image
Hemapriya Kanagala •

Wait, Bob actually gets Alice's file result? That's quite a bug 😄 It's crazy how one small detail missing from the cache key can lead to something like this. Also, really interesting to see how you tested it across different versions, Shubhra 😀

Collapse
 
shubhradev profile image
Shubhra Pokhariya •

Thank you so much, Hema! 😊 Yes, Bob actually got Alice's cached result because the cache key ignored the file name and type. The two files had the same bytes, so they ended up sharing the cache entry. Thankfully, this was fixed in 16.4.5. 😄 Thanks for reading and sharing your thoughts!