├── .gitignore ├── .travis.yml ├── History.md ├── LICENSE-MIT ├── Readme.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | components 3 | build -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 8 -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.2.0 - November 25, 2014 3 | ------------------------- 4 | * add support for protocol relative urls 5 | 6 | 1.1.0 - February 8, 2013 7 | ------------------------ 8 | * support any protocol 9 | * support paths on localhost 10 | 11 | 1.0.0 - January 17, 2013 12 | ------------------------ 13 | * allow localhost to have a port 14 | 15 | 0.1.0 - September 8, 2013 16 | ------------------------- 17 | * make regexp match more valid url types 18 | 19 | 0.0.2 - August 2, 2013 20 | ---------------------- 21 | * remove loose matching 22 | 23 | 0.0.1 - August 2, 2013 24 | ---------------------- 25 | :sparkles: -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT LICENSE 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # is-url 2 | 3 | > **Note** 4 | > Segment has paused maintenance on this project, but may return it to an active status in the future. Issues and pull requests from external contributors are not being considered, although internal contributions may appear from time to time. The project remains available under its open source license for anyone to use. 5 | 6 | Check whether a string is a URL. 7 | 8 | ## Installation 9 | 10 | ```sh 11 | npm install is-url 12 | ``` 13 | 14 | ## API 15 | 16 | ### `isUrl(string)` 17 | 18 | Returns a Boolean indicating whether `string` is a URL. 19 | 20 | ## License 21 | 22 | MIT 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Expose `isUrl`. 4 | */ 5 | 6 | module.exports = isUrl; 7 | 8 | /** 9 | * RegExps. 10 | * A URL must match #1 and then at least one of #2/#3. 11 | * Use two levels of REs to avoid REDOS. 12 | */ 13 | 14 | var protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/; 15 | 16 | var localhostDomainRE = /^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/ 17 | var nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/; 18 | 19 | /** 20 | * Loosely validate a URL `string`. 21 | * 22 | * @param {String} string 23 | * @return {Boolean} 24 | */ 25 | 26 | function isUrl(string){ 27 | if (typeof string !== 'string') { 28 | return false; 29 | } 30 | 31 | var match = string.match(protocolAndDomainRE); 32 | if (!match) { 33 | return false; 34 | } 35 | 36 | var everythingAfterProtocol = match[1]; 37 | if (!everythingAfterProtocol) { 38 | return false; 39 | } 40 | 41 | if (localhostDomainRE.test(everythingAfterProtocol) || 42 | nonLocalhostDomainRE.test(everythingAfterProtocol)) { 43 | return true; 44 | } 45 | 46 | return false; 47 | } 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-url", 3 | "description": "Check whether a string is a URL.", 4 | "repository": "https://github.com/segmentio/is-url", 5 | "version": "1.2.4", 6 | "scripts": { 7 | "test": "mocha --reporter spec" 8 | }, 9 | "license": "MIT", 10 | "devDependencies": { 11 | "mocha": "*" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 2 | try { 3 | var url = require('is-url'); 4 | } catch (e) { 5 | var url = require('..'); 6 | } 7 | 8 | var assert = require('assert'); 9 | 10 | describe('is-url', function () { 11 | describe('valid', function () { 12 | it('http://google.com', function () { 13 | assert(url('http://google.com')); 14 | }); 15 | 16 | it('https://google.com', function () { 17 | assert(url('https://google.com')); 18 | }); 19 | 20 | it('ftp://google.com', function () { 21 | assert(url('ftp://google.com')); 22 | }); 23 | 24 | it('http://www.google.com', function () { 25 | assert(url('http://www.google.com')); 26 | }); 27 | 28 | it('http://google.com/something', function () { 29 | assert(url('http://google.com/something')); 30 | }); 31 | 32 | it('http://google.com?q=query', function () { 33 | assert(url('http://google.com?q=query')); 34 | }); 35 | 36 | it('http://google.com#hash', function () { 37 | assert(url('http://google.com#hash')); 38 | }); 39 | 40 | it('http://google.com/something?q=query#hash', function () { 41 | assert(url('http://google.com/something?q=query#hash')); 42 | }); 43 | 44 | it('http://google.co.uk', function () { 45 | assert(url('http://google.co.uk')); 46 | }); 47 | 48 | it('http://www.google.co.uk', function () { 49 | assert(url('http://www.google.co.uk')); 50 | }); 51 | 52 | it('http://google.cat', function () { 53 | assert(url('http://google.cat')); 54 | }); 55 | 56 | it('https://d1f4470da51b49289906b3d6cbd65074@app.getsentry.com/13176', function () { 57 | assert(url('https://d1f4470da51b49289906b3d6cbd65074@app.getsentry.com/13176')); 58 | }); 59 | 60 | it('http://0.0.0.0', function () { 61 | assert(url('http://0.0.0.0')); 62 | }); 63 | 64 | it('http://localhost', function () { 65 | assert(url('http://localhost')); 66 | }); 67 | 68 | it('postgres://u:p@example.com:5702/db', function () { 69 | assert(url('postgres://u:p@example.com:5702/db')); 70 | }); 71 | 72 | it('redis://:123@174.129.42.52:13271', function () { 73 | assert(url('redis://:123@174.129.42.52:13271')); 74 | }); 75 | 76 | it('mongodb://u:p@example.com:10064/db', function () { 77 | assert(url('mongodb://u:p@example.com:10064/db')); 78 | }); 79 | 80 | it('ws://chat.example.com/games', function () { 81 | assert(url('ws://chat.example.com/games')); 82 | }); 83 | 84 | it('wss://secure.example.com/biz', function () { 85 | assert(url('wss://secure.example.com/biz')); 86 | }); 87 | 88 | it('http://localhost:4000', function () { 89 | assert(url('http://localhost:4000')); 90 | }); 91 | 92 | it('http://localhost:342/a/path', function () { 93 | assert(url('http://localhost:342/a/path')); 94 | }); 95 | 96 | it('//google.com', function () { 97 | assert(url('//google.com')); 98 | }); 99 | }); 100 | 101 | describe('invalid', function () { 102 | it('http://', function () { 103 | assert(!url('http://')); 104 | }); 105 | 106 | it('http://google', function () { 107 | assert(!url('http://google')); 108 | }); 109 | 110 | it('http://google.', function () { 111 | assert(!url('http://google.')); 112 | }); 113 | 114 | it('google', function () { 115 | assert(!url('google')); 116 | }); 117 | 118 | it('google.com', function () { 119 | assert(!url('google.com')); 120 | }); 121 | 122 | it('empty', function () { 123 | assert(!url('')); 124 | }); 125 | 126 | it('undef', function () { 127 | assert(!url(undefined)); 128 | }); 129 | 130 | it('object', function () { 131 | assert(!url({})); 132 | }); 133 | 134 | it('re', function () { 135 | assert(!url(/abc/)); 136 | }); 137 | }); 138 | 139 | describe('redos', function () { 140 | it('redos exploit', function () { 141 | // Invalid. This should be discovered in under 1 second. 142 | var attackString = 'a://localhost' + '9'.repeat(100000) + '\t'; 143 | var before = process.hrtime(); 144 | assert(!url(attackString), 'attackString was valid'); 145 | var elapsed = process.hrtime(before); 146 | assert(elapsed[0] < 1, 'attackString took ' + elapsed[0] + ' > 1 seconds'); 147 | }); 148 | }); 149 | }); 150 | --------------------------------------------------------------------------------