I created a script to try to remove unsafe content at the time of injecting DOM (I'm using in extensions / addons for browsers):
var str = "<strong>Hello</strong> mundo <script src="http://site/badscript.js"></script>";CreateDOM(str);functionRemoveAttrs(target){varattrs=target.attributes,currentAttr;varvalidAttrs=["href", "class", "id", "target" ];
for (var i = attrs.length - 1; i >= 0; i--) {
currentAttr = attrs[i].name;
if (attrs[i].specified && validAttrs.indexOf(currentAttr) === -1) {
target.removeAttribute(currentAttr);
}
if (
currentAttr === "href" &&
/^(#|javascript[:])/gi.test(target.getAttribute("href"))
) {
target.parentNode.removeChild(currentAttr);
}
}
}
function RemoveEls(target)
{
var current;
//Remove elements insecure (blacklist)
var list = target.querySelectorAll("script,link,...");
for (var i = list.length - 1; i >= 0; i--) {
current = list[i];
current.parentNode.removeChild(current);
}
//Remove insecure attributes (whitelist)
list = target.getElementsByTagName("*");
for (i = list.length - 1; i >= 0; i--) {
RemoveAttrs(list[i]);
}
return target;
}
function CreateDOM(MinhaString)
{
var tmpDom = document.createElement("div");
tmpDom.innerHTML = MinhaString;
tmpDom = RemoveEls(tmpDom);
//Inject in container
document.getElementById("container").appendChild(tmpDom);
}
However, when submitting the extension to the link the moderator reviewed my code and sent me this message:
Your cleanDomString method is not safe, please replace: tmpDom.innerHTML = date; with: var tmpDom = (new DOMParser) .parseFromString (data, "text / html"). Body;
and remove: var tmpDom = document.createElement ("div");
or use: link
dmichnowicz; May 30, 2016 8:46:57 AM UTC
So I changed the code and it looked like this:
var str = "<strong>Hello</strong> mundo <script src="http://site/badscript.js"></script>";CreateDOM(str);functionRemoveAttrs(target){varattrs=target.attributes,currentAttr;varvalidAttrs=["href", "class", "id", "target" ];
for (var i = attrs.length - 1; i >= 0; i--) {
currentAttr = attrs[i].name;
if (attrs[i].specified && validAttrs.indexOf(currentAttr) === -1) {
target.removeAttribute(currentAttr);
}
if (
currentAttr === "href" &&
/^(#|javascript[:])/gi.test(target.getAttribute("href"))
) {
target.parentNode.removeChild(currentAttr);
}
}
}
function RemoveEls(target)
{
var current;
//Remove elements insecure (blacklist)
var list = target.querySelectorAll("script,link,...");
for (var i = list.length - 1; i >= 0; i--) {
current = list[i];
current.parentNode.removeChild(current);
}
//Remove insecure attributes (whitelist)
list = target.getElementsByTagName("*");
for (i = list.length - 1; i >= 0; i--) {
RemoveAttrs(list[i]);
}
return target;
}
function CreateDOM(MyString)
{
var tmpDom = (new DOMParser).parseFromString(MyString, "text/html").body;
tmpDom = RemoveEls(tmpDom);
//Inject in container
document.getElementById("container").appendChild(tmpDom);
}
Alright, I made the change as a request, but I did not understand where this improved security, it's just a curiosity at the study level.
What's the difference between the two in terms of security?