가 여기에 official JavaScript parser가하는 작업은 다음과 같습니다를 제외
// In the second stage, we run the text against regular expressions that look
// for non-JSON patterns. We are especially concerned with '()' and 'new'
// because they can cause invocation, and '=' because it can cause mutation.
// But just to be safe, we want to reject all unexpected forms.
// We split the second stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/.
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
// In the third stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
j = eval('(' + text + ')');
...
(가) 내장 된 최신 브라우저에 JSON parsing support,이 모든 (라이브러리 기반) 보안 JSON 파서가하는 일 (즉, 정규식 eval
전 시험).
안전 라이브러리
프로토 타입의 isJSON
기능 (공식 json2 구현에 추가).
Mootools 'JSON.decode
함수 (다시 regex test before eval
통해).
안전하지 않은 라이브러리 :
도장의 fromJson
하지 하지 보안 eval
ING를 제공합니다. Here is their entire implementation (minus comments) :
dojo.fromJson = function(json) {
return eval("(" + json + ")");
}
jQuery를이 보내고 안전한 JSON eval
'을 제공하지만, 공식 플러그인의 secureEvalJSON
기능 (라인 143)를 참조하지 않습니다. http://code.google.com/p/json-sans-eval/에서
정규식 중 하나입니다. – Hugoware
사실, 일부 JSON 파서는 정규식을 건너 뛰는 안전 모드가 있습니다. 이 모드는 빠르지 만 안전성이 떨어집니다. –
@Henrik : 덜 안전하다면 왜 "안전 모드"라고 부릅니까? ;) –