From 8e43c8c16709bf6c386a106f63e5391ffeaf37d7 Mon Sep 17 00:00:00 2001 From: RomanTsukanov Date: Thu, 5 Dec 2024 18:35:39 +0400 Subject: [PATCH 1/3] Add a description to JSON obfuscator --- utils/json_obfuscator.js | 217 ++++++++++++++++++++++----------------- 1 file changed, 122 insertions(+), 95 deletions(-) diff --git a/utils/json_obfuscator.js b/utils/json_obfuscator.js index acb0fe3727..f7915a54fc 100644 --- a/utils/json_obfuscator.js +++ b/utils/json_obfuscator.js @@ -1,3 +1,21 @@ +/** + * SurveyJS JSON Obfuscator + * ------------------------ + * This tool is designed to obfuscate survey JSON schemas by replacing titles, descriptions, placeholders, + * and other meaningful texts with randomized sequences of characters. The obfuscation may be useful if you + * need to attach a survey JSON schema to a bug report for investigation by the SurveyJS team + * and want to strip the schema of all meaningful data. + * + * Usage: + * + * 1. Clone the `survey-library` GitHub repository. + * 2. Open the console in the root folder and build the `survey-core` bundle: `npm run build_core`. + * 3. Run the following command: `node .\utils\json_obfuscator.js [path\to\the\survey\json\schema.json]`. + * + * This command produces a file named as the original with suffix `obf`. For example, `nps.json` => `nps.obf.json`. + * The obfuscated file is placed in the same folder as the original. + */ + const Survey = require("../build/survey-core/survey.core"); // eslint-disable-next-line no-undef @@ -10,121 +28,130 @@ let args = process.argv; if(!Array.isArray(args)) return; if(args.length < 3) { // eslint-disable-next-line no-console - console.error("Please provide link to the survey JSON file"); + console.error("Please provide a path to the survey JSON file."); return; } const fileName = args[2]; fs.readFile(fileName, (err, data) => { + if (err) { + // eslint-disable-next-line no-console + console.error("Cannot read the file: " + err); + return; + } + const newJSON = obfuscateJSON(data.toString().trim()); + const ext = path.extname(fileName); + const newFileName = fileName.substring(0, fileName.length - ext.length) + ".obf" + ext; + fs.writeFile(newFileName, newJSON, err => { if (err) { - // eslint-disable-next-line no-console - console.error("Unable to read the file: " + err); - return; + // eslint-disable-next-line no-console + console.error(err); + } else { + // eslint-disable-next-line no-console + console.log("File generated successfully: " + newFileName); } - const newJSON = obfuscateJSON(data.toString().trim()); - const ext = path.extname(fileName); - const newFileName = fileName.substring(0, fileName.length - ext.length) + ".obf" + ext; - fs.writeFile(newFileName, newJSON, err => { - if (err) { - // eslint-disable-next-line no-console - console.error(err); - } else { - // eslint-disable-next-line no-console - console.log("File generated correctly: " + newFileName); - } - }); + }); }); function obfuscateJSON(data) { - const model = new Survey.Model(JSON.parse(data)); - let index = 0; - model.getAllPanels().forEach(panel => { - panel.name = "panel" + (++index); - }); - const containers = [model]; - model.pages.forEach(page => containers.push(page)); - model.getAllPanels().forEach(panel => containers.push(panel)); - const propsToObs = ["title", "description", "requiredErrorText"]; - containers.forEach(container => obfuscatePropsText(container, propsToObs)); + const model = new Survey.Model(JSON.parse(data)); + let index = 0; + model.getAllPanels().forEach(panel => { + panel.name = "panel" + (++index); + }); + const containers = [model]; + model.pages.forEach(page => containers.push(page)); + model.getAllPanels().forEach(panel => containers.push(panel)); + const propsToObs = [ + "title", + "description", + "requiredErrorText", + "placeholder", + "otherText", + "otherPlaceholder", + "minRateDescription", + "maxRateDescription" + ]; + containers.forEach(container => obfuscatePropsText(container, propsToObs)); - let questions = model.getAllQuestions(false, true, false); - questions.forEach( q => { - if(q.getType() === "html") { - q.delete(); - return; - } - obfuscatePropsText(q, propsToObs); - ["choices", "columns", "rows", "validators"].forEach(name => { - obfuscateArrayText(q[name]); - }); - }); - questions = model.getAllQuestions(false, true, false); - const qNames = []; - index = 0; - questions.forEach(q => { - const newName = "q" + (++index); - const oldName = q.name; - q.name = newName; - qNames.push({ oldName: oldName, newName: newName }); - }); - let json = JSON.stringify(model.toJSON(), null, 2); - qNames.forEach(item => { - ["{", "{panel.", "{row."].forEach(prefix => - json = renameQuestionInExpression(json, prefix + item.oldName, prefix + item.newName, ["}", ".", "["]) - ); + let questions = model.getAllQuestions(false, true, false); + questions.forEach(q => { + if(q.getType() === "html") { + q.delete(); + return; + } + obfuscatePropsText(q, propsToObs); + ["choices", "columns", "rows", "validators"].forEach(name => { + obfuscateArrayText(q[name]); }); - return json; + }); + questions = model.getAllQuestions(false, true, false); + const qNames = []; + index = 0; + questions.forEach(q => { + const newName = "q" + (++index); + const oldName = q.name; + q.name = newName; + qNames.push({ oldName: oldName, newName: newName }); + }); + let json = JSON.stringify(model.toJSON(), null, 2); + qNames.forEach(item => { + ["{", "{panel.", "{row."].forEach(prefix => + json = renameQuestionInExpression(json, prefix + item.oldName, prefix + item.newName, ["}", ".", "["]) + ); + }); + return json; } function obfuscatePropsText(el, props) { - props.forEach( - prop => { - let isDone = false; - const loc = el["loc" + prop[0].toUpperCase() + prop.substring(1)]; - if(!!loc && !loc.isEmpty) { - data = loc.getJson(); - if(!!data && typeof data === "object") { - for(let key in data) { - data[key] = obfuscateText(data[key]); - } - loc.setJson(data); - isDone = true; - } - } - if(!isDone && !!el[prop]) el[prop] = obfuscateText(el[prop]); + props.forEach( + prop => { + let isDone = false; + const loc = el["loc" + prop[0].toUpperCase() + prop.substring(1)]; + if(!!loc && !loc.isEmpty) { + data = loc.getJson(); + if(!!data && typeof data === "object") { + for(let key in data) { + data[key] = obfuscateText(data[key]); + } + loc.setJson(data); + isDone = true; } - ); + } + if(!isDone && !!el[prop]) el[prop] = obfuscateText(el[prop]); + } + ); } function obfuscateArrayText(items) { - if(Array.isArray(items)) { - items.forEach(item => { - obfuscatePropsText(item, ["text", "title"]); - /* - if(item.text) { - item.text = obfuscateText(item.text); - } - if(item.title) { - item.title = obfuscateText(item.title); - } - */ - }); - } + if(Array.isArray(items)) { + items.forEach(item => { + obfuscatePropsText(item, ["text", "title"]); + /* + if(item.text) { + item.text = obfuscateText(item.text); + } + if(item.title) { + item.title = obfuscateText(item.title); + } + */ + }); + } } function obfuscateText(text) { - if(!text) return text; - let newText = ""; - for(let i = 0; i < text.length; i ++) { - const ch = text[i]; - let newCh = ch; - if(ch >= "a" && ch <= "z") newCh = getRandomChar("a", "z"); - if(ch >= "A" && ch <= "Z") newCh = getRandomChar("A", "Z"); - newText += newCh; - }; - return newText; + if(!text) return text; + let newText = ""; + for(let i = 0; i < text.length; i ++) { + const ch = text[i]; + let newCh = ch; + if(ch >= "a" && ch <= "z") newCh = getRandomChar("a", "z"); + if(ch >= "A" && ch <= "Z") newCh = getRandomChar("A", "Z"); + newText += newCh; + } + return newText; } function getRandomChar(min, max) { - const minI = min.codePointAt(0); - const maxI = max.codePointAt(0) - const val = Math.floor(Math.random() * (maxI - minI + 1)) + minI; - return String.fromCharCode(val); + const minI = min.codePointAt(0); + const maxI = max.codePointAt(0); + const val = Math.floor(Math.random() * (maxI - minI + 1)) + minI; + return String.fromCharCode(val); } function renameQuestionInExpression(json, oldName, newName, postFixes) { postFixes.forEach(post => { From a14da520e85f3fa823fab4122325737182340cf0 Mon Sep 17 00:00:00 2001 From: RomanTsukanov Date: Fri, 6 Dec 2024 10:17:50 +0400 Subject: [PATCH 2/3] Move the description to README.md --- utils/README.md | 20 +++++++++++++++++++ utils/json_obfuscator.js | 42 ++++++++++++---------------------------- 2 files changed, 32 insertions(+), 30 deletions(-) create mode 100644 utils/README.md diff --git a/utils/README.md b/utils/README.md new file mode 100644 index 0000000000..70ea0c278d --- /dev/null +++ b/utils/README.md @@ -0,0 +1,20 @@ +# SurveyJS JSON Obfuscator + +This tool is designed to obfuscate survey JSON schemas by replacing titles, descriptions, placeholders, and other meaningful texts with randomized sequences of characters. The obfuscation may be useful if you need to attach a survey JSON schema to a bug report for investigation by the SurveyJS team and want to strip the schema of all meaningful data. + +Usage: + +1. Clone the `survey-library` GitHub repository. +2. Open the console in the root folder and build the `survey-core` bundle: + + ``` + npm run build_core + ``` + +3. Run the following command: + + ``` + node .\utils\json_obfuscator.js [path\to\the\survey\json\schema.json] + ``` + +This command produces a file named as the original with suffix `obf`. For example, `nps.json` → `nps.obf.json`. The obfuscated file is placed in the same folder as the original. \ No newline at end of file diff --git a/utils/json_obfuscator.js b/utils/json_obfuscator.js index f7915a54fc..8a33260ce6 100644 --- a/utils/json_obfuscator.js +++ b/utils/json_obfuscator.js @@ -1,21 +1,3 @@ -/** - * SurveyJS JSON Obfuscator - * ------------------------ - * This tool is designed to obfuscate survey JSON schemas by replacing titles, descriptions, placeholders, - * and other meaningful texts with randomized sequences of characters. The obfuscation may be useful if you - * need to attach a survey JSON schema to a bug report for investigation by the SurveyJS team - * and want to strip the schema of all meaningful data. - * - * Usage: - * - * 1. Clone the `survey-library` GitHub repository. - * 2. Open the console in the root folder and build the `survey-core` bundle: `npm run build_core`. - * 3. Run the following command: `node .\utils\json_obfuscator.js [path\to\the\survey\json\schema.json]`. - * - * This command produces a file named as the original with suffix `obf`. For example, `nps.json` => `nps.obf.json`. - * The obfuscated file is placed in the same folder as the original. - */ - const Survey = require("../build/survey-core/survey.core"); // eslint-disable-next-line no-undef @@ -25,8 +7,8 @@ const path = require("path"); // eslint-disable-next-line no-undef let args = process.argv; -if(!Array.isArray(args)) return; -if(args.length < 3) { +if (!Array.isArray(args)) return; +if (args.length < 3) { // eslint-disable-next-line no-console console.error("Please provide a path to the survey JSON file."); return; @@ -75,7 +57,7 @@ function obfuscateJSON(data) { let questions = model.getAllQuestions(false, true, false); questions.forEach(q => { - if(q.getType() === "html") { + if (q.getType() === "html") { q.delete(); return; } @@ -106,22 +88,22 @@ function obfuscatePropsText(el, props) { prop => { let isDone = false; const loc = el["loc" + prop[0].toUpperCase() + prop.substring(1)]; - if(!!loc && !loc.isEmpty) { + if (!!loc && !loc.isEmpty) { data = loc.getJson(); - if(!!data && typeof data === "object") { - for(let key in data) { + if (!!data && typeof data === "object") { + for (let key in data) { data[key] = obfuscateText(data[key]); } loc.setJson(data); isDone = true; } } - if(!isDone && !!el[prop]) el[prop] = obfuscateText(el[prop]); + if (!isDone && !!el[prop]) el[prop] = obfuscateText(el[prop]); } ); } function obfuscateArrayText(items) { - if(Array.isArray(items)) { + if (Array.isArray(items)) { items.forEach(item => { obfuscatePropsText(item, ["text", "title"]); /* @@ -136,13 +118,13 @@ function obfuscateArrayText(items) { } } function obfuscateText(text) { - if(!text) return text; + if (!text) return text; let newText = ""; - for(let i = 0; i < text.length; i ++) { + for (let i = 0; i < text.length; i++) { const ch = text[i]; let newCh = ch; - if(ch >= "a" && ch <= "z") newCh = getRandomChar("a", "z"); - if(ch >= "A" && ch <= "Z") newCh = getRandomChar("A", "Z"); + if (ch >= "a" && ch <= "z") newCh = getRandomChar("a", "z"); + if (ch >= "A" && ch <= "Z") newCh = getRandomChar("A", "Z"); newText += newCh; } return newText; From f1d212f07bce455a95961f646e77a9f3e0b8f416 Mon Sep 17 00:00:00 2001 From: RomanTsukanov Date: Fri, 6 Dec 2024 10:44:53 +0400 Subject: [PATCH 3/3] Update utils/README.md Co-authored-by: Elena Gorbatkova <102306951+ElenaGorbatkova@users.noreply.github.com> --- utils/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/README.md b/utils/README.md index 70ea0c278d..028464f22c 100644 --- a/utils/README.md +++ b/utils/README.md @@ -1,6 +1,6 @@ # SurveyJS JSON Obfuscator -This tool is designed to obfuscate survey JSON schemas by replacing titles, descriptions, placeholders, and other meaningful texts with randomized sequences of characters. The obfuscation may be useful if you need to attach a survey JSON schema to a bug report for investigation by the SurveyJS team and want to strip the schema of all meaningful data. +This tool is designed to obfuscate survey JSON schemas by replacing titles, descriptions, placeholders, and other meaningful texts with randomized sequences of characters. The obfuscation may be useful if you need to attach a survey JSON schema to a bug report for investigation by the SurveyJS team and want to strip the schema of all sensitive data. Usage: