@posiwise/common-services 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1478,24 +1478,15 @@ class PermissionService {
1478
1478
  expr = this.handleNonBooleanPermissions(permission, expr, productKey, permission_key, productSlug);
1479
1479
  // Now expr is made of true/false values with &&, ||, ()
1480
1480
  // Safe parser: no eval() - CSP 'unsafe-eval' not required
1481
- const result = this.evaluateBooleanExpression(expr);
1482
- // DEBUG: inspect in console - remove after fixing CloudOlive permissions
1483
- console.debug('[PermissionService] evaluatePermissions', {
1484
- permission,
1485
- expr: expr.trim(),
1486
- result,
1487
- selectedProduct: PermissionService?.selectedProduct,
1488
- productKey,
1489
- permission_key,
1490
- productSlug
1491
- });
1492
- return result;
1481
+ return this.evaluateBooleanExpression(expr);
1493
1482
  }
1494
1483
  /** Safe boolean expression parser - replaces eval() for CSP compliance. */
1495
1484
  evaluateBooleanExpression(expr) {
1496
1485
  expr = expr.replace(/\s+/g, ' ').trim();
1497
- if (!expr)
1486
+ if (!expr) {
1487
+ console.warn('[evaluateBooleanExpression] empty expr', { expr: `"${expr}"` });
1498
1488
  return false;
1489
+ }
1499
1490
  if (expr === 'true')
1500
1491
  return true;
1501
1492
  if (expr === 'false')
@@ -1510,7 +1501,9 @@ class PermissionService {
1510
1501
  else if (depth === 0 && expr.substring(i, i + 2) === '||') {
1511
1502
  const left = expr.substring(0, i).trim();
1512
1503
  const right = expr.substring(i + 2).trim();
1513
- return (this.evaluateBooleanExpression(left) || this.evaluateBooleanExpression(right));
1504
+ const result = this.evaluateBooleanExpression(left) || this.evaluateBooleanExpression(right);
1505
+ console.debug('[evaluateBooleanExpression] ||', { expr, left, right, result });
1506
+ return result;
1514
1507
  }
1515
1508
  }
1516
1509
  depth = 0;
@@ -1523,35 +1516,41 @@ class PermissionService {
1523
1516
  else if (depth === 0 && expr.substring(i, i + 2) === '&&') {
1524
1517
  const left = expr.substring(0, i).trim();
1525
1518
  const right = expr.substring(i + 2).trim();
1526
- return (this.evaluateBooleanExpression(left) && this.evaluateBooleanExpression(right));
1519
+ const result = this.evaluateBooleanExpression(left) && this.evaluateBooleanExpression(right);
1520
+ console.debug('[evaluateBooleanExpression] &&', { expr, left, right, result });
1521
+ return result;
1527
1522
  }
1528
1523
  }
1529
- // Strip matching outer parens - only when first ( and last ) are a pair
1524
+ // Strip matching outer parens - only when first ( and last ) are a pair (fix for nested)
1530
1525
  if (expr.startsWith('(') && expr.endsWith(')')) {
1531
- let depth = 0;
1526
+ let d = 0;
1532
1527
  for (let j = 0; j < expr.length; j++) {
1533
1528
  if (expr[j] === '(')
1534
- depth++;
1529
+ d++;
1535
1530
  else if (expr[j] === ')')
1536
- depth--;
1537
- if (depth === 0) {
1531
+ d--;
1532
+ if (d === 0) {
1538
1533
  if (j === expr.length - 1) {
1539
- return this.evaluateBooleanExpression(expr.substring(1, expr.length - 1));
1534
+ const inner = expr.substring(1, expr.length - 1);
1535
+ const result = this.evaluateBooleanExpression(inner);
1536
+ console.debug('[evaluateBooleanExpression] strip parens', { expr, inner, result });
1537
+ return result;
1540
1538
  }
1541
- break; // First ( doesn't wrap entire expr
1539
+ break;
1542
1540
  }
1543
- if (depth < 0)
1541
+ if (d < 0)
1544
1542
  break;
1545
1543
  }
1546
1544
  }
1547
- // Unrecognized token - treat as false for safety
1548
- console.warn('[PermissionService] evaluateBooleanExpression: unrecognized expr', { expr });
1545
+ console.warn('[evaluateBooleanExpression] unrecognized', { expr });
1549
1546
  return false;
1550
1547
  }
1551
1548
  handleNonBooleanPermissions(permission, expr, productKey, permission_key, productSlug) {
1552
1549
  if (typeof permission !== 'boolean') {
1553
1550
  permission.split(' ').forEach(x => {
1554
1551
  const raw = x.trim();
1552
+ if (!raw)
1553
+ return;
1555
1554
  if (['||', '&&', '(', ')'].includes(raw)) {
1556
1555
  expr += ` ${raw} `;
1557
1556
  }