From 9f1fbc448067298740d20f57f0cfadf1633db897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Mon, 23 Nov 2026 15:33:15 +0100 Subject: [PATCH] fix(ocs): never hand out signing keys to link visitors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A public link token carries the link owner as user. If a link visitor ever reaches GetSigningKey they get the owner's signing key and can sign urls for everything the owner can read, until the key expires. Today the proxy public paths and the reva link scope keep visitors away from the endpoint, this adds the check in the handler itself. We now refuse guests and users with a public-share-role in the opaque, before reading or creating the key. ported from https://github.com/owncloud/ocis/pull/12332 related: https://github.com/opencloud-eu/omega/issues/879 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Jörn Friedrich Dreyer --- services/ocs/pkg/service/v0/users.go | 10 ++ services/ocs/pkg/service/v0/users_test.go | 91 +++++++++++++++++++ .../pkg/middleware/authentication_test.go | 4 + 3 files changed, 105 insertions(+) create mode 100644 services/ocs/pkg/service/v0/users_test.go diff --git a/services/ocs/pkg/service/v0/users.go b/services/ocs/pkg/service/v0/users.go index 66c32401ce..3094e1d5a8 100644 --- a/services/ocs/pkg/service/v0/users.go +++ b/services/ocs/pkg/service/v0/users.go @@ -6,9 +6,11 @@ import ( "errors" "net/http" + cs3user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" "github.com/opencloud-eu/opencloud/services/ocs/pkg/service/v0/data" "github.com/opencloud-eu/opencloud/services/ocs/pkg/service/v0/response" revactx "github.com/opencloud-eu/reva/v3/pkg/ctx" + "github.com/opencloud-eu/reva/v3/pkg/utils" "go-micro.dev/v4/store" ) @@ -23,6 +25,14 @@ func (o Ocs) GetSigningKey(w http.ResponseWriter, r *http.Request) { return } + // A public link token carries the link owner as user. Never hand out the owner's + // signing key to guests or link visitors, it would let them sign urls for + // everything the owner can access. + if u.GetId().GetType() == cs3user.UserType_USER_TYPE_GUEST || utils.ExistsInOpaque(u.GetOpaque(), "public-share-role") { + o.mustRender(w, r, response.ErrRender(data.MetaUnauthorized.StatusCode, "unauthorized")) + return + } + // use the user's UUID userID := u.Id.OpaqueId diff --git a/services/ocs/pkg/service/v0/users_test.go b/services/ocs/pkg/service/v0/users_test.go new file mode 100644 index 0000000000..ce08556cfd --- /dev/null +++ b/services/ocs/pkg/service/v0/users_test.go @@ -0,0 +1,91 @@ +package svc + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + cs3user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + cs3types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" + "github.com/opencloud-eu/opencloud/pkg/log" + "github.com/opencloud-eu/opencloud/services/ocs/pkg/service/v0/data" + revactx "github.com/opencloud-eu/reva/v3/pkg/ctx" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go-micro.dev/v4/store" +) + +type signingKeyResponse struct { + OCS struct { + Meta struct { + StatusCode int `json:"statuscode"` + } `json:"meta"` + Data struct { + User string `json:"user"` + SigningKey string `json:"signing-key"` + } `json:"data"` + } `json:"ocs"` +} + +func getSigningKey(t *testing.T, s store.Store, u *cs3user.User) signingKeyResponse { + t.Helper() + o := Ocs{logger: log.NewLogger(), store: s} + + req := httptest.NewRequest(http.MethodGet, "/ocs/v2.php/cloud/user/signing-key?format=json", nil) + req = req.WithContext(revactx.ContextSetUser(req.Context(), u)) + w := httptest.NewRecorder() + o.GetSigningKey(w, req) + + var body signingKeyResponse + require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body), w.Body.String()) + return body +} + +func TestGetSigningKey(t *testing.T) { + s := store.NewMemoryStore() + u := &cs3user.User{Id: &cs3user.UserId{OpaqueId: "alice-id", Type: cs3user.UserType_USER_TYPE_PRIMARY}} + + body := getSigningKey(t, s, u) + assert.Equal(t, "alice-id", body.OCS.Data.User) + require.NotEmpty(t, body.OCS.Data.SigningKey) + + // the key is persisted and returned again on the next request + rec, err := s.Read("alice-id") + require.NoError(t, err) + require.Len(t, rec, 1) + assert.Equal(t, body.OCS.Data.SigningKey, string(rec[0].Value)) + assert.Equal(t, body.OCS.Data.SigningKey, getSigningKey(t, s, u).OCS.Data.SigningKey) +} + +// A public link token carries the link owner as user, marked with the +// public-share-role opaque entry by the publicshares auth manager. The owner's +// signing key must never be handed out in that context, not even if it exists. +func TestGetSigningKeyRefusesPublicLinkAndGuests(t *testing.T) { + linkVisitor := &cs3user.User{ + Id: &cs3user.UserId{OpaqueId: "alice-id", Type: cs3user.UserType_USER_TYPE_PRIMARY}, + Opaque: &cs3types.Opaque{Map: map[string]*cs3types.OpaqueEntry{ + "public-share-role": {Decoder: "plain", Value: []byte("viewer")}, + }}, + } + guest := &cs3user.User{Id: &cs3user.UserId{OpaqueId: "guest-id", Type: cs3user.UserType_USER_TYPE_GUEST}} + + for name, u := range map[string]*cs3user.User{"public link": linkVisitor, "guest": guest} { + t.Run(name, func(t *testing.T) { + s := store.NewMemoryStore() + require.NoError(t, s.Write(&store.Record{Key: u.GetId().GetOpaqueId(), Value: []byte("existing-key")})) + + body := getSigningKey(t, s, u) + assert.Equal(t, data.MetaUnauthorized.StatusCode, body.OCS.Meta.StatusCode) + assert.Empty(t, body.OCS.Data.SigningKey) + assert.Empty(t, body.OCS.Data.User) + }) + } + + t.Run("does not create a key", func(t *testing.T) { + s := store.NewMemoryStore() + getSigningKey(t, s, linkVisitor) + _, err := s.Read("alice-id") + assert.ErrorIs(t, err, store.ErrNotFound) + }) +} diff --git a/services/proxy/pkg/middleware/authentication_test.go b/services/proxy/pkg/middleware/authentication_test.go index f17beed5ea..26a45ab9fe 100644 --- a/services/proxy/pkg/middleware/authentication_test.go +++ b/services/proxy/pkg/middleware/authentication_test.go @@ -40,6 +40,10 @@ var _ = Describe("authentication helpers", func() { Entry("token info path", "/ocs/v1.php/apps/files_sharing/api/v1/tokeninfo/unprotected", true), Entry("token info path", "/ocs/v2.php/apps/files_sharing/api/v1/tokeninfo/unprotected", true), Entry("capabilities", "/ocs/v1.php/cloud/capabilities", true), + // a public link token carries the link owner as user, so the signing key + // endpoint must never be public or link visitors could sign urls as the owner + Entry("signing-key v1 must not be public", "/ocs/v1.php/cloud/user/signing-key", false), + Entry("signing-key v2 must not be public", "/ocs/v2.php/cloud/user/signing-key", false), ) })