فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A04): attach SHA-256 integrity manifest and verify before backup writes
davoodmehraban89 committed
Oct 14, 2026 at 23:37 UTC
7d59f0320065b05fd89d934c635d14e7543e454f
1 file changed
+22
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.