Skip to content

Commit

Permalink
internal: fix missing trailing slash in outpost websocket (goauthenti…
Browse files Browse the repository at this point in the history
…k#12470)

Signed-off-by: Jens Langhammer <[email protected]>
  • Loading branch information
BeryJu authored Dec 23, 2024
1 parent 9462361 commit ee6fcdf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 10 additions & 4 deletions internal/outpost/ak/api_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"maps"
"net/http"
"net/url"
"strconv"
Expand All @@ -16,13 +17,16 @@ import (
"goauthentik.io/internal/constants"
)

func (ac *APIController) getWebsocketURL(akURL url.URL, outpostUUID string) *url.URL {
func (ac *APIController) getWebsocketURL(akURL url.URL, outpostUUID string, query url.Values) *url.URL {
wsUrl := &url.URL{}
wsUrl.Scheme = strings.ReplaceAll(akURL.Scheme, "http", "ws")
wsUrl.Host = akURL.Host
_p, _ := url.JoinPath(akURL.Path, "ws/outpost/", outpostUUID)
_p, _ := url.JoinPath(akURL.Path, "ws/outpost/", outpostUUID, "/")
wsUrl.Path = _p
wsUrl.RawQuery = akURL.Query().Encode()
v := url.Values{}
maps.Insert(v, maps.All(akURL.Query()))
maps.Insert(v, maps.All(query))
wsUrl.RawQuery = v.Encode()
return wsUrl
}

Expand All @@ -45,7 +49,9 @@ func (ac *APIController) initWS(akURL url.URL, outpostUUID string) error {
},
}

ws, _, err := dialer.Dial(ac.getWebsocketURL(akURL, outpostUUID).String(), header)
wsu := ac.getWebsocketURL(akURL, outpostUUID, query).String()
ac.logger.WithField("url", wsu).Debug("connecting to websocket")
ws, _, err := dialer.Dial(wsu, header)
if err != nil {
ac.logger.WithError(err).Warning("failed to connect websocket")
return err
Expand Down
18 changes: 14 additions & 4 deletions internal/outpost/ak/api_ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ func TestWebsocketURL(t *testing.T) {
u := URLMustParse("http://localhost:9000?foo=bar")
uuid := "23470845-7263-4fe3-bd79-ec1d7bf77d77"
ac := &APIController{}
nu := ac.getWebsocketURL(*u, uuid)
assert.Equal(t, "ws://localhost:9000/ws/outpost/23470845-7263-4fe3-bd79-ec1d7bf77d77?foo=bar", nu.String())
nu := ac.getWebsocketURL(*u, uuid, url.Values{})
assert.Equal(t, "ws://localhost:9000/ws/outpost/23470845-7263-4fe3-bd79-ec1d7bf77d77/?foo=bar", nu.String())
}

func TestWebsocketURL_Query(t *testing.T) {
u := URLMustParse("http://localhost:9000?foo=bar")
uuid := "23470845-7263-4fe3-bd79-ec1d7bf77d77"
ac := &APIController{}
v := url.Values{}
v.Set("bar", "baz")
nu := ac.getWebsocketURL(*u, uuid, v)
assert.Equal(t, "ws://localhost:9000/ws/outpost/23470845-7263-4fe3-bd79-ec1d7bf77d77/?bar=baz&foo=bar", nu.String())
}

func TestWebsocketURL_Subpath(t *testing.T) {
u := URLMustParse("http://localhost:9000/foo/bar/")
uuid := "23470845-7263-4fe3-bd79-ec1d7bf77d77"
ac := &APIController{}
nu := ac.getWebsocketURL(*u, uuid)
assert.Equal(t, "ws://localhost:9000/foo/bar/ws/outpost/23470845-7263-4fe3-bd79-ec1d7bf77d77", nu.String())
nu := ac.getWebsocketURL(*u, uuid, url.Values{})
assert.Equal(t, "ws://localhost:9000/foo/bar/ws/outpost/23470845-7263-4fe3-bd79-ec1d7bf77d77/", nu.String())
}

0 comments on commit ee6fcdf

Please sign in to comment.