Skip to content

Commit 175574b

Browse files
authored
fix: session cookie parsing optimizations (#1009)
## Summary of change This PR refactors the function used for parsing incoming request cookies and optimizes it to avoid parsing cookies (since we don't care about the value). > PS: The changes to the oauth2 and webauthn files were done during the build process (I think some formatting changes) ## Related issues - #1007 ## Test Plan - All tests in backend-sdk-testing should pass ## Documentation changes (If relevant, please create a PR in our [docs repo](https://github.com/supertokens/docs), or create a checklist here highlighting the necessary changes) ## Checklist for important updates - [x] Changelog has been updated - [ ] `coreDriverInterfaceSupported.json` file has been updated (if needed) - Along with the associated array in `lib/ts/version.ts` - [ ] `frontendDriverInterfaceSupported.json` file has been updated (if needed) - [ ] Changes to the version if needed - In `package.json` - In `package-lock.json` - In `lib/ts/version.ts` - [x] Had run `npm run build-pretty` - [x] Had installed and ran the pre-commit hook - [ ] If new thirdparty provider is added, - [ ] update switch statement in `recipe/thirdparty/providers/configUtils.ts` file, `createProvider` function. - [ ] add an icon on the user management dashboard. - [x] Issue this PR against the latest non released version branch. - To know which one it is, run find the latest released tag (`git tag`) in the format `vX.Y.Z`, and then find the latest branch (`git branch --all`) whose `X.Y` is greater than the latest released tag. - If no such branch exists, then create one from the latest released branch. - [ ] If have added a new web framework, update the `add-ts-no-check.js` file to include that - [ ] If added a new recipe / api interface, then make sure that the implementation of it uses NON arrow functions only (like `someFunc: function () {..}`). - [ ] If added a new recipe, then make sure to expose it inside the recipe folder present in the root of this repo. We also need to expose its types. - [ ] If added a new entry point, then make sure that it is importable by adding it to the `exports` in `package.json` ## Remaining TODOs for this PR
2 parents 8cd073e + b9dbf95 commit 175574b

File tree

6 files changed

+72
-45
lines changed

6 files changed

+72
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
## [23.0.0] - 2025-06-10
2121

22+
- Refactors internal logic of parsing cookies to check accessToken and optimizes it to avoid parsing unrelated cookies.
2223
- The `getConsentRequest`, `acceptConsentRequest`, `rejectConsentRequest`, `acceptLoginRequest`, `rejectLoginRequest` and `introspectToken` can now possibly return an `ErrorOAuth2`.
2324
- The `/oauth/introspect` can now possibly return an `ErrorAuth2`.
2425
- The `User` class now has a `fromApi` function to normalize the user object returned from the API.

lib/build/recipe/oauth2provider/constants.js

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/recipe/session/constants.js

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/recipe/session/cookieAndHeaders.js

Lines changed: 15 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/recipe/webauthn/constants.js

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/recipe/session/cookieAndHeaders.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -273,38 +273,31 @@ export function hasMultipleCookiesForTokenType(
273273
return false;
274274
}
275275

276-
const cookies = parseCookieStringFromRequestHeaderAllowingDuplicates(cookieString);
276+
const cookieNames = getCookieNamesFromRequestHeaderAllowingDuplicates(cookieString);
277277
const cookieName = config.getCookieNameForTokenType(req, tokenType, userContext);
278-
return cookies[cookieName] !== undefined && cookies[cookieName].length > 1;
278+
return cookieNames.filter((name) => name === cookieName).length > 1;
279279
}
280280

281281
// This function is required because cookies library (and most of the popular libraries in npm)
282282
// does not support parsing multiple cookies with the same name.
283-
function parseCookieStringFromRequestHeaderAllowingDuplicates(cookieString: string): Record<string, string[]> {
284-
const cookies: Record<string, string[]> = {};
283+
function getCookieNamesFromRequestHeaderAllowingDuplicates(cookieString: string): string[] {
284+
const cookieNames: string[] = [];
285285

286286
const cookiePairs = cookieString.split(";");
287287

288288
for (const cookiePair of cookiePairs) {
289-
const [name, value] = cookiePair
290-
.trim()
291-
.split("=")
292-
.map((part) => {
293-
try {
294-
return decodeURIComponent(part);
295-
} catch (e) {
296-
// this is there in case the cookie value is not encoded. This can happe
297-
// if the user has set their own cookie in a different format.
298-
return part;
299-
}
300-
});
289+
const [name, _] = cookiePair.trim().split("=");
301290

302-
if (cookies.hasOwnProperty(name)) {
303-
cookies[name].push(value);
304-
} else {
305-
cookies[name] = [value];
291+
// Try to decode the name or fallback to the original name
292+
let decodedName = name;
293+
try {
294+
decodedName = decodeURIComponent(name);
295+
} catch (e) {
296+
logDebugMessage(`getCookieNamesFromRequestHeaderAllowingDuplicates: Error decoding cookie name: ${name}`);
306297
}
298+
299+
cookieNames.push(decodedName);
307300
}
308301

309-
return cookies;
302+
return cookieNames;
310303
}

0 commit comments

Comments
 (0)