فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

fix(A04): enforce source-company ownership in restore service boundary

davoodmehraban89 committed Oct 20, 2026 at 10:11 UTC 7407bf3f34bd89a291929269b6a7ecc31c8cee83
3 files changed +27 -3
src/controllers/SettingsController.js
+1 -1
@@ -216,7 +216,7 @@ class SettingsControllerImpl {
216 throw new Error('پشتیبان متعلق به شرکت دیگری است؛ بازیابی در شرکت فعلی متوقف شد');
217 }
218
219 - return restoreCompanyBackup(backup.data, { companyId, userId: user.id, mode: options.mode || 'merge' });
219 + return restoreCompanyBackup(backup.data, { companyId, userId: user.id, mode: options.mode || 'merge', sourceCompanyId: backup.companyId });
220 }
221
222 async readFileAsText(file) {
src/core/CompanyBackupService.js
+8 -2
@@ -35,9 +35,9 @@ const stableRecord = value => {
35 const sameImmutableRecord = (a, b) =>
36 JSON.stringify(stableRecord(a)) === JSON.stringify(stableRecord(b));
37
38 -export async function restoreCompanyBackup(data, { companyId, userId, mode = 'merge' }) {
38 +export async function restoreCompanyBackup(data, { companyId, userId, mode = 'merge', sourceCompanyId = companyId }) {
39 if (!Auth.isAdmin())throw Error('بازنشانی یا بازیابی اطلاعات حسابداری فقط با مدیر مجاز است');
40 - if (!companyId || !userId || !['merge', 'replace'].includes(mode)) throw new Error('گزینه‌های بازیابی نامعتبر است');
40 + if (!companyId || !userId || !['merge', 'replace'].includes(mode) || !isId(sourceCompanyId)) throw new Error('گزینه‌های بازیابی نامعتبر است');
41 if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error('داده فایل پشتیبان نامعتبر است');
42
43 for (const store of COMPANY_BACKUP_STORES) {
@@ -49,6 +49,12 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
49 if (!item || typeof item !== 'object' || Array.isArray(item) || !isId(item.id)) {
50 throw new Error(`رکورد بدون شناسه معتبر در بخش ${store}`);
51 }
52 + // This service also accepts direct in-process callers. Never silently
53 + // relabel a foreign or ownerless source record to the target company.
54 + // Explicit cross-company migrations pass a distinct sourceCompanyId.
55 + if (item.companyId !== sourceCompanyId) {
56 + throw new Error('فایل پشتیبان حاوی رکورد متعلق به شرکت دیگری است');
57 + }
58 if (seenIds.has(item.id)) throw new Error(`شناسه تکراری در بخش ${store}`);
59 seenIds.add(item.id);
60 }
tests/backup-restore-integration.test.mjs
+18
@@ -7,6 +7,7 @@ import { StorageService } from '../src/core/StorageService.js';
7 import { CompanyService } from '../src/core/CompanyService.js';
8 import { Auth } from '../src/core/Auth.js';
9 import { SettingsController } from '../src/controllers/SettingsController.js';
10 +import { restoreCompanyBackup } from '../src/core/CompanyBackupService.js';
11
12 async function setup() {
13 await StorageService.clearAll();
@@ -101,6 +102,23 @@ test('A04: explicit cross-company restore cannot launder mixed-origin journal re
102 assert.equal((await StorageService.getAll('audit_events')).length, 0);
103 });
104
105 +test('A04: direct service restore refuses to relabel mixed source records', async () => {
106 + await setup();
107 + const rows = [
108 + { id: 'new-same-company', companyId: 'c1', name: 'Must not persist' },
109 + { id: 'new-foreign-company', companyId: 'c2', name: 'Must not retarget' }
110 + ];
111 + await assert.rejects(restoreCompanyBackup({ contacts: rows },
112 + { companyId: 'c1', userId: 'restore-admin' }), /شرکت دیگری/);
113 + assert.equal(await StorageService.get('contacts', 'new-same-company'), undefined);
114 + assert.equal(await StorageService.get('contacts', 'new-foreign-company'), undefined);
115 + assert.equal((await StorageService.getAll('audit_events')).length, 0);
116 + await assert.rejects(restoreCompanyBackup({ products: [
117 + { id: 'ownerless', name: 'Do not guess' }
118 + ] }, { companyId: 'c1', userId: 'restore-admin' }), /شرکت دیگری/);
119 + assert.equal(await StorageService.get('products', 'ownerless'), undefined);
120 +});
121 +
122 test('A04: company backup rejects duplicate IDs before any database writes', async () => {
123 await setup();
124 const backup = await SettingsController.exportBackup();