fix(solana): make compact-u16 parsing strict
What changed, and why it matters
This commit fixes the way Trezor's Solana app reads compact (variable-length) integer counts from transaction data. Previously, the parser would silently accept malformed or truncated numbers, which could make a transaction with missing data appear valid. The fix now rejects non-standard encodings, truncated inputs, and values that are too large, matching the behavior of Solana's official SDK.
Review whether the old `parse_var_int` behavior could have led to incorrect transaction display or signing decisions in Solana transactions, especially for address lookup table counts and account/instruction counts. Consider a security advisory if a malformed transaction could cause the device to sign something the user did not intend. Ensure the new strict parser is backported to supported firmware branches and that test vectors are run in CI.
Security signals we found
Strict compact-u16 parsing prevents malleability and truncation-based misinterpretation of Solana transaction fields.
Old parser could return 0 or partial values on truncated input, potentially causing a v0 transaction missing its lookup table count to be parsed as having no lookup tables.
Non-canonical encodings were previously accepted, violating Solana SDK short_vec semantics.
Value overflow check (> 0xFFFF) is retained and now reached only after a valid 3-byte encoding.
No changelog entry is explicitly marked with [no changelog].
Evidence from the diff
The parse_var_int function in core/src/apps/solana/transaction/parse.py was rewritten to implement strict compact-u16 (short_vec) decoding as used by solana-sdk. The old loop read bytes until EOF or a terminator, allowing non-canonical aliases (e.g., 0x80 0x00 for 0), overlong encodings (more than 3 bytes), and truncated inputs (returning partial/0 values). The new implementation reads at most 3 bytes, raises EOFError on truncation, raises ValueError for aliases and overlong encodings, and validates the final value fits in 16 bits. Tests were added covering valid, alias, truncated, overlong, and too-large vectors.
Changed components
core/src/apps/solana/transaction/parse.pycore/tests/test_apps.solana.transaction.pyTrezor Model T / Safe firmware Solana transaction parserInspect captured patch +60 / −5
### core/src/apps/solana/transaction/parse.py
@@ -7,14 +7,18 @@
def parse_var_int(serialized_tx: BufferReader) -> int:
+ # Compact-u16 as decoded by solana-sdk's short_vec::visit_byte():
+ # https://github.com/anza-xyz/solana-sdk/blob/e2adabb20e084efe4e5a872bb5be71051130bcd8/short-vec/src/lib.rs#L104-L133
value = 0
- shift = 0
- while serialized_tx.remaining_count():
- B = serialized_tx.get()
- value += (B & 0b01111111) << shift
- shift += 7
+ for i in range(3):
+ B = serialized_tx.get() # raises EOFError if truncated
+ if B == 0 and i != 0:
+ raise ValueError # alias (non-canonical encoding)
+ value |= (B & 0b01111111) << (7 * i)
if B & 0b10000000 == 0:
break
+ else:
+ raise ValueError # the third byte must be the last one
if value > 0xFFFF:
raise ValueError # compact-u16 value too large
### core/tests/test_apps.solana.transaction.py
@@ -1,12 +1,14 @@
from typing import TYPE_CHECKING
+from trezor.utils import BufferReader
from trezor.wire import DataError
from apps.common.writers import write_bytes_unchecked, write_uint8, write_uvarint
from common import unittest, utils
if not utils.BITCOIN_ONLY:
from apps.solana.transaction import Transaction
+ from apps.solana.transaction.parse import parse_var_int
from apps.solana.types import AddressType
if TYPE_CHECKING:
@@ -106,6 +108,55 @@ def build_tx(
)
+@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
+class TestSolanaCompactU16(unittest.TestCase):
+ # Vectors from solana-sdk's short_vec test_deserialize():
+ # https://github.com/anza-xyz/solana-sdk/blob/e2adabb20e084efe4e5a872bb5be71051130bcd8/short-vec/src/lib.rs#L337-L378
+
+ def test_valid(self):
+ vectors = (
+ (b"\x00", 0x0000),
+ (b"\x7f", 0x007F),
+ (b"\x80\x01", 0x0080),
+ (b"\xff\x01", 0x00FF),
+ (b"\x80\x02", 0x0100),
+ (b"\xff\x0f", 0x07FF),
+ (b"\xff\x7f", 0x3FFF),
+ (b"\x80\x80\x01", 0x4000),
+ (b"\xff\xff\x03", 0xFFFF),
+ )
+ for encoded, expected in vectors:
+ reader = BufferReader(encoded)
+ self.assertEqual(parse_var_int(reader), expected)
+ self.assertEqual(reader.remaining_count(), 0)
+
+ def test_invalid(self):
+ vectors = (
+ # aliases
+ (b"\x80\x00", ValueError),
+ (b"\x80\x80\x00", ValueError),
+ (b"\xff\x00", ValueError),
+ (b"\xff\x80\x00", ValueError),
+ (b"\x80\x81\x00", ValueError),
+ (b"\xff\x81\x00", ValueError),
+ (b"\x80\x82\x00", ValueError),
+ (b"\xff\x8f\x00", ValueError),
+ (b"\xff\xff\x00", ValueError),
+ # too short
+ (b"", EOFError),
+ (b"\x80", EOFError),
+ (b"\x80\x80", EOFError),
+ # too long
+ (b"\x80\x80\x80\x00", ValueError),
+ # too large
+ (b"\x80\x80\x04", ValueError),
+ (b"\x80\x80\x06", ValueError),
+ )
+ for encoded, error in vectors:
+ with self.assertRaises(error):
+ parse_var_int(BufferReader(encoded))
+
+
@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
class TestSolanaTransactionHeader(unittest.TestCase):
def test_valid(self):Why this scored 61/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.