Skip to content

Commit

Permalink
Moved config values to separate JSON file
Browse files Browse the repository at this point in the history
Closes #8.
  • Loading branch information
the-c0d3br34k3r authored and silvs110 committed Apr 5, 2024
1 parent c1eca17 commit 201e6c2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<script src="script/script.js"></script>
</head>

<body>
<body onload="init()">
<h1 id="title">Sensitive Info Sanitizer</h1>
<div id="uploader">
<input type="file" id="upload_button" name="upload_button" class="form-control"
Expand Down
15 changes: 15 additions & 0 deletions script/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Diff2HtmlConfiguration": {
"drawFileList": false,
"fileListToggle": false,
"fileListStartVisible": false,
"fileContentToggle": true,
"matching": "lines",
"outputFormat": "line-by-line",
"synchronisedScroll": true,
"highlight": true,
"renderNothingWhenEmpty": false
},
"SupportedFileExtensions": ["har"],
"SupportedActions": ["remove"]
}
87 changes: 52 additions & 35 deletions script/script.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
const diff2HtmlConfiguration = {
drawFileList: false,
fileListToggle: false,
fileListStartVisible: false,
fileContentToggle: true,
matching: 'lines',
outputFormat: 'line-by-line',
synchronisedScroll: true,
highlight: true,
renderNothingWhenEmpty: false,
};

let config = {};
let result = null;
let sanitizedFileName = null;
let supportedFileExtensions = ['har'];
let supportedActions = ['remove'];

const setConfigItem = (path, value, obj) => {
function init() {
console.log('Initializing...')
fetch("script/config.json")
.then(response => response.json())
.then((data) => {
config = data;
for (const supportedFileExtension of getConfig("SupportedFileExtensions", [])) {
// Load rules
const ruleFilePath = "rules/" + supportedFileExtension + ".yaml";
console.log("Loading " + ruleFilePath);
fetch(ruleFilePath)
.then(response => response.text())
.then((data) => {
localStorage.setItem(supportedFileExtension, data);
}).catch(error => errorFollowUp(error));
}
}).catch(error => errorFollowUp(error));
}

function errorFollowUp(message) {
console.error(message);
alert(message);
}

function getConfig(key, defaultValue=null) {
const typeOfKey = typeof key;
let errorMessage = null;

if(typeOfKey !== "string") {
errorMessage = `InvalidConfigKeyType: ${key} is of type ${typeOfKey}.
It should be a string.`;
} else if(!(key in config)) {
errorMessage = `ConfigKeyNotFound: ${key} not in config.`;
}

if(errorMessage != null) {
errorFollowUp(errorMessage);
return defaultValue;
}

return config[key];
}

const setItem = (path, value, obj) => {
// reduce the path array, each iteration dig further into the object properties
path.reduce((accumulator, key, i) => {
// if you are at the final key set the value
Expand All @@ -34,28 +64,15 @@ const setConfigItem = (path, value, obj) => {
return obj;
};

for (const supportedFileExtension of supportedFileExtensions) {
// Load rules
const ruleFilePath = "rules/" + supportedFileExtension + ".yaml";
console.log("Loading " + ruleFilePath);
fetch(ruleFilePath)
.then(response => response.text())
.then((data) => {
localStorage.setItem(supportedFileExtension, data);
}).catch(error => console.error(error));
}

async function sanitizeContent(file) {
document.getElementById("display_panel").hidden = false;
showSpinner();
const fileName = file.name;
const fileNameParts = file.name.split('.');
const fileExtension = fileNameParts.pop();

if (!supportedFileExtensions.includes(fileExtension)) {
const error = `Unsupported file extension: ${fileExtension}`;
console.error(error);
alert(error);
if (!getConfig("SupportedFileExtensions", {}).includes(fileExtension)) {
errorFollowUp(`UnsupportedFileExtension: ${fileExtension}`)
hideSpinner();
return;
}
Expand All @@ -66,8 +83,7 @@ async function sanitizeContent(file) {
try {
original_content = JSON.parse(content);
} catch (error) {
console.error(error);
alert(error);
errorFollowUp(error);
hideSpinner();
return;
}
Expand All @@ -89,7 +105,7 @@ async function sanitizeContent(file) {
const sanitizeActions = {};
for (const path in rules) {
const action = rules[path]["action"];
if (!supportedActions.includes(action)) {
if (!getConfig("SupportedActions", []).includes(action)) {
console.warn(`Skipping rule "${rules[path]["description"]}
- Unsupported action "${action}.`);
continue;
Expand All @@ -110,7 +126,7 @@ async function sanitizeContent(file) {
const path = sanitizeActions[action][index];
console.log(`${action} "${path}"`);
if (action === 'remove') {
result = setConfigItem(path.slice(1).split('/'), "<REMOVED>", result);
result = setItem(path.slice(1).split('/'), "<REMOVED>", result);
} else {
console.warn(`Unsupported ${action} on "${path}"`);
}
Expand All @@ -121,7 +137,8 @@ async function sanitizeContent(file) {
JSON.stringify(original_content, undefined, 2),
JSON.stringify(result, undefined, 2));
const targetElement = document.getElementById('sanitized_diff_div');
const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, diff2HtmlConfiguration);
const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString,
getConfig("Diff2HtmlConfiguration", {}));
diff2htmlUi.draw();
diff2htmlUi.highlightCode();
document.getElementById("download_button").disabled = false;
Expand Down

0 comments on commit 201e6c2

Please sign in to comment.