Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(documents-batch): validate document ownership in batch operations (#1078)

Corentin Thomasset5c5f5562d9e9
diff --git a/apps/papra-server/src/modules/documents/documents-batch/documents-batch.errors.ts b/apps/papra-server/src/modules/documents/documents-batch/documents-batch.errors.ts
new file mode 100644
index 00000000..1a151a22
--- /dev/null
+++ b/apps/papra-server/src/modules/documents/documents-batch/documents-batch.errors.ts
@@ -0,0 +1,7 @@
+import { createErrorFactory } from '../../shared/errors/errors';
+
+export const createDocumentIdsNotFromOrganizationError = createErrorFactory({
+ message: 'Some documents do not exist or do not belong to the organization.',
+ code: 'documents.batch.documents_not_from_organization',
+ statusCode: 400,
+});
diff --git a/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.test.ts b/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.test.ts
index f362273c..b53179f8 100644
--- a/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.test.ts
+++ b/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.test.ts
@@ -3,9 +3,11 @@ import { describe, expect, test } from 'vitest';
import { createInMemoryDatabase } from '../../app/database/database.test-utils';
import { createTestEventServices } from '../../app/events/events.test-utils';
import { ORGANIZATION_ROLES } from '../../organizations/organizations.constants';
+import { pick } from '../../shared/objects';
import { createDatabaseFts5DocumentSearchServices } from '../document-search/database-fts5/database-fts5.document-search-provider';
import { createDocumentsRepository } from '../documents.repository';
import { documentsTable } from '../documents.table';
+import { createDocumentIdsNotFromOrganizationError } from './documents-batch.errors';
import { resolveBatchTargetDocumentIds, trashDocumentsBatch } from './documents-batch.usecases';
function createStubSearchServices({ documentIds = [] }: { documentIds?: string[] } = {}) {
@@ -37,24 +39,82 @@ describe('documents-batch usecases', () => {
describe('resolveBatchTargetDocumentIds', () => {
test('returns the provided documentIds without invoking the search service', async () => {
const { services, calls } = createStubSearchServices();
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'organization-1', name: 'Organization 1' }],
+ documents: [
+ { ...baseDocument, id: 'doc-1', organizationId: 'organization-1', name: 'doc 1', originalSha256Hash: 'h1' },
+ { ...baseDocument, id: 'doc-2', organizationId: 'organization-1', name: 'doc 2', originalSha256Hash: 'h2' },
+ ],
+ });
const { documentIds } = await resolveBatchTargetDocumentIds({
filter: { documentIds: ['doc-1', 'doc-2'] },
organizationId: 'organization-1',
documentSearchServices: services,
+ documentsRepository: createDocumentsRepository({ db }),
});
expect(documentIds).to.eql(['doc-1', 'doc-2']);
expect(calls).to.eql([]);
});
+ test('throws an error if any of the provided documentIds do not belong to the organization', async () => {
+ const { services } = createStubSearchServices();
+ const { db } = await createInMemoryDatabase({
+ organizations: [
+ { id: 'organization-1', name: 'Organization 1' },
+ { id: 'organization-2', name: 'Organization 2' },
+ ],
+ documents: [
+ { ...baseDocument, id: 'doc-1', organizationId: 'organization-1', name: 'doc 1', originalSha256Hash: 'h1' },
+ { ...baseDocument, id: 'doc-2', organizationId: 'organization-2', name: 'doc 2', originalSha256Hash: 'h2' },
+ ],
+ });
+
+ await expect(resolveBatchTargetDocumentIds({
+ filter: { documentIds: ['doc-1', 'doc-2'] },
+ organizationId: 'organization-1',
+ documentSearchServices: services,
+ documentsRepository: createDocumentsRepository({ db }),
+ })).rejects.toThrow(createDocumentIdsNotFromOrganizationError());
+ });
+
+ test('throws an error if a document id does not exist', async () => {
+ const { services } = createStubSearchServices();
+ const { db } = await createInMemoryDatabase({
+ organizations: [{ id: 'organization-1', name: 'Organization 1' }],
+ documents: [
+ { ...baseDocument, id: 'doc-1', organizationId: 'organization-1', name: 'doc 1', originalSha256Hash: 'h1' },
+ ],
+ });
+
+ await expect(resolveBatchTargetDocumentIds({
+ filter: { documentIds: ['doc-1', 'doc-2'] },
+ organizationId: 'organization-1',
+ documentSearchServices: services,
+ documentsRepository: createDocumentsRepository({ db }),
+ })).rejects.toThrow(createDocumentIdsNotFromOrganizationError());
+ });
+
test('on the query path, delegates to the search service without enforcing a cap', async () => {
const { services, calls } = createStubSearchServices({ documentIds: ['doc-1', 'doc-2'] });
+ const { db } = await createInMemoryDatabase({
+ users: [
+ { id: 'user-1', email: '[email protected]' },
+ { id: 'user-2', email: '[email protected]' },
+ ],
+ organizations: [{ id: 'organization-1', name: 'Organization 1' }],
+ documents: [
+ { id: 'doc-1', ...baseDocument, name: 'doc 1', originalSha256Hash: 'h1', isDeleted: true, deletedBy: 'user-1', deletedAt: new Date('2026-01-01') },
+ { id: 'doc-2', ...baseDocument, name: 'doc 2', originalSha256Hash: 'h2' },
+ ],
+ });
const { documentIds } = await resolveBatchTargetDocumentIds({
filter: { query: 'tag:invoice' },
organizationId: 'organization-1',
documentSearchServices: services,
+ documentsRepository: createDocumentsRepository({ db }),
});
expect(documentIds).to.eql(['doc-1', 'doc-2']);
@@ -66,7 +126,7 @@ describe('documents-batch usecases', () => {
});
describe('trashDocumentsBatch', () => {
- test('trashes documents scoped to the organization, ignoring cross-org ids', async () => {
+ test('trashes documents scoped to the organization, throws if any document does not belong to the organization', async () => {
const { db } = await createInMemoryDatabase({
users: [{ id: 'user-1', email: '[email protected]' }],
organizations: [
@@ -84,23 +144,22 @@ describe('documents-batch usecases', () => {
const documentsRepository = createDocumentsRepository({ db });
const { services } = createStubSearchServices();
- const { trashedDocumentIds, trashedCount } = await trashDocumentsBatch({
- filter: { documentIds: ['doc-1', 'doc-2', 'doc-3'] },
+ await expect(trashDocumentsBatch({
+ filter: { documentIds: ['doc-1', 'doc-3'] },
organizationId: 'organization-1',
userId: 'user-1',
documentsRepository,
documentSearchServices: services,
eventServices: createTestEventServices(),
- });
-
- expect(trashedDocumentIds.toSorted()).to.eql(['doc-1', 'doc-2']);
- expect(trashedCount).to.eql(2);
+ })).rejects.toThrow(createDocumentIdsNotFromOrganizationError());
const records = await db.select().from(documentsTable);
- const byId = Object.fromEntries(records.map(r => [r.id, r]));
- expect(byId['doc-1']?.isDeleted).to.eql(true);
- expect(byId['doc-2']?.isDeleted).to.eql(true);
- expect(byId['doc-3']?.isDeleted).to.eql(false);
+ const byId = Object.fromEntries(records.map(r => [r.id, pick(r, ['id', 'isDeleted', 'deletedBy', 'deletedAt'])]));
+ expect(byId).to.eql({
+ 'doc-1': { id: 'doc-1', isDeleted: false, deletedBy: null, deletedAt: null },
+ 'doc-2': { id: 'doc-2', isDeleted: false, deletedBy: null, deletedAt: null },
+ 'doc-3': { id: 'doc-3', isDeleted: false, deletedBy: null, deletedAt: null },
+ });
});
test('does not re-trash documents that are already in the trash', async () => {
diff --git a/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.ts b/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.ts
index 619ea602..c89793d5 100644
--- a/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.ts
+++ b/apps/papra-server/src/modules/documents/documents-batch/documents-batch.usecases.ts
@@ -4,27 +4,48 @@ import type { DocumentSearchServices } from '../document-search/document-search.
import type { DocumentsRepository } from '../documents.repository';
import type { BatchTargetFilter } from './documents-batch.schemas';
import { createLogger } from '../../shared/logger/logger';
+import { createDocumentIdsNotFromOrganizationError } from './documents-batch.errors';
+
+async function resolvePlainDocumentIdsBatchTargetFilter({
+ documentIds,
+ documentsRepository,
+ organizationId,
+}: {
+ documentIds: string[];
+ documentsRepository: DocumentsRepository;
+ organizationId: string;
+}): Promise<{ documentIds: string[] }> {
+ const allDocumentsExistInOrganization = await documentsRepository.areAllDocumentsInOrganization({ documentIds, organizationId });
+
+ if (!allDocumentsExistInOrganization) {
+ throw createDocumentIdsNotFromOrganizationError();
+ }
+
+ return { documentIds };
+}
export async function resolveBatchTargetDocumentIds({
filter,
organizationId,
documentSearchServices,
+ documentsRepository,
logger = createLogger({ namespace: 'documents-batch.usecases' }),
}: {
filter: BatchTargetFilter;
organizationId: string;
documentSearchServices: DocumentSearchServices;
+ documentsRepository: DocumentsRepository;
logger?: Logger;
}): Promise<{ documentIds: string[] }> {
const startTime = Date.now();
- const { documentIds, filterKind } = 'documentIds' in filter
- ? { documentIds: filter.documentIds, filterKind: 'documentIds' as const }
- : { ...(await documentSearchServices.getDocumentIdsMatchingQuery({ organizationId, searchQuery: filter.query })), filterKind: 'query' as const };
+ const { documentIds } = 'documentIds' in filter
+ ? await resolvePlainDocumentIdsBatchTargetFilter({ documentIds: filter.documentIds, documentsRepository, organizationId })
+ : await documentSearchServices.getDocumentIdsMatchingQuery({ organizationId, searchQuery: filter.query });
logger.info({
organizationId,
- filterKind,
+ filterKind: 'documentIds' in filter ? 'documentIds' : 'query',
resolvedCount: documentIds.length,
durationMs: Date.now() - startTime,
}, 'Resolved batch target document ids');
@@ -51,7 +72,7 @@ export async function trashDocumentsBatch({
}) {
const startTime = Date.now();
- const { documentIds } = await resolveBatchTargetDocumentIds({ filter, organizationId, documentSearchServices, logger });
+ const { documentIds } = await resolveBatchTargetDocumentIds({ filter, organizationId, documentSearchServices, documentsRepository, logger });
const { trashedDocumentIds } = await documentsRepository.softDeleteDocuments({
documentIds,