Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a description to JSON obfuscator #9139

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions utils/README.md
Original file line number Diff line number Diff line change
@@ -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.
RomanTsukanov marked this conversation as resolved.
Show resolved Hide resolved

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.
203 changes: 106 additions & 97 deletions utils/json_obfuscator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,124 +7,133 @@ 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 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 => {
Expand Down
Loading