mojira.dev
MC-311234

The `written_book_content` component mishandles NaN and infinity, leading to deleted items and soft locks

Text was switched from JSON to NBT in 1.21.5, but the component written_book_content still checks the length of the data by turning it into a JSON string and checking the length of that, rather than turning it into SNBT. Since Gson by default doesn’t allow writing non-finite floating values, this results in an IllegalArgumentException. The relevant method is net/minecraft/network/chat/ComponentSerialization#flatRestrictedCodec, which appears to be only called from net/minecraft/world/item/component/WrittenBookContent#CONTENT_CODEC.

An exception caused by an affected item being in the player’s inventory is attached as stacktrace.txt. The world is attached as Join Exception.zip, and will kick the player on joining. The world Item Deletion.zip has command blocks to demonstrate a book getting deleted when a hover_event with show_item has NaNd added to the shown item’s custom_data component. Here are the commands:

# Create NaNd
execute positioned ~ -179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792 ~ run summon minecraft:eye_of_ender ~ ~179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792 ~ {UUID:[I;0,0,0,0]}
data modify storage minecraft:num nan set from entity 0-0-0-0-0 Pos[1]

# Get book
give @p minecraft:written_book[minecraft:written_book_content={pages:[{raw:{text:"text",hover_event:{action:show_item,id:diamond,components:{custom_data:{a:1}}}}}],author:me,title:title}]

# Modify book (deleting it unintentionally)
data modify block 0 -59 2 Items[0].components."minecraft:written_book_content".pages[0].raw.hover_event.components."minecraft:custom_data".a set from storage minecraft:num nan

This is related to MC-200070, which is where I got the commands for NaN from.

Note that, while I don’t currently see a way to actually create this item with vanilla Minecraft (which leads to soft locks, crashes, etc.), since net/minecraft/world/item/component/WrittenBookContent#STREAM_CODEC doesn’t use flatRestrictedCodec, it should be possible for a player with a hacked client to transfer it to vanilla players via a server. I have not actually tested this, but I do know that a hacked client in 1.21.5 can create this item and interact with it by dropping it on the ground and picking it back up.

Attachments

Comments 1

Thank you for helping us improve Minecraft! We saved your files:

[media][media][media]

mega12345mega

(Unassigned)

Plausible

Platform

Low

Save Data

26.2

Retrieved