mirror of
https://github.com/espressif/openthread.git
synced 2026-10-04 23:07:37 +00:00
[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:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user