Skip to content

Commit

Permalink
fix: normaliseSessionScope and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed May 1, 2024
1 parent e640844 commit 1353876
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [0.18.1] - 2024-05-01

- Fixed a bug in the `normaliseSessionScopeOrThrowError` util function that caused it to remove leading dots from the scope string.

## [0.18.0] - 2024-04-30

### Changes
Expand Down
67 changes: 67 additions & 0 deletions recipe/session/session_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package session

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNormaliseSessionScope(t *testing.T) {
t.Run("test with leading dot", func(t *testing.T) {
result, err := normaliseSessionScopeOrThrowError(".example.com")
assert.NoError(t, err)
assert.Equal(t, ".example.com", *result)
})

t.Run("test without leading dot", func(t *testing.T) {
result, err := normaliseSessionScopeOrThrowError("example.com")
assert.NoError(t, err)
assert.Equal(t, "example.com", *result)
})

t.Run("test with http prefix", func(t *testing.T) {
result, err := normaliseSessionScopeOrThrowError("http://example.com")
assert.NoError(t, err)
assert.Equal(t, "example.com", *result)
})

t.Run("test with https prefix", func(t *testing.T) {
result, err := normaliseSessionScopeOrThrowError("https://example.com")
assert.NoError(t, err)
assert.Equal(t, "example.com", *result)
})

t.Run("test with IP address", func(t *testing.T) {
result, err := normaliseSessionScopeOrThrowError("192.168.1.1")
assert.NoError(t, err)
assert.Equal(t, "192.168.1.1", *result)
})

t.Run("test with localhost", func(t *testing.T) {
result, err := normaliseSessionScopeOrThrowError("localhost")
assert.NoError(t, err)
assert.Equal(t, "localhost", *result)
})

t.Run("test with leading and trailing whitespace", func(t *testing.T) {
result, err := normaliseSessionScopeOrThrowError(" example.com ")
assert.NoError(t, err)
assert.Equal(t, "example.com", *result)
})
}
40 changes: 25 additions & 15 deletions recipe/session/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,35 +259,45 @@ func GetURLScheme(URL string) (string, error) {
}

func normaliseSessionScopeOrThrowError(sessionScope string) (*string, error) {
sessionScope = strings.TrimSpace(sessionScope)
sessionScope = strings.ToLower(sessionScope)
helper := func(scope string) (string, error) {
scope = strings.TrimSpace(scope)
scope = strings.ToLower(scope)

sessionScope = strings.TrimPrefix(sessionScope, ".")
scope = strings.TrimPrefix(scope, ".")

if !strings.HasPrefix(sessionScope, "http://") && !strings.HasPrefix(sessionScope, "https://") {
sessionScope = "http://" + sessionScope
if !strings.HasPrefix(scope, "http://") && !strings.HasPrefix(scope, "https://") {
scope = "http://" + scope
}

parsedURL, err := url.Parse(scope)
if err != nil {
return "", errors.New("please provide a valid sessionScope")
}

hostname := parsedURL.Hostname()

return hostname, nil
}

urlObj, err := url.Parse(sessionScope)
noDotNormalised, err := helper(sessionScope)
if err != nil {
return nil, errors.New("Please provide a valid sessionScope")
return nil, err
}

sessionScope = urlObj.Hostname()
sessionScope = strings.TrimPrefix(sessionScope, ".")

noDotNormalised := sessionScope

isAnIP, err := supertokens.IsAnIPAddress(sessionScope)
if err != nil {
return nil, err
}
if sessionScope == "localhost" || isAnIP {
noDotNormalised = sessionScope

if noDotNormalised == "localhost" || isAnIP {
return &noDotNormalised, nil
}

if strings.HasPrefix(sessionScope, ".") {
noDotNormalised = "." + sessionScope
noDotNormalised = "." + noDotNormalised
return &noDotNormalised, nil
}

return &noDotNormalised, nil
}

Expand Down
2 changes: 1 addition & 1 deletion supertokens/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

// VERSION current version of the lib
const VERSION = "0.18.0"
const VERSION = "0.18.1"

var (
cdiSupported = []string{"3.0"}
Expand Down

0 comments on commit 1353876

Please sign in to comment.