Conversation
Browsers may URL-encode special characters in cookie names (e.g. `@` becomes `%40`). The existing `decode` option only applies to cookie values, leaving names as raw strings from the `Cookie` header. This adds an optional `decodeName` function to `CookieParseOptions` that is applied to cookie names during parsing, before `filter`. Motivating use case: AWS Amplify writes auth cookies with usernames in the cookie name (e.g. `CognitoIdentityServiceProvider.<id>.user@test.local.idToken`). Browsers encode the `@` as `%40`, but server-side token providers look up the cookie by the decoded name. Without `decodeName`, the lookup fails silently.
📝 WalkthroughWalkthroughAdds an optional Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/cookie/parse.ts`:
- Around line 48-49: The call to options.decodeName(rawKey) is unguarded and can
throw (e.g., malformed percent-encodings), so wrap the invocation used to set
key in a try/catch and fall back to rawKey on error; specifically, in the
parsing flow where rawKey is produced by valueSlice and assigned to key, replace
the direct options.decodeName(rawKey) call with a guarded call that catches any
exception and returns rawKey if decoding fails (mirroring the existing try/catch
used for value decoding).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 2f45713d-653e-40fa-a3b9-34a48c1e8539
📒 Files selected for processing (3)
src/cookie/parse.tssrc/cookie/types.tstest/cookie-parse.test.ts
377b467 to
b4bcea9
Compare
Add optional `decodeName` function to `CookieParseOptions` that
decodes cookie names during parsing. The existing `decode` option
only applies to values — names are returned as raw strings from
the `Cookie` header.
Browsers may URL-encode special characters in cookie names (e.g.
`@` → `%40`), but server-side consumers often look up cookies by
the decoded name. Without `decodeName`, lookups fail silently.
Example:
parse('user%40host=token', { decodeName: decodeURIComponent })
// → { 'user@host': 'token' }
When used with `filter`, the filter receives the decoded name.
Errors from `decodeName` are not caught (same contract as a
user-provided `decode`).
b4bcea9 to
67659af
Compare
Description
Adds an optional
decodeNamefunction toCookieParseOptionsthat decodes cookie names during parsing.The existing
decodeoption only applies to cookie values. Cookie names are returned as raw strings from theCookieheader. Browsers may URL-encode special characters in cookie names (e.g.@→%40), but server-side consumers often look up cookies by the decoded name — causing silent lookup failures.Example
Motivation
AWS Amplify stores auth tokens in cookies keyed by username — e.g.
CognitoIdentityServiceProvider.<clientId>.user@test.local.idToken. Browsers URL-encode the@as%40when sending theCookieheader.Server-side, Amplify's
TokenStoreconstructs the lookup key with the raw (decoded) username, then reads it viacreateKeyValueStorageFromCookieStorageAdapter, which delegates to the framework's cookie getter. In h3/Nitro apps, that getter usescookie-es'sparse()— which doesn't decode names, so the lookup silently returnsundefinedand the user appears unauthenticated.Changes
src/cookie/types.ts—decodeNameoption onCookieParseOptions, note onfilterinteractionsrc/cookie/parse.ts— ApplydecodeNameto raw key beforefilter(2-line change)test/cookie-parse.test.ts— 5 tests: decode, no-op, independent from value decode, error propagation, filter interactionDesign decisions
decodeNameis applied beforefilter, so filter receives the decoded namedecodeper the existing JSDoc: "If you provide your own encode/decode scheme you must ensure errors are appropriately handled"decodeNameis not set