The minimalistic document archiving platform.
fix(tags): scope tag updates and deletions to their organization (#1080)
Corentin Thomasset47d44e0681bf
diff --git a/.changeset/brown-bugs-wait.md b/.changeset/brown-bugs-wait.mdnew file mode 100644index 00000000..4f876a7a--- /dev/null+++ b/.changeset/brown-bugs-wait.md@@ -0,0 +1,5 @@+---+"@papra/app": patch+---++Fixed an authorization issue where tag updates and deletions were not scoped to the organization in the URL. Tag mutation endpoints are now correctly scoped to the requested organization.diff --git a/.changeset/free-eyes-strive.md b/.changeset/free-eyes-strive.mdnew file mode 100644index 00000000..851d35d3--- /dev/null+++ b/.changeset/free-eyes-strive.md@@ -0,0 +1,5 @@+---+"@papra/app": patch+---++Tag deletion endpoint now returns a `204 No Content` status code instead of `200 OK` with an empty JSON object, and a `404 Not Found` status code is returned when trying to delete a tag that does not exists for the organization.diff --git a/apps/papra-server/src/modules/tags/tags.repository.test.ts b/apps/papra-server/src/modules/tags/tags.repository.test.tsindex 69ea2f2c..48f4359c 100644--- a/apps/papra-server/src/modules/tags/tags.repository.test.ts+++ b/apps/papra-server/src/modules/tags/tags.repository.test.ts@@ -2,7 +2,7 @@ import { describe, expect, test } from 'vitest';import { createInMemoryDatabase } from '../app/database/database.test-utils';import { ORGANIZATION_ROLES } from '../organizations/organizations.constants';import { isNil } from '../shared/utils';-import { createDocumentAlreadyHasTagError, createTagAlreadyExistsError } from './tags.errors';+import { createDocumentAlreadyHasTagError, createTagAlreadyExistsError, createTagNotFoundError } from './tags.errors';import { createTagsRepository } from './tags.repository';import { tagsTable } from './tags.table';@@ -43,6 +43,7 @@ describe('tags repository', () => {const { tag: updatedTag1 } = await tagsRepository.updateTag({tagId: tag1.id,+ organizationId: 'organization-1',name: 'Tag 1 Updated',description: 'Tag 1 Description',color: '#00aa00',@@ -65,7 +66,7 @@ describe('tags repository', () => {color: '#00aa00',});- await tagsRepository.deleteTag({ tagId: tag1.id });+ await tagsRepository.deleteTag({ tagId: tag1.id, organizationId: 'organization-1' });const { tags: tagsAfterDelete } = await tagsRepository.getOrganizationTags({ organizationId: 'organization-1' });@@ -73,6 +74,30 @@ describe('tags repository', () => {});});+ describe('deleteTag', () => {+ test('deleteTag scoped by organizationId does not delete tags belonging to another organization', async () => {+ const { db } = await createInMemoryDatabase({+ organizations: [+ { id: 'organization-1', name: 'Organization 1' },+ { id: 'organization-2', name: 'Organization 2' },+ ],+ tags: [+ { id: 'tag-1', organizationId: 'organization-2', name: 'Tag 1', normalizedName: 'tag 1', color: '#aa0000' },+ ],+ });++ const tagsRepository = createTagsRepository({ db });++ await expect(+ tagsRepository.deleteTag({ tagId: 'tag-1', organizationId: 'organization-1' }),+ ).rejects.toThrow(createTagNotFoundError());++ const tags = await db.select().from(tagsTable);+ expect(tags).to.have.length(1);+ expect(tags[0]).to.include({ id: 'tag-1', organizationId: 'organization-2' });+ });+ });+describe('addTagToDocument', () => {test('a tag can be be added only once to a document', async () => {const { db } = await createInMemoryDatabase({@@ -230,10 +255,31 @@ describe('tags repository', () => {const tagsRepository = createTagsRepository({ db });await expect(- tagsRepository.updateTag({ tagId: 'tag-2', name: 'Tag 1' }),+ tagsRepository.updateTag({ tagId: 'tag-2', organizationId: 'organization-1', name: 'Tag 1' }),).rejects.toThrow(createTagAlreadyExistsError());});+ test('updateTag scoped by organizationId does not modify tags belonging to another organization', async () => {+ const { db } = await createInMemoryDatabase({+ organizations: [+ { id: 'organization-1', name: 'Organization 1' },+ { id: 'organization-2', name: 'Organization 2' },+ ],+ tags: [+ { id: 'tag-1', organizationId: 'organization-2', name: 'Tag 1', normalizedName: 'tag 1', color: '#aa0000' },+ ],+ });++ const tagsRepository = createTagsRepository({ db });++ const { tag } = await tagsRepository.updateTag({ tagId: 'tag-1', organizationId: 'organization-1', name: 'Hijacked' });++ expect(tag).to.equal(undefined);++ const [storedTag] = await db.select().from(tagsTable);+ expect(storedTag).to.include({ id: 'tag-1', name: 'Tag 1', organizationId: 'organization-2' });+ });+test('when updating a tag name, the normalized name is also updated for uniqueness checks', async () => {const { db } = await createInMemoryDatabase({organizations: [{ id: 'organization-1', name: 'Organization 1' }],@@ -244,7 +290,7 @@ describe('tags repository', () => {const tagsRepository = createTagsRepository({ db });- await tagsRepository.updateTag({ tagId: 'tag-1', name: 'New Tag Name' });+ await tagsRepository.updateTag({ tagId: 'tag-1', organizationId: 'organization-1', name: 'New Tag Name' });const tags = await db.select().from(tagsTable);diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.tsindex ae7ca812..5494f505 100644--- a/apps/papra-server/src/modules/tags/tags.repository.ts+++ b/apps/papra-server/src/modules/tags/tags.repository.ts@@ -7,7 +7,7 @@ import { chunkArray } from '../shared/arrays/arrays.utils';import { isUniqueConstraintError } from '../shared/db/constraints.models';import { omitUndefined } from '../shared/objects';import { isDefined } from '../shared/utils';-import { createDocumentAlreadyHasTagError, createTagAlreadyExistsError } from './tags.errors';+import { createDocumentAlreadyHasTagError, createTagAlreadyExistsError, createTagNotFoundError } from './tags.errors';import { normalizeTagName } from './tags.repository.models';import { documentsTagsTable, tagsTable } from './tags.table';@@ -139,11 +139,23 @@ async function createTag({ tag, db }: { tag: { name: string; description?: strinreturn { tag: createdTag };}-async function deleteTag({ tagId, db }: { tagId: string; db: Database }) {- await db.delete(tagsTable).where(eq(tagsTable.id, tagId));+async function deleteTag({ tagId, organizationId, db }: { tagId: string; organizationId: string; db: Database }) {+ const deleteResult = await db+ .delete(tagsTable)+ .where(+ and(+ eq(tagsTable.id, tagId),+ eq(tagsTable.organizationId, organizationId),+ ),+ )+ .returning({ id: tagsTable.id });++ if (deleteResult.length === 0) {+ throw createTagNotFoundError();+ }}-async function updateTag({ tagId, name, description, color, db }: { tagId: string; name?: string; description?: string; color?: string; db: Database }) {+async function updateTag({ tagId, organizationId, name, description, color, db }: { tagId: string; organizationId: string; name?: string; description?: string; color?: string; db: Database }) {const [result, error] = await safely(db.update(tagsTable)@@ -156,7 +168,10 @@ async function updateTag({ tagId, name, description, color, db }: { tagId: strin}),).where(- eq(tagsTable.id, tagId),+ and(+ eq(tagsTable.id, tagId),+ eq(tagsTable.organizationId, organizationId),+ ),).returning(),);diff --git a/apps/papra-server/src/modules/tags/tags.routes.ts b/apps/papra-server/src/modules/tags/tags.routes.tsindex c12b31e4..bcb1d989 100644--- a/apps/papra-server/src/modules/tags/tags.routes.ts+++ b/apps/papra-server/src/modules/tags/tags.routes.ts@@ -108,7 +108,11 @@ function setupUpdateTagRoute({ app, db }: RouteDefinitionContext) {await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });- const { tag } = await tagsRepository.updateTag({ tagId, name, color, description });+ const { tag } = await tagsRepository.updateTag({ tagId, organizationId, name, color, description });++ if (!tag) {+ throw createTagNotFoundError();+ }return context.json({tag,@@ -135,9 +139,9 @@ function setupDeleteTagRoute({ app, db }: RouteDefinitionContext) {await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });- await tagsRepository.deleteTag({ tagId });+ await tagsRepository.deleteTag({ tagId, organizationId });- return context.json({});+ return context.body(null, 204);},);}