diff --git a/CHANGELOG.md b/CHANGELOG.md index ca81f77a..9cf100f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Coming soon! Please document any work in progress here as part of your PR. It will be moved to the next tag when released. +- fix open redirect in the `/auth` callback by validating the `state` query parameter against the alphanumeric nonce format issued by `generateStateNonce`; a crafted `state` containing path separators is now rejected with `400` instead of being reflected into the redirect `Location` + ## v0.50.0 - avoid potential gzip compression bomb diff --git a/handlers/auth.go b/handlers/auth.go index 82735682..5f9867a4 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -13,6 +13,7 @@ package handlers import ( "fmt" "net/http" + "regexp" "slices" "github.com/vouch/vouch-proxy/pkg/cfg" @@ -25,6 +26,12 @@ import ( "golang.org/x/oauth2" ) +// validStateNonce matches the state nonce format produced by generateStateNonce +// in login.go, which strips everything outside [a-zA-Z0-9]. Any state value that +// does not match was never issued by Vouch and must be rejected before it is +// reflected into a redirect target (prevents open-redirect via crafted state). +var validStateNonce = regexp.MustCompile(`^[a-zA-Z0-9]+$`) + // CallbackHandler /auth // - redirects to /auth/{state}/ with the state coming from the query parameter func CallbackHandler(w http.ResponseWriter, r *http.Request) { @@ -44,6 +51,15 @@ func CallbackHandler(w http.ResponseWriter, r *http.Request) { return } + // The state must be an alphanumeric nonce exactly as issued by generateStateNonce. + // Rejecting anything else prevents a crafted state (e.g. containing path separators + // such as "/" or "\") from being reflected into the redirect Location and resolving + // to an attacker-controlled origin (open redirect). + if !validStateNonce.MatchString(queryState) { + responses.Error400(w, r, fmt.Errorf("/auth: invalid state nonce in query %s", r.URL.RawQuery)) + return + } + // has to have a trailing / in its path, because the path of the session cookie is set to /auth/{state}/. // see note in login.go and https://github.com/vouch/vouch-proxy/issues/373 authStateURL := fmt.Sprintf("%s/auth/%s/?%s", cfg.Cfg.DocumentRoot, queryState, r.URL.RawQuery) diff --git a/handlers/auth_test.go b/handlers/auth_test.go index f074553c..aa7554fd 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -79,6 +79,38 @@ func TestCallbackHandlerDocumentRoot(t *testing.T) { } } +// TestCallbackHandlerStateOpenRedirect confirms that a crafted, non-nonce state +// value is rejected with 400 rather than reflected into the redirect Location. +// See the open-redirect fix in CallbackHandler (handlers/auth.go). +func TestCallbackHandlerStateOpenRedirect(t *testing.T) { + handlerA := http.HandlerFunc(CallbackHandler) + setUp("/config/testing/handler_login_url.yml") + + // states that were never issued by generateStateNonce (which emits [a-zA-Z0-9]+) + badStates := []string{ + "..%2f..%2f%5Cevil.example.com", // backslash scheme-relative payload from the report + "../../\\evil.example.com", // raw separators + "legit/state", // forward slash + "legit.state", // dot + "legit state", // space + } + + for _, bad := range badStates { + t.Run(bad, func(t *testing.T) { + reqAuth, err := http.NewRequest("GET", cfg.Cfg.DocumentRoot+"/auth?state="+bad, nil) + if err != nil { + t.Fatal(err) + } + reqAuth.Header.Set("Host", "my.example.com") + rrA := httptest.NewRecorder() + handlerA.ServeHTTP(rrA, reqAuth) + + assert.Equal(t, http.StatusBadRequest, rrA.Code, "crafted state must be rejected") + assert.Empty(t, rrA.Header().Get("Location"), "no redirect Location should be emitted for a crafted state") + }) + } +} + func TestAuthStateHandler(t *testing.T) { type args struct { w http.ResponseWriter