From 768cd264d79e8ba98f06561884b2f6dda4fd4f53 Mon Sep 17 00:00:00 2001 From: Sara Ahmadi Date: Fri, 13 Nov 2026 13:22:24 +0100 Subject: [PATCH] feat(graph): reject drive ids that belong to existing users Assisted-by: Claude Code:claude-opus-5-5 --- services/graph/pkg/service/v0/drives.go | 11 +++++ services/graph/pkg/service/v0/graph_test.go | 52 +++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/services/graph/pkg/service/v0/drives.go b/services/graph/pkg/service/v0/drives.go index 29b623645e..c1091f11d3 100644 --- a/services/graph/pkg/service/v0/drives.go +++ b/services/graph/pkg/service/v0/drives.go @@ -438,6 +438,17 @@ func (g Graph) CreateDrive(w http.ResponseWriter, r *http.Request) { errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "invalid drive id: must be a canonical UUID") return } + // personal spaces may use their owner's user id as space id, so an id that belongs to a user + // must not be taken by another space, or that user's personal space could not be created anymore + if _, err := g.identityBackend.GetUser(r.Context(), id, &godata.GoDataRequest{}); err == nil { + log.Debug().Str("id", id).Msg("could not create drive: id belongs to a user") + errorcode.NameAlreadyExists.Render(w, r, http.StatusConflict, "drive already exists") + return + } else if !errorcode.IsErrorCode(err, errorcode.ItemNotFound) { + log.Error().Err(err).Str("id", id).Msg("could not create drive: could not check whether the id belongs to a user") + errorcode.RenderError(w, r, err) + return + } csr.Opaque = utils.AppendPlainToOpaque(csr.Opaque, "spaceid", id) } diff --git a/services/graph/pkg/service/v0/graph_test.go b/services/graph/pkg/service/v0/graph_test.go index f6578cccb8..11a89671c0 100644 --- a/services/graph/pkg/service/v0/graph_test.go +++ b/services/graph/pkg/service/v0/graph_test.go @@ -39,6 +39,8 @@ import ( "github.com/opencloud-eu/opencloud/services/graph/pkg/config" "github.com/opencloud-eu/opencloud/services/graph/pkg/config/defaults" "github.com/opencloud-eu/opencloud/services/graph/pkg/errorcode" + "github.com/opencloud-eu/opencloud/services/graph/pkg/identity" + identitymocks "github.com/opencloud-eu/opencloud/services/graph/pkg/identity/mocks" "github.com/opencloud-eu/opencloud/services/graph/pkg/metrics" service "github.com/opencloud-eu/opencloud/services/graph/pkg/service/v0" "github.com/opencloud-eu/opencloud/services/graph/pkg/unifiedrole" @@ -51,6 +53,7 @@ var _ = Describe("Graph", func() { gatewaySelector pool.Selectable[gateway.GatewayAPIClient] eventsPublisher mocks.Publisher permissionService mocks.Permissions + identityBackend *identitymocks.Backend ctx context.Context cfg *config.Config rr *httptest.ResponseRecorder @@ -87,6 +90,7 @@ var _ = Describe("Graph", func() { eventsPublisher = mocks.Publisher{} permissionService = mocks.Permissions{} + identityBackend = &identitymocks.Backend{} var err error svc, err = service.NewService( @@ -95,6 +99,7 @@ var _ = Describe("Graph", func() { service.WithGatewaySelector(gatewaySelector), service.EventsPublisher(&eventsPublisher), service.PermissionService(&permissionService), + service.WithIdentityBackend(identityBackend), ) Expect(err).ToNot(HaveOccurred()) }) @@ -877,7 +882,7 @@ var _ = Describe("Graph", func() { }, nil) }) - DescribeTable("lets the storage provider generate the id when none is supplied", + DescribeTable("lets the storage provider generate the id when none is supplied, without looking up users", func(jsonBody string) { mockSuccessfulCreate("generated-id") @@ -887,12 +892,14 @@ var _ = Describe("Graph", func() { Expect(rr.Code).To(Equal(http.StatusCreated)) Expect(csr).ToNot(BeNil()) Expect(csr.GetOpaque().GetMap()).ToNot(HaveKey("spaceid")) + identityBackend.AssertNotCalled(GinkgoT(), "GetUser", mock.Anything, mock.Anything, mock.Anything) }, Entry("absent id", `{"name": "Test Space"}`), Entry("empty id", `{"name": "Test Space", "id": ""}`), ) - It("forwards the id to the storage provider", func() { + It("forwards the id to the storage provider when it does not belong to a user", func() { + identityBackend.On("GetUser", mock.Anything, spaceID, mock.Anything).Return(nil, identity.ErrNotFound) mockSuccessfulCreate(spaceID) jsonBody := []byte(`{"name": "Test Space", "id": "` + spaceID + `"}`) @@ -905,9 +912,46 @@ var _ = Describe("Graph", func() { var response libregraph.Drive Expect(json.Unmarshal(rr.Body.Bytes(), &response)).To(Succeed()) Expect(response.GetId()).To(Equal("pro-1$" + spaceID)) + identityBackend.AssertCalled(GinkgoT(), "GetUser", mock.Anything, spaceID, mock.Anything) }) - DescribeTable("rejects ids that are not canonical UUIDs before calling the storage provider", + // In the next two tests a successful CreateStorageSpace is mocked on purpose: a handler that + // skips the user lookup then fails on the status and AssertNotCalled checks, not on an + // unexpected mock call. + It("returns 409 Conflict without creating the space when the id belongs to a user", func() { + identityBackend.On("GetUser", mock.Anything, spaceID, mock.Anything).Return(&libregraph.User{Id: libregraph.PtrString(spaceID)}, nil) + mockSuccessfulCreate(spaceID) + + jsonBody := []byte(`{"name": "Test Space", "id": "` + spaceID + `"}`) + r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/drives", bytes.NewBuffer(jsonBody)).WithContext(ctx) + rr := httptest.NewRecorder() + svc.CreateDrive(rr, r) + Expect(rr.Code).To(Equal(http.StatusConflict)) + + var libreError libregraph.OdataError + Expect(json.Unmarshal(rr.Body.Bytes(), &libreError)).To(Succeed()) + Expect(libreError.Error.Code).To(Equal(errorcode.NameAlreadyExists.String())) + Expect(libreError.Error.Message).To(Equal("drive already exists")) + gatewayClient.AssertNotCalled(GinkgoT(), "CreateStorageSpace", mock.Anything, mock.Anything) + }) + + It("does not create the space when the identity backend fails to look up the id", func() { + identityBackend.On("GetUser", mock.Anything, spaceID, mock.Anything).Return(nil, errorcode.New(errorcode.ServiceNotAvailable, "identity backend unavailable")) + mockSuccessfulCreate(spaceID) + + jsonBody := []byte(`{"name": "Test Space", "id": "` + spaceID + `"}`) + r := httptest.NewRequest(http.MethodPost, "/graph/v1.0/drives", bytes.NewBuffer(jsonBody)).WithContext(ctx) + rr := httptest.NewRecorder() + svc.CreateDrive(rr, r) + Expect(rr.Code).To(Equal(http.StatusInternalServerError)) + + var libreError libregraph.OdataError + Expect(json.Unmarshal(rr.Body.Bytes(), &libreError)).To(Succeed()) + Expect(libreError.Error.Code).To(Equal(errorcode.ServiceNotAvailable.String())) + gatewayClient.AssertNotCalled(GinkgoT(), "CreateStorageSpace", mock.Anything, mock.Anything) + }) + + DescribeTable("rejects ids that are not canonical UUIDs before looking up users or calling the storage provider", func(id string) { gatewayClient.On("CreateStorageSpace", mock.Anything, mock.Anything).Return(&provider.CreateStorageSpaceResponse{ Status: status.NewInternal(ctx, "storage provider must not be called"), @@ -924,6 +968,7 @@ var _ = Describe("Graph", func() { Expect(json.Unmarshal(rr.Body.Bytes(), &libreError)).To(Succeed()) Expect(libreError.Error.Code).To(Equal(errorcode.InvalidRequest.String())) Expect(libreError.Error.Message).To(Equal("invalid drive id: must be a canonical UUID")) + identityBackend.AssertNotCalled(GinkgoT(), "GetUser", mock.Anything, mock.Anything, mock.Anything) gatewayClient.AssertNotCalled(GinkgoT(), "CreateStorageSpace", mock.Anything, mock.Anything) }, Entry("path traversal", "../../x"), @@ -937,6 +982,7 @@ var _ = Describe("Graph", func() { ) It("returns 409 Conflict when the storage provider reports that the space already exists", func() { + identityBackend.On("GetUser", mock.Anything, spaceID, mock.Anything).Return(nil, identity.ErrNotFound) gatewayClient.On("CreateStorageSpace", mock.Anything, mock.Anything).Return(&provider.CreateStorageSpaceResponse{ Status: status.NewAlreadyExists(ctx, nil, "decomposedfs: spaces: space already exists"), }, nil)