[mdns] add validation for DNS names in mDNS APIs (#12039)

This commit adds `Dns::Name::ValidateName()` and `ValidateLabel()`
helper methods to validate a DNS name or label.

These methods are used at the entry of the mDNS `Register*()` and
`Unregister*()` public APIs to validate the provided host, service,
and key names. This prevents issues with malformed names and improves
the robustness of the mDNS module.

Includes unit tests for the new validation methods.
This commit is contained in:
Abtin Keshavarzian
2025-10-17 21:25:42 -07:00
committed by GitHub
parent ca9a731e4f
commit 99f510a16c
7 changed files with 237 additions and 8 deletions
+31
View File
@@ -591,6 +591,37 @@ void TestDnsName(void)
VerifyOrQuit(!dnsName.Matches("Name.With.Dot", "_srv._tcp", "local."));
VerifyOrQuit(!dnsName.Matches("Name.With.Dot", "_srv._udp", "arpa."));
printf("----------------------------------------------------------------\n");
printf("Name::ValidateLabel\n");
SuccessOrQuit(Dns::Name::ValidateLabel("a"));
SuccessOrQuit(Dns::Name::ValidateLabel("hello"));
SuccessOrQuit(Dns::Name::ValidateLabel("012345678901234567890123456789012345678901234567890123456789012")); // 63
VerifyOrQuit(Dns::Name::ValidateLabel("0123456789012345678901234567890123456789012345678901234567890123") ==
kErrorInvalidArgs);
VerifyOrQuit(Dns::Name::ValidateLabel("") == kErrorInvalidArgs);
SuccessOrQuit(Dns::Name::ValidateName("a"));
SuccessOrQuit(Dns::Name::ValidateName("a.b.c"));
SuccessOrQuit(Dns::Name::ValidateName("a.b.c."));
SuccessOrQuit(Dns::Name::ValidateName("a.b.012345678901234567890123456789012345678901234567890123456789012."));
SuccessOrQuit(Dns::Name::ValidateName("."));
// Empty labels
VerifyOrQuit(Dns::Name::ValidateName("") == kErrorInvalidArgs);
VerifyOrQuit(Dns::Name::ValidateName("a..b") == kErrorInvalidArgs);
VerifyOrQuit(Dns::Name::ValidateName(".a.b") == kErrorInvalidArgs);
VerifyOrQuit(Dns::Name::ValidateName("a.b..") == kErrorInvalidArgs);
// Long labels or names
VerifyOrQuit(Dns::Name::ValidateName("a.b.0123456789012345678901234567890123456789012345678901234567890123.") ==
kErrorInvalidArgs);
VerifyOrQuit(Dns::Name::ValidateName("012345678901234567890123456789012345678901234567890123456789012."
"012345678901234567890123456789012345678901234567890123456789012."
"012345678901234567890123456789012345678901234567890123456789012."
"012345678901234567890123456789012345678901234567890123456789012") ==
kErrorInvalidArgs);
message->Free();
testFreeInstance(instance);
}