| 1 |
/*global unescape */ |
| 2 |
/*jshint curly: false, scripturl: true */ |
| 3 |
|
| 4 |
/* |
| 5 |
|
| 6 |
The MIT License (MIT) |
| 7 |
|
| 8 |
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors. |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person |
| 11 |
obtaining a copy of this software and associated documentation files |
| 12 |
(the "Software"), to deal in the Software without restriction, |
| 13 |
including without limitation the rights to use, copy, modify, merge, |
| 14 |
publish, distribute, sublicense, and/or sell copies of the Software, |
| 15 |
and to permit persons to whom the Software is furnished to do so, |
| 16 |
subject to the following conditions: |
| 17 |
|
| 18 |
The above copyright notice and this permission notice shall be |
| 19 |
included in all copies or substantial portions of the Software. |
| 20 |
|
| 21 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 25 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 26 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 27 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 28 |
SOFTWARE. |
| 29 |
*/ |
| 30 |
|
| 31 |
// |
| 32 |
// trivial bookmarklet/escaped script detector for the javascript beautifier |
| 33 |
// written by Einar Lielmanis <[email protected]> |
| 34 |
// |
| 35 |
// usage: |
| 36 |
// |
| 37 |
// if (Urlencoded.detect(some_string)) { |
| 38 |
// var unpacked = Urlencoded.unpack(some_string); |
| 39 |
// } |
| 40 |
// |
| 41 |
// |
| 42 |
|
| 43 |
/*jshint strict:false */ |
| 44 |
|
| 45 |
|
| 46 |
var isNode = (typeof module !== 'undefined' && module.exports); |
| 47 |
if (isNode) { |
| 48 |
var SanityTest = require(__dirname + '/../../test/sanitytest'); |
| 49 |
} |
| 50 |
|
| 51 |
var Urlencoded = { |
| 52 |
detect: function(str) { |
| 53 |
// the fact that script doesn't contain any space, but has %20 instead |
| 54 |
// should be sufficient check for now. |
| 55 |
if (str.indexOf(' ') === -1) { |
| 56 |
if (str.indexOf('%2') !== -1) return true; |
| 57 |
if (str.replace(/[^%]+/g, '').length > 3) return true; |
| 58 |
} |
| 59 |
return false; |
| 60 |
}, |
| 61 |
|
| 62 |
unpack: function(str) { |
| 63 |
if (Urlencoded.detect(str)) { |
| 64 |
if (str.indexOf('%2B') !== -1 || str.indexOf('%2b') !== -1) { |
| 65 |
// "+" escaped as "%2B" |
| 66 |
return unescape(str.replace(/\+/g, '%20')); |
| 67 |
} else { |
| 68 |
return unescape(str); |
| 69 |
} |
| 70 |
} |
| 71 |
return str; |
| 72 |
}, |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
run_tests: function(sanity_test) { |
| 77 |
var t = sanity_test || new SanityTest(); |
| 78 |
t.test_function(Urlencoded.detect, "Urlencoded.detect"); |
| 79 |
t.expect('', false); |
| 80 |
t.expect('var a = b', false); |
| 81 |
t.expect('var%20a+=+b', true); |
| 82 |
t.expect('var%20a=b', true); |
| 83 |
t.expect('var%20%21%22', true); |
| 84 |
t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();', true); |
| 85 |
t.test_function(Urlencoded.unpack, 'Urlencoded.unpack'); |
| 86 |
|
| 87 |
t.expect('javascript:(function(){var%20whatever={init:function(){alert(%22a%22+%22b%22)}};whatever.init()})();', |
| 88 |
'javascript:(function(){var whatever={init:function(){alert("a"+"b")}};whatever.init()})();' |
| 89 |
); |
| 90 |
t.expect('', ''); |
| 91 |
t.expect('abcd', 'abcd'); |
| 92 |
t.expect('var a = b', 'var a = b'); |
| 93 |
t.expect('var%20a=b', 'var a=b'); |
| 94 |
t.expect('var%20a=b+1', 'var a=b+1'); |
| 95 |
t.expect('var%20a=b%2b1', 'var a=b+1'); |
| 96 |
return t; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
}; |
| 101 |
|
| 102 |
if (isNode) { |
| 103 |
module.exports = Urlencoded; |
| 104 |
} |