aboutsummaryrefslogtreecommitdiff
path: root/uri.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2026-05-22 08:59:26 +0000
committerDaniel Eklöf <daniel@ekloef.se>2026-05-22 09:00:48 +0000
commit2eaa7beba1f35b31019a992de35be8a54b9ac028 (patch)
tree1932030c4f58c5524cf47ba624b40fa6bf27cd2b /uri.c
parenturi-parse: add a bunch of unit tests (diff)
uri-parse: fix out-of-bounds read with malformed %-encoded content
If the input URI ends with a trailing '%' (or a trailing '%N'), we read outside the provided buffer. On NULL terminated input, this happened to work out since we'd correctly detect an invalid %-sequence as soon as we read the NULL terminator. On input that is not NULL terminated, we're out of luck. This patch fixes this by also checking we have enough input left to even _try_ to read the %-digits. Also add unit tests for this particular case. Closes #2353
Diffstat (limited to 'uri.c')
-rw-r--r--uri.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/uri.c b/uri.c
index 3f3beb53..ddbfacdd 100644
--- a/uri.c
+++ b/uri.c
@@ -195,7 +195,9 @@ uri_parse(const char *uri, size_t len,
encoded_len -= prefix_len;
decoded_len += prefix_len;
- if (hex2nibble(next[1]) <= 15 && hex2nibble(next[2]) <= 15) {
+ if (encoded_len >= 3 &&
+ hex2nibble(next[1]) <= 15 && hex2nibble(next[2]) <= 15)
+ {
*p++ = hex2nibble(next[1]) << 4 | hex2nibble(next[2]);
decoded_len++;
encoded_len -= 3;
@@ -416,3 +418,30 @@ UNITTEST
xassert(query == NULL);
xassert(fragment == NULL);
}
+
+UNITTEST
+{
+ /* Malformed URI, trailing '%' */
+ const char uri[] = "file:///%ABNOT-PART-OF-INPUT";
+ char *path;
+ uri_parse(uri, 9, NULL, NULL, NULL, NULL, NULL, &path, NULL, NULL);
+ xassert(streq(path, "/%")); free(path);
+}
+
+UNITTEST
+{
+ /* Malformed URI, trailing '%2' */
+ const char uri[] = "file:///%2ANOT-PART-OF-INPUT";
+ char *path;
+ uri_parse(uri, 10, NULL, NULL, NULL, NULL, NULL, &path, NULL, NULL);
+ xassert(streq(path, "/%2")); free(path);
+}
+
+UNITTEST
+{
+ /* Malformed URI, trailing '%ag' */
+ const char uri[] = "file:///%ag";
+ char *path;
+ uri_parse(uri, sizeof(uri) - 1, NULL, NULL, NULL, NULL, NULL, &path, NULL, NULL);
+ xassert(streq(path, "/%ag")); free(path);
+}