Skip to content

Commit

Permalink
check for unknown items in tx body
Browse files Browse the repository at this point in the history
  • Loading branch information
janmazak committed Oct 31, 2023
1 parent e6b9a6e commit 3e6e8a9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/errors/parseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions src/txParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
22 changes: 22 additions & 0 deletions test/integration/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ describe('Parse', () => {
ParseErrorReason.INVALID_TX_BODY_CBOR,
)
})
it('Transaction body contains unknown items --- negative key', () => {
expect(() =>
decodeTxBody(
Buffer.from(
// tx body map key -17 not allowed
'a30081825820ba638246bd9be05aa46e865320c354efea75cf5796e88b763faaa30c9fbb78de003181825839000743d16cfe3c4fcc0c11c2403bbc10dbc7ecdd4477e053481a368e7a06e2ae44dff6770dc0f4ada3cf4cf2605008e27aecdb332ad349fda700021a0001e240',
'hex',
),
),
).to.throw(ParseError, ParseErrorReason.INVALID_TX_BODY_UNKNOWN_ITEMS)
})
it('Transaction body contains unknown items --- unsupported key', () => {
expect(() =>
decodeTxBody(
Buffer.from(
// tx body map key 10 is not defined in the CDDL
'a30081825820ba638246bd9be05aa46e865320c354efea75cf5796e88b763faaa30c9fbb78de000a81825839000743d16cfe3c4fcc0c11c2403bbc10dbc7ecdd4477e053481a368e7a06e2ae44dff6770dc0f4ada3cf4cf2605008e27aecdb332ad349fda700021a0001e240',
'hex',
),
),
).to.throw(ParseError, ParseErrorReason.INVALID_TX_BODY_UNKNOWN_ITEMS)
})
})

describe('Invalid transactions', () => {
Expand Down

0 comments on commit 3e6e8a9

Please sign in to comment.