Skip to content

Feature Request: Automatically Trim Whitespace from mDNS Hostnames / sanitize mDNS Hostname #5999

Backlog

Description

@twadejr

[I used an AI to troubleshoot and found an issue that may help the project. I apologize in advance if the details are poor or lacking. This is my first attempt to give back for the years I've been enjoying this project!!]

Feature Request: Automatically Trim Whitespace from mDNS Hostnames

Summary

Please consider automatically trimming leading and trailing whitespace from the mDNS hostname field in WLED's WiFi Setup configuration.

Problem observed

I accidentally entered a trailing space in the mDNS hostname field. The value looked correct at a glance, but WLED retained and advertised the whitespace as part of the hostname.

The intended hostname was:

ecenter.local

However, WLED advertised:

ecenter .local

I confirmed this using Avahi network discovery. Its output showed:

hostname = [ecenter\032.local]

The \032 represents an ASCII space, confirming that the trailing whitespace was actually included in the advertised hostname.

This caused an application relying on mDNS discovery and hostname resolution (LedFX) to discover the WLED device but fail to resolve its hostname and add it successfully.

Suggested improvements

  1. Automatically trim leading and trailing whitespace when saving the mDNS hostname.
  2. Validate the resulting hostname against permitted hostname characters.
  3. Display a warning if invalid characters are entered.
  4. Ideally, apply the same validation wherever WLED accepts a hostname used for network discovery.

Suggested reproduction steps

  1. Open WLED's WiFi Setup configuration.
  2. Enter a hostname with an accidental trailing space (for example, ecenter ).
  3. Save the configuration and allow WLED to advertise its mDNS hostname.
  4. Inspect the advertised hostname using an mDNS discovery tool.
  5. Observe that the hostname contains the encoded trailing space and that clients attempting to resolve the expected hostname may fail.

Expected behavior

WLED should automatically remove leading and trailing whitespace before saving and advertising the hostname, or clearly reject the entry with a useful validation message.

Why this matters

Trailing spaces are invisible in configuration fields and very easy to enter accidentally. This can create confusing networking problems that are difficult for users to diagnose, particularly with applications that depend on automatic device discovery.

This is a relatively small usability improvement that could prevent unnecessary troubleshooting and improve compatibility with other software.

I really appreciate the work that goes into WLED. It's an excellent project, and I've gotten a tremendous amount of enjoyment and usefulness out of it. I'd be happy to help test a fix.

Thank you for maintaining such a great project!

Activity

  1. softhack007 commented on Nov 29, 2026

    @softhack007
    Member

    @coderabbitai does mDNS have the same rules as DNS, so we could use our existing sanitizer function ?

    WLED/wled00/util.cpp

    Lines 146 to 147 in 5e852a8

    // in-place hostname sanitizer, extracted from prepareHostname()
    static void sanitizeHostname(char* hostname, size_t maxLen) {

  2. coderabbitai commented on Nov 29, 2026

    @coderabbitai
    Contributor
    Analysis chain

    Script executed:

    #!/bin/bash
    set -u
    echo '=== Issue #5999 ==='
    gh issue view 5999 --repo wled/WLED --json title,body,comments --jq '{title,body,comments: [.comments[] | {author: .author.login, body}]}'
    echo '=== Current util.cpp outline ==='
    ast-grep outline wled00/util.cpp --items all
    echo '=== Current util.cpp around referenced line ==='
    cat -n wled00/util.cpp | sed -n '105,210p'
    echo '=== Referenced revision ==='
    git show 5e852a84e612b9a230c6d081b77f66d990783afc:wled00/util.cpp 2>/dev/null | nl -ba | sed -n '105,210p'
    echo '=== Hostname and mDNS call sites ==='
    rg -n -C 3 '\b(prepareHostname|MDNS\.begin|MDNS\.addService|setHostname|escapedMac)\s*\(' wled00 usermods -g '*.{cpp,h}' | head -300
    echo '=== Project guidance ==='
    for f in AGENTS.md docs/hardening.instructions.md docs/securecode.instructions.md; do if [ -f "$f" ]; then rg -n -i -C 2 'hostname|mDNS|sanitiz' "$f"; fi; done

    Length of output: 28013


    web_search executed:

    Check official RFC 6762 and RFC 6763, RFC 1123 and RFC 1035 for the precise rules for mDNS host names versus DNS host names and DNS-SD service instance names. Are hostname labels restricted to ASCII letters, digits and hyphens, including numeric-leading hostnames and 63-byte label limit? Can DNS-SD instance labels contain spaces and UTF-8? Cite the relevant RFC sections.

    Length of output: 18471


    Script executed:

    #!/bin/bash
    set -u
    echo '=== All cmDNS uses ==='
    rg -n -C 4 '\bcmDNS\b|\bDEFAULT_MDNS_NAME\b' wled00 -g '*.{h,cpp}'
    echo '=== Startup functions ==='
    ast-grep outline wled00/wled.cpp --items all --match 'initConnection|initServer|begin'
    cat -n wled00/wled.cpp | sed -n '540,625p;760,795p;905,950p'
    echo '=== getWLEDhostname callers ==='
    rg -n -C 5 '\bgetWLEDhostname\s*\(' wled00 -g '*.{h,cpp}'

    Length of output: 22059


    @softhack007 Yes. We can reuse the sanitizer for the mDNS hostname label, although the rules are not identical.

    mDNS permits a broader range of characters than the traditional DNS hostname convention. For compatibility with clients, it is sensible to use letters, digits and hyphens, with no leading or trailing hyphen. A label can be at most 63 bytes. See RFC 6762 §3 and RFC 8766 §5.3. This also matches WLED’s hostname-hardening guidance in docs/securecode.instructions.md.

    The existing sanitizeHostname() would turn ecenter into ecenter. It also replaces internal spaces with hyphens, so it does more than trim whitespace. cmDNS currently holds at most 32 characters, within the DNS label limit.

    There is one important gap: getWLEDhostname() sanitizes a copy of cmDNS for the network hostname, but MDNS.begin() and ArduinoOTA.setHostname() still receive cmDNS directly. The sanitizer is also static to util.cpp. Reusing it for those paths would require exposing it or adding a shared wrapper. If we normalize the stored value, we should handle both settings input and loaded configuration, while preserving an empty cmDNS as the mDNS-disable setting and retaining the default-name behavior.

    This recommendation applies to the hostname, not DNS-SD service instance names. Those can contain spaces and Unicode text (RFC 6763 §4.1.1).


    Learnings used

    Learnt from: softhack007
    Repo: wled/WLED PR: 5436
    File: wled00/fcn_declare.h:394-395
    Timestamp: 2026-03-22T00:09:16.015Z
    Learning: In WLED (wled00/fcn_declare.h), the `prepareHostname(char* hostname, size_t maxLen)` signature change from the old `prepareHostname(char* hostname)` is intentional and breaking. There is no compatibility shim; any usermod or downstream caller must explicitly pass the buffer size. Do not suggest a default-size shim for this function.
    
    Learnt from: CR
    Repo: wled/WLED PR: 0
    File: docs/securecode.instructions.md:0-0
    Timestamp: 2026-11-03T20:54:08.584Z
    Learning: Applicability reference:
    AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
    See also: `.github/copilot-instructions.md`, `.github/agent-build.instructions.md`,
    `docs/cpp.instructions.md`, `docs/web.instructions.md`, `docs/cicd.instructions.md`,
    `docs/hardening.instructions.md`, `docs/securecode.instructions.md`.
    
    AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
    ## General Rules
    ### Security Hardening
    When writing or reviewing code in `wled00/`, `usermods/`, `wled00/data/`, or `.github/workflows/`,
    consult `docs/hardening.instructions.md` (concise checklist) and `docs/securecode.instructions.md` (detailed rules with examples).
    These files define WLED's threat model, trust boundary model, and WLED-specific constraints (no TLS baseline, no UDP authentication for protocol-defined
    multicast/broadcast, firewall-isolated deployment assumed).
    
    Source excerpt:
    # WLED Security Review Standards (Embedded + Web UI)
    
    ## Firmware Security (C++, OWASP A01/A04/A05/A10)
    
    ### FW12: mDNS and hostname sanitization
    
    - For `MDNS.begin()`, `MDNS.addService()`, and `ArduinoOTA.setHostname()`, ensure user-provided hostnames are RFC-compliant (letters/digits/hyphen, no leading/trailing hyphen) and clamped to 63 characters.
    

    You are interacting with an AI system.

  3. added this to the 16.1.x bugfixes milestone on Nov 30, 2026
  4. added a commit that references this issue on Dec 2, 2026
    44ccd1f
  5. softhack007 commented on Dec 2, 2026

    @softhack007
    Member

    this is partly solved now, by trimming the mDNS name.

    The mDNS name can still be malformed - this can be solved by using sanitizeHostname(), however its a potentially breaking change => candidate for 17.0.

  6. changed the title [-]Feature Request: Automatically Trim Whitespace from mDNS Hostnames[/-] [+]Feature Request: Automatically Trim Whitespace from mDNS Hostnames / sanitize mDNS Hostname[/+] on Dec 2, 2026
  7. added
    AI
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AIenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions