From 007c09d499b6bda0171a6f6a2292aa499ce71bdf Mon Sep 17 00:00:00 2001 From: Alex Ababii Date: Thu, 29 Oct 2026 12:33:03 +0100 Subject: [PATCH] fix(proxy): upd guest link authenticator middleware --- .../proxy/pkg/middleware/guest_link_auth.go | 45 ++++++++++++++----- .../pkg/middleware/guest_link_auth_test.go | 39 +++++++++++++--- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/services/proxy/pkg/middleware/guest_link_auth.go b/services/proxy/pkg/middleware/guest_link_auth.go index 0cb508401c..c3c07f21d2 100644 --- a/services/proxy/pkg/middleware/guest_link_auth.go +++ b/services/proxy/pkg/middleware/guest_link_auth.go @@ -86,6 +86,24 @@ func isGuestLinkPath(path string) bool { return false } +// hasOtherCredentials reports whether the request carries credentials that are +// handled by other authenticators. A guest session is a fallback identity: it +// must never shadow an explicitly sent Authorization header, a public share +// token or a signed URL. +func hasOtherCredentials(r *http.Request) bool { + query := r.URL.Query() + + if r.Header.Get(_headerAuthorization) != "" { + return true + } + + if query.Get(headerShareToken) != "" || r.Header.Get(headerShareToken) != "" { + return true + } + + return query.Get(_paramOCJWTSig) != "" || query.Get(_paramOCSignature) != "" +} + // readGuestCookie reads the guest-session cookie from the request. // Returns empty string if the cookie is missing or empty. func readGuestCookie(r *http.Request, name string) (string, error) { @@ -113,12 +131,21 @@ func clearGuestCookie(name string) *http.Cookie { // SuppressAuthenticationChallenge prevents other authentication mechanisms from challenging guest-cookie requests. func (m *GuestLinkAuthenticator) SuppressAuthenticationChallenge(req *http.Request) bool { - name := guestLinkCookieName(m.Config) - cookie, err := req.Cookie(name) - if err != nil { + if !isGuestLinkPath(req.URL.Path) { return false } - return cookie.Value != "" + + if hasOtherCredentials(req) { + return false + } + + name := guestLinkCookieName(m.Config) + cookie, err := req.Cookie(name) + if err != nil || cookie.Value == "" { + return false + } + + return true } // Authenticate implements the Authenticator interface for guest-link cookie authentication. @@ -138,13 +165,9 @@ func (m *GuestLinkAuthenticator) Authenticate(r *http.Request) AuthenticationRes return NotApplicable() } - // A guest session is a fallback identity: requests carrying their own - // credentials are authenticated (or rejected) by the respective authenticator. - query := r.URL.Query() - hasAuthHeader := r.Header.Get(_headerAuthorization) != "" - hasPublicToken := query.Get(headerShareToken) != "" || r.Header.Get(headerShareToken) != "" - hasSignedURL := query.Get(_paramOCJWTSig) != "" || query.Get(_paramOCSignature) != "" - if hasAuthHeader || hasPublicToken || hasSignedURL { + // Requests carrying their own credentials are authenticated (or rejected) by + // the respective authenticator, even if the browser also holds a guest session. + if hasOtherCredentials(r) { return NotApplicable() } diff --git a/services/proxy/pkg/middleware/guest_link_auth_test.go b/services/proxy/pkg/middleware/guest_link_auth_test.go index db59aa9871..87bf6cc9e3 100644 --- a/services/proxy/pkg/middleware/guest_link_auth_test.go +++ b/services/proxy/pkg/middleware/guest_link_auth_test.go @@ -452,15 +452,40 @@ func TestGuestLinkAuthenticator_ChallengeSuppression(t *testing.T) { }, } - reqNoCookie := httptest.NewRequest(http.MethodGet, testEndpoint, http.NoBody) - if authenticator.SuppressAuthenticationChallenge(reqNoCookie) { - t.Error("expected challenge not to be suppressed when no cookie present") + tests := []struct { + name string + url string + cookie bool + authHeader string + publicToken string + expect bool + }{ + {"no cookie", testEndpoint, false, "", "", false}, + {"cookie on guest path", testEndpoint, true, "", "", true}, + {"cookie on non-guest path", "http://example.com/graph/v1.0/me", true, "", "", false}, + {"cookie with auth header", testEndpoint, true, "Bearer token", "", false}, + {"cookie with public token header", testEndpoint, true, "", "abc", false}, + {"cookie with OC-Signature query", testEndpoint + "?OC-Signature=abc", true, "", "", false}, + {"cookie with oc-jwt-sig query", testEndpoint + "?oc-jwt-sig=abc", true, "", "", false}, } - reqWithCookie := httptest.NewRequest(http.MethodGet, testEndpoint, http.NoBody) - reqWithCookie.AddCookie(getCookie("some-jwt")) - if !authenticator.SuppressAuthenticationChallenge(reqWithCookie) { - t.Error("expected challenge to be suppressed when cookie present") + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, tt.url, http.NoBody) + if tt.cookie { + req.AddCookie(getCookie("some-jwt")) + } + if tt.authHeader != "" { + req.Header.Set(_headerAuthorization, tt.authHeader) + } + if tt.publicToken != "" { + req.Header.Set(headerShareToken, tt.publicToken) + } + + if got := authenticator.SuppressAuthenticationChallenge(req); got != tt.expect { + t.Errorf("expected challenge suppression %v, got %v", tt.expect, got) + } + }) } }