فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
A04/P0: prevent cross-company data leakage in company backup exports
Restrict company backup exports to active-company records and safely scoped finance marker; exclude global users/logs. Add synthetic privacy regression tests. GitHub Actions Node/Vite/security dependency audit and Windows preview build passed. No schema migration or public Release.
davoodmehraban89 committed
Oct 14, 2026 at 09:04 UTC
bea2b39b8c93c796ebe1b45249f477b372a09418
3 files changed
+81
-5
src/controllers/SettingsController.js
+23
-4
@@ -44,14 +44,33 @@ class SettingsControllerImpl {
44
data: {}
45
};
46
47
- // Capture one consistent read-only snapshot; exclude sensitive credentials.
47
+ // Capture one consistent read-only snapshot. A company backup must not
48
+ // include records belonging to other companies or global user/log stores.
49
+ // User identities are summarized in backup.user; credentials never export.
50
const companyId = await DataScope.companyId();
51
const snapshot = await StorageService.exportAll();
52
+ backup.scope = 'company';
53
+ backup.companyId = companyId;
54
+ backup.schemaVersion = snapshot.version;
55
+
56
for (const store of STORES) {
57
const rows = snapshot.data[store] || [];
52
- backup.data[store] = COMPANY_BACKUP_STORES.includes(store)
53
- ? rows.filter(record => record.companyId === companyId)
54
- : rows;
58
+ if (COMPANY_BACKUP_STORES.includes(store)) {
59
+ backup.data[store] = rows.filter(record => record.companyId === companyId);
60
+ } else if (store === 'companies') {
61
+ backup.data[store] = rows.filter(record => record.id === companyId);
62
+ } else if (store === 'settings') {
63
+ // restoreCompanyBackup currently restores only this one company-owned
64
+ // finance marker. Do not expose global settings or other companies'
65
+ // invoice settings, preferences and activation metadata.
66
+ backup.data[store] = rows.filter(record =>
67
+ record.companyId === companyId &&
68
+ record.id === `finance_v2_enabled_${companyId}`);
69
+ } else {
70
+ // In particular: users and logs are not restored by the
71
+ // company-scoped restore service and must not leak into exports.
72
+ backup.data[store] = [];
73
+ }
74
}
75
76
return backup;
tests/accounting-phase1.test.mjs
+1
-1
@@ -218,7 +218,7 @@ test('integration: Settings backup contains V2 ledger and restores it atomically
218
await StorageService.put('users', { id: 'some-user', username: 'demo', pwHash: 'test-hash', pwSalt: 'test-salt' });
219
await StorageService.put('account_chart', { id: 'foreign-account', companyId: 'c2', code: '1', name: 'Foreign assets', type: 'asset', level: 'group' });
220
const backup = await SettingsController.exportBackup();
221
- assert.equal(backup.data.users[0].pwHash, undefined);
221
+ assert.deepEqual(backup.data.users, [], 'company exports never include global user accounts');
222
assert.equal((await StorageService.get('users', 'some-user')).pwHash, 'test-hash');
223
assert.equal(backup.data.account_chart.length, 19);
224
assert.equal(backup.data.fiscal_periods.length, 1);
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
+});