From 080357a4f5738931b220a702537c1e4fe0d33fba Mon Sep 17 00:00:00 2001 From: GCHQDeveloper581 <63102987+GCHQDeveloper581@users.noreply.github.com> Date: Mon, 22 Jun 2026 11:51:12 +0100 Subject: [PATCH] Improve parameter validation for a number of operations where exceptions otherwise caused. (#2586) --- src/core/operations/BLAKE3.mjs | 6 ++- src/core/operations/BitShiftLeft.mjs | 5 ++- .../PseudoRandomNumberGenerator.mjs | 3 +- src/core/operations/ToBase.mjs | 8 ++-- src/core/operations/ToBinary.mjs | 5 ++- src/core/operations/XORBruteForce.mjs | 5 ++- src/core/operations/XORChecksum.mjs | 4 +- tests/operations/tests/BLAKE3.mjs | 40 ++++++++++++++++++- 8 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/core/operations/BLAKE3.mjs b/src/core/operations/BLAKE3.mjs index 53f7fdd6..a22eb0b8 100644 --- a/src/core/operations/BLAKE3.mjs +++ b/src/core/operations/BLAKE3.mjs @@ -30,7 +30,11 @@ class BLAKE3 extends Operation { this.args = [ { "name": "Size (bytes)", - "type": "number" + "type": "number", + "value": 16, + "min": 1, + "max": 65535, // arbitrary limit to prevent resource exhaustion + "integer": true, }, { "name": "Key", "type": "string", diff --git a/src/core/operations/BitShiftLeft.mjs b/src/core/operations/BitShiftLeft.mjs index cd9f4568..540ab659 100644 --- a/src/core/operations/BitShiftLeft.mjs +++ b/src/core/operations/BitShiftLeft.mjs @@ -27,7 +27,10 @@ class BitShiftLeft extends Operation { { "name": "Amount", "type": "number", - "value": 1 + "value": 1, + "min": 0, + "max": 7, + "integer": true, } ]; } diff --git a/src/core/operations/PseudoRandomNumberGenerator.mjs b/src/core/operations/PseudoRandomNumberGenerator.mjs index 53150566..da23c4de 100644 --- a/src/core/operations/PseudoRandomNumberGenerator.mjs +++ b/src/core/operations/PseudoRandomNumberGenerator.mjs @@ -31,7 +31,8 @@ class PseudoRandomNumberGenerator extends Operation { { "name": "Number of bytes", "type": "number", - "value": 32 + "value": 32, + "min": 1 }, { "name": "Output as", diff --git a/src/core/operations/ToBase.mjs b/src/core/operations/ToBase.mjs index 09a91571..4bf7ae83 100644 --- a/src/core/operations/ToBase.mjs +++ b/src/core/operations/ToBase.mjs @@ -28,7 +28,10 @@ class ToBase extends Operation { { "name": "Radix", "type": "number", - "value": 36 + "value": 36, + "min": 2, + "max": 36, + "integer": true, } ]; } @@ -43,9 +46,6 @@ class ToBase extends Operation { throw new OperationError("Error: Input must be a number"); } const radix = args[0]; - if (radix < 2 || radix > 36) { - throw new OperationError("Error: Radix argument must be between 2 and 36"); - } return input.toString(radix); } diff --git a/src/core/operations/ToBinary.mjs b/src/core/operations/ToBinary.mjs index ba72a55b..b19f94f0 100644 --- a/src/core/operations/ToBinary.mjs +++ b/src/core/operations/ToBinary.mjs @@ -35,7 +35,10 @@ class ToBinary extends Operation { { "name": "Byte Length", "type": "number", - "value": 8 + "value": 8, + "min": 1, + "max": 256, // arbitrary - significantly larger than word size for any known machine ("640k ought to be enough for anybody") + "integer": true } ]; } diff --git a/src/core/operations/XORBruteForce.mjs b/src/core/operations/XORBruteForce.mjs index 8c097731..96ea8ad0 100644 --- a/src/core/operations/XORBruteForce.mjs +++ b/src/core/operations/XORBruteForce.mjs @@ -31,7 +31,10 @@ class XORBruteForce extends Operation { { "name": "Key length", "type": "number", - "value": 1 + "value": 1, + "min": 1, + "max": 2, + "integer": true }, { "name": "Sample length", diff --git a/src/core/operations/XORChecksum.mjs b/src/core/operations/XORChecksum.mjs index ca9c6fac..338b4bea 100644 --- a/src/core/operations/XORChecksum.mjs +++ b/src/core/operations/XORChecksum.mjs @@ -7,7 +7,7 @@ import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; import { toHex } from "../lib/Hex.mjs"; -import OperationError from "../errors/OperationError.mjs"; +import OperationError from "../errors/OperationError.mjs"; /** * XOR Checksum operation @@ -43,7 +43,7 @@ class XORChecksum extends Operation { run(input, args) { const blocksize = args[0]; - + if (!Number.isInteger(blocksize) || blocksize <= 0) { throw new OperationError("Blocksize must be a positive integer."); } diff --git a/tests/operations/tests/BLAKE3.mjs b/tests/operations/tests/BLAKE3.mjs index b3c14e99..e15144b2 100644 --- a/tests/operations/tests/BLAKE3.mjs +++ b/tests/operations/tests/BLAKE3.mjs @@ -69,5 +69,43 @@ TestRegister.addTests([ { "op": "BLAKE3", "args": [16390, "ThiskeyisexactlythirtytwoBytesLo"] } ] - } + }, +// test vectors from https://github.com/BLAKE3-team/BLAKE3/blob/master/test_vectors/test_vectors.json + { + name: "BLAKE3: Std test vector - 0 bytes input, plain hash", + input: "", + expectedOutput: "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262e00f03e7b69af26b7faaf09fcd333050338ddfe085b8cc869ca98b206c08243a26f5487789e8f660afe6c99ef9e0c52b92e7393024a80459cf91f476f9ffdbda7001c22e159b402631f277ca96f2defdf1078282314e763699a31c5363165421cce14d", + recipeConfig: [ + { + "op": "BLAKE3", + "args": [131, ""] + } + ] + }, + { + name: "BLAKE3: Std test vector - 0 bytes input, keyed hash", + input: "", + expectedOutput: "92b2b75604ed3c761f9d6f62392c8a9227ad0ea3f09573e783f1498a4ed60d26b18171a2f22a4b94822c701f107153dba24918c4bae4d2945c20ece13387627d3b73cbf97b797d5e59948c7ef788f54372df45e45e4293c7dc18c1d41144a9758be58960856be1eabbe22c2653190de560ca3b2ac4aa692a9210694254c371e851bc8f", + recipeConfig: [ + { + "op": "BLAKE3", + "args": [131, "whats the Elvish word for friend"] + } + ] + }, + { + name: "BLAKE3: Std test vector - 7 bytes input, keyed hash", + input: "0001020304050607", + expectedOutput: "be2f5495c61cba1bb348a34948c004045e3bd4dae8f0fe82bf44d0da245a060048eb5e68ce6dea1eb0229e144f578b3aa7e9f4f85febd135df8525e6fe40c6f0340d13dd09b255ccd5112a94238f2be3c0b5b7ecde06580426a93e0708555a265305abf86d874e34b4995b788e37a823491f25127a502fe0704baa6bfdf04e76c13276", + recipeConfig: [ + { + "op": "From Hex", + args: [], + }, + { + "op": "BLAKE3", + "args": [131, "whats the Elvish word for friend"] + } + ] + }, ]);