Loosen restriction on valid hostnames

This changes fixes an issue with GitPod integration, where large numbers
in a hostname were incorrectly causing redbean to refuse a request.
This commit is contained in:
Justine Tunney 2022-07-09 04:24:51 -07:00
parent c9e68b0ebc
commit 896db8843f
2 changed files with 15 additions and 7 deletions

View file

@ -55,17 +55,18 @@ bool IsAcceptableHost(const char *s, size_t n) {
int c, b, j;
if (n == -1) n = s ? strlen(s) : 0;
if (!n) return true;
if (n > DNS_NAME_MAX) return false;
if (n > DNS_NAME_MAX) {
return false;
}
for (b = j = i = 0; i < n; ++i) {
c = s[i] & 255;
if (isdigit(c)) {
b *= 10;
b += c - '0';
if (b > 255) {
} else if (c == '.') {
if (!i || s[i - 1] == '.') {
return false;
}
} else if (c == '.') {
if (!i || s[i - 1] == '.') return false;
b = 0;
++j;
} else {
@ -81,7 +82,11 @@ bool IsAcceptableHost(const char *s, size_t n) {
}
}
}
if (j != 3) return false;
if (i && s[i - 1] == '.') return false;
if (j != 3) {
return false;
}
if (i && s[i - 1] == '.') {
return false;
}
return true;
}

View file

@ -29,7 +29,6 @@ TEST(IsAcceptableHost, test) {
EXPECT_TRUE(IsAcceptableHost("1.2.3.4.5.arpa", -1));
EXPECT_TRUE(IsAcceptableHost("255.255.255.255", -1));
EXPECT_FALSE(IsAcceptableHost("255.255.255", -1));
EXPECT_FALSE(IsAcceptableHost("256.255.255.255", -1));
EXPECT_TRUE(IsAcceptableHost("hello.example", -1));
EXPECT_FALSE(IsAcceptableHost("hello..example", -1));
EXPECT_TRUE(IsAcceptableHost("hello", -1));
@ -46,6 +45,10 @@ TEST(IsAcceptableHost, test) {
EXPECT_TRUE(IsAcceptableHost("there-.example", -1));
EXPECT_FALSE(IsAcceptableHost("ther#e.example", -1));
EXPECT_TRUE(IsAcceptableHost("localhost", -1));
EXPECT_TRUE(IsAcceptableHost("8080-gitpodio-empty-abs4xad1abc", -1));
EXPECT_TRUE(IsAcceptableHost("ws-eu53", -1));
EXPECT_TRUE(IsAcceptableHost("gitpod", -1));
EXPECT_TRUE(IsAcceptableHost("io", -1));
}
TEST(IsAcceptablePort, test) {