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

fix(A06): preserve local users' roles and company grants during snapshot restore

* fix(A06): preserve local authorization and enforce user limits during full backup import * test(A06): reject authorization escalation, duplicate usernames and excessive backup users

davoodmehraban89 committed Oct 16, 2026 at 17:43 UTC 57e2f89aae3908c29c41fe6c3795df7d17ac4df0
2 files changed +100 -4
src/core/StorageService.js
+32 -4
@@ -430,18 +430,46 @@ class StorageServiceImpl {
430 }
431
432 async _importUsers(tx, items) {
433 + const localUsers = await tx.getAll('users');
434 + const byId = new Map(localUsers.map(u => [u.id, u]));
435 + const imported = new Map();
436 for (const incoming of items) {
437 const user = stripCredentials(incoming);
438 if (!user.id) user.id = this.uid();
436 - const existing = await tx.get('users', user.id);
439 + const existing = byId.get(user.id);
440 if (existing) {
438 - CREDENTIAL_FIELDS.forEach(f => { if (existing[f] !== undefined) user[f] = existing[f]; });
439 - if (existing.mustSetPassword !== undefined) user.mustSetPassword = existing.mustSetPassword;
441 + // Passwords and access grants belong to the authenticated local
442 + // installation. A JSON backup cannot promote a user to admin or
443 + // assign another company's permissions.
444 + CREDENTIAL_FIELDS.forEach(f => {
445 + if (existing[f] !== undefined) user[f] = existing[f];
446 + });
447 + user.role = existing.role;
448 + user.companyIds = existing.companyIds;
449 + if (existing.mustSetPassword !== undefined) {
450 + user.mustSetPassword = existing.mustSetPassword;
451 + }
452 } else {
453 + user.role = 'user';
454 + user.companyIds = [];
455 user.mustSetPassword = true;
456 }
443 - await tx.putRaw('users', user);
457 + imported.set(user.id, user);
458 + }
459 + const final = new Map(byId);
460 + for (const [id, user] of imported) final.set(id, user);
461 + if (final.size > 4) {
462 + throw Error('فایل پشتیبان بیش از چهار کاربر برای این نصب ایجاد می‌کند');
463 + }
464 + const names = new Set();
465 + for (const user of final.values()) {
466 + const name = typeof user.username === 'string' ? user.username.trim() : '';
467 + if (!name || names.has(name)) {
468 + throw Error('نام کاربری تکراری یا نامعتبر در فایل پشتیبان');
469 + }
470 + names.add(name);
471 }
472 + for (const user of imported.values()) await tx.putRaw('users', user);
473 }
474
475 // ---------- شناسه یکتا ----------
tests/a06-backup-user-privileges.test.mjs new
+68
@@ -0,0 +1,68 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import 'fake-indexeddb/auto';
4 +import { StorageService } from '../src/core/StorageService.js';
5 +
6 +const backup = data => ({ app: 'Factor-easyPro', version: 7, data });
7 +
8 +test('A06: a full backup cannot promote a local staff member or alter company grants', async () => {
9 + await StorageService.clearAll();
10 + await StorageService.put('users', {
11 + id: 'staff', username: 'staff', role: 'user',
12 + companyIds: ['allowed-c1'], pwHash: 'LOCAL_HASH', pwSalt: 'LOCAL_SALT',
13 + mustSetPassword: false
14 + });
15 + await StorageService.importAll(backup({ users: [{
16 + id: 'staff', username: 'renamed-staff', role: 'admin',
17 + companyIds: ['private-c2'], pwHash: 'FORGED',
18 + pwSalt: 'FORGED', displayName: 'Updated name'
19 + }] }));
20 + const restored = await StorageService.get('users', 'staff');
21 + assert.equal(restored.role, 'user');
22 + assert.deepEqual(restored.companyIds, ['allowed-c1']);
23 + assert.equal(restored.pwHash, 'LOCAL_HASH');
24 + assert.equal(restored.pwSalt, 'LOCAL_SALT');
25 + assert.equal(restored.mustSetPassword, false);
26 + assert.equal(restored.username, 'renamed-staff');
27 +});
28 +
29 +test('A06: imported new admin role is always downgraded and requires password setup', async () => {
30 + await StorageService.clearAll();
31 + await StorageService.put('users', {
32 + id: 'local-admin', username: 'manager', role: 'admin',
33 + pwHash: 'KEEP', mustSetPassword: false
34 + });
35 + await StorageService.importAll(backup({ users: [{
36 + id: 'from-backup', username: 'foreign-admin', role: 'admin',
37 + companyIds: ['other-company'], pwHash: 'FAKE', mustSetPassword: false
38 + }] }));
39 + const local = await StorageService.get('users', 'local-admin');
40 + const imported = await StorageService.get('users', 'from-backup');
41 + assert.equal(local.role, 'admin');
42 + assert.equal(local.pwHash, 'KEEP');
43 + assert.equal(imported.role, 'user');
44 + assert.deepEqual(imported.companyIds, []);
45 + assert.equal(imported.pwHash, undefined);
46 + assert.equal(imported.mustSetPassword, true);
47 +});
48 +
49 +test('A06: duplicate names and >4 combined users abort the entire backup transaction', async () => {
50 + await StorageService.clearAll();
51 + await StorageService.put('users', {
52 + id: 'local-admin', username: 'manager', role: 'admin',
53 + pwHash: 'KEEP'
54 + });
55 + await assert.rejects(StorageService.importAll(backup({
56 + products: [{ id: 'rollback-product', companyId: 'c1', name: 'Undo' }],
57 + users: [{ id: 'another', username: 'manager', role: 'user' }]
58 + })), /تکراری/);
59 + assert.equal(await StorageService.get('users', 'another'), undefined);
60 + assert.equal(await StorageService.get('products', 'rollback-product'), undefined);
61 +
62 + await assert.rejects(StorageService.importAll(backup({
63 + users: ['one', 'two', 'three', 'four'].map((name, i) => ({
64 + id: 'import-' + i, username: name, role: 'user'
65 + }))
66 + })), /چهار کاربر/);
67 + assert.equal((await StorageService.getAll('users')).length, 1);
68 +});