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

fix(A04): verify encrypted-backup checksum before company restore

* fix(A04): attach SHA-256 integrity manifest and verify before backup writes * test(A04): checked restore blocks tampered data while accepting supported legacy files

davoodmehraban89 committed Oct 14, 2026 at 23:47 UTC 277e7ff3eac95827d58c1156ac3445f3706b5898
2 files changed +55
src/controllers/SettingsController.js
+22
@@ -82,6 +82,13 @@ class SettingsControllerImpl {
82
83 downloadBackup() {
84 return this.exportBackup().then(async backup => {
85 + // SHA-256 detects accidental corruption in the restored data payload.
86 + // The AES-GCM encryption wrapper provides authenticated integrity of the
87 + // whole downloaded .fbackup file; checksum alone is NOT a digital signature.
88 + const digest = await crypto.subtle.digest('SHA-256',
89 + new TextEncoder().encode(JSON.stringify(backup.data)));
90 + backup.integrity = { algorithm: 'SHA-256',
91 + digest: Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, '0')).join('') };
92 const json = JSON.stringify(backup, null, 2);
93 const password = prompt('رمز محافظت از فایل پشتیبان (حداقل ۱۲ نویسه):');
94 if (!password || password.length < 12) throw new Error('رمز پشتیبان باید حداقل ۱۲ نویسه باشد');
@@ -158,6 +165,21 @@ class SettingsControllerImpl {
165 throw new Error('ساختار یا نسخه فایل پشتیبان شرکت نامعتبر است');
166 }
167
168 + if (backup.integrity !== undefined) {
169 + const integrity = backup.integrity;
170 + if (!integrity || integrity.algorithm !== 'SHA-256' ||
171 + typeof integrity.digest !== 'string' || !/^[0-9a-f]{64}$/.test(integrity.digest)) {
172 + throw new Error('اطلاعات کنترل سلامت فایل پشتیبان نامعتبر است');
173 + }
174 + const digest = await crypto.subtle.digest('SHA-256',
175 + new TextEncoder().encode(JSON.stringify(backup.data)));
176 + const actual = Array.from(new Uint8Array(digest),
177 + byte => byte.toString(16).padStart(2, '0')).join('');
178 + if (actual !== integrity.digest) {
179 + throw new Error('داده‌های پشتیبان تغییر کرده یا آسیب دیده‌اند؛ بازیابی متوقف شد');
180 + }
181 + }
182 +
183 const companyId = await DataScope.companyId();
184 // Prevent an accidental merge of one company's books into another.
185 // Explicit cross-company migration is a separate, intentional operation.
tests/backup-restore-integration.test.mjs
+33
@@ -136,3 +136,36 @@ test('A04: exported appVersion, package and desktop installer versions agree', a
136 assert.equal(desktop.version, appPackage.version);
137 assert.equal(backup.schemaVersion, (await StorageService.exportAll()).version);
138 });
139 +
140 +test('A04: checked backup restores successfully and rejects a corrupted payload before writes', async () => {
141 + await setup();
142 + const backup = await SettingsController.exportBackup();
143 + backup.data.contacts = [{ id: 'checksum-customer', companyId: 'c1', name: 'Original' }];
144 + const hashed = async data => {
145 + const bytes = await crypto.subtle.digest('SHA-256',
146 + new TextEncoder().encode(JSON.stringify(data)));
147 + return Array.from(new Uint8Array(bytes), x => x.toString(16).padStart(2, '0')).join('');
148 + };
149 + backup.integrity = { algorithm: 'SHA-256', digest: await hashed(backup.data) };
150 + await SettingsController.importBackup(backup);
151 + assert.equal((await StorageService.get('contacts', 'checksum-customer')).name, 'Original');
152 +
153 + await StorageService.delete('contacts', 'checksum-customer');
154 + const corrupted = structuredClone(backup);
155 + corrupted.data.contacts[0].name = 'Tampered';
156 + await assert.rejects(SettingsController.importBackup(corrupted), /تغییر کرده|آسیب/);
157 + assert.equal(await StorageService.get('contacts', 'checksum-customer'), undefined);
158 + await assert.rejects(SettingsController.importBackup({
159 + ...backup, integrity: { algorithm: 'MD5', digest: 'bad' }
160 + }), /نامعتبر/);
161 + assert.equal(await StorageService.get('contacts', 'checksum-customer'), undefined);
162 +});
163 +
164 +test('A04: older backup envelopes without optional checksum retain supported compatibility', async () => {
165 + await setup();
166 + const backup = await SettingsController.exportBackup();
167 + assert.equal(backup.integrity, undefined);
168 + backup.data.contacts = [{ id: 'legacy-contact', companyId: 'c1', name: 'Legacy' }];
169 + await SettingsController.importBackup(backup);
170 + assert.equal((await StorageService.get('contacts', 'legacy-contact')).name, 'Legacy');
171 +});