Skip to content

Commit a75bc32

Browse files
committed
* src/ne_uri.c (ne_uri_parse): Restrict the maximum allowed port to
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.
1 parent ea26872 commit a75bc32

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/ne_uri.c

+21-11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
/* any characters which should be path-escaped: */
9595
#define URI_ESCAPE ((URI_GENDELIM & ~(FS)) | URI_SUBDELIM | OT | PC)
9696

97+
/* Maximum allowed port number. */
98+
#define MAX_PORT (65535)
99+
97100
static const unsigned short uri_chars[256] = {
98101
/* 0xXX x0 x2 x4 x6 x8 xA xC xE */
99102
/* 0x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT,
@@ -220,23 +223,30 @@ int ne_uri_parse(const char *uri, ne_uri *parsed)
220223

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

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

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

229-
while (p < pa) {
230-
if (!(uri_lookup(*p) & URI_DIGIT))
231-
return -1;
233+
/* => p = port */
232234

233-
p++;
234-
}
235+
/* port = *DIGIT
236+
*
237+
* Note: port can be the empty string, in which case now:
238+
* p == pa and port is parsed as 0, as desired. */
239+
while (p < pa && port <= MAX_PORT && (uri_lookup(*p) & URI_DIGIT) != 0)
240+
port = 10*port + *p++-'0';
241+
242+
/* If p did not reach pa there was some non-digit present
243+
* or the integer was too large, so fail. */
244+
if (p != pa || port > MAX_PORT) return -1;
235245

236-
parsed->port = atoi(s);
246+
parsed->port = port;
237247
}
238248

239-
s = pa;
249+
s = pa; /* Next, parse path-abempty */
240250
}
241251

242252
/* => s = path-abempty / path-absolute / path-rootless

test/uri-tests.c

+2
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ static int failparse(void)
384384
"http://fish/[foo]/bar",
385385
"http://foo:80bar",
386386
"http://foo:80:80/bar",
387+
"http://foo:8000000000000000000000000000000000000000000000000/bar",
388+
"http://foo:65536/bar",
387389
NULL
388390
};
389391
int n;

0 commit comments

Comments
 (0)