handle invalid AMF decode input
This commit is contained in:
parent
1c73e03f5d
commit
664b47c4ff
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
|
import OperationError from "../errors/OperationError.mjs";
|
||||||
import "reflect-metadata"; // Required as a shim for the amf library
|
import "reflect-metadata"; // Required as a shim for the amf library
|
||||||
import { AMF0, AMF3 } from "@astronautlabs/amf";
|
import { AMF0, AMF3 } from "@astronautlabs/amf";
|
||||||
|
|
||||||
@ -44,7 +45,16 @@ class AMFDecode extends Operation {
|
|||||||
const [format] = args;
|
const [format] = args;
|
||||||
const handler = format === "AMF0" ? AMF0 : AMF3;
|
const handler = format === "AMF0" ? AMF0 : AMF3;
|
||||||
const encoded = new Uint8Array(input);
|
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.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
54
tests/operations/tests/AMFDecode.mjs
Normal file
54
tests/operations/tests/AMFDecode.mjs
Normal 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"]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
Loading…
x
Reference in New Issue
Block a user