Skip to content

Commit

Permalink
Fixed a bug in IPv6 parser: The machine architecture will no longer e…
Browse files Browse the repository at this point in the history
…ffect IPv6 address parsing
  • Loading branch information
NI committed Nov 2, 2021
1 parent c3af1e6 commit a2d99eb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
19 changes: 5 additions & 14 deletions ui/commands/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,13 @@ export function parseIPv4(d) {
*
* @param {string} d IP address
*
* @returns {Uint16Array} Parsed IPv6 Address
* @returns {Uint8Array} Parsed IPv6 Address
*
* @throws {Exception} When the given ip address was not an IPv6 addr
*
*/
export function parseIPv6(d) {
const addrSeg = 8;

let s = d.split(":");

if (s.length > addrSeg || s.length <= 1) {
Expand All @@ -258,41 +257,33 @@ export function parseIPv6(d) {

if (s[0].charAt(0) === "[") {
s[0] = s[0].substring(1, s[0].length);

let end = s.length - 1;

if (s[end].charAt(s[end].length - 1) !== "]") {
throw new Exception("Invalid address");
}

s[end] = s[end].substring(0, s[end].length - 1);
}

let r = new Uint16Array(addrSeg),
let r = new Uint8Array(addrSeg * 2),
rIndexShift = 0;

for (let i = 0; i < s.length; i++) {
if (s[i].length <= 0) {
rIndexShift = addrSeg - s.length;

continue;
}

if (!isHex(s[i])) {
throw new Exception("Invalid address");
}

let ii = parseInt(s[i], 16); // Only support hex

if (isNaN(ii)) {
throw new Exception("Invalid address");
}

if (ii > 0xffff) {
throw new Exception("Invalid address");
}

r[rIndexShift + i] = ii;
let j = (rIndexShift + i) * 2;
r[j] = ii >> 8;
r[j + 1] = ii & 0xff;
}

return r;
Expand Down
61 changes: 39 additions & 22 deletions ui/commands/common_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,46 +77,65 @@ describe("Common", () => {
{
sample: "2001:db8:1f70:0:999:de8:7648:6e8",
expectingFailure: false,
expected: new Uint16Array([
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
expected: new Uint8Array([
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
0x76, 0x48, 0x6, 0xe8,
]),
},
{
sample: "2001:db8:85a3::8a2e:370:7334",
expectingFailure: false,
expected: new Uint16Array([
0x2001, 0xdb8, 0x85a3, 0x0, 0x0, 0x8a2e, 0x370, 0x7334,
expected: new Uint8Array([
0x20, 0x01, 0xd, 0xb8, 0x85, 0xa3, 0x0, 0x0, 0x0, 0x0, 0x8a, 0x2e,
0x3, 0x70, 0x73, 0x34,
]),
},
{
sample: "fdef:90fb:4138::8ca",
expectingFailure: false,
expected: new Uint8Array([
0xfd, 0xef, 0x90, 0xfb, 0x41, 0x38, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x8, 0xca,
]),
},
{
sample: "::1",
expectingFailure: false,
expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x01]),
expected: new Uint8Array([
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x1,
]),
},
{
sample: "::",
expectingFailure: false,
expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x00]),
expected: new Uint8Array([
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0,
]),
},
{
sample: "2001:db8:1f70::999:de8:7648:6e8",
expectingFailure: false,
expected: new Uint16Array([
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
expected: new Uint8Array([
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
0x76, 0x48, 0x6, 0xe8,
]),
},
{
sample: "2001:0db8:ac10:fe01::",
expectingFailure: false,
expected: new Uint16Array([
0x2001, 0x0db8, 0xac10, 0xfe01, 0x0, 0x0, 0x0, 0x0,
expected: new Uint8Array([
0x20, 0x01, 0x0d, 0xb8, 0xac, 0x10, 0xfe, 0x01, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
]),
},
{
sample: "::7f00:1",
expectingFailure: false,
expected: new Uint16Array([
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x7f00, 0x0001,
expected: new Uint8Array([
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7f,
0x00, 0x00, 0x01,
]),
},
{
Expand Down Expand Up @@ -199,21 +218,19 @@ describe("Common", () => {
{
sample: "2001:db8:1f70::999:de8:7648:6e8",
expectedType: "IPv6",
expectedAddr: new Uint8Array(
new Uint16Array([
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
]).buffer
),
expectedAddr: new Uint8Array([
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
0x76, 0x48, 0x6, 0xe8,
]),
expectedPort: 22,
},
{
sample: "[2001:db8:1f70::999:de8:7648:6e8]:100",
expectedType: "IPv6",
expectedAddr: new Uint8Array(
new Uint16Array([
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
]).buffer
),
expectedAddr: new Uint8Array([
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
0x76, 0x48, 0x6, 0xe8,
]),
expectedPort: 100,
},
];
Expand Down

0 comments on commit a2d99eb

Please sign in to comment.