mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 18:16:47 -05:00
fix(proxy); restrict non guest related credentials in guest link authenticator middelware
This commit is contained in:
1 parent
413082e97e
commit
654d02f86d
3 files changed
+65
-13
No files matched your search
@@ -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.")
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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{
|
||||
|
||||
Reference in new issue
Block a user