Skip to content

Commit 83e5b92

Browse files
committed
Fix logic to handle AWS public URLS
1 parent 6f4ec90 commit 83e5b92

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

recipe/session/config_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,3 +1215,84 @@ func TestThatJWKSAndOpenIdEndpointsAreExposed(t *testing.T) {
12151215
assert.NotNil(t, openIdAPI)
12161216
assert.Equal(t, openIdAPI.PathWithoutAPIBasePath.GetAsStringDangerous(), "/.well-known/openid-configuration")
12171217
}
1218+
1219+
func TestCookieSameSiteWithEC2PublicURL(t *testing.T) {
1220+
apiBasePath := "/"
1221+
configValue := supertokens.TypeInput{
1222+
Supertokens: &supertokens.ConnectionInfo{
1223+
ConnectionURI: "http://localhost:8080",
1224+
},
1225+
AppInfo: supertokens.AppInfo{
1226+
AppName: "SuperTokens",
1227+
APIDomain: "https://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com:3001",
1228+
WebsiteDomain: "https://blog.supertokens.com",
1229+
APIBasePath: &apiBasePath,
1230+
},
1231+
RecipeList: []supertokens.Recipe{
1232+
Init(&sessmodels.TypeInput{
1233+
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
1234+
return sessmodels.CookieTransferMethod
1235+
},
1236+
}),
1237+
},
1238+
}
1239+
1240+
BeforeEach()
1241+
1242+
unittesting.StartUpST("localhost", "8080")
1243+
1244+
defer AfterEach()
1245+
1246+
err := supertokens.Init(configValue)
1247+
1248+
if err != nil {
1249+
t.Error(err.Error())
1250+
}
1251+
1252+
recipe, err := getRecipeInstanceOrThrowError()
1253+
1254+
if err != nil {
1255+
t.Error(err.Error())
1256+
}
1257+
1258+
assert.True(t, recipe.Config.CookieDomain == nil)
1259+
assert.Equal(t, recipe.Config.CookieSameSite, "none")
1260+
assert.True(t, recipe.Config.CookieSecure)
1261+
1262+
resetAll()
1263+
1264+
configValue = supertokens.TypeInput{
1265+
Supertokens: &supertokens.ConnectionInfo{
1266+
ConnectionURI: "http://localhost:8080",
1267+
},
1268+
AppInfo: supertokens.AppInfo{
1269+
AppName: "SuperTokens",
1270+
APIDomain: "http://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com:3001",
1271+
WebsiteDomain: "http://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com:3000",
1272+
APIBasePath: &apiBasePath,
1273+
},
1274+
RecipeList: []supertokens.Recipe{
1275+
Init(&sessmodels.TypeInput{
1276+
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
1277+
return sessmodels.CookieTransferMethod
1278+
},
1279+
}),
1280+
},
1281+
}
1282+
1283+
err = supertokens.Init(configValue)
1284+
1285+
if err != nil {
1286+
t.Error(err.Error())
1287+
}
1288+
1289+
recipe, err = getRecipeInstanceOrThrowError()
1290+
1291+
if err != nil {
1292+
t.Error(err.Error())
1293+
}
1294+
1295+
assert.True(t, recipe.Config.CookieDomain == nil)
1296+
assert.Equal(t, recipe.Config.CookieSameSite, "lax")
1297+
assert.False(t, recipe.Config.CookieSecure)
1298+
}

supertokens/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ func GetTopLevelDomainForSameSiteResolution(URL string) (string, error) {
318318
if strings.HasPrefix(hostname, "localhost") || strings.HasPrefix(hostname, "localhost.org") || isAnIP {
319319
return "localhost", nil
320320
}
321+
322+
/**
323+
EffectiveTLDPlusOne fails if the TLD and the input domain are the same which is true in the case of some aws URLS
324+
which are listedhere: https://publicsuffix.org/list/public_suffix_list.dat
325+
326+
Instead, we use PublicSuffix to get the parsed suffix. EffectiveTLDPlusOne internally uses PublicSuffix
327+
*/
328+
_publicSuffix, _ := publicsuffix.PublicSuffix(hostname)
329+
330+
// This check is added because of this issue: https://github.com/supertokens/supertokens-python/issues/394
331+
if strings.HasSuffix(hostname, ".amazonaws.com") && strings.HasSuffix(_publicSuffix, hostname) {
332+
return hostname, nil
333+
}
334+
321335
parsedURL, err := publicsuffix.EffectiveTLDPlusOne(hostname)
322336
if err != nil {
323337
return "", errors.New("Please make sure that the apiDomain and websiteDomain have correct values")

0 commit comments

Comments
 (0)