wireaddr: is_dnsaddr allow underscore in hostname

The hostname part of a DNS FQDN can allow for additional characters
other than specified in `man 7 hostname`.

This extends is_dnsaddr and the test issue #5657.
Also fixes a typo in a comment.

Changelog-Fixed: wireaddr: #5657 allow '_' underscore in hostname part of DNS FQDN
This commit is contained in:
Michael Schmoock 2022-12-05 10:58:25 +01:00 committed by Christian Decker
parent a96ff3b097
commit 29f81baac9
2 changed files with 17 additions and 3 deletions

View File

@ -126,11 +126,14 @@ int main(int argc, char *argv[])
assert(is_dnsaddr("123example.com")); assert(is_dnsaddr("123example.com"));
assert(is_dnsaddr("example123.com")); assert(is_dnsaddr("example123.com"));
assert(is_dnsaddr("is-valid.3hostname123.com")); assert(is_dnsaddr("is-valid.3hostname123.com"));
assert(is_dnsaddr("just-a-hostname-with-dashes"));
assert(is_dnsaddr("lightningd_dest.underscore.allowed.in.hostname.part.com"));
assert(!is_dnsaddr("UPPERCASE.invalid.com")); assert(!is_dnsaddr("UPPERCASE.invalid.com"));
assert(!is_dnsaddr("-.invalid.com")); assert(!is_dnsaddr("-.invalid.com"));
assert(!is_dnsaddr("invalid.-example.com")); assert(!is_dnsaddr("invalid.-example.com"));
assert(!is_dnsaddr("invalid.example-.com")); assert(!is_dnsaddr("invalid.example-.com"));
assert(!is_dnsaddr("invalid..example.com")); assert(!is_dnsaddr("invalid..example.com"));
assert(!is_dnsaddr("underscore.not.allowed.in.domain_name.com"));
/* Grossly invalid. */ /* Grossly invalid. */
assert(!separate_address_and_port(tmpctx, "[", &ip, &port)); assert(!separate_address_and_port(tmpctx, "[", &ip, &port));

View File

@ -375,18 +375,24 @@ bool is_wildcardaddr(const char *arg)
return streq(arg, ""); return streq(arg, "");
} }
/* Rules: /* The rules to check for DNS FQDNs, see `man 7 hostname`
* *
* - not longer than 255 * - not longer than 255
* - segments are separated with . dot * - segments are separated with . dot
* - segments do not start or end with - hyphen * - segments do not start or end with - hyphen
* - segments must be longer thant zero * - segments must be longer than zero
* - lowercase a-z and digits 0-9 and - hyphen * - allow lowercase a-z and digits 0-9 and - hyphen
* - additionall we allow for an '_' underscore in the hostname part.
*
* See issue #5657
* https://github.com/ElementsProject/lightning/issues/5657
* https://en.wikipedia.org/wiki/Hostname
*/ */
bool is_dnsaddr(const char *arg) bool is_dnsaddr(const char *arg)
{ {
size_t i, arglen; size_t i, arglen;
int lastdot; int lastdot;
int numdot;
if (is_ipaddr(arg) || is_toraddr(arg) || is_wildcardaddr(arg)) if (is_ipaddr(arg) || is_toraddr(arg) || is_wildcardaddr(arg))
return false; return false;
@ -396,8 +402,10 @@ bool is_dnsaddr(const char *arg)
if (arglen > 255) if (arglen > 255)
return false; return false;
lastdot = -1; lastdot = -1;
numdot = 0;
for (i = 0; i < arglen; i++) { for (i = 0; i < arglen; i++) {
if (arg[i] == '.') { if (arg[i] == '.') {
numdot++;
/* segment must be longer than zero */ /* segment must be longer than zero */
if (i - lastdot == 1) if (i - lastdot == 1)
return false; return false;
@ -416,6 +424,9 @@ bool is_dnsaddr(const char *arg)
continue; continue;
if (arg[i] == '-') if (arg[i] == '-')
continue; continue;
/* allow for _ underscores in the first hostname part */
if (arg[i] == '_' && numdot == 0)
continue;
return false; return false;
} }
return true; return true;