Skip to content

Commit 396f007

Browse files
committed
chore: add localhost exception
1 parent 8974543 commit 396f007

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

internal/utilities/request.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ func IsRedirectURLValid(config *conf.GlobalConfiguration, redirectURL string) bo
101101

102102
// As long as the referrer came from the site, we will redirect back there
103103
if berr == nil && rerr == nil && base.Hostname() == refurl.Hostname() {
104-
// ensure schema and port haven't changed
105-
// most browsers should be checking insecure protocol switching but be double check
106-
if base.Scheme == refurl.Scheme && base.Port() == refurl.Port() {
107-
return true
104+
// ensure scheme hasn't changed; most browsers also check this but double check here
105+
if base.Scheme == refurl.Scheme {
106+
// Per RFC 8252 Section 7.3, native apps using a localhost redirect URI
107+
// MUST be allowed to use variable port numbers, so skip the port check
108+
// for loopback addresses.
109+
if base.Port() == refurl.Port() || isLocalhost(refurl.Hostname()) {
110+
return true
111+
}
108112
}
109113
}
110114

internal/utilities/request_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ func TestIsRedirectURLValidSameOrigin(t *tst.T) {
5959
redirectURL: "https://example.com:9001/path",
6060
want: false,
6161
},
62+
// RFC 8252 Section 7.3: variable ports must be allowed for localhost
63+
{
64+
desc: "localhost with different port allowed (RFC 8252 Section 7.3)",
65+
siteURL: "http://localhost:3000",
66+
redirectURL: "http://localhost:8080/callback",
67+
want: true,
68+
},
69+
{
70+
desc: "127.0.0.1 with different port allowed (RFC 8252 Section 7.3)",
71+
siteURL: "http://127.0.0.1:3000",
72+
redirectURL: "http://127.0.0.1:8080/callback",
73+
want: true,
74+
},
75+
{
76+
desc: "localhost without port in redirect allowed (RFC 8252 Section 7.3)",
77+
siteURL: "http://localhost:3000",
78+
redirectURL: "http://localhost/callback",
79+
want: true,
80+
},
81+
{
82+
desc: "localhost scheme downgrade still rejected despite RFC 8252",
83+
siteURL: "https://localhost:3000",
84+
redirectURL: "http://localhost:8080/callback",
85+
want: false,
86+
},
87+
{
88+
desc: "non-localhost variable port still rejected",
89+
siteURL: "https://example.com:9000",
90+
redirectURL: "https://example.com:9001/path",
91+
want: false,
92+
},
6293
}
6394

6495
for _, c := range cases {

0 commit comments

Comments
 (0)