From 3c658126f4069fd39789a6816519ecb3ea6ee17e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 6 Feb 2026 00:53:57 +0000 Subject: [PATCH] Add validation to prevent arbitrary Owner permission assignment - Use enum constant instead of string literal in test - Add validation in add_user and add_group endpoints to prevent Owner permission assignment - Owner permission can only be assigned automatically on collection creation Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> --- server/models/Collection.test.ts | 3 ++- server/routes/api/collections/collections.ts | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/server/models/Collection.test.ts b/server/models/Collection.test.ts index cfba9e59f9..1fc7c2c53e 100644 --- a/server/models/Collection.test.ts +++ b/server/models/Collection.test.ts @@ -1,5 +1,6 @@ import { randomUUID } from "node:crypto"; import { randomString } from "@shared/random"; +import { CollectionPermission } from "@shared/types"; import slugify from "@shared/utils/slugify"; import { buildUser, @@ -562,6 +563,6 @@ describe("Owner permission", () => { const ownerMembership = membership.find((m) => m.userId === user.id); expect(ownerMembership).toBeDefined(); - expect(ownerMembership?.permission).toBe("owner"); + expect(ownerMembership?.permission).toBe(CollectionPermission.Owner); }); }); diff --git a/server/routes/api/collections/collections.ts b/server/routes/api/collections/collections.ts index fde56b9ef0..a34fb86f4e 100644 --- a/server/routes/api/collections/collections.ts +++ b/server/routes/api/collections/collections.ts @@ -43,7 +43,7 @@ import { RateLimiterStrategy } from "@server/utils/RateLimiter"; import { collectionIndexing } from "@server/utils/indexing"; import pagination from "../middlewares/pagination"; import * as T from "./schema"; -import { InvalidRequestError } from "@server/errors"; +import { InvalidRequestError, ValidationError } from "@server/errors"; const router = new Router(); @@ -215,6 +215,14 @@ router.post( authorize(user, "update", collection); authorize(user, "read", group); + // Prevent assigning Owner permission via this endpoint + // Owner permission is only assigned automatically on collection creation + if (permission === CollectionPermission.Owner) { + throw ValidationError( + "Owner permission can only be assigned automatically on collection creation" + ); + } + let membership = await GroupMembership.findOne({ where: { collectionId: id, @@ -375,6 +383,15 @@ router.post( authorize(actor, "update", collection); authorize(actor, "read", user); + // Prevent assigning Owner permission via this endpoint + // Owner permission is only assigned automatically on collection creation + const requestedPermission = permission || user.defaultCollectionPermission; + if (requestedPermission === CollectionPermission.Owner) { + throw ValidationError( + "Owner permission can only be assigned automatically on collection creation" + ); + } + let membership = await UserMembership.findOne({ where: { collectionId: id,