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

fix(A04): validate company backup envelope and prevent implicit cross-company restore

davoodmehraban89 committed Oct 14, 2026 at 22:21 UTC afb7c4ab12d136ae54a74e8511adada692fec0d8
1 file changed +19 -3
src/controllers/SettingsController.js
+19 -3
@@ -141,11 +141,27 @@ class SettingsControllerImpl {
141 backup = JSON.parse(new TextDecoder().decode(clear));
142 } catch (_) { throw new Error('رمز نادرست است یا فایل پشتیبان آسیب دیده است'); }
143 }
144 - if (!backup || !backup.data) {
145 - throw new Error('ساختار فایل بکاپ نامعتبر است');
144 + // Never send an unauthenticated, foreign-app or future-schema envelope to
145 + // the financial restore transaction. The encrypted file is authenticated
146 + // by AES-GCM; legacy plaintext JSON still requires structural validation.
147 + if (!backup || typeof backup !== 'object' || Array.isArray(backup) ||
148 + !['Factor-easyPro', 'Finora Pro'].includes(backup.app) ||
149 + backup.scope !== 'company' ||
150 + !Number.isSafeInteger(backup.schemaVersion) ||
151 + backup.schemaVersion < 1 || backup.schemaVersion > 6 ||
152 + typeof backup.companyId !== 'string' || !backup.companyId.trim() ||
153 + !backup.data || typeof backup.data !== 'object' || Array.isArray(backup.data)) {
154 + throw new Error('ساختار یا نسخه فایل پشتیبان شرکت نامعتبر است');
155 }
156
148 - return restoreCompanyBackup(backup.data, { companyId: await DataScope.companyId(), userId: user.id, mode: options.mode || 'merge' });
157 + const companyId = await DataScope.companyId();
158 + // Prevent an accidental merge of one company's books into another.
159 + // Explicit cross-company migration is a separate, intentional operation.
160 + if (backup.companyId !== companyId && options.allowCrossCompany !== true) {
161 + throw new Error('پشتیبان متعلق به شرکت دیگری است؛ بازیابی در شرکت فعلی متوقف شد');
162 + }
163 +
164 + return restoreCompanyBackup(backup.data, { companyId, userId: user.id, mode: options.mode || 'merge' });
165 }
166
167 async readFileAsText(file) {