diff options
Diffstat (limited to 'node_modules/safe-regex')
-rw-r--r-- | node_modules/safe-regex/.travis.yml | 4 | ||||
-rw-r--r-- | node_modules/safe-regex/LICENSE | 18 | ||||
-rw-r--r-- | node_modules/safe-regex/example/safe.js | 3 | ||||
-rw-r--r-- | node_modules/safe-regex/index.js | 43 | ||||
-rw-r--r-- | node_modules/safe-regex/package.json | 78 | ||||
-rw-r--r-- | node_modules/safe-regex/readme.markdown | 65 | ||||
-rw-r--r-- | node_modules/safe-regex/test/regex.js | 50 |
7 files changed, 261 insertions, 0 deletions
diff --git a/node_modules/safe-regex/.travis.yml b/node_modules/safe-regex/.travis.yml new file mode 100644 index 0000000..cc4dba2 --- /dev/null +++ b/node_modules/safe-regex/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "0.8" + - "0.10" diff --git a/node_modules/safe-regex/LICENSE b/node_modules/safe-regex/LICENSE new file mode 100644 index 0000000..ee27ba4 --- /dev/null +++ b/node_modules/safe-regex/LICENSE @@ -0,0 +1,18 @@ +This software is released under the MIT license: + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/safe-regex/example/safe.js b/node_modules/safe-regex/example/safe.js new file mode 100644 index 0000000..f486f59 --- /dev/null +++ b/node_modules/safe-regex/example/safe.js @@ -0,0 +1,3 @@ +var safe = require('../'); +var regex = process.argv.slice(2).join(' '); +console.log(safe(regex)); diff --git a/node_modules/safe-regex/index.js b/node_modules/safe-regex/index.js new file mode 100644 index 0000000..488f501 --- /dev/null +++ b/node_modules/safe-regex/index.js @@ -0,0 +1,43 @@ +var parse = require('ret'); +var types = parse.types; + +module.exports = function (re, opts) { + if (!opts) opts = {}; + var replimit = opts.limit === undefined ? 25 : opts.limit; + + if (isRegExp(re)) re = re.source; + else if (typeof re !== 'string') re = String(re); + + try { re = parse(re) } + catch (err) { return false } + + var reps = 0; + return (function walk (node, starHeight) { + if (node.type === types.REPETITION) { + starHeight ++; + reps ++; + if (starHeight > 1) return false; + if (reps > replimit) return false; + } + + if (node.options) { + for (var i = 0, len = node.options.length; i < len; i++) { + var ok = walk({ stack: node.options[i] }, starHeight); + if (!ok) return false; + } + } + var stack = node.stack || (node.value && node.value.stack); + if (!stack) return true; + + for (var i = 0; i < stack.length; i++) { + var ok = walk(stack[i], starHeight); + if (!ok) return false; + } + + return true; + })(re, 0); +}; + +function isRegExp (x) { + return {}.toString.call(x) === '[object RegExp]'; +} diff --git a/node_modules/safe-regex/package.json b/node_modules/safe-regex/package.json new file mode 100644 index 0000000..42ddd91 --- /dev/null +++ b/node_modules/safe-regex/package.json @@ -0,0 +1,78 @@ +{ + "_args": [ + [ + "safe-regex@1.1.0", + "/home/dstaesse/git/website" + ] + ], + "_development": true, + "_from": "safe-regex@1.1.0", + "_id": "safe-regex@1.1.0", + "_inBundle": false, + "_integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=", + "_location": "/safe-regex", + "_phantomChildren": {}, + "_requested": { + "type": "version", + "registry": true, + "raw": "safe-regex@1.1.0", + "name": "safe-regex", + "escapedName": "safe-regex", + "rawSpec": "1.1.0", + "saveSpec": null, + "fetchSpec": "1.1.0" + }, + "_requiredBy": [ + "/regex-not", + "/to-regex" + ], + "_resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", + "_spec": "1.1.0", + "_where": "/home/dstaesse/git/website", + "author": { + "name": "James Halliday", + "email": "mail@substack.net", + "url": "http://substack.net" + }, + "bugs": { + "url": "https://github.com/substack/safe-regex/issues" + }, + "dependencies": { + "ret": "~0.1.10" + }, + "description": "detect possibly catastrophic, exponential-time regular expressions", + "devDependencies": { + "tape": "^3.5.0" + }, + "homepage": "https://github.com/substack/safe-regex", + "keywords": [ + "catastrophic", + "exponential", + "regex", + "safe", + "sandbox" + ], + "license": "MIT", + "main": "index.js", + "name": "safe-regex", + "repository": { + "type": "git", + "url": "git://github.com/substack/safe-regex.git" + }, + "scripts": { + "test": "tape test/*.js" + }, + "testling": { + "files": "test/*.js", + "browsers": [ + "ie/8", + "ie/9", + "ie/10", + "firefox/latest", + "chrome/latest", + "opera/latest", + "safari/latest" + ] + }, + "version": "1.1.0" +} diff --git a/node_modules/safe-regex/readme.markdown b/node_modules/safe-regex/readme.markdown new file mode 100644 index 0000000..83673ac --- /dev/null +++ b/node_modules/safe-regex/readme.markdown @@ -0,0 +1,65 @@ +# safe-regex + +detect potentially +[catastrophic](http://regular-expressions.mobi/catastrophic.html) +[exponential-time](http://perlgeek.de/blog-en/perl-tips/in-search-of-an-exponetial-regexp.html) +regular expressions by limiting the +[star height](https://en.wikipedia.org/wiki/Star_height) to 1 + +WARNING: This module merely *seems* to work given all the catastrophic regular +expressions I could find scouring the internet, but I don't have enough of a +background in automata to be absolutely sure that this module will catch all +exponential-time cases. + +[![browser support](https://ci.testling.com/substack/safe-regex.png)](https://ci.testling.com/substack/safe-regex) + +[![build status](https://secure.travis-ci.org/substack/safe-regex.png)](http://travis-ci.org/substack/safe-regex) + +# example + +``` js +var safe = require('safe-regex'); +var regex = process.argv.slice(2).join(' '); +console.log(safe(regex)); +``` + +``` +$ node safe.js '(x+x+)+y' +false +$ node safe.js '(beep|boop)*' +true +$ node safe.js '(a+){10}' +false +$ node safe.js '\blocation\s*:[^:\n]+\b(Oakland|San Francisco)\b' +true +``` + +# methods + +``` js +var safe = require('safe-regex') +``` + +## var ok = safe(re, opts={}) + +Return a boolean `ok` whether or not the regex `re` is safe and not possibly +catastrophic. + +`re` can be a `RegExp` object or just a string. + +If the `re` is a string and is an invalid regex, returns `false`. + +* `opts.limit` - maximum number of allowed repetitions in the entire regex. +Default: `25`. + +# install + +With [npm](https://npmjs.org) do: + +``` +npm install safe-regex +``` + +# license + +MIT diff --git a/node_modules/safe-regex/test/regex.js b/node_modules/safe-regex/test/regex.js new file mode 100644 index 0000000..0bda850 --- /dev/null +++ b/node_modules/safe-regex/test/regex.js @@ -0,0 +1,50 @@ +var safe = require('../'); +var test = require('tape'); + +var good = [ + /\bOakland\b/, + /\b(Oakland|San Francisco)\b/i, + /^\d+1337\d+$/i, + /^\d+(1337|404)\d+$/i, + /^\d+(1337|404)*\d+$/i, + RegExp(Array(26).join('a?') + Array(26).join('a')), +]; + +test('safe regex', function (t) { + t.plan(good.length); + good.forEach(function (re) { + t.equal(safe(re), true); + }); +}); + + +var bad = [ + /^(a?){25}(a){25}$/, + RegExp(Array(27).join('a?') + Array(27).join('a')), + /(x+x+)+y/, + /foo|(x+x+)+y/, + /(a+){10}y/, + /(a+){2}y/, + /(.*){1,32000}[bc]/ +]; + +test('unsafe regex', function (t) { + t.plan(bad.length); + bad.forEach(function (re) { + t.equal(safe(re), false); + }); +}); + +var invalid = [ + '*Oakland*', + 'hey(yoo))', + 'abcde(?>hellow)', + '[abc' +]; + +test('invalid regex', function (t) { + t.plan(invalid.length); + invalid.forEach(function (re) { + t.equal(safe(re), false); + }); +}); |