Skip to content

Commit

Permalink
Validate ports: must be numeric and less than 2^16
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Macbeth committed Oct 15, 2019
1 parent dacd009 commit 0a7b676
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ export default class URL implements IURL {
if (portIndex > 0) {
i += 1;
const portStart = i;
let nonNumeric = false;
for (; i <= end; i += 1) {
const code = this._href.charCodeAt(i);
if (BREAK_HOST_ON.indexOf(code) !== -1) {
Expand All @@ -442,11 +443,22 @@ export default class URL implements IURL {
this._username = this._href.slice(start, portIndex || i);
this._password = this._href.slice(portIndex + 1, i);
return this._extractHostname(i + 1, end);
} else if (code < 48 || code > 57) {
// non numeric character in port
nonNumeric = true;
}
}
if (!this._port) {
this._port = this.href.slice(portStart, i);
}
// validate port - cannot contain non-numeric characters
if (nonNumeric) {
throw new TypeError('Invalid URL: port contains non numeric character');
}
// cannot be greater than 65535
if (this._port.length >= 5 && parseInt(this._port, 10) > 65535) {
throw new TypeError('Invalid URL: invalid port number');
}
}
this._host = this._href.slice(start, !stopped ? i + 1 : i);
this.origin = `${this._protocol}//${this._host}`;
Expand Down

0 comments on commit 0a7b676

Please sign in to comment.