فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A03): require transaction-scoped authorization and validate restored audit metadata
davoodmehraban89 committed
Oct 19, 2026 at 18:42 UTC
82de09de2ac35fc6bda778d5dade3d0ce79142e1
1 file changed
+25
-3
src/core/AuditService.js
+25
-3
@@ -3,6 +3,10 @@ import { DataScope } from './DataScope.js';
3
import { StorageService } from './StorageService.js';
4
5
const trustedAuditRows = new WeakSet();
6
+const authorizedRestoreTransactions = new WeakMap();
7
+const validUtc = value => typeof value === 'string' &&
8
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(value) &&
9
+ !Number.isNaN(Date.parse(value));
10
export const isAuditServiceRow = row => !!row && trustedAuditRows.has(row);
11
12
const VALID_TYPES = new Set([
@@ -48,13 +52,31 @@ class AuditServiceImpl {
52
return event;
53
}
54
51
- // Historical restore is a distinct audited entry point; callers must
52
- // validate snapshot ownership and immutable conflicts before invoking it.
55
+ // Restore permission is scoped to a single transaction and company.
56
+ // Only validated restore flows should invoke this entry point.
57
+ authorizeRestore(tx, { companyId, rows }) {
58
+ if (!Auth.isAdmin() || !validId(companyId) || !Array.isArray(rows) ||
59
+ rows.some(row => !row || row.companyId !== companyId)) {
60
+ throw new Error('مجوز بازیابی ممیزی نامعتبر است');
61
+ }
62
+ authorizedRestoreTransactions.set(tx, { companyId, rows: new WeakSet(rows) });
63
+ }
64
+
65
async restoreHistorical(tx, row) {
66
+ const authorization = authorizedRestoreTransactions.get(tx);
67
+ if (!authorization || !authorization.rows.has(row) ||
68
+ authorization.companyId !== row?.companyId) {
69
+ throw new Error('بازیابی مستقیم ممیزی خارج از جریان مجاز است');
70
+ }
71
if (!row || typeof row !== 'object' || Array.isArray(row) ||
55
- !validId(row.id) || !validId(row.companyId)) {
72
+ !validId(row.id) || !validId(row.companyId) ||
73
+ !validId(row.actorId) || !VALID_TYPES.has(row.eventType) ||
74
+ !validId(row.commandKey) || !validId(row.sourceId) ||
75
+ (row.businessDate !== undefined && !validBusinessDate(row.businessDate)) ||
76
+ (row.occurredAtUtc !== undefined && !validUtc(row.occurredAtUtc))) {
77
throw new Error('رکورد تاریخچه ممیزی نامعتبر است');
78
}
79
+ authorization.rows.delete(row);
80
const copy = { ...row };
81
trustedAuditRows.add(copy);
82
await tx.add('audit_events', copy);