فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A03): scope audit and corrections by company
* fix(A03): treat audit and immutable correction stores as company-scoped * test(A03): prevent cross-company audit/correction leaks in generic DataScope
davoodmehraban89 committed
Oct 16, 2026 at 16:51 UTC
5464284bebc0d6b389bb2c2c8ace73e87be16b41
2 files changed
+38
-1
src/core/DataScope.js
+1
-1
@@ -1,7 +1,7 @@
1
import { CompanyService } from './CompanyService.js';
2
import { StorageService } from './StorageService.js';
3
4
-const COMPANY_SCOPED = new Set(['contacts','products','units','categories','treasury','invoices','invoice_items','stock_movements','transactions','cheques','expenses','account_chart','fiscal_periods','journal_entries','journal_lines','warehouses','inventory_balances','stocktakes','stocktake_lines','settlements','account_mappings']);
4
+const COMPANY_SCOPED = new Set(['contacts','products','units','categories','treasury','invoices','invoice_items','stock_movements','transactions','cheques','expenses','account_chart','fiscal_periods','journal_entries','journal_lines','warehouses','inventory_balances','stocktakes','stocktake_lines','settlements','account_mappings','audit_events','document_corrections']);
5
6
class DataScopeImpl {
7
async companyId(){ const c=await CompanyService.current(); if(!c) throw new Error('شرکت فعالی انتخاب نشده است'); return c.id; }
tests/a03-company-scope-immutable-stores.test.mjs
new
+37
@@ -0,0 +1,37 @@
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 { DataScope } from '../src/core/DataScope.js';
6
+import { Auth } from '../src/core/Auth.js';
7
+import { CompanyService } from '../src/core/CompanyService.js';
8
+
9
+test('A03: common scoped reads do not leak another company audit history or corrections', async () => {
10
+ await StorageService.clearAll();
11
+ Auth.currentUser = { id: 'staff-one', username: 'one', role: 'user', companyIds: ['c1'] };
12
+ await StorageService.put('companies', { id: 'c1', name: 'Company One' });
13
+ await StorageService.put('companies', { id: 'c2', name: 'Company Two' });
14
+ CompanyService.activeCompanyId = 'c1';
15
+
16
+ await StorageService.put('audit_events', {
17
+ id: 'event-c1', companyId: 'c1', commandKey: 'c1:test', eventType: 'journal_posted'
18
+ });
19
+ await StorageService.put('audit_events', {
20
+ id: 'event-c2', companyId: 'c2', commandKey: 'c2:test', eventType: 'journal_posted'
21
+ });
22
+ await StorageService.put('document_corrections', {
23
+ id: 'cor-c1', companyId: 'c1', idempotencyKey: 'c1:cor'
24
+ });
25
+ await StorageService.put('document_corrections', {
26
+ id: 'cor-c2', companyId: 'c2', idempotencyKey: 'c2:cor'
27
+ });
28
+ assert.deepEqual((await DataScope.list('audit_events')).map(row => row.id), ['event-c1']);
29
+ assert.deepEqual((await DataScope.list('document_corrections')).map(row => row.id), ['cor-c1']);
30
+ assert.equal((await DataScope.stamp('audit_events', { eventType: 'journal_posted' })).companyId, 'c1');
31
+ assert.equal((await DataScope.stamp('document_corrections', { sourceId: 'x' })).companyId, 'c1');
32
+
33
+ CompanyService.activeCompanyId = 'c2';
34
+ // Even if a tampered UI points at another company, allowedFor returns only c1.
35
+ assert.deepEqual((await DataScope.list('audit_events')).map(row => row.id), ['event-c1']);
36
+ assert.deepEqual((await DataScope.list('document_corrections')).map(row => row.id), ['cor-c1']);
37
+});