Allowing all printable charactors to be used as hostname

This commit is contained in:
NI
2020-11-10 15:05:00 +08:00
parent 09f92c05ca
commit ffdab9f037
2 changed files with 94 additions and 109 deletions

View File

@@ -15,9 +15,8 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import * as iconv from "iconv-lite";
import * as buffer from "buffer/"; import * as buffer from "buffer/";
import * as iconv from "iconv-lite";
import Exception from "./exception.js"; import Exception from "./exception.js";
const availableEncodings = [ const availableEncodings = [
@@ -55,7 +54,7 @@ const availableEncodings = [
"shift-jis", "shift-jis",
"euc-kr", "euc-kr",
"utf-16be", "utf-16be",
"utf-16le" "utf-16le",
]; ];
export const charsetPresets = (() => { export const charsetPresets = (() => {
@@ -79,77 +78,35 @@ export const charsetPresets = (() => {
})(); })();
const numCharators = { const numCharators = {
"0": true, 0: true,
"1": true, 1: true,
"2": true, 2: true,
"3": true, 3: true,
"4": true, 4: true,
"5": true, 5: true,
"6": true, 6: true,
"7": true, 7: true,
"8": true, 8: true,
"9": true 9: true,
}; };
const hexCharators = { const hexCharators = {
"0": true, 0: true,
"1": true, 1: true,
"2": true, 2: true,
"3": true, 3: true,
"4": true, 4: true,
"5": true, 5: true,
"6": true, 6: true,
"7": true, 7: true,
"8": true, 8: true,
"9": 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,
a: true, a: true,
b: true, b: true,
c: true, c: true,
d: true, d: true,
e: true, e: true,
f: 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 * @param {string} d Input data
* *
@@ -199,12 +158,38 @@ export function isHex(d) {
* *
*/ */
function isHostname(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 (dChar >= 32 && dChar <= 126) {
if (!hostnameCharators[dd[i]]) { continue;
return false;
} }
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; return true;
@@ -369,7 +354,7 @@ function parseIP(d) {
try { try {
return { return {
type: "IPv4", type: "IPv4",
data: parseIPv4(d) data: parseIPv4(d),
}; };
} catch (e) { } catch (e) {
// Do nothing // Do nothing
@@ -378,7 +363,7 @@ function parseIP(d) {
try { try {
return { return {
type: "IPv6", type: "IPv6",
data: new Uint8Array(parseIPv6(d).buffer) data: new Uint8Array(parseIPv6(d).buffer),
}; };
} catch (e) { } catch (e) {
// Do nothing // Do nothing
@@ -386,7 +371,7 @@ function parseIP(d) {
return { return {
type: "Hostname", type: "Hostname",
data: parseHostname(d) data: parseHostname(d),
}; };
} }
@@ -401,7 +386,7 @@ export function splitHostPort(d, defPort) {
return { return {
type: a.type, type: a.type,
addr: a.data, addr: a.data,
port: defPort port: defPort,
}; };
} }
@@ -428,6 +413,6 @@ export function splitHostPort(d, defPort) {
return { return {
type: a.type, type: a.type,
addr: a.data, addr: a.data,
port: portNum port: portNum,
}; };
} }

View File

@@ -24,33 +24,33 @@ describe("Common", () => {
{ {
sample: "127.0.0.1", sample: "127.0.0.1",
expectingFailure: false, expectingFailure: false,
expected: new Uint8Array([127, 0, 0, 1]) expected: new Uint8Array([127, 0, 0, 1]),
}, },
{ {
sample: "255.255.255.255", sample: "255.255.255.255",
expectingFailure: false, expectingFailure: false,
expected: new Uint8Array([255, 255, 255, 255]) expected: new Uint8Array([255, 255, 255, 255]),
}, },
{ {
sample: "255.255.a.255", sample: "255.255.a.255",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
}, },
{ {
sample: "255.255.255", sample: "255.255.255",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
}, },
{ {
sample: "2001:db8:1f70::999:de8:7648:6e8", sample: "2001:db8:1f70::999:de8:7648:6e8",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
}, },
{ {
sample: "a.ssh.vaguly.com", sample: "a.ssh.vaguly.com",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
} },
]; ];
for (let i in tests) { for (let i in tests) {
@@ -85,8 +85,8 @@ describe("Common", () => {
0x999, 0x999,
0xde8, 0xde8,
0x7648, 0x7648,
0x6e8 0x6e8,
]) ]),
}, },
{ {
sample: "2001:db8:85a3::8a2e:370:7334", sample: "2001:db8:85a3::8a2e:370:7334",
@@ -99,18 +99,18 @@ describe("Common", () => {
0x0, 0x0,
0x8a2e, 0x8a2e,
0x370, 0x370,
0x7334 0x7334,
]) ]),
}, },
{ {
sample: "::1", sample: "::1",
expectingFailure: false, 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: "::", sample: "::",
expectingFailure: false, 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", sample: "2001:db8:1f70::999:de8:7648:6e8",
@@ -123,8 +123,8 @@ describe("Common", () => {
0x999, 0x999,
0xde8, 0xde8,
0x7648, 0x7648,
0x6e8 0x6e8,
]) ]),
}, },
{ {
sample: "2001:0db8:ac10:fe01::", sample: "2001:0db8:ac10:fe01::",
@@ -137,8 +137,8 @@ describe("Common", () => {
0x0, 0x0,
0x0, 0x0,
0x0, 0x0,
0x0 0x0,
]) ]),
}, },
{ {
sample: "::7f00:1", sample: "::7f00:1",
@@ -151,34 +151,34 @@ describe("Common", () => {
0x0000, 0x0000,
0x0000, 0x0000,
0x7f00, 0x7f00,
0x0001 0x0001,
]) ]),
}, },
{ {
sample: "127.0.0.1", sample: "127.0.0.1",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
}, },
{ {
sample: "255.255.255.255", sample: "255.255.255.255",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
}, },
{ {
sample: "255.255.a.255", sample: "255.255.a.255",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
}, },
{ {
sample: "255.255.255", sample: "255.255.255",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
}, },
{ {
sample: "a.ssh.vaguly.com", sample: "a.ssh.vaguly.com",
expectingFailure: true, expectingFailure: true,
expected: null expected: null,
} },
]; ];
for (let i in tests) { for (let i in tests) {
@@ -207,13 +207,13 @@ describe("Common", () => {
sample: "ssh.vaguly.com", sample: "ssh.vaguly.com",
expectedType: "Hostname", expectedType: "Hostname",
expectedAddr: common.strToUint8Array("ssh.vaguly.com"), expectedAddr: common.strToUint8Array("ssh.vaguly.com"),
expectedPort: 22 expectedPort: 22,
}, },
{ {
sample: "ssh.vaguly.com:22", sample: "ssh.vaguly.com:22",
expectedType: "Hostname", expectedType: "Hostname",
expectedAddr: common.strToUint8Array("ssh.vaguly.com"), expectedAddr: common.strToUint8Array("ssh.vaguly.com"),
expectedPort: 22 expectedPort: 22,
}, },
// IPv4 // IPv4
@@ -221,13 +221,13 @@ describe("Common", () => {
sample: "10.220.179.110", sample: "10.220.179.110",
expectedType: "IPv4", expectedType: "IPv4",
expectedAddr: new Uint8Array([10, 220, 179, 110]), expectedAddr: new Uint8Array([10, 220, 179, 110]),
expectedPort: 22 expectedPort: 22,
}, },
{ {
sample: "10.220.179.110:3333", sample: "10.220.179.110:3333",
expectedType: "IPv4", expectedType: "IPv4",
expectedAddr: new Uint8Array([10, 220, 179, 110]), expectedAddr: new Uint8Array([10, 220, 179, 110]),
expectedPort: 3333 expectedPort: 3333,
}, },
// IPv6 // IPv6
@@ -243,10 +243,10 @@ describe("Common", () => {
0x999, 0x999,
0xde8, 0xde8,
0x7648, 0x7648,
0x6e8 0x6e8,
]).buffer ]).buffer
), ),
expectedPort: 22 expectedPort: 22,
}, },
{ {
sample: "[2001:db8:1f70::999:de8:7648:6e8]:100", sample: "[2001:db8:1f70::999:de8:7648:6e8]:100",
@@ -260,11 +260,11 @@ describe("Common", () => {
0x999, 0x999,
0xde8, 0xde8,
0x7648, 0x7648,
0x6e8 0x6e8,
]).buffer ]).buffer
), ),
expectedPort: 100 expectedPort: 100,
} },
]; ];
for (let i in tests) { for (let i in tests) {