I have a usecase where the contents of the mustache HTML template could potentially come from the application/end-user (i.e. The content of the script tag in the below code snippet.)
<script id="template" type="x-tmpl-mustache">
Hello {{ name }}!
</script>
As this could potentially lead to execution of malicious code, I'm doing
Is there anything further that needs to be considered for security of the application?
I think it's not a "moustache" problem if we follow the philosophy "small, sharp tools". Then before mapping an unsecure data (third party JSON) to template you are to validate the data with other tools.
The simplest approach to start with is to replace string fields, contaning unsecure data.
function clearJson(userStringData){
return JSON.parse(userStringData, function(k,v) {
// string values containg something like
// html tags or js block braces will not pass
return String(v).match('[<>{}]') ? 'UNSAFE' : v;
});
}
The field of code injection is too wide to have a short answer on your question. You may apply any approach which is advanced enough for your application: define data formats the application expects from user and then in runtime remove incoming suspicious data not matching these formats.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With