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

fix(A04): preserve immutable audit replay across JSON key reorder

* fix(A04): compare immutable company restore history independently of property order * test(A04): accept shuffled audited JSON fields, reject changed facts

davoodmehraban89 committed Oct 16, 2026 at 16:55 UTC f75a57f781e70d67d06b1960cfd13910b0ef53e6
2 files changed +35 -1
src/core/CompanyBackupService.js
+14 -1
@@ -16,6 +16,19 @@ export const COMPANY_BACKUP_STORES = Object.freeze([
16 const V2_STORES = new Set(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines']);
17 const isId = id => typeof id === 'string' && id.trim().length > 0;
18
19 +// Backups serialize object fields in arbitrary insertion order. Immutable
20 +// financial history equality must compare values, never field order.
21 +const stableRecord = value => {
22 + if (Array.isArray(value)) return value.map(stableRecord);
23 + if (value && typeof value === 'object') {
24 + return Object.fromEntries(Object.keys(value).sort()
25 + .map(key => [key, stableRecord(value[key])]));
26 + }
27 + return value;
28 +};
29 +const sameImmutableRecord = (a, b) =>
30 + JSON.stringify(stableRecord(a)) === JSON.stringify(stableRecord(b));
31 +
32 export async function restoreCompanyBackup(data, { companyId, userId, mode = 'merge' }) {
33 if (!Auth.isAdmin())throw Error('بازنشانی یا بازیابی اطلاعات حسابداری فقط با مدیر مجاز است');
34 if (!companyId || !userId || !['merge', 'replace'].includes(mode)) throw new Error('گزینه‌های بازیابی نامعتبر است');
@@ -55,7 +68,7 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
68 throw new Error(`شناسه رکورد ${store}/${row.id} متعلق به شرکت دیگر است؛ بازیابی متوقف شد`);
69 }
70 if (existing && immutable) {
58 - if (JSON.stringify(existing) !== JSON.stringify(row)) {
71 + if (!sameImmutableRecord(existing, row)) {
72 throw new Error('بازنویسی سابقه ممیزی یا اصلاح سند مجاز نیست');
73 }
74 counts.skipped++;
tests/a03-audit-integration.test.mjs
+21
@@ -161,3 +161,24 @@ test('A03/A04: successful backup restore receives an audit event; rejected resto
161 await assert.rejects(SettingsController.importBackup(tampered, { mode: 'replace' }), /بازنویسی سابقه/);
162 assert.equal((await AuditService.list()).length, afterRestore.length);
163 });
164 +
165 +test('A04: immutable company audit replay ignores JSON property ordering but rejects value changes', async () => {
166 + await setup();
167 + const backup = await SettingsController.exportBackup();
168 + const original = backup.data.audit_events[0];
169 + assert.ok(original, 'a period-opened audit record must exist');
170 + const reordered = structuredClone(backup);
171 + reordered.data.audit_events[0] = Object.fromEntries(
172 + Object.entries(reordered.data.audit_events[0]).reverse()
173 + );
174 + // Key order changes do not alter a signed accounting fact.
175 + const result = await SettingsController.importBackup(reordered);
176 + assert.ok(result.skipped >= 1);
177 + assert.deepEqual(await StorageService.get('audit_events', original.id), original);
178 +
179 + const changed = structuredClone(reordered);
180 + changed.data.audit_events[0].sourceId = 'FORGED_PERIOD';
181 + await assert.rejects(SettingsController.importBackup(changed, { mode: 'replace' }),
182 + /بازنویسی سابقه/);
183 + assert.equal((await StorageService.get('audit_events', original.id)).sourceId, original.sourceId);
184 +});