From 664b47c4ff57d59462de6527e49170666fdd8bdb Mon Sep 17 00:00:00 2001 From: skywalker Date: Sat, 6 Jun 2026 19:00:46 +0800 Subject: [PATCH] handle invalid AMF decode input --- src/core/operations/AMFDecode.mjs | 12 ++++++- tests/operations/tests/AMFDecode.mjs | 54 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/operations/tests/AMFDecode.mjs diff --git a/src/core/operations/AMFDecode.mjs b/src/core/operations/AMFDecode.mjs index 50a0d551..1b125c6e 100644 --- a/src/core/operations/AMFDecode.mjs +++ b/src/core/operations/AMFDecode.mjs @@ -5,6 +5,7 @@ */ import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; import "reflect-metadata"; // Required as a shim for the amf library import { AMF0, AMF3 } from "@astronautlabs/amf"; @@ -44,7 +45,16 @@ class AMFDecode extends Operation { const [format] = args; const handler = format === "AMF0" ? AMF0 : AMF3; const encoded = new Uint8Array(input); - return handler.Value.deserialize(encoded); + + if (encoded.length === 0) { + throw new OperationError(`Could not decode ${format} data: input is empty.`); + } + + try { + return handler.Value.deserialize(encoded); + } catch { + throw new OperationError(`Could not decode ${format} data. The input may be invalid or incomplete.`); + } } } diff --git a/tests/operations/tests/AMFDecode.mjs b/tests/operations/tests/AMFDecode.mjs new file mode 100644 index 00000000..cbb7c289 --- /dev/null +++ b/tests/operations/tests/AMFDecode.mjs @@ -0,0 +1,54 @@ +/** + * AMF Decode tests. + * + * @copyright Crown Copyright 2026 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "AMF3 Decode: empty input error", + input: "", + expectedOutput: "Could not decode AMF3 data: input is empty.", + recipeConfig: [ + { + op: "AMF Decode", + args: ["AMF3"] + } + ], + }, + { + name: "AMF3 Decode: newline input error", + input: "\n", + expectedOutput: "Could not decode AMF3 data. The input may be invalid or incomplete.", + recipeConfig: [ + { + op: "AMF Decode", + args: ["AMF3"] + } + ], + }, + { + name: "AMF3 Decode: truncated object input error", + input: "\x0a\x13\x01\x03a\x05\x40\x08", + expectedOutput: "Could not decode AMF3 data. The input may be invalid or incomplete.", + recipeConfig: [ + { + op: "AMF Decode", + args: ["AMF3"] + } + ], + }, + { + name: "AMF3 Decode: truncated array input error", + input: "\x09\x13\x01\x03a\x05\x40\x08", + expectedOutput: "Could not decode AMF3 data. The input may be invalid or incomplete.", + recipeConfig: [ + { + op: "AMF Decode", + args: ["AMF3"] + } + ], + }, +]);