oncletom/tld.js — pull request #33 by oncletom (2014-01-15 14:07:43+00:00)
Title: tldjs chokes on weird domains

Hi

Thanks for making tldjs, it is immensely useful :-)

I've incorporated it in a project and tested it against a database of 25.000 domains.

It seems that tldjs can crash the whole server (node process) when given bad input. Example:

```
$ node
> var tld = require('tldjs');
> tld.getSubdomain('www.weir)domain.com');

SyntaxError: Invalid regular expression: /\.?weir)domain\.com$/: Unmatched ')'
at new RegExp ()
at tld.getSubdomain (./node_modules/tldjs/lib/tld.js:189:28)
at repl:1:5
```

Looks like the problem is that the input strings are not properly escaped, before they are put to use inside a regular expression.

If I may suggest a solution also, this is from Mozilla MDN documentation:

```
if (! RegExp.quote) {
RegExp.quote = function(s) {
return String(s).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
}
```

The function from MDN can be called with an arbitrary string, and will properly escape it for use inside a regular expression.

Thanks for reading :-)