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

Impose maximum on port string in URI parser #182

Merged
merged 1 commit into from
Dec 2, 2024
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
32 changes: 21 additions & 11 deletions src/ne_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
/* any characters which should be path-escaped: */
#define URI_ESCAPE ((URI_GENDELIM & ~(FS)) | URI_SUBDELIM | OT | PC)

/* Maximum allowed port number. */
#define MAX_PORT (65535)

static const unsigned short uri_chars[256] = {
/* 0xXX x0 x2 x4 x6 x8 xA xC xE */
/* 0x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
Expand Down Expand Up @@ -220,23 +223,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 <= MAX_PORT && (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 || port > MAX_PORT) 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
2 changes: 2 additions & 0 deletions test/uri-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ static int failparse(void)
"http://fish/[foo]/bar",
"http://foo:80bar",
"http://foo:80:80/bar",
"http://foo:8000000000000000000000000000000000000000000000000/bar",
"http://foo:65536/bar",
NULL
};
int n;
Expand Down
Loading