-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to do client side HTML sanitiziation from a string? #10
Comments
I guess the DOM node being cleaned should be detached or even better be inside a new document created by |
Would this be a safe way of doing it? Sanitize.prototype.clean_string = function(string) {
var elm = document.implementation.createHTMLDocument().body;
elm.innerHTML = string;
cleaned_fragment = this.clean_node(elm);
elm = document.implementation.createHTMLDocument().body;
elm.appendChild(cleaned_fragment);
return elm.innerHTML;
}; |
Here's an example I've been using to go from string --> sanitized string: var input = '<p id="para1"><b><a href="http://foo.com/">foo</a></b><img src="http://foo.com/bar.jpg" /></p>';
// Wrap input in a dummy element (required for Santize)
var dummyInputNode = document.createElement('div');
dummyInputNode.innerHTML = input;
var scrubber = new Sanitizer(this.sanitizeConfig);
// Get a DocumentFragment back after cleaning
var cleanFragment = scrubber.clean_node(dummyInputNode);
// Wrap the fragment in a div in order to generate HTML from fragment
var dummyOutputNode = document.createElement('div');
dummyOutputNode.appendChild(cleanFragment.cloneNode(true));
// Get the html string from inside the div
var cleanHTML = dummyOutputNode.innerHTML; |
@jasonseney without |
@wader - Thank you for tip. Do you happen to have an example handy that replicates this behavior? |
Sure, take a look at http://jsfiddle.net/sYdJy/ |
Is there a "recommended" way from the author? I guess, there should be. |
P.S. It's not always easy to generate HTML from a string, e.g you can't just insert a |
The way @wader given works well even with scripts. |
Hello, should I be able to do safe client side HTML sanitiziation from a string using Sanitize.js? as it seems to use an existing DOM tree should I use innerHTML to create the tree first? this feels unsafe so i did a small test and it seems to be a bad idea.
This will popup and alert dialog.
The text was updated successfully, but these errors were encountered: