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

Regression test prevents cross-company data leakage in backup exports

davoodmehraban89 committed Oct 14, 2026 at 08:57 UTC 13cb40c72c299f5f9dd52513df90db4180490716
1 file changed +57
tests/backup-company-isolation.test.mjs new
+57
@@ -0,0 +1,57 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import 'fake-indexeddb/auto';
4 +import { StorageService } from '../src/core/StorageService.js';
5 +import { CompanyService } from '../src/core/CompanyService.js';
6 +import { Auth } from '../src/core/Auth.js';
7 +import { SettingsController } from '../src/controllers/SettingsController.js';
8 +
9 +test('company backup never exposes another company or global users/logs',async()=>{
10 + await StorageService.clearAll();
11 + Auth.currentUser={id:'admin-c1',role:'admin',username:'admin'};
12 + CompanyService.activeCompanyId='c1';
13 +
14 + await StorageService.put('companies',{id:'c1',name:'Company One'});
15 + await StorageService.put('companies',{id:'c2',name:'SECRET_COMPANY_TWO'});
16 + await StorageService.put('contacts',{id:'contact-c1',companyId:'c1',name:'Customer One'});
17 + await StorageService.put('contacts',{id:'contact-c2',companyId:'c2',name:'SECRET_CUSTOMER_TWO'});
18 + await StorageService.put('settings',{id:'finance_v2_enabled_c1',companyId:'c1',enabled:true});
19 + await StorageService.put('settings',{id:'finance_v2_enabled_c2',companyId:'c2',enabled:true,token:'SECRET_FOREIGN_SETTING'});
20 + await StorageService.put('settings',{id:'global_setting',value:'SECRET_GLOBAL_SETTING'});
21 + await StorageService.put('users',{id:'foreign-user',username:'SECRET_OTHER_USER',pwHash:'SECRET_PASSWORD_HASH'});
22 + await StorageService.put('logs',{id:'system-log',message:'SECRET_SENSITIVE_LOG'});
23 +
24 + const backup=await SettingsController.exportBackup();
25 + assert.equal(backup.scope,'company');
26 + assert.equal(backup.companyId,'c1');
27 + assert.ok(Number.isInteger(backup.schemaVersion));
28 + assert.deepEqual(backup.data.companies.map(x=>x.id),['c1']);
29 + assert.deepEqual(backup.data.contacts.map(x=>x.id),['contact-c1']);
30 + assert.deepEqual(backup.data.settings.map(x=>x.id),['finance_v2_enabled_c1']);
31 + assert.deepEqual(backup.data.users,[]);
32 + assert.deepEqual(backup.data.logs,[]);
33 + const exported=JSON.stringify(backup);
34 + for(const sensitive of ['SECRET_COMPANY_TWO','SECRET_CUSTOMER_TWO',
35 + 'SECRET_FOREIGN_SETTING','SECRET_GLOBAL_SETTING','SECRET_OTHER_USER',
36 + 'SECRET_PASSWORD_HASH','SECRET_SENSITIVE_LOG']){
37 + assert.ok(!exported.includes(sensitive),'A company-scoped backup leaked '+sensitive);
38 + }
39 + // The export operation cannot modify or erase the source records.
40 + assert.equal((await StorageService.get('companies','c2')).name,'SECRET_COMPANY_TWO');
41 + assert.equal((await StorageService.get('users','foreign-user')).pwHash,'SECRET_PASSWORD_HASH');
42 +});
43 +
44 +test('company backup preserves only same-company records from all financial stores',async()=>{
45 + await StorageService.clearAll();
46 + Auth.currentUser={id:'admin-c1',role:'admin',username:'admin'};
47 + CompanyService.activeCompanyId='c1';
48 + await StorageService.put('companies',{id:'c1',name:'One'});
49 + await StorageService.put('companies',{id:'c2',name:'Two'});
50 + await StorageService.put('invoices',{id:'i-c1',companyId:'c1',number:'120'});
51 + await StorageService.put('invoices',{id:'i-c2',companyId:'c2',number:'121'});
52 + await StorageService.put('journal_entries',{id:'j-c1',companyId:'c1',sourceKey:'c1-only'});
53 + await StorageService.put('journal_entries',{id:'j-c2',companyId:'c2',sourceKey:'c2-only'});
54 + const backup=await SettingsController.exportBackup();
55 + assert.deepEqual(backup.data.invoices.map(i=>i.id),['i-c1']);
56 + assert.deepEqual(backup.data.journal_entries.map(j=>j.id),['j-c1']);
57 +});