Skip to content

Validate buttonless DFU advertising name length#562

Open
nrbrook wants to merge 1 commit into
nordicsemi:mainfrom
nrbrook:name-length-fix
Open

Validate buttonless DFU advertising name length#562
nrbrook wants to merge 1 commit into
nordicsemi:mainfrom
nrbrook:name-length-fix

Conversation

@nrbrook

@nrbrook nrbrook commented May 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Validate buttonless DFU advertising name requests against the current write-with-response payload before writing.
  • Preserve user-specified names instead of truncating them, and fail with DFUError.invalidAdvertisementName when the name is too long for the current connection.
  • Document the conservative 18-byte limit for default ATT MTU and the runtime MTU-dependent behavior.

Problem

The existing implementation truncates a user-specified alternative advertising name to 20 bytes, matching the buttonless DFU service's maximum advertising name length. However, the full set-name write also includes the opcode and length byte, so a 20-byte name requires a 22-byte ATT write payload.

With the default ATT MTU, the write-with-response payload can be only 20 bytes. In that case, CoreBluetooth may send the oversized set-name command using ATT Prepare Write requests instead of a single ATT Write Request. Nordic's buttonless DFU set-name command is handled as a single write, and does not respond to prepare write, resulting in a connection timeout.

This change validates against the actual maximumWriteValueLength(for: .withResponse) at runtime. User-specified names that do not fit fail clearly instead of being truncated.

@CLAassistant

CLAassistant commented May 5, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@philips77

Copy link
Copy Markdown
Contributor

Hi,
Good catch.

There are, however, few things that perhaps would make it easier...
First of all, maximumWriteValueLength(for: .withResponse) always returns 512, as Long Write is supported by iOS, and I don't think the central knows whether it is supported by the peripheral. It is the .withoutResponse that may return 20, had the MTU not been increased.

So we are only talking about limiting the name from 20 to 18, which perhaps could just be solved by trumcating it to 18...?

The name must fit into the advertising packet, which is 31 bytes long. 3 bytes are used for flags, 2 for the Local Name and length, leaving 26 for the max length of name, assuming there's no other data there. By default, the DFU bootloader advertises with a Secure DFU UUID, which is 16-bit long, which consumes additional 6 bytes, hence the limit was set to 20. However, as you said, if the ATT MTU is 23 (default), and 2 bytes are used for the opcode and command, the max size is in fact 18 bytes.

As far as I remember, the library is using a default name with format DFUxxxxx, where x are random numbers. Do you need anything longer than that?

I admit, that the limit of 20 is incorrect, but limit of 18 would seem to be fine. Or am I not seeing the whole picture? :)

@philips77 philips77 added the bug label May 8, 2026
@nrbrook

nrbrook commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Yes, I forgot about it returning 512, and just limited in my code. So a hard limit of 18 might be better. Do you agree with the approach of erroring rather than trimming?

I use a custom name for identification in the event the device gets stuck in the bootloader, for recovery.

@philips77

Copy link
Copy Markdown
Contributor

On one hand, error seems more "fair", as the lib woundn't do any magic. On the other hand it "a bit" breaks compatibility, but on the 3rd hand - it's not that a lot of people are setting the name to 19 characters :)

If you find it better, let's keep the error. I like your comments and work you've done.

Reject user-specified advertising names longer than 18 UTF-8 bytes before CoreBluetooth can fall back to ATT Prepare Write, which Nordic's buttonless DFU set-name command does not handle.

The limit is hard-coded rather than derived from `maximumWriteValueLength(for: .withResponse)` because CoreBluetooth always reports 512 once Long Write is supported, so the dynamic value is not a useful guard against the prepare-write fallback. 18 bytes leaves room for the 1-byte op code and 1-byte length field within the 20-byte payload of the default ATT MTU.
@nrbrook nrbrook force-pushed the name-length-fix branch from dff0d4d to 56f46ec Compare May 8, 2026 13:36
@nrbrook

nrbrook commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Personally I think an error is better, especially as it is a fixed limit. I've updated the changes to 18 bytes. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants