From 654d02f86dac2c8c32c124d1290314beec5460dc Mon Sep 17 00:00:00 2001 From: Alex Ababii Date: Thu, 29 Oct 2026 11:48:31 +0100 Subject: [PATCH] fix(proxy); restrict non guest related credentials in guest link authenticator middelware --- services/proxy/pkg/command/server.go | 20 ++++---- .../proxy/pkg/middleware/guest_link_auth.go | 10 ++-- .../pkg/middleware/guest_link_auth_test.go | 48 +++++++++++++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/services/proxy/pkg/command/server.go b/services/proxy/pkg/command/server.go index c8406b370e..4fc14bdbf7 100644 --- a/services/proxy/pkg/command/server.go +++ b/services/proxy/pkg/command/server.go @@ -354,16 +354,6 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config, } authenticators = append(authenticators, oidcAuthenticator) - if cfg.Commons != nil && cfg.Commons.EnableGuestLinks { - authenticators = append(authenticators, &middleware.GuestLinkAuthenticator{ - Logger: logger, - RevaGatewaySelector: gatewaySelector, - Config: middleware.GuestLinkAuthConfig{ - CookieName: cfg.GuestLinkAuth.CookieName, - }, - }) - } - authenticators = append(authenticators, middleware.PublicShareAuthenticator{ Logger: logger, RevaGatewaySelector: gatewaySelector, @@ -387,6 +377,16 @@ func loadMiddlewares(logger log.Logger, cfg *config.Config, // validated by the config parser trustedProxies, _ := pkgmiddleware.ParseTrustedProxies(cfg.ClientIP.TrustedProxies) + if cfg.Commons != nil && cfg.Commons.EnableGuestLinks { + authenticators = append(authenticators, &middleware.GuestLinkAuthenticator{ + Logger: logger, + RevaGatewaySelector: gatewaySelector, + Config: middleware.GuestLinkAuthConfig{ + CookieName: cfg.GuestLinkAuth.CookieName, + }, + }) + } + cspConfig, err := middleware.LoadCSPConfig(cfg) if err != nil { logger.Fatal().Err(err).Msg("Failed to load CSP configuration.") diff --git a/services/proxy/pkg/middleware/guest_link_auth.go b/services/proxy/pkg/middleware/guest_link_auth.go index 154c5942e0..0cb508401c 100644 --- a/services/proxy/pkg/middleware/guest_link_auth.go +++ b/services/proxy/pkg/middleware/guest_link_auth.go @@ -138,9 +138,13 @@ func (m *GuestLinkAuthenticator) Authenticate(r *http.Request) AuthenticationRes return NotApplicable() } - // Public link requests are left to the PublicShareAuthenticator, even if the - // browser also holds a guest session. - if r.URL.Query().Get(headerShareToken) != "" || r.Header.Get(headerShareToken) != "" { + // 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 { 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 c2af488978..db59aa9871 100644 --- a/services/proxy/pkg/middleware/guest_link_auth_test.go +++ b/services/proxy/pkg/middleware/guest_link_auth_test.go @@ -153,6 +153,54 @@ func TestGuestLinkAuthenticator_PublicShareToken(t *testing.T) { } } +func TestGuestLinkAuthenticator_OtherCredentials(t *testing.T) { + tests := []struct { + name string + path string + authHeader string + }{ + {"bearer token in header", "/dav/files/user/file.txt", "Bearer expired-token"}, + {"signed url with OC-Signature", "/dav/files/user/file.txt?OC-Signature=abc", ""}, + {"signed url with oc-jwt-sig", "/dav/files/user/file.txt?oc-jwt-sig=abc", ""}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pool.RemoveSelector("GatewaySelector" + "eu.opencloud.api.gateway") + authenticator := &GuestLinkAuthenticator{ + Logger: log.NewLogger(), + Config: GuestLinkAuthConfig{ + CookieName: "__Host-oc_guest_session", + }, + RevaGatewaySelector: pool.GetSelector[gateway.GatewayAPIClient]( + "GatewaySelector", + "eu.opencloud.api.gateway", + func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient { + return mockGatewayClient{ + AuthenticateFunc: func(authType, clientID, clientSecret string) (string, rpcv1beta1.Code) { + t.Error("expected no guest authentication when other credentials are present") + return "", rpcv1beta1.Code_CODE_UNAUTHENTICATED + }, + } + }, + ), + } + + req := httptest.NewRequest(http.MethodGet, "http://example.com"+tt.path, http.NoBody) + req.AddCookie(getCookie("some-jwt")) + if tt.authHeader != "" { + req.Header.Set("Authorization", tt.authHeader) + } + + result := authenticator.Authenticate(req) + + if result.State != AuthenticationNotApplicable { + t.Errorf("expected AuthenticationNotApplicable, got %v", result.State) + } + }) + } +} + func TestGuestLinkAuthenticator_Success(t *testing.T) { pool.RemoveSelector("GatewaySelector" + "eu.opencloud.api.gateway") authenticator := &GuestLinkAuthenticator{