Skip to content

Commit

Permalink
Merge pull request #642 from juliandescottes/sanitize-strings
Browse files Browse the repository at this point in the history
sanitize strings coming from user inputs
  • Loading branch information
juliandescottes authored Feb 23, 2017
2 parents 6f4413f + 11a063d commit 62b1b8b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/js/controller/HeaderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}

if (this.piskelName_) {
this.piskelName_.innerHTML = name;
this.piskelName_.textContent = name;
}
} catch (e) {
console.warn('Could not update header : ' + e.message);
Expand Down
5 changes: 4 additions & 1 deletion src/js/controller/dialogs/BrowseLocalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@

keys.forEach((function (key) {
var date = pskl.utils.DateUtils.format(key.date, '{{Y}}/{{M}}/{{D}} {{H}}:{{m}}');
html += pskl.utils.Template.replace(this.localStorageItemTemplate_, {name : key.name, date : date});
html += pskl.utils.Template.replace(this.localStorageItemTemplate_, {
name : key.name,
date : date
});
}).bind(this));

var tableBody_ = this.piskelList.get(0).tBodies[0];
Expand Down
2 changes: 1 addition & 1 deletion src/js/controller/dialogs/ImportImageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
this.importedImage_.onload = function () {};

var fileName = this.extractFileNameFromPath_(this.file_.name);
this.fileNameContainer.html(fileName);
this.fileNameContainer.text(fileName);
this.fileNameContainer.attr('title', fileName);

this.resizeWidth.val(w);
Expand Down
2 changes: 1 addition & 1 deletion src/js/controller/settings/SaveController.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

ns.SaveController.prototype.insertSavePartials_ = function () {
this.getPartials_().forEach(function (partial) {
pskl.utils.Template.insert(this.saveForm, 'beforeend', partial);
this.saveForm.insertAdjacentHTML('beforeend', pskl.utils.Template.get(partial));
}.bind(this));
};

Expand Down
47 changes: 32 additions & 15 deletions src/js/utils/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,12 @@
},

createFromHTML : function (html) {
var dummyEl = document.createElement('div');
var dummyEl = ns.Template._getDummyEl();
dummyEl.innerHTML = html;
return dummyEl.children[0];
},

insert : function (parent, position, templateId, dict) {
var html = pskl.utils.Template.getAndReplace(templateId, dict);
parent.insertAdjacentHTML(position, html);
},
var element = dummyEl.children[0];
dummyEl.innerHTML = '';

getAndReplace : function (templateId, dict) {
var result = '';
var tpl = pskl.utils.Template.get(templateId);
if (tpl) {
result = pskl.utils.Template.replace(tpl, dict);
}
return result;
return element;
},

replace : function (template, dict) {
Expand All @@ -49,10 +38,38 @@
value = '';
}
}

// Sanitize all values expect if the key is surrounded by `!`
if (!/^!.*!$/.test(key)) {
value = ns.Template.sanitize(value);
}

template = template.replace(new RegExp('\\{\\{' + key + '\\}\\}', 'g'), value);
}
}
return template;
},

/**
* Sanitize the provided string to make it safer for using in templates.
*/
sanitize : function (string) {
var dummyEl = ns.Template._getDummyEl();

// Apply the unsafe string as text content and
dummyEl.textContent = string;
var sanitizedString = dummyEl.innerHTML;

dummyEl.innerHTML = '';

return sanitizedString;
},

_getDummyEl : function () {
if (!ns.Template._dummyEl) {
ns.Template._dummyEl = document.createElement('div');
}
return ns.Template._dummyEl;
}
};
})();
3 changes: 2 additions & 1 deletion src/js/utils/TooltipFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
return pskl.utils.Template.replace(tpl, {
helptext : helpText,
shortcut : shortcut,
descriptors : this.formatDescriptors_(descriptors)
// Avoid sanitization for descriptors (markup)
'!descriptors!' : this.formatDescriptors_(descriptors)
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/templates/misc-templates.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<script type="text/template" id="tooltip-container-template">
<div class='tooltip-container'>
<div>{{helptext}} <span class='tooltip-shortcut'>{{shortcut}}</span></div>
{{descriptors}}
{{!descriptors!}}
</div>
</script>

Expand Down

0 comments on commit 62b1b8b

Please sign in to comment.