Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(subscription): proper file size limit enforcing

Corentin Thomasset6a5bcef5adf1
diff --git a/apps/papra-server/src/modules/documents/documents.routes.ts b/apps/papra-server/src/modules/documents/documents.routes.ts
index b1faca09..971bfeb3 100644
--- a/apps/papra-server/src/modules/documents/documents.routes.ts
+++ b/apps/papra-server/src/modules/documents/documents.routes.ts
@@ -6,8 +6,11 @@ import { getUser } from '../app/auth/auth.models';
import { organizationIdSchema } from '../organizations/organization.schemas';
import { createOrganizationsRepository } from '../organizations/organizations.repository';
import { ensureUserIsInOrganization } from '../organizations/organizations.usecases';
+import { createPlansRepository } from '../plans/plans.repository';
+import { getOrganizationPlan } from '../plans/plans.usecases';
import { getFileStreamFromMultipartForm } from '../shared/streams/file-upload';
import { validateJsonBody, validateParams, validateQuery } from '../shared/validation/validation';
+import { createSubscriptionsRepository } from '../subscriptions/subscriptions.repository';
import { createDocumentIsNotDeletedError } from './documents.errors';
import { formatDocumentForApi, formatDocumentsForApi, isDocumentSizeLimitEnabled } from './documents.models';
import { createDocumentsRepository } from './documents.repository';
@@ -45,12 +48,17 @@ function setupCreateDocumentRoute({ app, ...deps }: RouteDefinitionContext) {
const organizationsRepository = createOrganizationsRepository({ db });
await ensureUserIsInOrganization({ userId, organizationId, organizationsRepository });
- const { maxUploadSize } = config.documentsStorage;
+ // Get organization's plan-specific upload limit
+ const plansRepository = createPlansRepository({ config });
+ const subscriptionsRepository = createSubscriptionsRepository({ db });
+
+ const { organizationPlan } = await getOrganizationPlan({ organizationId, plansRepository, subscriptionsRepository });
+ const { maxFileSize } = organizationPlan.limits;
const { fileStream, fileName, mimeType } = await getFileStreamFromMultipartForm({
body: context.req.raw.body,
headers: context.req.header(),
- maxFileSize: isDocumentSizeLimitEnabled({ maxUploadSize }) ? maxUploadSize : undefined,
+ maxFileSize: isDocumentSizeLimitEnabled({ maxUploadSize: maxFileSize }) ? maxFileSize : undefined,
});
const createDocument = createDocumentCreationUsecase({ ...deps });
diff --git a/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.ts b/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.ts
index 0dc35e74..57c8e975 100644
--- a/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.ts
+++ b/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.ts
@@ -5,6 +5,7 @@ import { createServer } from '../../app/server';
import { createTestServerDependencies } from '../../app/server.test-utils';
import { overrideConfig } from '../../config/config.test-utils';
import { ORGANIZATION_ROLES } from '../../organizations/organizations.constants';
+import { PLUS_PLAN_ID, PRO_PLAN_ID } from '../../plans/plans.constants';
import { documentsTable } from '../documents.table';
import { inMemoryStorageDriverFactory } from '../storage/drivers/memory/memory.storage-driver';
@@ -247,5 +248,123 @@ describe('documents e2e', () => {
expect(retrievedDocument).to.eql({ ...document, tags: [] });
}
});
+
+ test('organizations on Plus plan should be able to upload files up to 100 MiB (not limited by global config)', async () => {
+ const { db } = await createInMemoryDatabase({
+ users: [{ id: 'usr_111111111111111111111111', email: '[email protected]' }],
+ organizations: [{ id: 'org_222222222222222222222222', name: 'Plus Org', customerId: 'cus_plus123' }],
+ organizationMembers: [{ organizationId: 'org_222222222222222222222222', userId: 'usr_111111111111111111111111', role: ORGANIZATION_ROLES.OWNER }],
+ organizationSubscriptions: [{
+ id: 'sub_plus123',
+ customerId: 'cus_plus123',
+ organizationId: 'org_222222222222222222222222',
+ planId: PLUS_PLAN_ID,
+ status: 'active',
+ seatsCount: 5,
+ currentPeriodStart: new Date('2024-01-01'),
+ currentPeriodEnd: new Date('2024-02-01'),
+ cancelAtPeriodEnd: false,
+ }],
+ });
+
+ const { app } = createServer(createTestServerDependencies({
+ db,
+ config: overrideConfig({
+ env: 'test',
+ documentsStorage: {
+ driver: 'in-memory',
+ // Global config set to 10 MiB (simulating free tier limit)
+ maxUploadSize: 1024 * 1024 * 10, // 10 MiB
+ },
+ }),
+ }));
+
+ // File size: 50 MiB - exceeds global config (10 MiB) but within Plus plan limit (100 MiB)
+ const fileSizeBytes = 1024 * 1024 * 50; // 50 MiB
+ const formData = new FormData();
+ formData.append('file', new File(['a'.repeat(fileSizeBytes)], 'large-document.txt', { type: 'text/plain' }));
+ const body = new Response(formData);
+
+ const createDocumentResponse = await app.request(
+ '/api/organizations/org_222222222222222222222222/documents',
+ {
+ method: 'POST',
+ headers: {
+ ...Object.fromEntries(body.headers.entries()),
+ },
+ body: await body.arrayBuffer(),
+ },
+ { loggedInUserId: 'usr_111111111111111111111111' },
+ );
+
+ // Should succeed because Plus plan allows 100 MiB
+ expect(createDocumentResponse.status).to.eql(200);
+ const { document } = (await createDocumentResponse.json()) as { document: Document };
+
+ expect(document).to.include({
+ name: 'large-document.txt',
+ mimeType: 'text/plain',
+ originalSize: fileSizeBytes,
+ });
+ });
+
+ test('organizations on Pro plan should be able to upload files up to 500 MiB (not limited by global config)', async () => {
+ const { db } = await createInMemoryDatabase({
+ users: [{ id: 'usr_111111111111111111111111', email: '[email protected]' }],
+ organizations: [{ id: 'org_333333333333333333333333', name: 'Pro Org', customerId: 'cus_pro123' }],
+ organizationMembers: [{ organizationId: 'org_333333333333333333333333', userId: 'usr_111111111111111111111111', role: ORGANIZATION_ROLES.OWNER }],
+ organizationSubscriptions: [{
+ id: 'sub_pro123',
+ customerId: 'cus_pro123',
+ organizationId: 'org_333333333333333333333333',
+ planId: PRO_PLAN_ID,
+ status: 'active',
+ seatsCount: 20,
+ currentPeriodStart: new Date('2024-01-01'),
+ currentPeriodEnd: new Date('2024-02-01'),
+ cancelAtPeriodEnd: false,
+ }],
+ });
+
+ const { app } = createServer(createTestServerDependencies({
+ db,
+ config: overrideConfig({
+ env: 'test',
+ documentsStorage: {
+ driver: 'in-memory',
+ // Global config set to 10 MiB (simulating free tier limit)
+ maxUploadSize: 1024 * 1024 * 10, // 10 MiB
+ },
+ }),
+ }));
+
+ // File size: 200 MiB - exceeds global config (10 MiB) but within Pro plan limit (500 MiB)
+ const fileSizeBytes = 1024 * 1024 * 200; // 200 MiB
+ const formData = new FormData();
+ formData.append('file', new File(['a'.repeat(fileSizeBytes)], 'very-large-document.txt', { type: 'text/plain' }));
+ const body = new Response(formData);
+
+ const createDocumentResponse = await app.request(
+ '/api/organizations/org_333333333333333333333333/documents',
+ {
+ method: 'POST',
+ headers: {
+ ...Object.fromEntries(body.headers.entries()),
+ },
+ body: await body.arrayBuffer(),
+ },
+ { loggedInUserId: 'usr_111111111111111111111111' },
+ );
+
+ // Should succeed because Pro plan allows 500 MiB
+ expect(createDocumentResponse.status).to.eql(200);
+ const { document } = (await createDocumentResponse.json()) as { document: Document };
+
+ expect(document).to.include({
+ name: 'very-large-document.txt',
+ mimeType: 'text/plain',
+ originalSize: fileSizeBytes,
+ });
+ });
});
});