Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wireaddr: relax is_dnsaddr to allow for underscore in hostname #5789

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/test/run-ip_port_parsing.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ int main(int argc, char *argv[])
assert(is_dnsaddr("123example.com"));
assert(is_dnsaddr("example123.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("punycode.xn--bcher-kva.valid.com"));
assert(!is_dnsaddr("UPPERCASE.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("underscore.not.allowed.in.domain_name.com"));

/* Grossly invalid. */
assert(!separate_address_and_port(tmpctx, "[", &ip, &port));
Expand Down
17 changes: 14 additions & 3 deletions common/wireaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,18 +375,24 @@ bool is_wildcardaddr(const char *arg)
return streq(arg, "");
}

/* Rules:
/* The rules to check for DNS FQDNs, see `man 7 hostname`
*
* - not longer than 255
* - segments are separated with . dot
* - segments do not start or end with - hyphen
* - segments must be longer thant zero
* - lowercase a-z and digits 0-9 and - hyphen
* - segments must be longer than zero
* - 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)
{
size_t i, arglen;
int lastdot;
int numdot;

if (is_ipaddr(arg) || is_toraddr(arg) || is_wildcardaddr(arg))
return false;
Expand All @@ -396,8 +402,10 @@ bool is_dnsaddr(const char *arg)
if (arglen > 255)
return false;
lastdot = -1;
numdot = 0;
for (i = 0; i < arglen; i++) {
if (arg[i] == '.') {
numdot++;
/* segment must be longer than zero */
if (i - lastdot == 1)
return false;
Expand All @@ -416,6 +424,9 @@ bool is_dnsaddr(const char *arg)
continue;
if (arg[i] == '-')
continue;
/* allow for _ underscores in the first hostname part */
if (arg[i] == '_' && numdot == 0)
continue;
return false;
}
return true;
Expand Down