mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-24 06:11:06 +00:00
Validate transaction metadata
This commit is contained in:
parent
a5d3b63912
commit
7a1e773b49
@ -4,3 +4,4 @@ INVALID_ADDRESS = wire.ProcessError("Invalid address")
|
||||
NETWORK_MISMATCH = wire.ProcessError("Output address network mismatch!")
|
||||
INVALID_CERTIFICATE = wire.ProcessError("Invalid certificate")
|
||||
INVALID_WITHDRAWAL = wire.ProcessError("Invalid withdrawal")
|
||||
INVALID_METADATA = wire.ProcessError("Invalid metadata")
|
||||
|
@ -24,6 +24,7 @@ from .address import (
|
||||
from .byron_address import get_address_attributes
|
||||
from .helpers import (
|
||||
INVALID_CERTIFICATE,
|
||||
INVALID_METADATA,
|
||||
INVALID_WITHDRAWAL,
|
||||
network_ids,
|
||||
protocol_magics,
|
||||
@ -79,9 +80,7 @@ async def sign_tx(
|
||||
_validate_outputs(keychain, msg.outputs, msg.protocol_magic, msg.network_id)
|
||||
_validate_certificates(msg.certificates)
|
||||
_validate_withdrawals(msg.withdrawals)
|
||||
|
||||
if msg.metadata and len(msg.metadata) > MAX_METADATA_LENGTH:
|
||||
raise wire.ProcessError("Invalid metadata")
|
||||
_validate_metadata(msg.metadata)
|
||||
|
||||
# display the transaction in UI
|
||||
await _show_tx(ctx, keychain, msg)
|
||||
@ -158,6 +157,23 @@ def _validate_withdrawals(withdrawals: List[CardanoTxWithdrawalType]) -> None:
|
||||
raise INVALID_WITHDRAWAL
|
||||
|
||||
|
||||
def _validate_metadata(metadata: bytes) -> None:
|
||||
if not metadata:
|
||||
return
|
||||
|
||||
if len(metadata) > MAX_METADATA_LENGTH:
|
||||
raise INVALID_METADATA
|
||||
|
||||
try:
|
||||
# this also raises an error if there's some data remaining
|
||||
decoded = cbor.decode(metadata)
|
||||
except:
|
||||
raise INVALID_METADATA
|
||||
|
||||
if not isinstance(decoded, dict):
|
||||
raise INVALID_METADATA
|
||||
|
||||
|
||||
def _serialize_tx(keychain: seed.Keychain, msg: Cardano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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
# error message
|
||||
"Invalid metadata",
|
||||
),
|
||||
# Metadata is a list
|
||||
(
|
||||
# protocol magic
|
||||
PROTOCOL_MAGICS["mainnet"],
|
||||
# network id
|
||||
NETWORK_IDS["mainnet"],
|
||||
# inputs
|
||||
[SAMPLE_INPUTS["shelley_input"]],
|
||||
# outputs
|
||||
[SAMPLE_OUTPUTS["simple_byron_output"]],
|
||||
# fee
|
||||
42,
|
||||
# ttl
|
||||
10,
|
||||
# certificates
|
||||
[],
|
||||
# withdrawals
|
||||
[],
|
||||
# metadata
|
||||
"82a200a11864a118c843aa00ff01a119012c590100aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0A",
|
||||
# error message
|
||||
"Invalid metadata",
|
||||
),
|
||||
# Metadata is incomplete
|
||||
(
|
||||
# protocol magic
|
||||
PROTOCOL_MAGICS["mainnet"],
|
||||
# network id
|
||||
NETWORK_IDS["mainnet"],
|
||||
# inputs
|
||||
[SAMPLE_INPUTS["shelley_input"]],
|
||||
# outputs
|
||||
[SAMPLE_OUTPUTS["simple_byron_output"]],
|
||||
# fee
|
||||
42,
|
||||
# ttl
|
||||
10,
|
||||
# certificates
|
||||
[],
|
||||
# withdrawals
|
||||
[],
|
||||
# metadata
|
||||
"a200a11864a118c843aa00ff01",
|
||||
# error message
|
||||
"Invalid metadata",
|
||||
),
|
||||
# Metadata has leftover data
|
||||
(
|
||||
# protocol magic
|
||||
PROTOCOL_MAGICS["mainnet"],
|
||||
# network id
|
||||
NETWORK_IDS["mainnet"],
|
||||
# inputs
|
||||
[SAMPLE_INPUTS["shelley_input"]],
|
||||
# outputs
|
||||
[SAMPLE_OUTPUTS["simple_byron_output"]],
|
||||
# fee
|
||||
42,
|
||||
# ttl
|
||||
10,
|
||||
# certificates
|
||||
[],
|
||||
# withdrawals
|
||||
[],
|
||||
# metadata
|
||||
"a200a11864a118c843aa00ff01a119012c590100aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa000000",
|
||||
# error message
|
||||
"Invalid metadata",
|
||||
),
|
||||
|
Loading…
Reference in New Issue
Block a user