calc: cipher/option import parsed, never eval'd (parseCipherArgs/cipherFromExport; option names whitelisted, values JSON scalars); manifest icons point at files that exist (36→32)

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.
This commit is contained in:
malonehunter committed 2026-09-12 21:05:36 -04:00
1 parent a88532f421
commit 777089b91d
6 files changed
+94 -51

No files matched your search

+37 -36
View File
@@ -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;
+20 -6
View File
@@ -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
}
+2 -2
View File
@@ -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
+26 -1
View File
@@ -216,4 +216,29 @@ class cipher { // cipher constructor class
}
}
}
}
// ================= 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(<args>)`. The <args> 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])
}
+5 -2
View File
@@ -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
+4 -4
View File
@@ -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"
}
}