From 1e0e7f16a70349134571b787793e58961bd5a3fd Mon Sep 17 00:00:00 2001 From: n1474335 Date: Mon, 22 Feb 2021 19:13:38 +0000 Subject: [PATCH] Added numeric validation for arguments in Binary and Hex operattions. Fixes #1178 --- src/core/lib/Binary.mjs | 4 ++++ src/core/lib/Hex.mjs | 4 ++++ src/core/lib/PublicKey.mjs | 4 ++-- src/core/operations/FromBinary.mjs | 3 ++- src/core/operations/ToHexdump.mjs | 7 ++++++- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/core/lib/Binary.mjs b/src/core/lib/Binary.mjs index d28691e5..dc63fc58 100644 --- a/src/core/lib/Binary.mjs +++ b/src/core/lib/Binary.mjs @@ -7,6 +7,7 @@ */ import Utils from "../Utils.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** @@ -58,6 +59,9 @@ export function toBinary(data, delim="Space", padding=8) { * fromBinary("00010000:00100000:00110000", "Colon"); */ export function fromBinary(data, delim="Space", byteLen=8) { + if (byteLen < 1 || Math.round(byteLen) !== byteLen) + throw new OperationError("Byte length must be a positive integer"); + const delimRegex = Utils.regexRep(delim); data = data.replace(delimRegex, ""); diff --git a/src/core/lib/Hex.mjs b/src/core/lib/Hex.mjs index 9c70e91c..b7e8e908 100644 --- a/src/core/lib/Hex.mjs +++ b/src/core/lib/Hex.mjs @@ -7,6 +7,7 @@ */ import Utils from "../Utils.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** @@ -100,6 +101,9 @@ export function toHexFast(data) { * fromHex("0a:14:1e", "Colon"); */ export function fromHex(data, delim="Auto", byteLen=2) { + if (byteLen < 1 || Math.round(byteLen) !== byteLen) + throw new OperationError("Byte length must be a positive integer"); + if (delim !== "None") { const delimRegex = delim === "Auto" ? /[^a-f\d]|(0x)/gi : Utils.regexRep(delim); data = data.replace(delimRegex, ""); diff --git a/src/core/lib/PublicKey.mjs b/src/core/lib/PublicKey.mjs index 9ec990fe..8c04cdd9 100644 --- a/src/core/lib/PublicKey.mjs +++ b/src/core/lib/PublicKey.mjs @@ -15,7 +15,7 @@ import { toHex, fromHex } from "./Hex.mjs"; * @param {number} indent * @returns {string} */ -export function formatDnStr (dnStr, indent) { +export function formatDnStr(dnStr, indent) { const fields = dnStr.substr(1).replace(/([^\\])\//g, "$1$1/").split(/[^\\]\//); let output = "", maxKeyLen = 0, @@ -54,7 +54,7 @@ export function formatDnStr (dnStr, indent) { * @param {number} indent * @returns {string} */ -export function formatByteStr (byteStr, length, indent) { +export function formatByteStr(byteStr, length, indent) { byteStr = toHex(fromHex(byteStr), ":"); length = length * 3; let output = ""; diff --git a/src/core/operations/FromBinary.mjs b/src/core/operations/FromBinary.mjs index 0bb7a45c..9a51fc8b 100644 --- a/src/core/operations/FromBinary.mjs +++ b/src/core/operations/FromBinary.mjs @@ -35,7 +35,8 @@ class FromBinary extends Operation { { "name": "Byte Length", "type": "number", - "value": 8 + "value": 8, + "min": 1 } ]; this.checks = [ diff --git a/src/core/operations/ToHexdump.mjs b/src/core/operations/ToHexdump.mjs index ed212ce4..c657adeb 100644 --- a/src/core/operations/ToHexdump.mjs +++ b/src/core/operations/ToHexdump.mjs @@ -6,6 +6,7 @@ import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** * To Hexdump operation @@ -28,7 +29,8 @@ class ToHexdump extends Operation { { "name": "Width", "type": "number", - "value": 16 + "value": 16, + "min": 1 }, { "name": "Upper case hex", @@ -58,6 +60,9 @@ class ToHexdump extends Operation { const [length, upperCase, includeFinalLength, unixFormat] = args; const padding = 2; + if (length < 1 || Math.round(length) !== length) + throw new OperationError("Width must be a positive integer"); + let output = ""; for (let i = 0; i < data.length; i += length) { const buff = data.slice(i, i+length);