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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package handlers
import (
"fmt"
"net/http"
"regexp"
"slices"

"github.com/vouch/vouch-proxy/pkg/cfg"
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down