Skip to content

Commit 7883dd5

Browse files
authored
[bugfix] Convert IDNs to punycode before using as session name (#458)
* convert hostname to punycode for session name * test punycode
1 parent af97d6b commit 7883dd5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

internal/router/session.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/spf13/viper"
3232
"github.com/superseriousbusiness/gotosocial/internal/config"
3333
"github.com/superseriousbusiness/gotosocial/internal/db"
34+
"golang.org/x/net/idna"
3435
)
3536

3637
// SessionOptions returns the standard set of options to use for each session.
@@ -61,7 +62,14 @@ func SessionName() (string, error) {
6162
return "", fmt.Errorf("could not derive hostname without port from %s://%s", protocol, host)
6263
}
6364

64-
return fmt.Sprintf("gotosocial-%s", strippedHostname), nil
65+
// make sure IDNs are converted to punycode or the cookie library breaks:
66+
// see https://en.wikipedia.org/wiki/Punycode
67+
punyHostname, err := idna.New().ToASCII(strippedHostname)
68+
if err != nil {
69+
return "", fmt.Errorf("could not convert %s to punycode: %s", strippedHostname, err)
70+
}
71+
72+
return fmt.Sprintf("gotosocial-%s", punyHostname), nil
6573
}
6674

6775
func useSession(ctx context.Context, sessionDB db.Session, engine *gin.Engine) error {

internal/router/session_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ func (suite *SessionTestSuite) TestDeriveSessionOK() {
8282
suite.Equal("gotosocial-example.org", sessionName)
8383
}
8484

85+
func (suite *SessionTestSuite) TestDeriveSessionIDNOK() {
86+
viper.Set(config.Keys.Protocol, "https")
87+
viper.Set(config.Keys.Host, "fóid.org")
88+
89+
sessionName, err := router.SessionName()
90+
suite.NoError(err)
91+
suite.Equal("gotosocial-xn--fid-gna.org", sessionName)
92+
}
93+
8594
func TestSessionTestSuite(t *testing.T) {
8695
suite.Run(t, &SessionTestSuite{})
8796
}

0 commit comments

Comments
 (0)