nsurl: Add support for IPv6 literals

Unfortunately, despite previous assertions to the contrary,
we do need to deal with IPv6 literals.  For now we validate
just that they are encased by square brackets and consist only
of hex digits and colons.  We do not validate that they are
actually valid IPv6 addresses.

Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
This commit is contained in:
Daniel Silverstone 2024-05-25 12:05:20 +01:00
parent 1cf1391916
commit 553dc93ec8
No known key found for this signature in database
GPG Key ID: C30DF439F2987D74

View File

@ -1263,6 +1263,9 @@ void nsurl__calc_hash(nsurl *url)
* Valid hostnames are valid DNS names. This means they must consist only of
* the ASCII characters a-z A-Z 0-9 '.', '_', or '-'.
*
* Unfortunately we also need to deal with IPv6 literals which are constrained
* but strange. Surrounded by '[' and ']' there are hex digits and colons
*
* \param host The hostname to check
* \return NSERROR_OK if the hostname is valid
*/
@ -1271,6 +1274,20 @@ static nserror nsurl__check_host_valid(lwc_string *host)
const char *chptr = lwc_string_data(host);
size_t nchrs = lwc_string_length(host);
if (*chptr == '[' && chptr[nchrs-1] == ']') {
/* Treat this as an IPv6 Literal */
chptr++;
nchrs -= 2;
while (nchrs--) {
const char ch = *chptr++;
if (!isxdigit(ch) && ch != ':') {
/* Not hex digit or colon */
return NSERROR_INVALID;
}
}
return NSERROR_OK;
}
while (nchrs--) {
const char ch = *chptr++;
if (!isalnum(ch) && !(ch == '.' || ch == '-' || ch == '_')) {