I have a simple app that posts simple data as statuses to FB. Unfortunately, I have found that FB does not sanitize the data coming in from their API, or going out of their API. To deal with this, briefly, the answer is to sanitize responses from their API.
In short, what I would like is indicated on line 36 in the following code snippet. (As an example test, once you're logged into FB and have given this app permission to post on your behalf, post the string "<iframe src="http://www.google.com"></iframe>" by pressing enter. This will show up sanitized on your wall within FB, but an iframe will appear in this simple page).
<!DOCTYPE html>
<html>
<head>
<title>Sanitize my FB Data</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
type="text/javascript"></script>
<script src="../../lib/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<input id="fbPost" type="text">
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function () {
FB.init({
appId : "my_app_id",
status : true,
cookie : true,
xfbml : true,
oauth : true
});
$("#fbPost").keyup(function (event) {
if (event.keyCode == 13) {
var data = {
message: $("#fbPost").val()
};
FB.api('/me/feed', 'post', data, function (resp) {
var _index = resp.id.indexOf("_");
var postId = resp.id.substr(_index + 1);
FB.api('/' + postId, function (postData) {
// This line is insecure!
$("body").prepend(postData.message);
// Would like: sanitize(postData.message)
});
});
}
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
<div class="fb-login-button"
scope="read_stream,publish_stream">
Login with Facebook
</div>
</body>
</html>
What I would like to do is, in short, sanitize data in JavaScript. For an app that uses only the JavaScript SDK to read/write FB data, and would prefer remaining hands off to FB user information entirely on the server, this is an obvious problem.
I've checked out this answer, using the internal sanitizer in Caja, but unfortunately, it requires use of the 'html4' global object. I'm unaware of any other input sanitizers in JavaScript.
So: are there any comprehensive input sanitizers available in JavaScript today?