fix: validate text encoding options (#2497)

This commit is contained in:
Syed Ishmum Ahnaf 2026-06-05 15:47:48 +06:00 committed by GitHub
parent 3034d63f90
commit 20b46bf1a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View File

@ -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));
}

View File

@ -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;
}

View File

@ -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==",