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

fix(A06): fail closed for corrupted trial and offline license timestamps

Stop existing malformed persisted trial state from silently resetting to a new free trial, and reject invalid/future trial and offline lease dates/counters without deleting customer data. New installations still initialize a normal trial. Verified: 187 Node tests and Windows package #38949288384; real NSIS installed upgrade and WebView2 #38949288708. Production issuer public key still requires explicit provisioning; A06 not accepted.

davoodmehraban89 committed Oct 16, 2026 at 22:18 UTC d1d0ec7405779bc81749e2a9c6d9b3cd4c3d2356
3 files changed +90 -17
src/core/LicensePolicy.mjs
+47 -11
@@ -1,13 +1,49 @@
1 -export const TRIAL_DAYS=15;
2 -export const TRIAL_INVOICE_LIMIT=20;
3 -export const OFFLINE_LEASE_DAYS=30;
4 -export const WARNING_DAY=25;
5 -export function trialStatus(state,nowMs=Date.now()){
6 - const started=new Date(state?.trialStartedAt||nowMs),elapsedDays=Math.floor((nowMs-started.getTime())/86400000),invoiceCount=Number(state?.issuedInvoiceCount)||0;
7 - return{mode:'trial',elapsedDays,invoiceCount,daysRemaining:Math.max(0,TRIAL_DAYS-elapsedDays),invoicesRemaining:Math.max(0,TRIAL_INVOICE_LIMIT-invoiceCount),readOnly:elapsedDays>=TRIAL_DAYS||invoiceCount>=TRIAL_INVOICE_LIMIT,maxCompanies:5,maxUsers:4,maxDevices:1};
1 +export const TRIAL_DAYS = 15;
2 +export const TRIAL_INVOICE_LIMIT = 20;
3 +export const OFFLINE_LEASE_DAYS = 30;
4 +export const WARNING_DAY = 25;
5 +const FUTURE_CLOCK_TOLERANCE_MS = 5 * 60 * 1000;
6 +
7 +// A corrupt or missing persisted trial date/counter must never create a new
8 +// writable 15-day trial. Newly installed clients receive a valid date/counter
9 +// during LicenseService.init() before status() is used.
10 +export function trialStatus(state, nowMs = Date.now()) {
11 + const started = typeof state?.trialStartedAt === 'string'
12 + ? Date.parse(state.trialStartedAt) : NaN;
13 + const count = state?.issuedInvoiceCount;
14 + const validClock = Number.isFinite(nowMs);
15 + const validStart = validClock && Number.isFinite(started) &&
16 + started <= nowMs + FUTURE_CLOCK_TOLERANCE_MS;
17 + const validCount = Number.isSafeInteger(count) && count >= 0;
18 + const elapsedDays = validStart
19 + ? Math.max(0, Math.floor((nowMs - started) / 86400000)) : TRIAL_DAYS;
20 + const invoiceCount = validCount ? count : TRIAL_INVOICE_LIMIT;
21 + const readOnly = !validStart || !validCount ||
22 + elapsedDays >= TRIAL_DAYS || invoiceCount >= TRIAL_INVOICE_LIMIT;
23 + return {
24 + mode: 'trial', elapsedDays, invoiceCount,
25 + daysRemaining: readOnly ? 0 : Math.max(0, TRIAL_DAYS - elapsedDays),
26 + invoicesRemaining: readOnly ? 0 : Math.max(0, TRIAL_INVOICE_LIMIT - invoiceCount),
27 + readOnly, maxCompanies: 5, maxUsers: 4, maxDevices: 1
28 + };
29 }
9 -export function licensedStatus(license,nowMs=Date.now()){
10 - const checked=license?.lastOnlineCheckAt?new Date(license.lastOnlineCheckAt):null,ageDays=checked?(nowMs-checked.getTime())/86400000:Infinity;
11 - const expired=!license?.perpetual&&license?.expiresAt&&nowMs>new Date(license.expiresAt).getTime();
12 - return{mode:'licensed',readOnly:!!expired||ageDays>OFFLINE_LEASE_DAYS,warning:ageDays>=WARNING_DAY,maxCompanies:Math.min(5,Number(license?.maxCompanies)||1),maxUsers:Math.min(4,Number(license?.maxUsers)||1),maxDevices:Number(license?.maxDevices)||1};
30 +
31 +export function licensedStatus(license, nowMs = Date.now()) {
32 + const checked = typeof license?.lastOnlineCheckAt === 'string'
33 + ? Date.parse(license.lastOnlineCheckAt) : NaN;
34 + const validLease = Number.isFinite(nowMs) && Number.isFinite(checked) &&
35 + checked <= nowMs + FUTURE_CLOCK_TOLERANCE_MS;
36 + const ageDays = validLease ? Math.max(0, (nowMs - checked) / 86400000) : Infinity;
37 + const expiry = typeof license?.expiresAt === 'string'
38 + ? Date.parse(license.expiresAt) : NaN;
39 + const expired = license?.perpetual !== true &&
40 + (!Number.isFinite(expiry) || nowMs > expiry);
41 + return {
42 + mode: 'licensed',
43 + readOnly: !validLease || expired || ageDays > OFFLINE_LEASE_DAYS,
44 + warning: ageDays >= WARNING_DAY,
45 + maxCompanies: Math.min(5, Number(license?.maxCompanies) || 1),
46 + maxUsers: Math.min(4, Number(license?.maxUsers) || 1),
47 + maxDevices: Number(license?.maxDevices) || 1
48 + };
49 }
src/core/LicenseService.js
+12 -6
@@ -20,18 +20,24 @@ class LicenseServiceImpl {
20
21 async init() {
22 let stored = await SecureState.get(KEY);
23 - if (!stored) {
24 - try { stored = JSON.parse(localStorage.getItem(LEGACY_KEY) || 'null'); }
25 - catch (_) { /* Invalid legacy state is not an entitlement. */ }
23 + let hadStoredState = stored !== null && stored !== undefined;
24 + if (!hadStoredState) {
25 + const legacyRaw = localStorage.getItem(LEGACY_KEY);
26 + if (legacyRaw !== null) {
27 + hadStoredState = true;
28 + try { stored = JSON.parse(legacyRaw); }
29 + catch (_) { stored = null; /* Invalid legacy state must fail closed. */ }
30 + }
31 }
32 stored = stored && typeof stored === 'object' && !Array.isArray(stored) ? stored : {};
33 const now = Date.now();
29 - if (!stored.trialStartedAt || !Number.isFinite(Date.parse(stored.trialStartedAt))) {
34 + if (!hadStoredState) {
35 + // Only a truly new installation receives an initial free trial.
36 stored.trialStartedAt = new Date(now).toISOString();
31 - }
32 - if (!Number.isSafeInteger(stored.issuedInvoiceCount) || stored.issuedInvoiceCount < 0) {
37 stored.issuedInvoiceCount = 0;
38 }
39 + // Never rewrite malformed persisted dates/counters into a fresh trial.
40 + // trialStatus() leaves financial data readable but rejects further writes.
41 if (!stored.installationId || typeof stored.installationId !== 'string') {
42 stored.installationId = crypto.randomUUID();
43 }
tests/license-policy.test.mjs
+31
@@ -5,3 +5,34 @@ test('trial expires at 20 issued invoices',()=>assert.equal(trialStatus({trialSt
5 test('trial remains writable before both limits',()=>assert.equal(trialStatus({trialStartedAt:new Date(now-14*DAY).toISOString(),issuedInvoiceCount:19},now).readOnly,false));
6 test('licensed offline lease warns at day 25',()=>assert.equal(licensedStatus({active:true,perpetual:true,lastOnlineCheckAt:new Date(now-25*DAY).toISOString()},now).warning,true));
7 test('licensed offline lease is read only after day 30',()=>assert.equal(licensedStatus({active:true,perpetual:true,lastOnlineCheckAt:new Date(now-31*DAY).toISOString()},now).readOnly,true));
8 +
9 +test('A06: malformed persisted trial dates, counters and future dates fail closed without NaN UI', () => {
10 + const samples = [
11 + {}, { trialStartedAt: 'garbage', issuedInvoiceCount: 0 },
12 + { trialStartedAt: new Date(now).toISOString(), issuedInvoiceCount: -1 },
13 + { trialStartedAt: new Date(now).toISOString(), issuedInvoiceCount: '0' },
14 + { trialStartedAt: new Date(now + DAY).toISOString(), issuedInvoiceCount: 0 }
15 + ];
16 + for (const state of samples) {
17 + const status = trialStatus(state, now);
18 + assert.equal(status.readOnly, true);
19 + assert.equal(status.daysRemaining, 0);
20 + assert.equal(status.invoicesRemaining, 0);
21 + assert.ok(Number.isFinite(status.elapsedDays));
22 + }
23 +});
24 +test('A06: malformed or future lease and missing expiry cannot grant signed-license writes', () => {
25 + const values = [
26 + { perpetual: true, lastOnlineCheckAt: 'invalid' },
27 + { perpetual: true, lastOnlineCheckAt: new Date(now + DAY).toISOString() },
28 + { perpetual: false, lastOnlineCheckAt: new Date(now).toISOString() },
29 + { perpetual: false, expiresAt: 'invalid', lastOnlineCheckAt: new Date(now).toISOString() }
30 + ];
31 + for (const state of values) {
32 + assert.equal(licensedStatus(state, now).readOnly, true);
33 + }
34 + assert.equal(licensedStatus({
35 + perpetual: false, expiresAt: new Date(now + DAY).toISOString(),
36 + lastOnlineCheckAt: new Date(now).toISOString()
37 + }, now).readOnly, false);
38 +});