diff --git a/ui/commands/common.js b/ui/commands/common.js index c5aa857..421e672 100644 --- a/ui/commands/common.js +++ b/ui/commands/common.js @@ -15,9 +15,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import * as iconv from "iconv-lite"; import * as buffer from "buffer/"; - +import * as iconv from "iconv-lite"; import Exception from "./exception.js"; const availableEncodings = [ @@ -55,7 +54,7 @@ const availableEncodings = [ "shift-jis", "euc-kr", "utf-16be", - "utf-16le" + "utf-16le", ]; export const charsetPresets = (() => { @@ -79,77 +78,35 @@ export const charsetPresets = (() => { })(); const numCharators = { - "0": true, - "1": true, - "2": true, - "3": true, - "4": true, - "5": true, - "6": true, - "7": true, - "8": true, - "9": true + 0: true, + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + 9: true, }; const hexCharators = { - "0": true, - "1": true, - "2": true, - "3": true, - "4": true, - "5": true, - "6": true, - "7": true, - "8": true, - "9": true, - a: true, - b: true, - c: true, - d: true, - e: true, - f: true -}; - -const hostnameCharators = { - "0": true, - "1": true, - "2": true, - "3": true, - "4": true, - "5": true, - "6": true, - "7": true, - "8": true, - "9": true, + 0: true, + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + 9: true, a: true, b: true, c: true, d: true, e: true, f: true, - g: true, - h: true, - i: true, - j: true, - k: true, - l: true, - n: true, - m: true, - o: true, - p: true, - q: true, - r: true, - s: true, - t: true, - u: true, - v: true, - w: true, - x: true, - y: true, - z: true, - ".": true, - "-": true, - _: true }; /** @@ -191,7 +148,9 @@ export function isHex(d) { } /** - * Test whether or not given string is all hex + * Test whether or not given string is a valid hostname as far as the Sshwifty + * client consider. This function will return true if the string contains only + * printable charactors * * @param {string} d Input data * @@ -199,12 +158,38 @@ export function isHex(d) { * */ function isHostname(d) { - let dd = d.toLowerCase(); + for (let i = 0; i < d.length; i++) { + const dChar = d.charCodeAt(i); - for (let i = 0; i < dd.length; i++) { - if (!hostnameCharators[dd[i]]) { - return false; + if (dChar >= 32 && dChar <= 126) { + continue; } + + if (dChar === 128) { + continue; + } + + if (dChar >= 130 && dChar <= 140) { + continue; + } + + if (dChar === 142) { + continue; + } + + if (dChar >= 145 && dChar <= 156) { + continue; + } + + if (dChar >= 158 && dChar <= 159) { + continue; + } + + if (dChar >= 161 && dChar <= 255) { + continue; + } + + return false; } return true; @@ -369,7 +354,7 @@ function parseIP(d) { try { return { type: "IPv4", - data: parseIPv4(d) + data: parseIPv4(d), }; } catch (e) { // Do nothing @@ -378,7 +363,7 @@ function parseIP(d) { try { return { type: "IPv6", - data: new Uint8Array(parseIPv6(d).buffer) + data: new Uint8Array(parseIPv6(d).buffer), }; } catch (e) { // Do nothing @@ -386,7 +371,7 @@ function parseIP(d) { return { type: "Hostname", - data: parseHostname(d) + data: parseHostname(d), }; } @@ -401,7 +386,7 @@ export function splitHostPort(d, defPort) { return { type: a.type, addr: a.data, - port: defPort + port: defPort, }; } @@ -428,6 +413,6 @@ export function splitHostPort(d, defPort) { return { type: a.type, addr: a.data, - port: portNum + port: portNum, }; } diff --git a/ui/commands/common_test.js b/ui/commands/common_test.js index 887f2da..dae624c 100644 --- a/ui/commands/common_test.js +++ b/ui/commands/common_test.js @@ -24,33 +24,33 @@ describe("Common", () => { { sample: "127.0.0.1", expectingFailure: false, - expected: new Uint8Array([127, 0, 0, 1]) + expected: new Uint8Array([127, 0, 0, 1]), }, { sample: "255.255.255.255", expectingFailure: false, - expected: new Uint8Array([255, 255, 255, 255]) + expected: new Uint8Array([255, 255, 255, 255]), }, { sample: "255.255.a.255", expectingFailure: true, - expected: null + expected: null, }, { sample: "255.255.255", expectingFailure: true, - expected: null + expected: null, }, { sample: "2001:db8:1f70::999:de8:7648:6e8", expectingFailure: true, - expected: null + expected: null, }, { sample: "a.ssh.vaguly.com", expectingFailure: true, - expected: null - } + expected: null, + }, ]; for (let i in tests) { @@ -85,8 +85,8 @@ describe("Common", () => { 0x999, 0xde8, 0x7648, - 0x6e8 - ]) + 0x6e8, + ]), }, { sample: "2001:db8:85a3::8a2e:370:7334", @@ -99,18 +99,18 @@ describe("Common", () => { 0x0, 0x8a2e, 0x370, - 0x7334 - ]) + 0x7334, + ]), }, { sample: "::1", expectingFailure: false, - expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x01]) + expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x01]), }, { sample: "::", expectingFailure: false, - expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x00]) + expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x00]), }, { sample: "2001:db8:1f70::999:de8:7648:6e8", @@ -123,8 +123,8 @@ describe("Common", () => { 0x999, 0xde8, 0x7648, - 0x6e8 - ]) + 0x6e8, + ]), }, { sample: "2001:0db8:ac10:fe01::", @@ -137,8 +137,8 @@ describe("Common", () => { 0x0, 0x0, 0x0, - 0x0 - ]) + 0x0, + ]), }, { sample: "::7f00:1", @@ -151,34 +151,34 @@ describe("Common", () => { 0x0000, 0x0000, 0x7f00, - 0x0001 - ]) + 0x0001, + ]), }, { sample: "127.0.0.1", expectingFailure: true, - expected: null + expected: null, }, { sample: "255.255.255.255", expectingFailure: true, - expected: null + expected: null, }, { sample: "255.255.a.255", expectingFailure: true, - expected: null + expected: null, }, { sample: "255.255.255", expectingFailure: true, - expected: null + expected: null, }, { sample: "a.ssh.vaguly.com", expectingFailure: true, - expected: null - } + expected: null, + }, ]; for (let i in tests) { @@ -207,13 +207,13 @@ describe("Common", () => { sample: "ssh.vaguly.com", expectedType: "Hostname", expectedAddr: common.strToUint8Array("ssh.vaguly.com"), - expectedPort: 22 + expectedPort: 22, }, { sample: "ssh.vaguly.com:22", expectedType: "Hostname", expectedAddr: common.strToUint8Array("ssh.vaguly.com"), - expectedPort: 22 + expectedPort: 22, }, // IPv4 @@ -221,13 +221,13 @@ describe("Common", () => { sample: "10.220.179.110", expectedType: "IPv4", expectedAddr: new Uint8Array([10, 220, 179, 110]), - expectedPort: 22 + expectedPort: 22, }, { sample: "10.220.179.110:3333", expectedType: "IPv4", expectedAddr: new Uint8Array([10, 220, 179, 110]), - expectedPort: 3333 + expectedPort: 3333, }, // IPv6 @@ -243,10 +243,10 @@ describe("Common", () => { 0x999, 0xde8, 0x7648, - 0x6e8 + 0x6e8, ]).buffer ), - expectedPort: 22 + expectedPort: 22, }, { sample: "[2001:db8:1f70::999:de8:7648:6e8]:100", @@ -260,11 +260,11 @@ describe("Common", () => { 0x999, 0xde8, 0x7648, - 0x6e8 + 0x6e8, ]).buffer ), - expectedPort: 100 - } + expectedPort: 100, + }, ]; for (let i in tests) {