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

fix(A03): remove public audit restore authorization mint

davoodmehraban89 committed Oct 20, 2026 at 09:15 UTC f83b8e6c9dd55cd62ecf4a2441463af0e6048956
6 files changed +29 -13
STATUS.md
+2
@@ -80,3 +80,5 @@ Review CI on latest A03 PR head, fix failing fixtures and ensure restore authori
80 - Final approval and merge remain withheld until CI and repeat review.
81
82 - GitHub Actions run 39376283188 on SHA 894e1f54409a7f12455ee27b6e3d8b80f4899a55: completed FAILURE, npm test 262 total / 260 passed / 2 failed. Failures A02 rollback and A03 direct append-only protection; root cause: eager genesis-based normalization of a legacy-only snapshot intended to append after existing audit history. Changed legacy-only imports to defer chain index/hash to the active restore transaction. Mixed legacy+hashed chains remain prevalidated. Await rerun.
83 +
84 +- Security review found public AuditService.authorizeRestore permission mint. Replaced with scoped restoreFromBackup workflow, removed public mint and added negative regression test. CI pending; no pass claim for new code.
reviews/A03-agent2.md
+2
@@ -7,3 +7,5 @@ Findings: verifyChain(null) previously threw instead of returning an invalid res
7 Review method: sequential pass; no independent agent claim. No final approval while security and performance limitations remain.
8
9 CI review finding: eager legacy-only migration pre-hashes with chainIndex 0 and violates existing append sequence. Fixed by deferring legacy-only hashing to transactional restore. This is a correctness fix, not a relaxation of hash validation.
10 +
11 +Follow-up security finding: AuditService.authorizeRestore was public, allowing an admin caller to mint permission outside backup restore. Refactored into restoreFromBackup which performs ownership/admin checks and internally manages one-use transaction authorization. Direct restoreHistorical without that scoped context still rejects. Client-side malicious admin/raw IndexedDB remains outside security boundary. CI pending.
src/core/AuditService.js
+11 -4
@@ -59,16 +59,23 @@ class AuditServiceImpl {
59 return event;
60 }
61
62 - // Restore permission is scoped to a single transaction and company.
63 - // Only validated restore flows should invoke this entry point.
64 - authorizeRestore(tx, { companyId, rows }) {
62 + // Only the validated backup restoration entry point may mint a transient
63 + // transaction-scoped permission. Direct historical restoration remains denied.
64 + async restoreFromBackup(tx, { companyId, rows }) {
65 if (!Auth.isAdmin() || !validId(companyId) || !Array.isArray(rows) ||
66 - rows.some(row => !row || row.companyId !== companyId)) {
66 + rows.some(row => !row || typeof row !== 'object' || Array.isArray(row) ||
67 + row.companyId !== companyId)) {
68 throw new Error('مجوز بازیابی ممیزی نامعتبر است');
69 }
70 const permissions = authorizedRestoreTransactions.get(tx) || new Map();
71 + if (permissions.has(companyId)) throw new Error('بازیابی ممیزی هم‌زمان مجاز نیست');
72 permissions.set(companyId, new WeakSet(rows));
73 authorizedRestoreTransactions.set(tx, permissions);
74 + try {
75 + for (const row of rows) await this.restoreHistorical(tx, row);
76 + } finally {
77 + permissions.delete(companyId);
78 + }
79 }
80
81 async restoreHistorical(tx, row) {
src/core/CompanyBackupService.js
+1 -2
@@ -129,8 +129,7 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
129 if (existing) counts.updated++;
130 else counts.added++;
131 if (store === 'audit_events') {
132 - AuditService.authorizeRestore(tx, { companyId, rows: [row] });
133 - await AuditService.restoreHistorical(tx, row);
132 + await AuditService.restoreFromBackup(tx, { companyId, rows: [row] });
133 } else await tx.putRaw(store, row);
134 }
135 }
src/core/StorageService.js
+1 -7
@@ -469,12 +469,6 @@ class StorageServiceImpl {
469 }
470 }
471 await this.transaction(STORES, async tx => {
472 - const auditRows = auditRowsForValidation;
473 - for (const companyId of new Set(auditRows.map(row => row.companyId))) {
474 - AuditService.authorizeRestore(tx, {
475 - companyId, rows: auditRows.filter(row => row.companyId === companyId)
476 - });
477 - }
472 for (const store of STORES) {
473 const items = json.data[store];
474 if (!items) continue;
@@ -497,7 +491,7 @@ class StorageServiceImpl {
491 throw new Error('بازنویسی سابقه ممیزی یا اصلاح سند از پشتیبان مجاز نیست');
492 }
493 } else {
500 - if (store === 'audit_events') await AuditService.restoreHistorical(tx, item);
494 + if (store === 'audit_events') await AuditService.restoreFromBackup(tx, { companyId: item.companyId, rows: [item] });
495 else await tx.add(store, { ...item });
496 }
497 }
tests/a03-audit-integration.test.mjs
+12
@@ -345,3 +345,15 @@ test('A03: company import rejects corrupted JSON and incorrect hash anchor witho
345 await assert.rejects(SettingsController.importBackup(wrong), /لنگر پشتیبان|زنجیره ممیزی/);
346 assert.equal((await AuditService.list()).length, count);
347 });
348 +
349 +
350 +test('A03: restore authorization cannot be minted directly by an admin', async () => {
351 + await setup();
352 + assert.equal(typeof AuditService.authorizeRestore, 'undefined');
353 + await assert.rejects(StorageService.transaction('audit_events', tx =>
354 + AuditService.restoreHistorical(tx, {
355 + id: 'untrusted-restore', companyId: company, actorId: 'audit-admin',
356 + eventType: 'journal_posted', sourceId: 'untrusted', commandKey: 'untrusted'
357 + })), /بازیابی مستقیم ممیزی/);
358 + assert.equal(await StorageService.get('audit_events', 'untrusted-restore'), undefined);
359 +});