Skip to content

Commit f180063

Browse files
authored
fix: fixed cookie samesite validation (#335)
1 parent 8a7f248 commit f180063

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

CHANGELOG.md

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

1010
### Changes
1111

12+
- Fixes Cookie sameSite config validation.
1213
- Fixes a few typos
1314
- Changes `getEmailForUserIdForEmailVerification` function inside thirdpartypasswordless to take into account passwordless emails and return an empty string in case a passwordless email doesn't exist. This helps situations where the dev wants to customise the email verification functions in the thirdpartypasswordless recipe.
1415

lib/build/recipe/session/utils.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,13 @@ function validateAndNormaliseUserInput(recipeInstance, appInfo, config) {
180180
if (
181181
cookieSameSite === "none" &&
182182
!cookieSecure &&
183-
!(topLevelAPIDomain === "localhost" || utils_1.isAnIpAddress(topLevelAPIDomain)) &&
184-
!(topLevelWebsiteDomain === "localhost" || utils_1.isAnIpAddress(topLevelWebsiteDomain))
183+
!(
184+
(topLevelAPIDomain === "localhost" || utils_1.isAnIpAddress(topLevelAPIDomain)) &&
185+
(topLevelWebsiteDomain === "localhost" || utils_1.isAnIpAddress(topLevelWebsiteDomain))
186+
)
185187
) {
188+
// We can allow insecure cookie when both website & API domain are localhost or an IP
189+
// When either of them is a different domain, API domain needs to have https and a secure cookie to work
186190
throw new Error(
187191
"Since your API and website domain are different, for sessions to work, please use https on your apiDomain and dont set cookieSecure to false."
188192
);

lib/ts/recipe/session/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,13 @@ export function validateAndNormaliseUserInput(
194194
if (
195195
cookieSameSite === "none" &&
196196
!cookieSecure &&
197-
!(topLevelAPIDomain === "localhost" || isAnIpAddress(topLevelAPIDomain)) &&
198-
!(topLevelWebsiteDomain === "localhost" || isAnIpAddress(topLevelWebsiteDomain))
197+
!(
198+
(topLevelAPIDomain === "localhost" || isAnIpAddress(topLevelAPIDomain)) &&
199+
(topLevelWebsiteDomain === "localhost" || isAnIpAddress(topLevelWebsiteDomain))
200+
)
199201
) {
202+
// We can allow insecure cookie when both website & API domain are localhost or an IP
203+
// When either of them is a different domain, API domain needs to have https and a secure cookie to work
200204
throw new Error(
201205
"Since your API and website domain are different, for sessions to work, please use https on your apiDomain and dont set cookieSecure to false."
202206
);

test/config.test.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,78 @@ describe(`configTest: ${printPath("[test/config.test.js]")}`, function () {
478478
}
479479
});
480480

481+
it("sameSite none invalid domain values", async function () {
482+
const domainCombinations = [
483+
["http://localhost:3000", "http://supertokensapi.io"],
484+
["http://127.0.0.1:3000", "http://supertokensapi.io"],
485+
["http://supertokens.io", "http://localhost:8000"],
486+
["http://supertokens.io", "http://127.0.0.1:8000"],
487+
["http://supertokens.io", "http://supertokensapi.io"],
488+
];
489+
490+
for (const domainCombination of domainCombinations) {
491+
try {
492+
STExpress.init({
493+
supertokens: {
494+
connectionURI: "http://localhost:8080",
495+
},
496+
appInfo: {
497+
appName: "SuperTokens",
498+
websiteDomain: domainCombination[0],
499+
apiDomain: domainCombination[1],
500+
},
501+
recipeList: [Session.init({ cookieSameSite: "none" })],
502+
});
503+
assert(false);
504+
} catch (e) {
505+
assert(
506+
e.message ===
507+
"Since your API and website domain are different, for sessions to work, please use https on your apiDomain and dont set cookieSecure to false."
508+
);
509+
}
510+
resetAll();
511+
}
512+
});
513+
514+
it("sameSite none valid domain values", async function () {
515+
const domainCombinations = [
516+
["http://localhost:3000", "http://localhost:8000"],
517+
["http://127.0.0.1:3000", "http://localhost:8000"],
518+
["http://localhost:3000", "http://127.0.0.1:8000"],
519+
["http://127.0.0.1:3000", "http://127.0.0.1:8000"],
520+
521+
["https://localhost:3000", "https://localhost:8000"],
522+
["https://127.0.0.1:3000", "https://localhost:8000"],
523+
["https://localhost:3000", "https://127.0.0.1:8000"],
524+
["https://127.0.0.1:3000", "https://127.0.0.1:8000"],
525+
526+
["https://supertokens.io", "https://api.supertokens.io"],
527+
["https://supertokens.io", "https://supertokensapi.io"],
528+
529+
["http://localhost:3000", "https://supertokensapi.io"],
530+
["http://127.0.0.1:3000", "https://supertokensapi.io"],
531+
];
532+
533+
for (const domainCombination of domainCombinations) {
534+
try {
535+
STExpress.init({
536+
supertokens: {
537+
connectionURI: "http://localhost:8080",
538+
},
539+
appInfo: {
540+
appName: "SuperTokens",
541+
websiteDomain: domainCombination[0],
542+
apiDomain: domainCombination[1],
543+
},
544+
recipeList: [Session.init({ cookieSameSite: "none" })],
545+
});
546+
} catch (e) {
547+
assert(false);
548+
}
549+
resetAll();
550+
}
551+
});
552+
481553
it("testing sessionScope normalisation", async function () {
482554
assert(normaliseSessionScopeOrThrowError("api.example.com") === "api.example.com");
483555
assert(normaliseSessionScopeOrThrowError("http://api.example.com") === "api.example.com");
@@ -978,7 +1050,7 @@ describe(`configTest: ${printPath("[test/config.test.js]")}`, function () {
9781050
connectionURI: "http://localhost:8080",
9791051
},
9801052
appInfo: {
981-
apiDomain: "127.0.0.1:3000",
1053+
apiDomain: "https://127.0.0.1:3000",
9821054
appName: "SuperTokens",
9831055
websiteDomain: "google.com",
9841056
apiBasePath: "test/",

0 commit comments

Comments
 (0)