From cbba8e12b8b3b40567a206920223e292ee2582f8 Mon Sep 17 00:00:00 2001 From: Jan Mazak Date: Thu, 7 Sep 2023 12:35:15 +0200 Subject: [PATCH] check for unknown items in tx body --- src/errors/parseError.ts | 1 + src/txParsers.ts | 6 ++++++ test/integration/parse.test.ts | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/errors/parseError.ts b/src/errors/parseError.ts index f35b9e3..8c605da 100644 --- a/src/errors/parseError.ts +++ b/src/errors/parseError.ts @@ -58,6 +58,7 @@ export enum ParseErrorReason { INVALID_REFERENCE_INPUT = 'Invalid transaction reference input', INVALID_TX_BODY_CBOR = 'Invalid transaction body CBOR', + INVALID_TX_BODY_UNKNOWN_ITEMS = 'Transaction body contains unknown items', INVALID_TX_INPUTS = 'Invalid transaction inputs', INVALID_TX_OUTPUTS = 'Invalid transaction outputs', INVALID_FEE = 'Invalid transaction fee', diff --git a/src/txParsers.ts b/src/txParsers.ts index f17fb94..5f5a9a1 100644 --- a/src/txParsers.ts +++ b/src/txParsers.ts @@ -656,6 +656,12 @@ export const parseTxBody = (unparsedTxBody: unknown): TransactionBody => { isMapWithKeysOfType(unparsedTxBody, isNumber), ParseErrorReason.INVALID_TX_BODY_CBOR, ) + validate( + Array.from(unparsedTxBody.keys()).every((key) => + Object.values(TransactionBodyKeys).includes(key), + ), + ParseErrorReason.INVALID_TX_BODY_UNKNOWN_ITEMS, + ) return { inputs: parseInputs(unparsedTxBody.get(TransactionBodyKeys.INPUTS)), diff --git a/test/integration/parse.test.ts b/test/integration/parse.test.ts index 17d78b6..1d9492d 100644 --- a/test/integration/parse.test.ts +++ b/test/integration/parse.test.ts @@ -39,6 +39,26 @@ describe('Parse', () => { ParseErrorReason.INVALID_TX_BODY_CBOR, ) }) + it('Transaction body contains unknown items --- negative key', () => { + expect(() => + decodeTxBody( + Buffer.from( + 'a30081825820ba638246bd9be05aa46e865320c354efea75cf5796e88b763faaa30c9fbb78de003181825839000743d16cfe3c4fcc0c11c2403bbc10dbc7ecdd4477e053481a368e7a06e2ae44dff6770dc0f4ada3cf4cf2605008e27aecdb332ad349fda700021a0001e240', + 'hex', + ), + ), + ).to.throw(ParseError, ParseErrorReason.INVALID_TX_BODY_UNKNOWN_ITEMS) + }) + it('Transaction body contains unknown items --- unsupported key', () => { + expect(() => + decodeTxBody( + Buffer.from( + 'a30081825820ba638246bd9be05aa46e865320c354efea75cf5796e88b763faaa30c9fbb78de000a81825839000743d16cfe3c4fcc0c11c2403bbc10dbc7ecdd4477e053481a368e7a06e2ae44dff6770dc0f4ada3cf4cf2605008e27aecdb332ad349fda700021a0001e240', + 'hex', + ), + ), + ).to.throw(ParseError, ParseErrorReason.INVALID_TX_BODY_UNKNOWN_ITEMS) + }) }) describe('Invalid transactions', () => {