Skip to content

Cap server-sent friendly names in UTF-8 bytes - #339

Merged
maximmaxim345 merged 1 commit into
mainfrom
cap-server-names
Nov 3, 2026
Merged

maximmaxim345 merged 1 commit into
mainfrom
cap-server-names

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

#329 capped the text fields a client sends in client/hello. The server sends two friendly names that the client stores and shows: name in server/hello and group_name in group/update. Neither has a length limit.

This caps both at 64 bytes in UTF-8, so a client can hold them in fixed storage instead of an allocation whose size the server controls. 64 bytes matches the client-side caps from #329, which follow Matter's 64-byte version strings and UPnP's friendly-name guidance. The cap counts bytes, not characters, so both sides count the same way.

A client that receives a longer value truncates it at a UTF-8 character boundary instead of closing the connection. The server would send the same name again on every reconnect, so closing would leave the client disconnecting over and over, with nothing to show the user that a name is the cause. A group_name the server didn't check would disconnect every client in the group. This matches the client/pair-pending message, the one other display string with a cap, where the receiver truncates.

The server's mDNS TXT name is not capped, matching #329's handling of the client's TXT name. It SHOULD match the server/hello name and is only a discovery hint.

kahrendt added a commit to Sendspin/sendspin-cpp that referenced this pull request Nov 3, 2026
## Summary

Removes the server-identity heap allocations between accept and
admission. The connection kept the server's id and name as a
`ServerInformationObject` (two `std::string`s), and the published slot
copied it. A 43-character id is too long for either standard library's
inline string storage, and under libstdc++ so is a name over 15
characters. Admission made 3 such allocations on libc++ and 5 on
libstdc++ (ESP-IDF); it now makes 1 on both.

Depends on Sendspin/spec#339, which caps `server/hello`'s `name` and
`group/update`'s `group_name` at 64 bytes in UTF-8.

## Change

- `InlineText<N>` (`src/inline_text.h`) holds bounded text inside the
object; `IdText` is now `InlineText<PEER_ID_SIZE>`. `utf8_truncate()`
cuts text at a character boundary.
- The connection holds the server id as an `IdText` and the name as a
64-byte `ServerName`. `get_server_id()` returns `const IdText&`, and the
helpers that took the id as a `std::string` take a `std::string_view`.
- The published slot holds the same inline copies.
`get_server_information()` builds the public `ServerInformationObject`
on the caller's thread, so the public API is unchanged.
- `read_friendly_name()` truncates `server/hello`'s `name` and
`group/update`'s `group_name` to 64 bytes with one warning log.
`group_name` stays a `std::string` in the public `GroupUpdateObject`.
- `tools/stack_usage/README.md` is re-derived from fresh ESP32 builds:
- protocol task bound at -Og: 6,656 → 6,704 (`ServerHelloMessage` now
holds the name inline);
  - reachable figure at -Og: 6,400 → 6,368;
  - the 7,168 default does not change.

## Tests

- `test_inline_text.cpp`: a table of truncation cases (at the cap,
before a lead byte, inside 2-, 3- and 4-byte characters, malformed
continuation runs, only continuation bytes before the cut), plus
`InlineText::assign()` refusing text over its capacity.
- `Protocol.FriendlyNamesAreTruncatedToTheirCap`: a name at the cap is
kept whole, and one with a character straddling the cap is truncated
before it, for both fields.
- `JunkProbeDoesNotBlockRealServer` now also checks the published name
through `get_server_information()`. No test checked it before, so a
mutant that dropped it passed.
- `ADMISSION_ALLOCATIONS` is lowered to 1, checked under clang / libc++
and GCC 16 / libstdc++. The one left is the trust-change note
`on_handshake_complete()` queues.
- Mutants killed: no truncation in `read_friendly_name()`, unbounded
back-off, the `len > 0` guard removed (ASan out-of-bounds read), and the
published name dropped.

@maximmaxim345 maximmaxim345 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch, missed them. Thanks @kahrendt!

@maximmaxim345
maximmaxim345 merged commit adae05c into main Nov 3, 2026
1 check passed
@maximmaxim345
maximmaxim345 deleted the cap-server-names branch November 3, 2026 18:35
kahrendt pushed a commit that referenced this pull request Nov 5, 2026
<!-- Describe the change and why it is needed. -->

A recognized message can carry a value outside its field's fixed range,
such as `output_delay_ms` above 5000 in `client/state` or a controller
`volume` of 101. Most fields don't say what the receiver does with one.

This makes such a value a protocol error, and the receiver MUST close
the connection. Only a broken implementation sends one, and closing
matches how the spec treats other protocol errors. The rule only covers
fixed ranges and length limits. Bounds relative to another value, like
the visualizer `rate_max` or the player timing bounds, can be crossed by
a message in flight, and the receiver often can't check them reliably. A
field or message with its own rule keeps it: the server still ignores a
`seek` outside 0 to `seek_max_ms`, a bad `client/init` is still an init
failure, and the #344 `client/hello` caps keep their MAY close.

This also removes handling the rule now covers:
- truncating an over-long server `name` or `group_name` (from #339) and
`client/pair-pending` `message` (from #194)
- the SHOULD close for an artwork `channels` array longer than 4
- the player clamp rules for `output_delay_ms` and `min_buffer_ms`,
since their 0-5000 field ranges already bind the sender

It also adjusts text the rule would otherwise break:
- an artwork client that truncates its `channels` array keeps at least
one entry
- metadata `track` states its range (at least 1), and `year` no longer
says "YYYY format"
- values inside ignored inactive-role objects are left unvalidated
- sending a value outside a range or length limit an earlier revision
defined needs negotiation, like a new field

So plain text over its byte cap now closes the connection too, including
a client `name` or `device_info` string over 64 bytes.

- [x] I accept the [Contributor License
Agreement](../blob/main/CONTRIBUTOR-LICENSE-AGREEMENT.md) for this
contribution.
maximmaxim345 added a commit to Sendspin/aiosendspin that referenced this pull request Nov 17, 2026
# What does this implement/fix?

<!-- Quick description and explanation of changes. -->

Clients that follow the spec close the connection when the server/hello
`name` or the group/update `group_name` is longer than 64 bytes in
UTF-8. A long or non-Latin server name therefore made them reconnect in
a loop.

The server now cuts both names to 64 bytes without splitting a
character. It cuts the mDNS `name` the same way. `SendspinServer.name`
and `SendspinGroup.group_name` still return the full name.

**Related issue or spec PR (if applicable):**

- Sendspin/spec#339
- Sendspin/spec#347

## Types of changes

<!--
Tick exactly one box. CI (.github/workflows/pr-labels.yaml) applies the
matching label, and the release notes use it to sort this change.
-->

- [x] Bugfix (non-breaking change which fixes an issue): `bugfix`
- [ ] New feature (non-breaking change which adds functionality):
`new-feature`
- [ ] Enhancement to an existing feature: `enhancement`
- [ ] Breaking change (changes the public API or the protocol in a way
that is not backwards compatible): `breaking-change`
- [ ] Refactor (no behavior change): `refactor`
- [ ] Documentation only: `documentation`
- [ ] Maintenance / chore: `maintenance`
- [ ] CI / workflow change: `ci`
- [ ] Dependencies bump: `dependencies`
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