fix(proxy): upd guest link authenticator middleware

This commit is contained in:
Alex Ababii committed 2026-11-06 12:29:04 +01:00
1 parent 654d02f86d
commit 007c09d499
2 files changed
+66 -18

No files matched your search

@@ -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()
}
@@ -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)
}
})
}
}