Skip to content

Commit

Permalink
fix: skip redirect url validation when it's the base href (argoproj#1…
Browse files Browse the repository at this point in the history
…0058)

Signed-off-by: CI <[email protected]>

nicer way of doing it

Signed-off-by: CI <[email protected]>
  • Loading branch information
crenshaw-dev committed Jul 27, 2022
1 parent 44fea52 commit 1f54062
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion util/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (a *ClientApp) verifyAppState(r *http.Request, w http.ResponseWriter, state
redirectURL := a.baseHRef
parts := strings.SplitN(cookieVal, ":", 2)
if len(parts) == 2 && parts[1] != "" {
if !isValidRedirectURL(parts[1], []string{a.settings.URL}) {
if !isValidRedirectURL(parts[1], []string{a.settings.URL, a.baseHRef}) {
sanitizedUrl := parts[1]
if len(sanitizedUrl) > 100 {
sanitizedUrl = sanitizedUrl[:100]
Expand Down
46 changes: 46 additions & 0 deletions util/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,52 @@ requestedScopes: ["oidc"]`, oidcTestServer.URL),
})
}

func Test_Login_Flow(t *testing.T) {
// Show that SSO login works when no redirect URL is provided, and we fall back to the configured base href for the
// Argo CD instance.

oidcTestServer := test.GetOIDCTestServer(t)
t.Cleanup(oidcTestServer.Close)

cdSettings := &settings.ArgoCDSettings{
URL: "https://argocd.example.com",
OIDCConfigRAW: fmt.Sprintf(`
name: Test
issuer: %s
clientID: xxx
clientSecret: yyy
requestedScopes: ["oidc"]`, oidcTestServer.URL),
OIDCTLSInsecureSkipVerify: true,
}

// The base href (the last argument for NewClientApp) is what HandleLogin will fall back to when no explicit
// redirect URL is given.
app, err := NewClientApp(cdSettings, "", "/")
require.NoError(t, err)

w := httptest.NewRecorder()

req := httptest.NewRequest("GET", "https://argocd.example.com/auth/login", nil)

app.HandleLogin(w, req)

redirectUrl, err := w.Result().Location()
require.NoError(t, err)

state := redirectUrl.Query()["state"]

req = httptest.NewRequest("GET", fmt.Sprintf("https://argocd.example.com/auth/callback?state=%s&code=abc", state), nil)
for _, cookie := range w.Result().Cookies() {
req.AddCookie(cookie)
}

w = httptest.NewRecorder()

app.HandleCallback(w, req)

assert.NotContains(t, w.Body.String(), InvalidRedirectURLError.Error())
}

func TestClientApp_HandleCallback(t *testing.T) {
oidcTestServer := test.GetOIDCTestServer(t)
t.Cleanup(oidcTestServer.Close)
Expand Down

0 comments on commit 1f54062

Please sign in to comment.