Skip to content

Commit

Permalink
improving invalid port detection
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 5, 2019
1 parent a23d551 commit be6d9f3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"parserOptions": {
},
"rules": {
"no-var": "off",
"no-else-return": "error",
"no-multi-spaces": "error",
"no-whitespace-before-property": "error",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@
var m, isIPv6;
if (host[0] === '[') {
// This is IPv6, with [::] being the shortest possible
m = host.match(/((\[[0-9a-z:%]{2,45}])(?::(-?[0-9]+[^/?]*))?)/i);
m = host.match(/((\[[0-9a-z:%]{2,45}])(?::(-?[0-9a-z]+[^/?]*))?)/i);
isIPv6 = true;
} else {
// It is either IPv4 or domain/socket
m = host.match(/(([a-z0-9%.$-]*)(?::(-?[0-9]+[^/?]*))?)/i);
m = host.match(/(([a-z0-9%.$-]*)(?::(-?[0-9a-z]+[^/?]*))?)/i);
}
if (m) {
var h = {};
Expand Down
9 changes: 6 additions & 3 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,15 @@ describe('hosts', () => {
expect(parse('[a-b-c]')).toEqual({});
});
it('must throw on invalid ports', () => {
expect(() => {
parse(':bad');
}).toThrow('Invalid port: bad');
expect(() => {
parse('[::]:1a');
}).toThrow('Invalid port: 1a');
expect(parse('[::]:abc')).toEqual({hosts: [{name: '[::]', type: 'IPv6'}]});
expect(() => {
parse('[::]:abc');
}).toThrow('Invalid port: abc');
});
it('must allow valid ports', () => {
expect(parse('[::]:1')).toEqual({hosts: [{name: '[::]', port: 1, type: 'IPv6'}]});
Expand Down Expand Up @@ -197,8 +202,6 @@ describe('port', () => {
expect(() => {
parse(':12345a');
}).toThrow('Invalid port: 12345a');
expect(parse(':abc')).toEqual({});
expect(parse('@:abc123')).toEqual({});
});
it('must allow simplified access to the first port number', () => {
expect(parse('').port).toBeUndefined();
Expand Down

0 comments on commit be6d9f3

Please sign in to comment.