فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A03): defer legacy-only audit migration until transactional restore
davoodmehraban89 committed
Oct 20, 2026 at 09:13 UTC
1a4d24ecda9582357377395f1e880891176b209d
7 files changed
+24
-3
STATUS.md
+2
@@ -78,3 +78,5 @@ Review CI on latest A03 PR head, fix failing fixtures and ensure restore authori
78
- Security/quality pass: found malformed non-array input could throw from verifyChain; addressed with invalid result. Public authorizeRestore remains a client trust-boundary limitation, not a cryptographic authorization mechanism. O(n) audit-history read per append remains a scalability concern; no unverified performance claim.
79
- Edge-case pass: missing explicit tests for empty/single chains, mixed legacy+hashed history, corrupted JSON, wrong anchors and concurrent writes. Added regression tests; results pending CI.
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.
reviews/A03-agent1.md
+2
@@ -5,3 +5,5 @@ Scope: PR #116 and requirements, fresh read of AuditService.js, AuditChain.js, S
5
Evidence: GitHub Actions run 39374969382, SHA 95f6c976a04a72e2ddaf27f20773994e871e0250, completed success; Linux 257/257 and Windows 257/257.
6
7
Findings: four tamper types and legacy v6 migration covered. Missing explicit concurrency, malformed backup JSON and wrong-anchor rollback coverage. New regression tests added in this change; await CI. No final approval yet.
8
+
9
+Regression CI: run 39376283188 failed 2/262 on SHA 894e1f5. Root cause legacy-only backup import must not pre-hash at genesis when restoring into nonempty history. Fixed; approval withheld pending CI.
reviews/A03-agent2.md
+2
@@ -5,3 +5,5 @@ Fresh-read evidence: src/core/AuditChain.js lines 28-42 canonical serialization
5
Findings: verifyChain(null) previously threw instead of returning an invalid result; fixed with Array.isArray guard and regression test. SHA-256 known vector tested; eventHash excludes itself and includes prevHash. Head in same backup is NOT independently trusted; a malicious client can rewrite both data and anchor. Public authorizeRestore is callable by an admin client and cannot be treated as a strong security boundary. Audit append reads all company history, O(n) per append; scalability issue remains, requires indexed-head migration outside A03 scope.
6
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.
reviews/A03-agent3.md
+2
@@ -5,3 +5,5 @@ Fresh-read evidence: src/core/AuditChain.js lines 31-65; SettingsController.js l
5
Findings: no explicit tests for empty/single log, partial hash metadata, mixed legacy prefix and hashed suffix, corrupt JSON, wrong anchor, concurrent writes. Added tests and prepareAuditRows migration/validation helper. Await CI to establish behavior.
6
7
Review method: sequential pass, not an independent subagent.
8
+
9
+Regression finding: import of a legacy-only snapshot into an existing chain failed; covered by pre-existing A02 rollback and A03 append-only tests. Fixed; CI rerun pending.
src/controllers/SettingsController.js
+4
-1
@@ -188,7 +188,10 @@ class SettingsControllerImpl {
188
189
const history = backup.data.audit_events || [];
190
if (!Array.isArray(history)) throw new Error('سوابق ممیزی پشتیبان نامعتبر است');
191
- backup.data.audit_events = prepareAuditRows(history, backup.auditAnchor ?? null);
191
+ if (backup.auditAnchor !== undefined || history.some(row =>
192
+ row?.eventHash !== undefined || row?.chainIndex !== undefined || row?.prevHash !== undefined)) {
193
+ backup.data.audit_events = prepareAuditRows(history, backup.auditAnchor ?? null);
194
+ }
195
196
const companyId = await DataScope.companyId();
197
// Prevent an accidental merge of one company's books into another.
src/core/CompanyBackupService.js
+4
-1
@@ -54,7 +54,10 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
54
}
55
}
56
57
- const auditRows = prepareAuditRows(data.audit_events || []);
57
+ const sourceAudit = data.audit_events || [];
58
+ const auditRows = sourceAudit.some(row =>
59
+ row?.eventHash !== undefined || row?.chainIndex !== undefined || row?.prevHash !== undefined)
60
+ ? prepareAuditRows(sourceAudit) : sourceAudit;
61
const counts = { added: 0, updated: 0, skipped: 0 };
62
// Reads, ownership validation, foreign-key validation and writes share one IndexedDB transaction.
63
return StorageService.transaction([...COMPANY_BACKUP_STORES, 'settings'], async tx => {
src/core/StorageService.js
+8
-1
@@ -447,7 +447,14 @@ class StorageServiceImpl {
447
}
448
449
const incomingAudit = json.data.audit_events || [];
450
- const auditRowsForValidation = prepareAuditRows(incomingAudit);
450
+ // Legacy-only snapshots have no genesis chain; their rows must be linked
451
+ // to existing history inside the write transaction, not pre-hashed at index zero.
452
+ const needsChainValidation = json.auditAnchors !== undefined ||
453
+ incomingAudit.some(row => row?.eventHash !== undefined || row?.chainIndex !== undefined || row?.prevHash !== undefined);
454
+ const auditRowsForValidation = needsChainValidation
455
+ ? [...new Set(incomingAudit.map(row => row?.companyId))].flatMap(companyId =>
456
+ prepareAuditRows(incomingAudit.filter(row => row?.companyId === companyId)))
457
+ : incomingAudit;
458
if (json.auditAnchors !== undefined) {
459
if (!json.auditAnchors || typeof json.auditAnchors !== 'object' || Array.isArray(json.auditAnchors))
460
throw new Error('لنگر پشتیبان نامعتبر است');