Evaluate calculator and goal tree formulas without eval
The production CSP forbids unsafe-eval, so the Function() call behind both tools threw, every result came back empty and the calculator save and update buttons stayed disabled in production. Dev worked because Vite sends no CSP. Both now reuse the eval-free parser of the table formula engine, and a test fails if client code evaluates strings again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
337e08caea
commit
08e28ee70d
5 changed files with 33 additions and 14 deletions
|
|
@ -1,4 +1,5 @@
|
|||
// Rôle : regroupe les helpers purs du calculateur et de son arbre de résultats.
|
||||
import { evaluateArithmetic } from "./tableFormulaEngine.js";
|
||||
export const DISPLAY_NUMBER_FORMATTER = new Intl.NumberFormat("fr-FR", { maximumFractionDigits: 6 });
|
||||
export const BASE_TOKEN = "base";
|
||||
|
||||
|
|
@ -8,12 +9,7 @@ export function calculateExpression(expression, baseValue = null) {
|
|||
const withBase = normalized.replace(/\bbase\b/gi, () => Number.isFinite(baseValue) ? `(${formatResult(baseValue)})` : "NaN");
|
||||
if (!/^[\d\s+\-*/().]+$/.test(withBase)) return null;
|
||||
|
||||
try {
|
||||
const value = Function(`"use strict"; return (${withBase})`)();
|
||||
return Number.isFinite(value) ? value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
return evaluateArithmetic(withBase).value;
|
||||
}
|
||||
|
||||
export function parseCalculatorCurrent(value) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
// Rôle : regroupe les calculs purs de l'arbre d'objectifs.
|
||||
import { getLatestDailyReset, getLatestWeeklyReset } from "./resetUtils.js";
|
||||
import { evaluateArithmetic } from "./tableFormulaEngine.js";
|
||||
export const GOAL_TREE_KINDS = ["step", "text", "checklist"];
|
||||
export const GOAL_TREE_RESET_TYPES = ["none", "daily", "weekly"];
|
||||
export const GOAL_TREE_ICON_NAMES = ["cube", "fire", "flask", "grass", "leaf", "meat", "powder", "tear", "ball"];
|
||||
|
|
@ -38,13 +39,9 @@ export function calculateGoalTreeFormula(expression, baseValue = null, { clamp =
|
|||
const withBase = normalized.replace(/\bbase\b/gi, () => Number.isFinite(baseValue) ? `(${formatResult(baseValue)})` : "NaN");
|
||||
if (!/^[\d\s+\-*/().]+$/.test(withBase)) return null;
|
||||
|
||||
try {
|
||||
const value = Function(`"use strict"; return (${withBase})`)();
|
||||
if (!Number.isFinite(value)) return null;
|
||||
return clamp ? Math.max(0, value) : value;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const { value } = evaluateArithmetic(withBase);
|
||||
if (value == null) return null;
|
||||
return clamp ? Math.max(0, value) : value;
|
||||
}
|
||||
|
||||
export function formatGoalTreeNumber(value) {
|
||||
|
|
|
|||
|
|
@ -161,6 +161,13 @@ export function evaluateFormula(expression, resolveReference) {
|
|||
}
|
||||
}
|
||||
|
||||
// Évalue une expression purement arithmétique sans eval, compatible avec la CSP de production.
|
||||
export function evaluateArithmetic(expression) {
|
||||
return evaluateFormula(expression, () => {
|
||||
throw new Error("invalid");
|
||||
});
|
||||
}
|
||||
|
||||
export function evaluateTableCell(address, cells, dimensions, stack = []) {
|
||||
return evaluateTableCellWithCache(address, cells, dimensions, stack, new Map());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue