diff --git a/tests/server-cache.test.mjs b/tests/server-cache.test.mjs index f708820..cb20722 100644 --- a/tests/server-cache.test.mjs +++ b/tests/server-cache.test.mjs @@ -1,6 +1,6 @@ // Rôle : vérifie les en-têtes de cache du serveur Node sur des fichiers de production représentatifs. import { createHash } from "node:crypto"; -import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import assert from "node:assert/strict"; @@ -122,3 +122,14 @@ test("every response carries the security headers and the CSP allows only the in assert.ok(headers.get("referrer-policy")); } }); + +test("client code never evaluates strings, since the production CSP forbids unsafe-eval", async () => { + const entries = await readdir("website/src", { recursive: true, withFileTypes: true }); + const sources = entries.filter((entry) => entry.isFile() && /\.(jsx?|mjs)$/.test(entry.name)); + assert.ok(sources.length > 0); + for (const entry of sources) { + const path = join(entry.parentPath, entry.name); + const source = await readFile(path, "utf8"); + assert.doesNotMatch(source, /(? { assert.equal(calculateExpression("2 + 3 * 4"), 14); assert.equal(calculateExpression("base / 2", 10), 5); assert.equal(calculateExpression("window.alert(1)"), null); + assert.equal(calculateExpression("(2 + 3) * -2"), -10); + assert.equal(calculateExpression("base * 3", -5), -15); + assert.equal(calculateExpression("1,5 * 2"), 3); + assert.equal(calculateExpression("10 / 0"), null); + assert.equal(calculateExpression("2 (3)"), null); + assert.equal(calculateExpression("1.2.3"), null); + assert.equal(calculateExpression("(1 + 2"), null); + assert.equal(calculateExpression("base * 2"), null); assert.equal(calculateRemainingValue("base * 8", "20", 10), 60); assert.equal(calculateRemainingValue("80", "20,5"), 59.5); assert.equal(calculateRemainingValue("80", "invalid"), null); diff --git a/website/src/features/toolboxes/modules/calculatorUtils.js b/website/src/features/toolboxes/modules/calculatorUtils.js index 36fd6c4..c9caa95 100644 --- a/website/src/features/toolboxes/modules/calculatorUtils.js +++ b/website/src/features/toolboxes/modules/calculatorUtils.js @@ -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) { diff --git a/website/src/features/toolboxes/modules/goalTreeUtils.js b/website/src/features/toolboxes/modules/goalTreeUtils.js index 08b0530..26759b1 100644 --- a/website/src/features/toolboxes/modules/goalTreeUtils.js +++ b/website/src/features/toolboxes/modules/goalTreeUtils.js @@ -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) { diff --git a/website/src/features/toolboxes/modules/tableFormulaEngine.js b/website/src/features/toolboxes/modules/tableFormulaEngine.js index d51b078..41e0b88 100644 --- a/website/src/features/toolboxes/modules/tableFormulaEngine.js +++ b/website/src/features/toolboxes/modules/tableFormulaEngine.js @@ -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()); }