Skip to content

Fix #268: return null from Eip4Token NFT link getters when R9 is not Coll[Byte] - #269

Open
Kshot3000 wants to merge 1 commit into
ergoplatform:developfrom
Kshot3000:fix/eip4-nft-link-malformed-r9
Open

Kshot3000 wants to merge 1 commit into
ergoplatform:developfrom
Kshot3000:fix/eip4-nft-link-malformed-r9

Conversation

@Kshot3000

Copy link
Copy Markdown
Contributor

Fixes #268.

Problem

Eip4Token.getNftContentLink() converted R9 (or the first element of its pair form) with an unchecked cast to Coll and an unchecked byte conversion, and getNftCoverImageLink() did the same for the pair's second element. R9 of a minting box is untrusted chain data, so an NFT-typed token with a malformed R9 crashed both getters with ClassCastException instead of returning null as their @Nullable contract promises — e.g. a Coll[Long] R9 fails inside collToByteArray ([J cannot be cast to [B), and a pair whose link element is a Long fails at the cast itself, even when the pair's other element is a perfectly valid link.

Fail-first, on pristine develop plus only the new test: nftLinkWithMalformedR9Test fails with ClassCastException: class [J cannot be cast to class [B at Eip4Token.java:182 on the Coll[Long] R9 case; the valid-shape pins in the same test (direct Coll[Byte] link, pair of Coll[Byte] links) pass, as do the other 4 tests in the class.

Fix

Both getters now route through collByteValueToStringOrNull, which applies the same element-type check getR7ByteArrayOrNull already uses for R7 — the value must be a Coll whose item type is Byte — and returns null for any other shape. The check is per element, so a pair with one valid link element still returns that link. Valid Coll[Byte] links return byte-identical strings, including an empty one.

Tests

New Eip4TokenBuilderTest.nftLinkWithMalformedR9Test pins the two valid shapes and four malformed ones (Coll[Long] R9; (Long, Coll[Byte]); (Coll[Byte], Long); (Coll[Long], Coll[Long])), asserting null for each malformed element and the surviving valid link for the mixed pairs.

Verification: Eip4TokenBuilderTest 5/5 post-fix per its TEST-*.xml report (tests=5, failures=0, including the live explorer test); full sbt test green across all modules (common 18/18, lib-impl 4 passed / 1 skipped, appkit 96/96).

Note: this PR, #265 and #267 all touch Eip4Token.java, in disjoint areas (the R9 link getters here, getNftContentHash in #265, the constructor at the top of the file in #267), so they should apply in any order.


Submitted by @Kshot9000 (https://x.com/kshot9000) · Tips: ERG 9fcM5RWnAjmP4vx5bnW6yohB6H9bLq8sJbaPLHtwZLtQPB32Pvy

…ot Coll[Byte]

getNftContentLink converted R9 (or the first element of its pair form)
with an unchecked cast to Coll and an unchecked byte conversion, and
getNftCoverImageLink did the same for the pair's second element. R9 of
a minting box is arbitrary chain data: a Coll[Long] R9 crashed the
content getter with a ClassCastException from collToByteArray, and a
pair whose link element is not a Coll (or not Coll[Byte]) crashed both
getters the same way, instead of returning null as the @nullable
contract promises for unavailable links.

Both getters now route through collByteValueToStringOrNull, which
applies the same element-type check getR7ByteArrayOrNull already uses
for R7 (Coll whose item type is Byte) and returns null for any other
shape. The check is per element, so a pair with one valid link element
still returns that link. Valid Coll[Byte] links return byte-identical
strings.
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.

Eip4Token NFT link getters crash on malformed R9 instead of returning null

1 participant