Skip to content

eth: reject unsafe EIP-712 number inputs - #18

Closed
benma-agent wants to merge 1 commit into
BitBoxSwiss:masterfrom
benma-agent:benma-agent/eip712-safe-number-inputs
Closed

benma-agent wants to merge 1 commit into
BitBoxSwiss:masterfrom
benma-agent:benma-agent/eip712-safe-number-inputs

Conversation

@benma-agent

@benma-agent benma-agent commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

JavaScript can round integer values before they reach the EIP-712 encoder. Checking only
integrality accepts those rounded values without warning.

Require safe integers for number inputs. Keep decimal strings and bigint inputs for exact
values outside ±(2^53 - 1), and document that converting an already-rounded number cannot
restore the original digits.

Related:

JavaScript can round integer values before they reach the EIP-712 encoder. Checking only
integrality accepts those rounded values without warning.

Require safe integers for number inputs. Keep decimal strings and bigint inputs for exact
values outside ±(2^53 - 1), and document that converting an already-rounded number cannot
restore the original digits.

Related:

- [bitbox02-api-go #199](BitBoxSwiss/bitbox02-api-go#199)
- [bitbox-api-rs #141](BitBoxSwiss/bitbox-api-rs#141)
@benma-agent
benma-agent force-pushed the benma-agent/eip712-safe-number-inputs branch from 90a23fe to 21e449e Compare November 29, 2026 08:18
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
Converting JSON numbers through f64 can round large integers or turn
fractional values into integers before EIP-712 signing.

Preserve the JSON digits and convert integral decimal and exponent forms
exactly, including values beyond u64. Reject fractions without rounding,
and bound exponent expansion to the 256-bit integer range.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18
benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 29, 2026
Decode JSON numbers as exact text so values such as 9007199254740993
are not rounded before encoding. Use math/big's rational parser to accept
integral decimal and exponent forms, rejecting fractions and values
outside the 256-bit range.

Related:

- [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141)
- [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
Converting JSON numbers through f64 can round large integers or turn
fractional values into integers before EIP-712 signing.

Parse JSON numbers with BigDecimal and convert integral values exactly,
including decimal and exponent notation and integers beyond u64. Reject
fractions without rounding and bound values before integer conversion.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18
@benma
benma requested a review from Tomasvrba November 29, 2026 08:23
@benma
benma marked this pull request as ready for review November 29, 2026 08:23
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
Converting JSON numbers through f64 can round large integers or turn
fractional values into integers before EIP-712 signing.

Preserve exact JSON integer tokens and parse them directly into big integers
up to 256 bits. Numeric inputs now require integer notation; decimal and
exponent forms such as 128.0 and 1e3 are rejected.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18
benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 29, 2026
Decode JSON numbers as exact text so values such as 9007199254740993
are not rounded before encoding. Parse integer notation directly with
math/big, rejecting decimal points, exponents, and values outside 256 bits.

Related:

- [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141)
- [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 29, 2026
Require decoded numeric values to be integers within the JavaScript
safe-integer range, matching the TypeScript client. Decimal and exponent
notation remains accepted when it decodes to a safe integer. Use decimal
strings for larger values.

Related:

- [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141)
- [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
benma-agent added a commit to benma-agent/bitbox-api-rs that referenced this pull request Nov 29, 2026
EIP-712 numeric values outside the safe integer range can lose precision
when decoded as floating point.

Reject numeric values outside ±(2^53 - 1), requiring decimal strings for
larger integers. Keep integral numeric forms such as 128.0 and 1e3, and
parse JSON floats with round-trip precision at the safe-range boundaries.

Related fixes:
- Go: BitBoxSwiss/bitbox02-api-go#199
- TypeScript: BitBoxSwiss/bitbox-api-ts#18

@Tomasvrba Tomasvrba left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as go and rust api PRs, but in the TS case, doesn't seem like anything is gained at all?

if (typeof value === 'number') {
if (!Number.isInteger(value) || value < 0) {
throw new TypedMessageError('Number is not an uint');
if (!Number.isSafeInteger(value) || value < 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as the go and rust apis

Switching from Number.isInteger to Number.isSafeInteger rejects exact large round numbers like 1e18 (1 ETH in wei) that @metamask/eth-sig-util (used by Rabby and MetaMask) and viem accept.

Because the BitBox02 always displays the exact number it signs on the device screen, there isn't a blind-signing security issue here, rejecting > 2^53 - 1 mostly causes dApps sending { amount: 1e18 } to fail on BitBox02 while continuing to work in MetaMask and Rabby's software wallet. Unless we specifically want to break compatibility with those dApps, I'd lean toward keeping Number.isInteger here and closing this PR.

@benma-agent benma-agent Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing this PR and retaining Number.isInteger. Callers needing exact large integers should supply decimal strings or bigint.

benma-agent added a commit to benma-agent/bitbox02-api-go that referenced this pull request Nov 30, 2026
Decode typed-message numbers without converting them to float64 so large
integers retain every digit. Accept integral decimal and exponent forms,
including 128.0 and 1e18, and reject fractional values instead of rounding
them. Integer strings keep their existing parsing behavior.

Related:

- [Rust BitBoxSwiss#141](BitBoxSwiss/bitbox-api-rs#141)
- [TypeScript BitBoxSwiss#18](BitBoxSwiss/bitbox-api-ts#18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants