XSS
Is XSS an issue with selectize.js if I'm not actually submitting any data to a server? Is there any way they can hack a separate submitable form that is on the same page that a selectize input is on?
No, XSS is not possible unless you use a custom render function. So just don't use a custom render function with this library and you are safe.
XSS could still be possible when you render your <select> in HTML, but that's the same even if you don't use selectize.js.
Method function at line 3239 of selectize.js gets user input for the value element. This element’s value then flows through the code without being properly sanitized or validated and is eventually displayed to the user in method function at line 2186 of selectize.js.
.... 3257. return self.renderCache[templateName][value];
2280. $dropdown_content.prepend(self.render('option_create', {input: query}));
Any solution for this potential XSS attack issue?
You are correct, all user input should be sanitized when being presented to the user. However the content that selectize is processing here is coming from you, the developer.
When you "selectize()" an HTML element on the page, you need to make ensure that its been sanitized. This is no different than rendering HTML from the server in any other context.
<div class="control-group">
<label for="select-xss">Beast:</label>
<select id="select-xss" class="demo-default" data-placeholder="Select an exploit...">
<option value="">None</option>
<option value="1">alert('hi!')</option>
<option value="2">%3Cscript%3Eevil_script()%3C/script%3E</option>
</select>
</div>
<script>
$('#select-beast-empty').selectize({
allowEmptyOption: true,
create: true
});
</script>
The above is bad even without selectize being involved. If you allow user input to be directly rendered onto the page and executed, whether by selectize or any other JS on-page, then you're in a heap of trouble. It should be properly escaped.
@arunn87 - if you followed the call to render, you would see escape_html is called. The query parameter is not rendered to the page on that line, it's passed to the render function which does escape it.
If you can provide a sample of safe and sanitized HTML being executed by selectize, I can dig in further.
Until then, closing this issue.
Just to clarify for others reading this:
You can safely use a custom render function in Selectize — just make sure to use the escape function that’s passed as the second argument. Here’s a basic example:
render: {
option: function(item, escape) {
return `<div>${escape(item.name)}</div>`;
}
}
The escape() function makes sure that any special HTML characters (like <, >, ", etc.) are safely escaped to prevent XSS. As long as you use it wherever you're inserting dynamic content, you're good.