handle invalid AMF decode input

This commit is contained in:
skywalker 2026-06-06 19:00:46 +08:00
parent 1c73e03f5d
commit 664b47c4ff
2 changed files with 65 additions and 1 deletions

View File

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

View File

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