From bc934ddc34efda415c610407a436c4a021335381 Mon Sep 17 00:00:00 2001 From: miner Date: Thu, 4 Jan 2024 18:18:20 -0500 Subject: [PATCH] modified: .env modified: CeresHelper.js modified: build.sh modified: shortBuild.sh --- .env | 2 +- CeresHelper.js | 50 ++++++++++++++++++++++++++++++++++++++------------ build.sh | 3 +-- shortBuild.sh | 3 +-- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/.env b/.env index a031e1d..7040814 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -VERSION=2.0.2 \ No newline at end of file +CERESJS=https://cdn.jsdelivr.net/gh/Pterodactylus/Ceres.js@master/dist/ceres.js \ No newline at end of file diff --git a/CeresHelper.js b/CeresHelper.js index 192609c..dbd063d 100644 --- a/CeresHelper.js +++ b/CeresHelper.js @@ -118,7 +118,7 @@ export class Ceres { this.instance.delete(); } - parseFunctionFromJson(jsonFunction, variablesMapping) { + static parseFunctionFromJson(jsonFunction, variablesMapping) { let parsedFunction = jsonFunction; // Replace variables with their corresponding indexed representations @@ -127,19 +127,49 @@ export class Ceres { parsedFunction = parsedFunction.replace(regex, `x[${index}]`); }); - return new Function('x', `return ${parsedFunction}`); + return parsedFunction; } - sanitizeInput(input) { - // Return input without any potentially harmful JavaScript string - return input.replace(/[<>]/g, ""); - } + static sanitizeInput(mathExpression) { + const validExpression = /^[0-9\s+\-/*()\[\]^sincoxetalaqrtPi.E]*$/; + const validStructure = /^[\d\s+\-/*()\[\]^]*$/; + + if (!validExpression.test(mathExpression) || !validStructure.test(mathExpression.replace(/sin|cos|tan|cot|sec|csc|log|ln|sqrt|exp|abs|x|E|Pi/g,''))){ + throw new Error(`Invalid mathematical expression: ${mathExpression}`); + } + + mathExpression = mathExpression.replace('^', '**'); + + // Translation to Javascript functions + mathExpression = mathExpression + .replace(/sin/g, 'Math.sin') + .replace(/cos/g, 'Math.cos') + .replace(/tan/g, 'Math.tan') + .replace(/cot/g, 'Math.cot') + .replace(/sec/g, 'Math.sec') + .replace(/csc/g, 'Math.csc') + .replace(/log/g, 'Math.log10') + .replace(/ln/g, 'Math.log') + .replace(/sqrt/g, 'Math.sqrt') + .replace(/exp/g, 'Math.exp') + .replace(/abs/g, 'Math.abs') + .replace(/Pi/g, 'Math.PI') + .replace(/E/g, 'Math.E'); + + return mathExpression; + } setSystemFromJson(jsonSystem) { + + //console.log(jsonSystem.functions) + let variables = jsonSystem.variables + jsonSystem.functions = jsonSystem.functions.map(function(x) { return Ceres.parseFunctionFromJson(x, variables); }); + // sanitize the input to prevent injection attacks - jsonSystem.functions = jsonSystem.functions.map(this.sanitizeInput); + jsonSystem.functions = jsonSystem.functions.map(Ceres.sanitizeInput); + //console.log(jsonSystem.functions) - jsonSystem.functions.forEach(jsonFunction => this.addFunction(this.parseFunctionFromJson(jsonFunction, jsonSystem.variables))); + jsonSystem.functions.forEach(jsonFunction => this.addFunction(new Function('x', `return ${jsonFunction}`))); Object.keys(jsonSystem.variables).forEach((varName, index) => { let variable = jsonSystem.variables[varName]; @@ -152,10 +182,6 @@ export class Ceres { }); } - generateInitialGuess(variablesMapping) { - return Object.keys(variablesMapping).map(varName => variablesMapping[varName].guess); - } - async run(jsonSystem, max_numb_iterations = 2000, parameter_tolerance = 1e-10, function_tolerance = 1e-16, gradient_tolerance = 1e-16, max_solver_time_in_seconds = 100, initial_trust_region_radius = 1e4, max_trust_region_radius = 1e16, max_num_consecutive_invalid_steps = 5) { this.setSystemFromJson(jsonSystem); let initial_guess = this.generateInitialGuess(jsonSystem.variables); diff --git a/build.sh b/build.sh index 5fdbafe..7d1c48f 100644 --- a/build.sh +++ b/build.sh @@ -69,8 +69,7 @@ sed -i 's/include(CeresCodeGeneration)/# include(CeresCodeGeneration)/' $bdir/in #$bdir/emsdk/upstream/emscripten/emconfigure cmake $cwd/ -DCMAKE_INSTALL_PREFIX=$bdir/installpkg $bdir/emsdk/upstream/emscripten/emcmake cmake $cwd/ -DCMAKE_INSTALL_PREFIX=$bdir/installpkg $bdir/emsdk/upstream/emscripten/emmake make -cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-v$VERSION.js -cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-latest.mjs +cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/ceres.js #~/emsdk/upstream/emscripten/emrun --browser "explorer.exe" ~/ceres.js-master/index.html #~/emsdk/upstream/emscripten/emrun --browser "explorer.exe" ~/ceres.js-master/test.html diff --git a/shortBuild.sh b/shortBuild.sh index 10f7ee3..b2b2447 100644 --- a/shortBuild.sh +++ b/shortBuild.sh @@ -14,8 +14,7 @@ $bdir/emsdk/upstream/emscripten/emcmake cmake $cwd/ -DCMAKE_INSTALL_PREFIX=$bdir $bdir/emsdk/upstream/emscripten/emmake make cat $cwd/CeresHelper.js >> $bdir/Ceres.js/Ceres.js -cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-v$VERSION.mjs -cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/Ceres-latest.mjs +cp --verbose $bdir/Ceres.js/Ceres.js $cwd/dist/ceres.js #cp --verbose $bdir/Ceres.js/Ceres.wasm.map $cwd/Ceres.wasm.map cd $cwd/app