Skip to content

Allow receivers to limit fragmented message and artwork sizes - #322

Merged
kahrendt merged 2 commits into
mainfrom
fix/fragment-size-limit
Oct 21, 2026
Merged

kahrendt merged 2 commits into
mainfrom
fix/fragment-size-limit

Conversation

@maximmaxim345

@maximmaxim345 maximmaxim345 commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

The fragmentation rules had no size limit, so a peer could send a first fragment followed by non-last fragments forever and grow the receiver's reassembly buffer without bound.

Receivers may now close the connection once the reassembled data passes a limit they choose. I didn't pick a fixed number since the spec doesn't bound sample rate, channels or bit depth. A 150 ms PCM chunk at 384 kHz, 8 channels and 24-bit is already about 1.38 MB, and a fixed cap large enough for that wouldn't protect small devices. Each receiver knows what it can receive, so it can pick its own.

Artwork had the same gap, since total_size is a uint32 and the per-message cap says nothing about the whole image. Clients may now discard an image above a limit they choose. I went with discarding instead of closing, so playback keeps running and a resent image can't cause a reconnect loop.

The limits say "valid message" and "valid image" instead of "from a well-behaved client or server", since "valid" is what the rest of the spec already uses.

@maximmaxim345 maximmaxim345 changed the title Allow receivers to limit the size of fragmented messages Allow receivers to limit fragmented message and artwork sizes Oct 21, 2026

@kahrendt kahrendt 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.

This is a good change for embedded!

@kahrendt
kahrendt merged commit d949d3f into main Oct 21, 2026
1 check passed
@kahrendt
kahrendt deleted the fix/fragment-size-limit branch October 21, 2026 11:58
kahrendt pushed a commit that referenced this pull request Nov 4, 2026
<!-- Describe the change and why it is needed. -->

`client/hello` is the only message whose content a server can't know in
advance. It can carry support objects for application-specific roles,
and it arrives before any role is negotiated, so a server that doesn't
implement such a role still has to parse its object. A server that
parses JSON with a fixed nesting limit closes the connection on a deeply
nested object, even though unknown fields must be ignored. No server can
meet the "large enough that no valid message it can receive exceeds it"
from #322 for `client/hello` either, since it has no maximum size. When
the server initiated the connection and playback is running, it
reconnects after a drop, so an oversized hello turns into a reconnect
loop.

A `client/hello` now has at most 10 nesting levels and 16384 bytes, and
servers must accept anything within those limits:
- 10 levels, counting the root object as level 1. The deepest
`client/hello` today is 5 levels, for example
`player@v1_support.supported_formats`. 10 is also ArduinoJson's default
limit, and it counts levels the same way.
- 16384 bytes. A `client/hello` with every field at its maximum length
and 82 formats is 6257 bytes. The formats cover 8 sample rates from 44.1
to 384 kHz, mono and stereo, every PCM and FLAC bit depth, and Opus.
Only listing every channel count from 1 to 8 goes over, at 22223 bytes.
A player over the cap drops its lowest priority formats.

I first looked at size caps for every JSON message and length caps for
the metadata strings. I left them out, since every other message is
either defined by the spec or only sent for roles both sides implement,
so the spec already controls how large and how deep it gets.
`server/hello` can't carry application-specific support objects, so it
needs no cap.

sendspin-server-cpp already accepts both limits.

- [x] I accept the [Contributor License
Agreement](../blob/main/CONTRIBUTOR-LICENSE-AGREEMENT.md) for this
contribution.
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