Created
December 1, 2013 21:57
-
-
Save daogurtsov/7741368 to your computer and use it in GitHub Desktop.
Revisions
-
daogurtsov created this gist
Dec 1, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ var entityMap = { "&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/' }; var htmlMap = { "&": "&", "<": "<", ">": ">", '"': '"', ''': "'", '/': "/" }; function escapeHtml(string) { return String(string).replace(/[&<>"'\/]/g, function (s) { return entityMap[s]; }); } function parseHtml(string) { return String(string).replace(/&|<|>|"|'|//g, function (s) { return htmlMap[s]; }); } var json = { key1: "<IMG SRC=JaVaScRiPt:alert('XSS')>", key2: { key3: "></SCRIPT>\">'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>", key4: "'';!--\"<XSS>=&{()}", key5:[{key6:"<SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT>"},{key7:"<IMG SRC=\"javascript:alert('XSS');\">"},"<IMG SRC=JaVaScRiPt:alert('XSS')>", {key9:{ key10:"<SCRIPT/SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>",key11:"<iframe src=http://ha.ckers.org/scriptlet.html <" }}] } }; function recursiveJsonParse(json, func) { return $.each(json, function(key, val) { if(val){ if (typeof val == "string"){ json[key] = func(val); } else if (typeof val != "number") { json[key] = recursiveJsonParse(val, func); } } }); }; console.log( json ); var parsed = recursiveJsonParse(json, escapeHtml); console.log( parsed ); // var reverse = recursiveJsonParse(parsed,parseHtml); // console.log( reverse );