From 20b46bf1a0a788fbb9bd095ed45e3bb2e1d548f8 Mon Sep 17 00:00:00 2001 From: Syed Ishmum Ahnaf Date: Fri, 5 Jun 2026 15:47:48 +0600 Subject: [PATCH] fix: validate text encoding options (#2497) --- src/core/operations/DecodeText.mjs | 4 ++++ src/core/operations/EncodeText.mjs | 4 ++++ tests/operations/tests/CharEnc.mjs | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/core/operations/DecodeText.mjs b/src/core/operations/DecodeText.mjs index 0fc9d2b5..baf23336 100644 --- a/src/core/operations/DecodeText.mjs +++ b/src/core/operations/DecodeText.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import cptable from "codepage"; import {CHR_ENC_CODE_PAGES} from "../lib/ChrEnc.mjs"; @@ -48,6 +49,9 @@ class DecodeText extends Operation { */ run(input, args) { const format = CHR_ENC_CODE_PAGES[args[0]]; + if (!format) { + throw new OperationError("Invalid encoding"); + } return cptable.utils.decode(format, new Uint8Array(input)); } diff --git a/src/core/operations/EncodeText.mjs b/src/core/operations/EncodeText.mjs index 8cc1450f..5cc09742 100644 --- a/src/core/operations/EncodeText.mjs +++ b/src/core/operations/EncodeText.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import cptable from "codepage"; import {CHR_ENC_CODE_PAGES} from "../lib/ChrEnc.mjs"; @@ -48,6 +49,9 @@ class EncodeText extends Operation { */ run(input, args) { const format = CHR_ENC_CODE_PAGES[args[0]]; + if (!format) { + throw new OperationError("Invalid encoding"); + } const encoded = cptable.utils.encode(format, input); return new Uint8Array(encoded).buffer; } diff --git a/tests/operations/tests/CharEnc.mjs b/tests/operations/tests/CharEnc.mjs index eecfaab6..83f71ca9 100644 --- a/tests/operations/tests/CharEnc.mjs +++ b/tests/operations/tests/CharEnc.mjs @@ -68,6 +68,32 @@ TestRegister.addTests([ }, ], }, + { + name: "Encode text: empty encoding", + input: "hello", + expectedOutput: "Invalid encoding", + recipeConfig: [ + { + "op": "Encode text", + "args": [""] + }, + ], + }, + { + name: "Decode text: empty encoding", + input: "68 65 6c 6c 6f", + expectedOutput: "Invalid encoding", + recipeConfig: [ + { + "op": "From Hex", + "args": ["Space"] + }, + { + "op": "Decode text", + "args": [""] + }, + ], + }, { name: "Generate Base64 Windows PowerShell", input: "ZABpAHIAIAAiAGMAOgBcAHAAcgBvAGcAcgBhAG0AIABmAGkAbABlAHMAIgAgAA==",