Skip to content

Commit

Permalink
fix(xss): don't use innerHTML to set text (#1242)
Browse files Browse the repository at this point in the history
We had a lot of instances of innerHTML that were simply setting text, in
quite a few cases parsing it as HTML was even counter productive as it
could theoreticly result in syntax errors and corrupted output (e.g. the
output of JSON.stringify definitely shouldn't be parsed as HTML).

There is only one occurence of innerHTML left and that is for the
node/edge title as the docs clearly state that any string passed will be
parsed as HTML.
  • Loading branch information
Thomaash authored Dec 27, 2020
1 parent 5a183d7 commit cda306a
Show file tree
Hide file tree
Showing 24 changed files with 229 additions and 108 deletions.
4 changes: 3 additions & 1 deletion cypress/pages/pollution.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
);

const root = document.getElementById(id);
root.innerText = "";
while (root.firstChild) {
root.removeChild(root.firstChild);
}

const table = document.createElement("table");
["added", "changed", "missing"].forEach((key) => {
Expand Down
6 changes: 3 additions & 3 deletions docs-kr/network/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
<script>
function toggleGettingStarted(aThis) {
var gettingStartedDiv = document.getElementById("gettingStarted");
if (aThis.innerHTML.indexOf("Show") !== -1) {
if (aThis.innerText.indexOf("Show") !== -1) {
gettingStartedDiv.className = "";
aThis.innerHTML = "Hide the getting started again.";
aThis.innerText = "Hide the getting started again.";
} else {
gettingStartedDiv.className = "hidden";
aThis.innerHTML = "Show the getting started!";
aThis.innerText = "Show the getting started!";
}
}
</script>
Expand Down
6 changes: 3 additions & 3 deletions docs/network/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
<script>
function toggleGettingStarted(aThis) {
var gettingStartedDiv = document.getElementById("gettingStarted");
if (aThis.innerHTML.indexOf("Show") !== -1) {
if (aThis.innerText.indexOf("Show") !== -1) {
gettingStartedDiv.className = "";
aThis.innerHTML = "Hide the getting started again.";
aThis.innerText = "Hide the getting started again.";
} else {
gettingStartedDiv.className = "hidden";
aThis.innerHTML = "Show the getting started!";
aThis.innerText = "Show the getting started!";
}
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions examples/network/data/dynamicData.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
// create an array with nodes
nodes = new vis.DataSet();
nodes.on("*", function () {
document.getElementById("nodes").innerHTML = JSON.stringify(
document.getElementById("nodes").innerText = JSON.stringify(
nodes.get(),
null,
4
Expand All @@ -147,7 +147,7 @@
// create an array with edges
edges = new vis.DataSet();
edges.on("*", function () {
document.getElementById("edges").innerHTML = JSON.stringify(
document.getElementById("edges").innerText = JSON.stringify(
edges.get(),
null,
4
Expand Down
4 changes: 2 additions & 2 deletions examples/network/data/importingFromGephi.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ <h4>Node Content:</h4>
network.on("click", function (params) {
if (params.nodes.length > 0) {
var data = nodes.get(params.nodes[0]); // get the data from selected node
nodeContent.innerHTML = JSON.stringify(data, undefined, 3); // show the data in the div
nodeContent.innerText = JSON.stringify(data, undefined, 3); // show the data in the div
}
});

Expand Down Expand Up @@ -179,7 +179,7 @@ <h4>Node Content:</h4>
edges.add(parsed.edges);

var data = nodes.get(2); // get the data from node 2 as example
nodeContent.innerHTML = JSON.stringify(data, undefined, 3); // show the data in the div
nodeContent.innerText = JSON.stringify(data, undefined, 3); // show the data in the div
network.fit(); // zoom to fit
}
</script>
Expand Down
14 changes: 10 additions & 4 deletions examples/network/edgeStyles/arrowTypes.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@

// update list of arrow types in html body
var nof_types = arrow_types.length;
var list_contents = [];
var mylist = document.getElementById("arrow_type_list");
while (mylist.firstChild) {
mylist.removeChild(mylist.firstChild);
}
for (var i = 0; i < nof_types; i++) {
list_contents.push("<code>'" + arrow_types[i] + "'</code>");
if (i > 0) {
mylist.appendChild(document.createTextNode(", "));
}
const code = document.createElement("code");
code.innerText = arrow_types[i];
mylist.appendChild(code);
}
var mylist = document.getElementById("arrow_type_list");
mylist.innerHTML = list_contents.join(", ");

// create an array with nodes
var node_attrs = new Array();
Expand Down
86 changes: 64 additions & 22 deletions examples/network/events/interactionEvents.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
</p>

<div id="mynetwork"></div>
<pre id="eventSpan"></pre>

<h2 id="eventSpanHeading"></h2>
<pre id="eventSpanContent"></pre>

<script type="text/javascript">
// create an array with nodes
Expand Down Expand Up @@ -61,23 +63,36 @@

network.on("click", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>Click event:</h2>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText = "Click event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
console.log(
"click event, getNodeAt returns: " +
this.getNodeAt(params.pointer.DOM)
);
});
network.on("doubleClick", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>doubleClick event:</h2>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"doubleClick event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
});
network.on("oncontext", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>oncontext (right click) event:</h2>" +
JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"oncontext (right click) event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
});
network.on("dragStart", function (params) {
// There's no point in displaying this event on screen, it gets immediately overwritten
Expand All @@ -90,13 +105,23 @@
});
network.on("dragging", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>dragging event:</h2>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"dragging event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
});
network.on("dragEnd", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>dragEnd event:</h2>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"dragEnd event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
console.log("dragEnd Event:", params);
console.log(
"dragEnd event, getNodeAt returns: " +
Expand All @@ -105,24 +130,41 @@
});
network.on("controlNodeDragging", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>control node dragging event:</h2>" +
JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"control node dragging event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
});
network.on("controlNodeDragEnd", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>control node drag end event:</h2>" +
JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"control node drag end event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
console.log("controlNodeDragEnd Event:", params);
});
network.on("zoom", function (params) {
document.getElementById("eventSpan").innerHTML =
"<h2>zoom event:</h2>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText = "zoom event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
});
network.on("showPopup", function (params) {
document.getElementById("eventSpan").innerHTML =
"<h2>showPopup event: </h2>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"showPopup event: ";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
});
network.on("hidePopup", function () {
console.log("hidePopup Event");
Expand Down
32 changes: 23 additions & 9 deletions examples/network/events/physicsEvents.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
<p>Create a simple network with some nodes and edges.</p>

<div id="mynetwork"></div>
<pre id="eventSpan"></pre>

<h2 id="eventSpanHeading"></h2>
<pre id="eventSpanContent"></pre>

<script type="text/javascript">
// create an array with nodes
var nodes = new vis.DataSet([
Expand Down Expand Up @@ -49,23 +52,34 @@
var network = new vis.Network(container, data, options);

network.on("startStabilizing", function (params) {
document.getElementById("eventSpan").innerHTML =
"<h3>Starting Stabilization</h3>";
document.getElementById("eventSpanHeading").innerText =
"Starting Stabilization";
document.getElementById("eventSpanContent").innerText = "";
console.log("started");
});
network.on("stabilizationProgress", function (params) {
document.getElementById("eventSpan").innerHTML =
"<h3>Stabilization progress</h3>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText =
"Stabilization progress";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
console.log("progress:", params);
});
network.on("stabilizationIterationsDone", function (params) {
document.getElementById("eventSpan").innerHTML =
"<h3>Stabilization iterations complete</h3>";
document.getElementById("eventSpanHeading").innerText =
"Stabilization iterations complete";
document.getElementById("eventSpanContent").innerText = "";
console.log("finished stabilization interations");
});
network.on("stabilized", function (params) {
document.getElementById("eventSpan").innerHTML =
"<h3>Stabilized!</h3>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText = "Stabilized!";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
console.log("stabilized!", params);
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions examples/network/exampleApplications/loadingBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,11 @@
var width = Math.max(minWidth, maxWidth * widthFactor);

document.getElementById("bar").style.width = width + "px";
document.getElementById("text").innerHTML =
document.getElementById("text").innerText =
Math.round(widthFactor * 100) + "%";
});
network.once("stabilizationIterationsDone", function () {
document.getElementById("text").innerHTML = "100%";
document.getElementById("text").innerText = "100%";
document.getElementById("bar").style.width = "496px";
document.getElementById("loadingBar").style.opacity = 0;
// really clean the dom element
Expand Down
12 changes: 9 additions & 3 deletions examples/network/labels/labelAlignment.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
</p>

<div id="mynetwork"></div>
<pre id="eventSpan"></pre>

<h2 id="eventSpanHeading"></h2>
<pre id="eventSpanContent"></pre>

<script type="text/javascript">
// create an array with nodes
Expand Down Expand Up @@ -77,8 +79,12 @@

network.on("click", function (params) {
params.event = "[original event]";
document.getElementById("eventSpan").innerHTML =
"<h2>Click event:</h2>" + JSON.stringify(params, null, 4);
document.getElementById("eventSpanHeading").innerText = "Click event:";
document.getElementById("eventSpanContent").innerText = JSON.stringify(
params,
null,
4
);
});
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion examples/network/layout/hierarchicalLayout.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

// add event listeners
network.on("select", function (params) {
document.getElementById("selection").innerHTML =
document.getElementById("selection").innerText =
"Selection: " + params.nodes;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ <h3>Hierarchical Layout Overlap Avoidance</h3>

// add event listeners
network.on("select", function (params) {
document.getElementById("selection").innerHTML =
document.getElementById("selection").innerText =
"Selection: " + params.nodes;
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/network/layout/hierarchicalLayoutUserdefined.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@

// add event listeners
network.on("select", function (params) {
document.getElementById("selection").innerHTML =
document.getElementById("selection").innerText =
"Selection: " + params.nodes;
});
}
Expand Down
Loading

0 comments on commit cda306a

Please sign in to comment.