Skip to content

Commit

Permalink
* src/ne_uri.c (ne_uri_parse): Restrict the maximum allowed port to
Browse files Browse the repository at this point in the history
  65535, parse the port number directly rather than via atoi().

* test/uri-tests.c (failparse): Test that an excessively long port
  number fails to parse.
  • Loading branch information
notroj committed Nov 30, 2024
1 parent ea26872 commit 7b835c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/ne_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,30 @@ int ne_uri_parse(const char *uri, ne_uri *parsed)

parsed->host = ne_strndup(s, p - s);

if (p != pa && p + 1 != pa) {
p++;
/* Iff p and pa (=> path-abempty) differ, the optional port
* section is present and parsed here: */
if (p != pa) {
unsigned int port = 0;

s = p;
/* => s = port */
if (*p++ != ':') return -1;

while (p < pa) {
if (!(uri_lookup(*p) & URI_DIGIT))
return -1;
/* => p = port */

p++;
}
/* port = *DIGIT
*
* Note: port can be the empty string, in which case now:
* p == pa and port is parsed as 0, as desired. */
while (p < pa && port < 65536 && (uri_lookup(*p) & URI_DIGIT) != 0)
port = 10*port + *p++-'0';

/* If p did not reach pa there was some non-digit present
* or the integer was too large, so fail. */
if (p != pa) return -1;

parsed->port = atoi(s);
parsed->port = port;
}

s = pa;
s = pa; /* Next, parse path-abempty */
}

/* => s = path-abempty / path-absolute / path-rootless
Expand Down
1 change: 1 addition & 0 deletions test/uri-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ static int failparse(void)
"http://fish/[foo]/bar",
"http://foo:80bar",
"http://foo:80:80/bar",
"http://foo:8000000000000000000000000000000000000000000000000/bar",
NULL
};
int n;
Expand Down

0 comments on commit 7b835c5

Please sign in to comment.