From 777089b91d7e9924727a677b69bc7a0e7c22596b Mon Sep 17 00:00:00 2001 From: malonehunter Date: Sat, 12 Sep 2026 21:05:36 -0400 Subject: [PATCH] =?UTF-8?q?calc:=20cipher/option=20import=20parsed,=20neve?= =?UTF-8?q?r=20eval'd=20(parseCipherArgs/cipherFromExport;=20option=20name?= =?UTF-8?q?s=20whitelisted,=20values=20JSON=20scalars);=20manifest=20icons?= =?UTF-8?q?=20point=20at=20files=20that=20exist=20(36=E2=86=9232)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes the five eval() sites: imported cipher files and the localStorage settings copy can no longer execute code, and the calculator now works under a CSP without 'unsafe-eval'. Export format unchanged for booleans/numbers. Parity commit with the instance tree; release gated. --- calc/calc.js | 73 ++++++++++++++++++++++---------------------- calc/export-csv.js | 26 ++++++++++++---- calc/export.js | 4 +-- calc/gematria.js | 27 +++++++++++++++- calc/localstorage.js | 7 +++-- manifest.json | 8 ++--- 6 files changed, 94 insertions(+), 51 deletions(-) diff --git a/calc/calc.js b/calc/calc.js index 7e49c0a..fa3e7ea 100644 --- a/calc/calc.js +++ b/calc/calc.js @@ -92,45 +92,46 @@ var coderainLitDefault = 0.19; var optImageScale = 1.0; // image scaling factor for screenshots var calcOptionsArr = [ - // used to export/import settings - "'optNumCalcMethod'+' = '+optNumCalcMethod", - "'optFiltCrossCipherMatch'+' = '+optFiltCrossCipherMatch", - "'optFiltSameCipherMatch'+' = '+optFiltSameCipherMatch", - "'optHltSameCipher'+' = '+optHltSameCipher", - "'optShowOnlyMatching'+' = '+optShowOnlyMatching", - "'autoMatchEnabled'+' = '+autoMatchEnabled", - "'optNewPhrasesGoFirst'+' = '+optNewPhrasesGoFirst", - "'optShowExtraCiphers'+' = '+optShowExtraCiphers", - "'optAllowPhraseComments'+' = '+optAllowPhraseComments", - "'liveDatabaseMode'+' = '+liveDatabaseMode", - "'optLetterWordCount'+' = '+optLetterWordCount", - "'optWordBreakdown'+' = '+optWordBreakdown", - "'optCompactBreakdown'+' = '+optCompactBreakdown", - "'optShowCipherChart'+' = '+optShowCipherChart", - "'optGradientCharts'+' = '+optGradientCharts", - "'optLoadUserHistCiphers'+' = '+optLoadUserHistCiphers", - "'optMatrixCodeRain'+' = '+optMatrixCodeRain", - "'interfaceHue'+' = '+interfaceHue", - "'interfaceSat'+' = '+interfaceSat", - "'interfaceLit'+' = '+interfaceLit", - "'fontHue'+' = '+fontHue", - "'fontSat'+' = '+fontSat", - "'fontLit'+' = '+fontLit", - "'coderainHue'+' = '+coderainHue", - "'coderainSat'+' = '+coderainSat", - "'coderainLit'+' = '+coderainLit", - "'optPhraseLimit'+' = '+optPhraseLimit", - "'dbPageItems'+' = '+dbPageItems", - "'dbScrollItems'+' = '+dbScrollItems", - "'optForceTwoColumnLayout'+' = '+optForceTwoColumnLayout", - "'optColoredCiphers'+' = '+optColoredCiphers", - "'optGemSubstitutionMode'+' = '+optGemSubstitutionMode", - "'optGemMultCharPos'+' = '+optGemMultCharPos", - "'optGemMultCharPosReverse'+' = '+optGemMultCharPosReverse", + // used to export/import settings — the NAMES of the option globals; exportCalcOptions() reads + // window[name], importCalcOptions() accepts only these names (no eval on either side) + "optNumCalcMethod", + "optFiltCrossCipherMatch", + "optFiltSameCipherMatch", + "optHltSameCipher", + "optShowOnlyMatching", + "autoMatchEnabled", + "optNewPhrasesGoFirst", + "optShowExtraCiphers", + "optAllowPhraseComments", + "liveDatabaseMode", + "optLetterWordCount", + "optWordBreakdown", + "optCompactBreakdown", + "optShowCipherChart", + "optGradientCharts", + "optLoadUserHistCiphers", + "optMatrixCodeRain", + "interfaceHue", + "interfaceSat", + "interfaceLit", + "fontHue", + "fontSat", + "fontLit", + "coderainHue", + "coderainSat", + "coderainLit", + "optPhraseLimit", + "dbPageItems", + "dbScrollItems", + "optForceTwoColumnLayout", + "optColoredCiphers", + "optGemSubstitutionMode", + "optGemMultCharPos", + "optGemMultCharPosReverse", '"encDefAlphArr"+" = \x27"+String(encPrevAlphStr).replace(/,/g,"")+"\x27"', '"encDefVowArr"+" = \x27"+String(encPrevVowStr).replace(/,/g,"")+"\x27"', '"encDefExcLetArr"+" = \x27"+String(encPrevExcLetStr).replace(/,/g,"")+"\x27"', - "'optImageScale'+' = '+optImageScale", + "optImageScale", ]; var runOnceRestoreCalcSet = true; diff --git a/calc/export-csv.js b/calc/export-csv.js index b0831f8..b09b6f5 100644 --- a/calc/export-csv.js +++ b/calc/export-csv.js @@ -98,10 +98,13 @@ function importFileAction(file, hasLocalFile) { file = ciph[0].replace(/(\t| +|\r|\n)/g, "").slice(10,-1) // remove tabs, consequtive spaces, line breaks - "new cipher" at start, last bracket ciph = file.split(",new cipher") // split string into array - cipherList = []; cCat = []; defaultCipherArray = [] // clear arrays with previously defined ciphers, categories, default ciphers + var imported = [] // parse every cipher first — one malformed entry aborts the import and keeps the current ciphers for (n = 0; n < ciph.length; n++) { - cipherList.push(eval("new cipher("+ciph[n].slice(1,-1)+")")) // remove parethesis, evaluate string as javascript code + var c = cipherFromExport(ciph[n].slice(1,-1)) // remove parentheses; parsed as data, never eval'd + if (c === null) { displayCalcNotification("Cipher import failed: malformed cipher #"+(n+1), 3000); return } + imported.push(c) } + cipherList = imported; cCat = []; defaultCipherArray = [] // replace previously defined ciphers, categories, default ciphers document.getElementById("calcOptionsPanel").innerHTML = "" // clear menu panel initCalc() // reinit. @@ -148,10 +151,13 @@ function importFileAction(file, hasLocalFile) { file = ciph[0].replace(/(\t| +|\r|\n)/g, "").slice(10,-1) // remove tabs, consequtive spaces, line breaks - "new cipher" at start, last bracket ciph = file.split(",new cipher") // split string into array - cipherList = []; cCat = []; defaultCipherArray = [] // clear arrays with previously defined ciphers, categories, default ciphers + var imported = [] // parse every cipher first — one malformed entry aborts the import and keeps the current ciphers for (n = 0; n < ciph.length; n++) { - cipherList.push(eval("new cipher("+ciph[n].slice(1,-1)+")")) // remove parethesis, evaluate string as javascript code + var c = cipherFromExport(ciph[n].slice(1,-1)) // remove parentheses; parsed as data, never eval'd + if (c === null) { displayCalcNotification("Cipher import failed: malformed cipher #"+(n+1), 3000); return } + imported.push(c) } + cipherList = imported; cCat = []; defaultCipherArray = [] // replace previously defined ciphers, categories, default ciphers document.getElementById("calcOptionsPanel").innerHTML = "" // clear menu panel initCalc() // reinit updateTables() // update tables @@ -240,9 +246,17 @@ function isJsonString(str) { return true; } -function importCalcOptions(calcOpt) { // load user options +function importCalcOptions(calcOpt) { // load user options: "name = value" strings; names must be in calcOptionsArr, values are JSON scalars — parsed, never eval'd if (typeof calcOpt !== 'undefined' && calcOpt !== null) { - for (var i = 0; i < calcOpt.length; i++) eval(calcOpt[i]) + for (var i = 0; i < calcOpt.length; i++) { + if (typeof calcOpt[i] !== "string") continue + var m = calcOpt[i].match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*([\s\S]+?)\s*$/) + if (m === null || calcOptionsArr.indexOf(m[1]) === -1) continue // unknown option name → ignored + var v + try { v = JSON.parse(m[2]) } catch (e) { continue } // not a JSON scalar → ignored + if (typeof v !== "boolean" && typeof v !== "number" && typeof v !== "string") continue + window[m[1]] = v // the option globals are top-level `var`s, i.e. window properties + } } toggleCodeRain() // update coderain } diff --git a/calc/export.js b/calc/export.js index ae948a2..e4ad2d8 100644 --- a/calc/export.js +++ b/calc/export.js @@ -241,10 +241,10 @@ function exportCiphersDB(expAllCiph = false) { return out } -function exportCalcOptions() { +function exportCalcOptions() { // one JSON string per option: "name = value" (same text as before for booleans/numbers); values read by name, never eval'd var o = "calcOptions = [\n\t" for (var i = 0; i < calcOptionsArr.length; i++) { - o += "\x22"+eval(calcOptionsArr[i])+"\x22,\n\t" // quotes - \x22 + o += JSON.stringify(calcOptionsArr[i]+" = "+JSON.stringify(window[calcOptionsArr[i]]))+",\n\t" } o = o.slice(0,-3) + "\n]\n" // remove comma, new line, tab; new line, close array, new line return o diff --git a/calc/gematria.js b/calc/gematria.js index 3844dc8..5f14271 100644 --- a/calc/gematria.js +++ b/calc/gematria.js @@ -216,4 +216,29 @@ class cipher { // cipher constructor class } } -} \ No newline at end of file +} +// ================= Cipher import (parsed, never eval'd) ================= +// Exported cipher files (exportCiphers / exportCiphersDB), the localStorage settings copy and +// pasted "// ciphers.js" files carry each cipher as `new cipher()`. The text is +// JSON-compatible by construction of the exporter: "name","category",H,S,L,[charcodes],[values], +// diacriticsAsRegular,enabled,caseSensitive. It used to be eval()'d — imported files and the +// localStorage copy (which any XSS could have written) could execute code. Now it is parsed and +// shape-checked; anything that is not exactly a cipher argument list is rejected (null). +function parseCipherArgs(argStr) { + var args + try { args = JSON.parse("[" + argStr + "]") } catch (e) { return null } + if (!Array.isArray(args) || args.length < 7 || args.length > 10) return null + var isIntArr = function (a) { return Array.isArray(a) && a.every(function (x) { return Number.isInteger(x) }) } + var isBoolOrAbsent = function (b) { return b === undefined || typeof b === "boolean" } + if (typeof args[0] !== "string" || typeof args[1] !== "string") return null // name, category + if (![2, 3, 4].every(function (i) { return typeof args[i] === "number" && isFinite(args[i]) })) return null // H, S, L + if (!isIntArr(args[5]) || !isIntArr(args[6]) || args[5].length !== args[6].length) return null // charcodes, values + if (![7, 8, 9].every(function (i) { return isBoolOrAbsent(args[i]) })) return null // flags (constructor defaults apply when absent) + return args +} + +function cipherFromExport(argStr) { // → a cipher, or null when the text is not a well-formed cipher argument list + var a = parseCipherArgs(argStr) + if (a === null) return null + return new cipher(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]) +} diff --git a/calc/localstorage.js b/calc/localstorage.js index f70467d..a58e6a8 100644 --- a/calc/localstorage.js +++ b/calc/localstorage.js @@ -32,10 +32,13 @@ function restoreCalcSettingsLocalStorage(silentMode = false) { file = ciph[0].replace(/(\t| +|\r|\n)/g, "").slice(10,-1) // remove tabs, consequtive spaces, line breaks - "new cipher" at start, last bracket ciph = file.split(",new cipher") // split string into array - cipherList = []; cCat = []; defaultCipherArray = [] // clear arrays with previously defined ciphers, categories, default ciphers + var imported = [] // parse every cipher first — one malformed entry aborts the import and keeps the current ciphers for (n = 0; n < ciph.length; n++) { - cipherList.push(eval("new cipher("+ciph[n].slice(1,-1)+")")) // remove parethesis, evaluate string as javascript code + var c = cipherFromExport(ciph[n].slice(1,-1)) // remove parentheses; parsed as data, never eval'd + if (c === null) { displayCalcNotification("Cipher import failed: malformed cipher #"+(n+1), 3000); return } + imported.push(c) } + cipherList = imported; cCat = []; defaultCipherArray = [] // replace previously defined ciphers, categories, default ciphers document.getElementById("calcOptionsPanel").innerHTML = "" // clear menu panel initCalc() // reinit updateTables() // update tables diff --git a/manifest.json b/manifest.json index 1d52b58..b34b394 100644 --- a/manifest.json +++ b/manifest.json @@ -4,13 +4,13 @@ "background_color": "#202631", "icons": [ { - "src": "res/calc-logo-36.png", - "sizes": "36x36", + "src": "res/calc-logo-32.png", + "sizes": "32x32", "type": "image/png", "density": 0.75 }, { - "src": "res/calc-logo-36.png", + "src": "res/calc-logo-48.png", "sizes": "48x48", "type": "image/png", "density": 1.0 @@ -42,4 +42,4 @@ ], "start_url": "index.html", "display": "fullscreen" -} \ No newline at end of file +}