فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A03): block transactional bulk-clear of audit history with native rollback proof
Protect normal IndexedDB StorageService transaction.clear on audit_events and document_corrections, including rollback of any prior writes in same transaction. Preserve intentional full-database maintenance via separate atomic clearAll. Require the 11th real native Windows NSIS/Tauri/WebView2 financial safety scenario. Verified same PR SHA: PR Quality #38959511822 (190 Node tests, Vite/audit + Windows NSIS), native installed Windows #38959512012 (11 financial scenarios, upgrade, backup, RTL PDF, offline routes, 60k benchmark, forced-process crash), Windows synthetic baseline #38959511988. Phase A accepted:false; no public release.
davoodmehraban89 committed
Oct 16, 2026 at 23:42 UTC
b16448b955d3c380e5c2d4f6df4b89970e6d589d
4 files changed
+104
-4
scripts/windows-vm-native-lab.ps1
+1
@@ -172,6 +172,7 @@ try {
172
'partial-sale-and-inventory',
173
'sale-return-immutable-stock-and-ledger',
174
'purchase-return-and-stock-guardrails',
175
+ 'append-only-history-bulk-clear-protection',
176
'backup-and-atomic-rejection',
177
'rtl-invoice-print-settings',
178
'fiscal-close-atomic-guardrails',
src/core/StorageService.js
+23
-4
@@ -251,7 +251,9 @@ class StorageServiceImpl {
251
delete: (s, id) => APPEND_ONLY_STORES.has(s)
252
? Promise.reject(new Error('حذف مستقیم سابقه ممیزی یا اصلاح سند مجاز نیست'))
253
: wrap(os(s).delete(id)).then(() => undefined),
254
- clear: (s) => wrap(os(s).clear()).then(() => undefined)
254
+ clear: (s) => APPEND_ONLY_STORES.has(s)
255
+ ? Promise.reject(new Error('پاکسازی مستقیم سابقه ممیزی یا اصلاح سند مجاز نیست'))
256
+ : wrap(os(s).clear()).then(() => undefined)
257
};
258
259
let result;
@@ -326,9 +328,26 @@ class StorageServiceImpl {
328
return this._exec(store, 'readonly', s => s.count());
329
}
330
329
- clearAll() {
330
- return this.transaction(STORES, async tx => {
331
- await Promise.all(STORES.map(name => tx.clear(name)));
331
+ // Explicit full-database reset/maintenance is intentionally separate from
332
+ // the ordinary transaction API. That API must never permit clearing
333
+ // append-only audit or correction history on its own.
334
+ // Keep the full reset atomic and wait for IDB transaction completion.
335
+ async clearAll() {
336
+ if (globalThis.FINORA?.License) globalThis.FINORA.License.assertWritable();
337
+ const db = await this.init();
338
+ return new Promise((resolve, reject) => {
339
+ let tx;
340
+ try {
341
+ tx = db.transaction(STORES, 'readwrite');
342
+ tx.oncomplete = () => resolve();
343
+ tx.onabort = () => reject(tx.error || new Error('بازنشانی کامل پایگاه داده لغو شد'));
344
+ for (const name of STORES) tx.objectStore(name).clear();
345
+ } catch (err) {
346
+ if (tx) {
347
+ try { tx.abort(); } catch (_) { /* Transaction may already be inactive. */ }
348
+ }
349
+ reject(err);
350
+ }
351
});
352
}
353
tests/a03-direct-append-only-protection.test.mjs
+37
@@ -47,3 +47,40 @@ test('A03: document correction history refuses replay writes and deletion', asyn
47
/حذف مستقیم/);
48
assert.deepEqual(await StorageService.get('document_corrections', correction.id), saved);
49
});
50
+
51
+test('A03: transaction clear cannot erase either append-only history store or commit earlier writes', async () => {
52
+ await StorageService.clearAll();
53
+ for (const store of ['audit_events', 'document_corrections']) {
54
+ const historical = {
55
+ id: 'retained-' + store,
56
+ companyId: 'c1',
57
+ ...(store === 'audit_events'
58
+ ? { commandKey: 'c1:retained-audit', eventType: 'journal_posted' }
59
+ : { idempotencyKey: 'c1:retained-correction', sourceId: 'sale-1' })
60
+ };
61
+ await StorageService.put(store, historical);
62
+ const original = structuredClone(await StorageService.get(store, historical.id));
63
+ const sentinel = 'must-rollback-' + store;
64
+ await assert.rejects(
65
+ StorageService.transaction(['contacts', store], async tx => {
66
+ await tx.putRaw('contacts', { id: sentinel, companyId: 'c1', name: 'Aborted write' });
67
+ await tx.clear(store);
68
+ }),
69
+ /پاکسازی مستقیم/
70
+ );
71
+ assert.equal(await StorageService.get('contacts', sentinel), undefined,
72
+ 'A rejected history clear must roll back the earlier contact write');
73
+ assert.deepEqual(await StorageService.get(store, historical.id), original,
74
+ 'Append-only history must survive the blocked bulk clear');
75
+ assert.equal((await StorageService.getAll(store)).length, 1);
76
+ }
77
+ // Clearing a regular non-immutable table remains an authorized transaction.
78
+ await StorageService.put('contacts', { id: 'temporary', companyId: 'c1', name: 'Disposable' });
79
+ await StorageService.transaction('contacts', async tx => tx.clear('contacts'));
80
+ assert.equal((await StorageService.getAll('contacts')).length, 0);
81
+ // Explicit maintenance reset retains the previous full-database semantics.
82
+ // It is NOT exposed as a normal transaction clear of immutable history.
83
+ await StorageService.clearAll();
84
+ assert.equal((await StorageService.getAll('audit_events')).length, 0);
85
+ assert.equal((await StorageService.getAll('document_corrections')).length, 0);
86
+});
tests/windows-vm-native-functional-matrix.cjs
+43
@@ -440,6 +440,48 @@ async function nativePurchaseReturnGuardrails() {
440
overReturnRejectedAtomically:true };
441
}
442
443
+async function nativeHistoryClearFailClosed() {
444
+ const check = (value, message) => { if (!value) throw Error(message); };
445
+ const store = FINORA.Storage;
446
+ const company = await FINORA.Company.current();
447
+ check(company?.id && FINORA.Auth.isAdmin(),
448
+ 'History protection must run in installed VM with synthetic administrator');
449
+ const preserved = {};
450
+ for (const name of ['audit_events', 'document_corrections']) {
451
+ const history = (await store.getAll(name))
452
+ .filter(row => row.companyId === company.id);
453
+ check(history.length > 0, 'Native immutable history prerequisite missing: ' + name);
454
+ preserved[name] = history.map(row => JSON.stringify(row)).sort();
455
+ const sentinel = 'vm-a03-aborted-clear-' + name;
456
+ let rejected = false;
457
+ try {
458
+ await store.transaction(['contacts', name], async tx => {
459
+ await tx.putRaw('contacts', {
460
+ id: sentinel, companyId: company.id,
461
+ name: 'VM ONLY must roll back before history clear'
462
+ });
463
+ await tx.clear(name);
464
+ });
465
+ } catch (error) {
466
+ rejected = String(error).includes('پاکسازی مستقیم');
467
+ }
468
+ check(rejected, 'Installed IndexedDB accepted a bulk history clear: ' + name);
469
+ check(!(await store.get('contacts', sentinel)),
470
+ 'Rejected native history-clear transaction committed earlier writes');
471
+ const after = (await store.getAll(name))
472
+ .filter(row => row.companyId === company.id)
473
+ .map(row => JSON.stringify(row)).sort();
474
+ check(JSON.stringify(after) === JSON.stringify(preserved[name]),
475
+ 'Failed clear mutated immutable history: ' + name);
476
+ }
477
+ check(!document.querySelector('.toast-error'),
478
+ 'Native history protection left unrelated UI error');
479
+ return { immutableAuditClearRejected: true,
480
+ immutableCorrectionClearRejected: true,
481
+ bothTransactionsRolledBack: true,
482
+ historyUnchanged: true };
483
+}
484
+
485
async function nativeBackupRestore() {
486
const check = (value, message) => { if (!value) throw Error(message); };
487
const S = FINORA.Storage;
@@ -657,6 +699,7 @@ module.exports = async function nativeFunctionalMatrix(cdp) {
699
['partial-sale-and-inventory', saleInvoice],
700
['sale-return-immutable-stock-and-ledger', nativeSaleReturnGuardrails],
701
['purchase-return-and-stock-guardrails', nativePurchaseReturnGuardrails],
702
+ ['append-only-history-bulk-clear-protection', nativeHistoryClearFailClosed],
703
['backup-and-atomic-rejection', nativeBackupRestore],
704
['rtl-invoice-print-settings', invoicePrintLayout],
705
['fiscal-close-atomic-guardrails', fiscalCloseGuardrails],