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

fix(A03/A04): reject full-backup overwrites of audited events

* fix(A03): make full-snapshot restores append-only for audited financial history * test(A03): enforce immutable audit/correction history and atomic rollback on full restore

davoodmehraban89 committed Oct 16, 2026 at 16:36 UTC 5427703fc4863de88fc27b12c1cea7725176bef5
2 files changed +96
src/core/StorageService.js
+33
@@ -44,6 +44,22 @@ const INDEXES = {
44 // فیلدهای مربوط به رمز عبور کاربران؛ هرگز در فایل پشتیبان نمی‌روند
45 const CREDENTIAL_FIELDS = ['pass', 'pwHash', 'pwSalt', 'pwIter', 'pwAlgo'];
46
47 +const APPEND_ONLY_STORES = new Set(['audit_events', 'document_corrections']);
48 +
49 +// Compare snapshot metadata without depending on JSON property insertion order.
50 +function snapshotComparable(value) {
51 + if (Array.isArray(value)) return value.map(snapshotComparable);
52 + if (value && typeof value === 'object') {
53 + const normalized = {};
54 + for (const key of Object.keys(value).sort()) normalized[key] = snapshotComparable(value[key]);
55 + return normalized;
56 + }
57 + return value;
58 +}
59 +const sameSnapshotRecord = (left, right) =>
60 + JSON.stringify(snapshotComparable(left)) === JSON.stringify(snapshotComparable(right));
61 +
62 +
63 function stripCredentials(user) {
64 const copy = { ...user };
65 CREDENTIAL_FIELDS.forEach(f => delete copy[f]);
@@ -349,6 +365,23 @@ class StorageServiceImpl {
365 if (!items) continue;
366 if (store === 'users') {
367 await this._importUsers(tx, items);
368 + } else if (APPEND_ONLY_STORES.has(store)) {
369 + // Backup replay may re-add history but can never rewrite it.
370 + // A conflicting record aborts the WHOLE multi-store restore.
371 + for (const item of items) {
372 + if (!item.id || typeof item.id !== 'string' ||
373 + !item.companyId || typeof item.companyId !== 'string') {
374 + throw new Error('رکورد تاریخچه ممیزی یا اصلاح سند نامعتبر است');
375 + }
376 + const old = await tx.get(store, item.id);
377 + if (old) {
378 + if (!sameSnapshotRecord(old, item)) {
379 + throw new Error('بازنویسی سابقه ممیزی یا اصلاح سند از پشتیبان مجاز نیست');
380 + }
381 + } else {
382 + await tx.add(store, { ...item });
383 + }
384 + }
385 } else {
386 await Promise.all(items.map(item => tx.putRaw(store, item)));
387 }
tests/a03-append-only-full-restore.test.mjs new
+63
@@ -0,0 +1,63 @@
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 +
6 +test('A03: full restore refuses to mutate prior audit events and rolls back earlier writes', async () => {
7 + await StorageService.clearAll();
8 + const audit = {
9 + id: 'audit-history-1', companyId: 'c1',
10 + commandKey: 'c1:historic', eventType: 'journal_posted',
11 + actorId: 'manager', sourceId: 'journal-1',
12 + result: 'committed', safeSummary: { lineCount: 2 }
13 + };
14 + await StorageService.put('audit_events', { ...audit });
15 + const before = await StorageService.get('audit_events', audit.id);
16 + await assert.rejects(StorageService.importAll({
17 + app: 'Factor-easyPro', version: 7,
18 + data: {
19 + products: [{ id: 'new-product', companyId: 'c1', name: 'must roll back' }],
20 + audit_events: [{ ...before, eventType: 'period_closed' }]
21 + }
22 + }), /بازنویسی سابقه/);
23 + assert.equal(await StorageService.get('products', 'new-product'), undefined);
24 + assert.deepEqual(await StorageService.get('audit_events', audit.id), before);
25 +
26 + // Identical replay with shuffled object keys is safe and idempotent.
27 + const replay = { companyId: 'c1', id: before.id, result: before.result,
28 + safeSummary: { lineCount: 2 }, commandKey: before.commandKey,
29 + eventType: before.eventType, actorId: before.actorId, sourceId: before.sourceId,
30 + createdAt: before.createdAt, updatedAt: before.updatedAt };
31 + await StorageService.importAll({
32 + app: 'Factor-easyPro', version: 7, data: { audit_events: [replay] }
33 + });
34 + assert.equal((await StorageService.getAll('audit_events')).length, 1);
35 +});
36 +
37 +test('A03: full restore preserves document corrections and rejects ID overwrite', async () => {
38 + await StorageService.clearAll();
39 + const correction = {
40 + id: 'correction-1', companyId: 'c1',
41 + idempotencyKey: 'return:invoice-1', sourceId: 'invoice-1',
42 + correctionType: 'return', resultId: 'return-1'
43 + };
44 + await StorageService.put('document_corrections', { ...correction });
45 + const before = await StorageService.get('document_corrections', correction.id);
46 + await assert.rejects(StorageService.importAll({
47 + app: 'Factor-easyPro', version: 7,
48 + data: { document_corrections: [{ ...before, resultId: 'forged' }] }
49 + }), /بازنویسی سابقه/);
50 + assert.deepEqual(await StorageService.get('document_corrections', correction.id), before);
51 +});
52 +
53 +test('A03: legitimate novel audit records may be restored without destroying existing history', async () => {
54 + await StorageService.clearAll();
55 + const newer = { id: 'new-audit', companyId: 'c1', commandKey: 'c1:novel',
56 + eventType: 'journal_posted', actorId: 'manager', sourceId: 'j1' };
57 + await StorageService.importAll({
58 + app: 'Factor-easyPro', version: 7,
59 + data: { audit_events: [newer] }
60 + });
61 + assert.equal((await StorageService.getAll('audit_events')).length, 1);
62 + assert.equal((await StorageService.get('audit_events', newer.id)).sourceId, 'j1');
63 +});