Skip to content

fix: return 404 for directory paths exceeding the KV key limit - #1100

Draft
DebasishKaran-1 wants to merge 2 commits into
nodejs:mainfrom
DebasishKaran-1:fix-kv-directory-key-limit
Draft

DebasishKaran-1 wants to merge 2 commits into
nodejs:mainfrom
DebasishKaran-1:fix-kv-directory-key-limit

Conversation

@DebasishKaran-1

Copy link
Copy Markdown

Closes #1049.

Workers KV rejects keys longer than 512 UTF-8 bytes. When a malformed request arrives with a path longer than that (as captured by Sentry in #1049), KvProvider.readDirectory tries to look it up in KV, KV responds with 414 UTF-8 encoded length ... exceeds key length limit of 512, and the error propagates.

As suggested in the issue, such a path cannot exist as a file or directory, so it can be rejected proactively: readDirectory now returns undefined (which leads to a 404) without hitting KV or R2 when the UTF-8 encoded path exceeds 512 bytes.

Changes:

  • src/providers/kvProvider.ts: byte-length guard at the top of readDirectory, using TextEncoder so multi-byte characters are measured by bytes, not characters.
  • src/providers/kvProvider.test.ts: regression tests covering an over-limit ASCII path, an over-limit multi-byte path (171 three-byte characters = 513 bytes but only 171 characters), and a path exactly at the 512-byte limit which is still looked up.

Validation:

  • The new over-limit tests fail without the guard and pass with it.
  • Full vitest suite (143 tests), ESLint, tsc --noEmit, and Prettier all pass locally.

This change was prepared with AI assistance and is submitted with the account owner's approval.

Add a check for maximum key length in readDirectory method

Signed-off-by: DebasishKaran-1 <Kalyanikaran1234@gmail.com>
Signed-off-by: DebasishKaran-1 <Kalyanikaran1234@gmail.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.

Error: KV GET failed: 414 UTF-8 encoded length of 528 exceeds key length limit of 512.

1 participant