javascript - JSON.parse escaping for anti-XSS and any character insertion into html, attributes, and values from WebSockets - St

I've read the cheat sheet, but I'm still unsure how exactly my data should be escaped to prot

I've read the cheat sheet, but I'm still unsure how exactly my data should be escaped to protect against XSS while allowing any valid character to be inserted into HTML, attributes, and variable values. Potential variable values are regexed before being put into any function like parseInt. Data is only received via a WebSocket connection.

Is JSON.parse safe to call on any string? If not, how must the data be made safe via javascript or at least tested to see if it doesn't conform?

When should the HTML and attribute escaping be done relative to JSON.parse?

I've read the cheat sheet, but I'm still unsure how exactly my data should be escaped to protect against XSS while allowing any valid character to be inserted into HTML, attributes, and variable values. Potential variable values are regexed before being put into any function like parseInt. Data is only received via a WebSocket connection.

Is JSON.parse safe to call on any string? If not, how must the data be made safe via javascript or at least tested to see if it doesn't conform?

When should the HTML and attribute escaping be done relative to JSON.parse?

Share Improve this question edited Mar 8, 2014 at 19:04 asked Mar 8, 2014 at 18:18 user1382306user1382306
Add a ment  | 

1 Answer 1

Reset to default 4

You need to feed vaild json data into JSON parse functions. Typically whatever creates the json string needs to create valid json and therefore that's what needs to escape the html (and other) characters.

And if you google, 'how to escape json' you'll get a lot of sites which show how to do it halfway.

Most will point out a small group of chars and say do this:

\b  Backspace (ascii code 08)
\f  Form feed (ascii code 0C)
\n  New line
\r  Carriage return
\t  Tab
\v  Vertical tab
\'  Apostrophe or single quote
\"  Double quote
\\  Backslash caracter

This is partially correct. You need to escape:

  • quotation mark (U+0022)
  • reverse solidus (U+005C)
  • control characters U+0000 to U+001
    which is everything below ascii32 (space)

    see: http://la.remifa.so/unicode/unicode.php?start=0000&end=007F

These characters get escaped by using \u + hexadecimal ie. "\u002F"

Here's the spec: http://www.ecma-international/publications/files/ECMA-ST/ECMA-404.pdf

Personally, I only use the \u-hex notation for all escape sequences and I never worry about if the json might be used inside of the JavaScript context.

Json and Javascript are pretty cool in that you could escape every char as \u+hex if you wanted which makes XXS pretty much impossible (especially when inside of double quotes).

Update:

Keep in mind escaping the json is only 1 part of a plete XSS safe site. You still need to worry about how the json string might be used, as it could be passed into a function as an argument or you might create an array with the data or you might place the string inside of an document.getElementById('xyz').innerHTML(json.data)

So if the json data stays in the javascript context, you're safe to use \u+hex escaping.

When the string is moving into the html context, you need to treat it as html:

 document.getElementById('xyz').innerHTML(json.data) //oh-no: now it will in html context

So you need to convert the JSON data with a function like this:

var __entityMap = {
    "&": "&",
    "<": "&lt;",
    ">": "&gt;",
    '"': '&quot;',
    "'": '&#39;',
    "/": '&#x2F;'
};

String.prototype.toHtml = function() {
    return String(this).replace(/[&<>"'\/]/g, function (s) {  
        return __entityMap[s];
    });
}

So now you can do this:

document.getElementById('xyz').innerHTML(json.data.toHtml() ) //ok -- now safe for html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745443994a4627958.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信