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

fix(A06): reject malformed entitlement policy payloads

* fix(A06): reject malformed or non-expiring signed entitlement payloads * test(A06): cover expiry, activation dates, device binding and seat limits

davoodmehraban89 committed Oct 14, 2026 at 22:34 UTC dcf5f67362f43dff9c4e476e3ec52477d6788c01
2 files changed +58 -4
src/core/EntitlementVerifier.mjs
+15 -4
@@ -6,9 +6,20 @@ export async function verifyEntitlement(token,publicJwk){
6 try{const key=await crypto.subtle.importKey('jwk',publicJwk,{name:'ECDSA',namedCurve:'P-256'},false,['verify']);return await crypto.subtle.verify({name:'ECDSA',hash:'SHA-256'},key,b64uToBytes(token.signature),enc.encode(canonical(token.payload)))}catch{return false}
7 }
8 export function validateEntitlementPayload(p,installationId,now=Date.now()){
9 - if(!p||p.product!=='Factor-easyPro'||p.installationId!==installationId)return false;
10 - if(p.notBefore&&now<new Date(p.notBefore).getTime())return false;
11 - if(!p.perpetual&&p.expiresAt&&now>new Date(p.expiresAt).getTime())return false;
12 - return Number(p.maxCompanies)>=1&&Number(p.maxCompanies)<=5&&Number(p.maxUsers)>=1&&Number(p.maxUsers)<=4&&Number(p.maxDevices)>=1;
9 + if(!p||p.product!=='Factor-easyPro'||typeof installationId!=='string'||!installationId||
10 + p.installationId!==installationId||!Number.isFinite(now))return false;
11 + const parseRequiredDate=(value)=>typeof value==='string'&&value.trim()!==''?Date.parse(value):NaN;
12 + if(p.notBefore!==undefined&&p.notBefore!==null){
13 + const start=parseRequiredDate(p.notBefore);
14 + if(!Number.isFinite(start)||now<start)return false;
15 + }
16 + if(p.perpetual!==true){
17 + const expiry=parseRequiredDate(p.expiresAt);
18 + // A non-perpetual entitlement without a real expiration is not a license.
19 + if(!Number.isFinite(expiry)||now>expiry)return false;
20 + }
21 + return Number.isSafeInteger(p.maxCompanies)&&p.maxCompanies>=1&&p.maxCompanies<=5&&
22 + Number.isSafeInteger(p.maxUsers)&&p.maxUsers>=1&&p.maxUsers<=4&&
23 + Number.isSafeInteger(p.maxDevices)&&p.maxDevices>=1;
24 }
25 export {canonical};
tests/entitlement-payload-validation.test.mjs new
+43
@@ -0,0 +1,43 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { validateEntitlementPayload } from '../src/core/EntitlementVerifier.mjs';
4 +
5 +const NOW = Date.UTC(2026, 9, 14);
6 +const VALID = {
7 + product: 'Factor-easyPro', installationId: 'device-1',
8 + maxCompanies: 5, maxUsers: 4, maxDevices: 1,
9 + notBefore: '2026-01-01T00:00:00Z',
10 + expiresAt: '2027-01-01T00:00:00Z',
11 + perpetual: false
12 +};
13 +
14 +test('A06: valid device-bound timed or perpetual entitlement policy can pass', () => {
15 + assert.equal(validateEntitlementPayload(VALID, 'device-1', NOW), true);
16 + assert.equal(validateEntitlementPayload({ ...VALID, perpetual: true, expiresAt: undefined }, 'device-1', NOW), true);
17 +});
18 +
19 +test('A06: malformed expiration cannot create an accidental perpetual license', () => {
20 + for (const expiresAt of [undefined, null, '', 'not-a-date', '2025-01-01T00:00:00Z']) {
21 + assert.equal(validateEntitlementPayload({ ...VALID, expiresAt }, 'device-1', NOW), false);
22 + }
23 +});
24 +
25 +test('A06: invalid notBefore, wrong installation and product are rejected', () => {
26 + for (const notBefore of ['bad-date', '', '2027-01-02T00:00:00Z']) {
27 + assert.equal(validateEntitlementPayload({ ...VALID, notBefore }, 'device-1', NOW), false);
28 + }
29 + assert.equal(validateEntitlementPayload(VALID, 'another-installation', NOW), false);
30 + assert.equal(validateEntitlementPayload({ ...VALID, product: 'other' }, 'device-1', NOW), false);
31 +});
32 +
33 +test('A06: license seat limits must be bounded integer quantities', () => {
34 + for (const value of [0, 6, '5', 1.5, Infinity, NaN]) {
35 + assert.equal(validateEntitlementPayload({ ...VALID, maxCompanies: value }, 'device-1', NOW), false);
36 + }
37 + for (const value of [0, 5, '4', 1.5]) {
38 + assert.equal(validateEntitlementPayload({ ...VALID, maxUsers: value }, 'device-1', NOW), false);
39 + }
40 + for (const value of [0, '1', 1.5]) {
41 + assert.equal(validateEntitlementPayload({ ...VALID, maxDevices: value }, 'device-1', NOW), false);
42 + }
43 +});