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

fix(A04): reject malformed backup record IDs before restore (#30)

* fix(backup): reject empty, fractional and unsafe record IDs before restore * test(backup): reject malformed IDs before restore * fix(backup): combine schema-version and record-ID validation * test(backup): add schema version regression coverage to combined fix * fix(backup): reject array-shaped backup envelopes * test(backup): reject malformed array payloads * fix(backup): reject duplicate primary keys before restore * test(backup): prevent silent duplicate ID overwrite

davoodmehraban89 committed Oct 14, 2026 at 11:33 UTC ac4a105917c525950de28e0b61403c77b9a3ab0b
3 files changed +75 -3
src/core/StorageService.js
+14 -3
@@ -290,13 +290,17 @@ class StorageServiceImpl {
290 * می‌دارند و کاربران جدید باید در اولین ورود رمز تعیین کنند.
291 */
292 async importAll(json) {
293 - if (!json || typeof json !== 'object' || !json.data || typeof json.data !== 'object') {
293 + if (!json || typeof json !== 'object' || Array.isArray(json) ||
294 + !json.data || typeof json.data !== 'object' || Array.isArray(json.data)) {
295 throw new Error('فرمت فایل نامعتبر است');
296 }
297 if (json.app && !['Finora Pro', 'Factor-easyPro'].includes(json.app)) {
298 throw new Error('این فایل پشتیبان فینورا پرو نیست');
299 }
299 - if (typeof json.version === 'number' && json.version > DB_VERSION) {
300 + if (!Number.isSafeInteger(json.version) || json.version < 1) {
301 + throw new Error('نسخه فایل پشتیبان نامعتبر است');
302 + }
303 + if (json.version > DB_VERSION) {
304 throw new Error('این پشتیبان مربوط به نسخه جدیدتری از برنامه است');
305 }
306
@@ -305,10 +309,17 @@ class StorageServiceImpl {
309 const items = json.data[store];
310 if (items === undefined) continue;
311 if (!Array.isArray(items)) throw new Error(`بخش «${store}» در فایل پشتیبان نامعتبر است`);
312 + const seenIds = new Set();
313 for (const item of items) {
314 const isObject = item && typeof item === 'object' && !Array.isArray(item);
310 - const idOk = isObject && (item.id === undefined || typeof item.id === 'string' || typeof item.id === 'number');
315 + const idOk = isObject && (item.id === undefined ||
316 + (typeof item.id === 'string' && item.id.trim().length > 0) ||
317 + (typeof item.id === 'number' && Number.isSafeInteger(item.id) && item.id > 0));
318 if (!idOk) throw new Error(`رکورد نامعتبر در بخش «${store}»`);
319 + if (item.id !== undefined) {
320 + if (seenIds.has(item.id)) throw new Error(`شناسه تکراری در بخش «${store}»`);
321 + seenIds.add(item.id);
322 + }
323 }
324 }
325
tests/backup-record-id-validation.test.mjs new
+38
@@ -0,0 +1,38 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { StorageService } from '../src/core/StorageService.js';
4 +
5 +test('backup import rejects malformed record IDs before any IndexedDB write', async () => {
6 + for (const id of ['', ' ', 0, -2, 1.5, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1]) {
7 + await assert.rejects(
8 + StorageService.importAll({
9 + app: 'Factor-easyPro',
10 + version: 6,
11 + data: { products: [{ id }] }
12 + }),
13 + /رکورد نامعتبر/
14 + );
15 + }
16 +});
17 +
18 +test('backup import rejects array-valued records', async () => {
19 + await assert.rejects(
20 + StorageService.importAll({
21 + app: 'Factor-easyPro',
22 + version: 6,
23 + data: { products: [[]] }
24 + }),
25 + /رکورد نامعتبر/
26 + );
27 +});
28 +
29 +test('backup import rejects duplicate IDs without opening the database', async () => {
30 + await assert.rejects(
31 + StorageService.importAll({
32 + app: 'Factor-easyPro',
33 + version: 6,
34 + data: { products: [{ id: 'same' }, { id: 'same' }] }
35 + }),
36 + /شناسه تکراری/
37 + );
38 +});
tests/backup-version-validation.test.mjs new
+23
@@ -0,0 +1,23 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { StorageService } from '../src/core/StorageService.js';
4 +
5 +test('backup import rejects invalid schema versions before opening the database', async () => {
6 + for (const version of [undefined, null, '6', 0, -1, 1.5, NaN, Infinity, 999]) {
7 + await assert.rejects(
8 + StorageService.importAll({ app: 'Factor-easyPro', version, data: {} }),
9 + /نسخه فایل پشتیبان نامعتبر است|نسخه جدیدتری/
10 + );
11 + }
12 +});
13 +
14 +test('backup import rejects an unrelated application before database access', async () => {
15 + await assert.rejects(
16 + StorageService.importAll({ app: 'unrelated', version: 6, data: {} }),
17 + /فینورا پرو/
18 + );
19 +});
20 +
21 +test('backup import rejects array-shaped payloads', async () => {
22 + await assert.rejects(StorageService.importAll({ app: 'Factor-easyPro', version: 6, data: [] }), /فرمت فایل نامعتبر/);
23 +});