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:
1 parent
a88532f421
commit
777089b91d
6 files changed
+92
-49
No files matched your search
+37
-36
@@ -92,45 +92,46 @@ var coderainLitDefault = 0.19;
|
|||||||
var optImageScale = 1.0; // image scaling factor for screenshots
|
var optImageScale = 1.0; // image scaling factor for screenshots
|
||||||
|
|
||||||
var calcOptionsArr = [
|
var calcOptionsArr = [
|
||||||
// used to export/import settings
|
// used to export/import settings — the NAMES of the option globals; exportCalcOptions() reads
|
||||||
"'optNumCalcMethod'+' = '+optNumCalcMethod",
|
// window[name], importCalcOptions() accepts only these names (no eval on either side)
|
||||||
"'optFiltCrossCipherMatch'+' = '+optFiltCrossCipherMatch",
|
"optNumCalcMethod",
|
||||||
"'optFiltSameCipherMatch'+' = '+optFiltSameCipherMatch",
|
"optFiltCrossCipherMatch",
|
||||||
"'optHltSameCipher'+' = '+optHltSameCipher",
|
"optFiltSameCipherMatch",
|
||||||
"'optShowOnlyMatching'+' = '+optShowOnlyMatching",
|
"optHltSameCipher",
|
||||||
"'autoMatchEnabled'+' = '+autoMatchEnabled",
|
"optShowOnlyMatching",
|
||||||
"'optNewPhrasesGoFirst'+' = '+optNewPhrasesGoFirst",
|
"autoMatchEnabled",
|
||||||
"'optShowExtraCiphers'+' = '+optShowExtraCiphers",
|
"optNewPhrasesGoFirst",
|
||||||
"'optAllowPhraseComments'+' = '+optAllowPhraseComments",
|
"optShowExtraCiphers",
|
||||||
"'liveDatabaseMode'+' = '+liveDatabaseMode",
|
"optAllowPhraseComments",
|
||||||
"'optLetterWordCount'+' = '+optLetterWordCount",
|
"liveDatabaseMode",
|
||||||
"'optWordBreakdown'+' = '+optWordBreakdown",
|
"optLetterWordCount",
|
||||||
"'optCompactBreakdown'+' = '+optCompactBreakdown",
|
"optWordBreakdown",
|
||||||
"'optShowCipherChart'+' = '+optShowCipherChart",
|
"optCompactBreakdown",
|
||||||
"'optGradientCharts'+' = '+optGradientCharts",
|
"optShowCipherChart",
|
||||||
"'optLoadUserHistCiphers'+' = '+optLoadUserHistCiphers",
|
"optGradientCharts",
|
||||||
"'optMatrixCodeRain'+' = '+optMatrixCodeRain",
|
"optLoadUserHistCiphers",
|
||||||
"'interfaceHue'+' = '+interfaceHue",
|
"optMatrixCodeRain",
|
||||||
"'interfaceSat'+' = '+interfaceSat",
|
"interfaceHue",
|
||||||
"'interfaceLit'+' = '+interfaceLit",
|
"interfaceSat",
|
||||||
"'fontHue'+' = '+fontHue",
|
"interfaceLit",
|
||||||
"'fontSat'+' = '+fontSat",
|
"fontHue",
|
||||||
"'fontLit'+' = '+fontLit",
|
"fontSat",
|
||||||
"'coderainHue'+' = '+coderainHue",
|
"fontLit",
|
||||||
"'coderainSat'+' = '+coderainSat",
|
"coderainHue",
|
||||||
"'coderainLit'+' = '+coderainLit",
|
"coderainSat",
|
||||||
"'optPhraseLimit'+' = '+optPhraseLimit",
|
"coderainLit",
|
||||||
"'dbPageItems'+' = '+dbPageItems",
|
"optPhraseLimit",
|
||||||
"'dbScrollItems'+' = '+dbScrollItems",
|
"dbPageItems",
|
||||||
"'optForceTwoColumnLayout'+' = '+optForceTwoColumnLayout",
|
"dbScrollItems",
|
||||||
"'optColoredCiphers'+' = '+optColoredCiphers",
|
"optForceTwoColumnLayout",
|
||||||
"'optGemSubstitutionMode'+' = '+optGemSubstitutionMode",
|
"optColoredCiphers",
|
||||||
"'optGemMultCharPos'+' = '+optGemMultCharPos",
|
"optGemSubstitutionMode",
|
||||||
"'optGemMultCharPosReverse'+' = '+optGemMultCharPosReverse",
|
"optGemMultCharPos",
|
||||||
|
"optGemMultCharPosReverse",
|
||||||
'"encDefAlphArr"+" = \x27"+String(encPrevAlphStr).replace(/,/g,"")+"\x27"',
|
'"encDefAlphArr"+" = \x27"+String(encPrevAlphStr).replace(/,/g,"")+"\x27"',
|
||||||
'"encDefVowArr"+" = \x27"+String(encPrevVowStr).replace(/,/g,"")+"\x27"',
|
'"encDefVowArr"+" = \x27"+String(encPrevVowStr).replace(/,/g,"")+"\x27"',
|
||||||
'"encDefExcLetArr"+" = \x27"+String(encPrevExcLetStr).replace(/,/g,"")+"\x27"',
|
'"encDefExcLetArr"+" = \x27"+String(encPrevExcLetStr).replace(/,/g,"")+"\x27"',
|
||||||
"'optImageScale'+' = '+optImageScale",
|
"optImageScale",
|
||||||
];
|
];
|
||||||
|
|
||||||
var runOnceRestoreCalcSet = true;
|
var runOnceRestoreCalcSet = true;
|
||||||
|
|||||||
+20
-6
@@ -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
|
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
|
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++) {
|
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
|
document.getElementById("calcOptionsPanel").innerHTML = "" // clear menu panel
|
||||||
|
|
||||||
initCalc() // reinit.
|
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
|
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
|
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++) {
|
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
|
document.getElementById("calcOptionsPanel").innerHTML = "" // clear menu panel
|
||||||
initCalc() // reinit
|
initCalc() // reinit
|
||||||
updateTables() // update tables
|
updateTables() // update tables
|
||||||
@@ -240,9 +246,17 @@ function isJsonString(str) {
|
|||||||
return true;
|
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) {
|
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
|
toggleCodeRain() // update coderain
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -241,10 +241,10 @@ function exportCiphersDB(expAllCiph = false) {
|
|||||||
return out
|
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"
|
var o = "calcOptions = [\n\t"
|
||||||
for (var i = 0; i < calcOptionsArr.length; i++) {
|
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
|
o = o.slice(0,-3) + "\n]\n" // remove comma, new line, tab; new line, close array, new line
|
||||||
return o
|
return o
|
||||||
|
|||||||
@@ -217,3 +217,28 @@ 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])
|
||||||
|
}
|
||||||
@@ -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
|
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
|
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++) {
|
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
|
document.getElementById("calcOptionsPanel").innerHTML = "" // clear menu panel
|
||||||
initCalc() // reinit
|
initCalc() // reinit
|
||||||
updateTables() // update tables
|
updateTables() // update tables
|
||||||
|
|||||||
+3
-3
@@ -4,13 +4,13 @@
|
|||||||
"background_color": "#202631",
|
"background_color": "#202631",
|
||||||
"icons": [
|
"icons": [
|
||||||
{
|
{
|
||||||
"src": "res/calc-logo-36.png",
|
"src": "res/calc-logo-32.png",
|
||||||
"sizes": "36x36",
|
"sizes": "32x32",
|
||||||
"type": "image/png",
|
"type": "image/png",
|
||||||
"density": 0.75
|
"density": 0.75
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"src": "res/calc-logo-36.png",
|
"src": "res/calc-logo-48.png",
|
||||||
"sizes": "48x48",
|
"sizes": "48x48",
|
||||||
"type": "image/png",
|
"type": "image/png",
|
||||||
"density": 1.0
|
"density": 1.0
|
||||||
|
|||||||
Reference in new issue
Block a user