Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions x/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"context"
"net/http"
"net/url"
"strings"

"github.com/golang/gddo/httputil"

Expand Down Expand Up @@ -61,6 +62,15 @@
source.Scheme = proto
}

if prefix := r.Header.Get("X-Forwarded-Prefix"); len(prefix) > 0 {
if !hasPathPrefix(source.Path, prefix) {
source.Path = joinPathPrefix(prefix, source.Path)
}
if source.RawPath != "" && !hasPathPrefix(source.RawPath, prefix) {
source.RawPath = joinPathPrefix(prefix, source.RawPath)
}
}

if source.Scheme == "" {
source.Scheme = "https"
if r.TLS == nil {
Expand All @@ -71,6 +81,30 @@
return &source
}

func hasPathPrefix(path, prefix string) bool {
prefix = strings.TrimSuffix(prefix, "/")
if prefix == "" {
return true
}

return path == prefix || strings.HasPrefix(path, prefix+"/")
}

func joinPathPrefix(prefix, path string) string {
switch {
case prefix == "" || prefix == "/":
return path
case path == "":
return prefix
case strings.HasSuffix(prefix, "/") && strings.HasPrefix(path, "/"):

Check warning

Code scanning / CodeQL

Bad redirect check Medium

This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
return prefix + strings.TrimPrefix(path, "/")
case !strings.HasSuffix(prefix, "/") && !strings.HasPrefix(path, "/"):

Check warning

Code scanning / CodeQL

Bad redirect check Medium

This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
This is a check that
this value
, which flows into a
redirect
, has a leading slash, but not that it does not have '/' or '' in its second position.
return prefix + "/" + path
default:
return prefix + path
}
}

// SendFlowCompletedAsRedirectOrJSON should be used when a login, registration, ... flow has been completed successfully.
// It will redirect the user to the provided URL if the request accepts HTML, or return a JSON response if the request is
// an SPA request
Expand Down
18 changes: 18 additions & 0 deletions x/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ func TestRequestURL(t *testing.T) {
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/foo"), Host: "foobar", Header: http.Header{"X-Forwarded-Host": []string{"notfoobar"}, "X-Forwarded-Proto": {"https"}},
}).String(), "https://notfoobar/foo")
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/self-service/login/browser?flow=123"),
Host: "foobar",
Header: http.Header{
"X-Forwarded-Host": []string{"notfoobar"},
"X-Forwarded-Proto": []string{"https"},
"X-Forwarded-Prefix": []string{"/.ory/kratos"},
},
}).String(), "https://notfoobar/.ory/kratos/self-service/login/browser?flow=123")
assert.EqualValues(t, RequestURL(&http.Request{
URL: urlx.ParseOrPanic("/.ory/kratos/self-service/login/browser?flow=123"),
Host: "foobar",
Header: http.Header{
"X-Forwarded-Host": []string{"notfoobar"},
"X-Forwarded-Proto": []string{"https"},
"X-Forwarded-Prefix": []string{"/.ory/kratos"},
},
}).String(), "https://notfoobar/.ory/kratos/self-service/login/browser?flow=123")
}

func TestAcceptToRedirectOrJSON(t *testing.T) {
Expand Down
Loading