diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2026-05-22 08:59:26 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2026-05-22 09:00:48 +0000 |
| commit | 2eaa7beba1f35b31019a992de35be8a54b9ac028 (patch) | |
| tree | 1932030c4f58c5524cf47ba624b40fa6bf27cd2b | |
| parent | uri-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
| -rw-r--r-- | CHANGELOG.md | 7 | ||||
| -rw-r--r-- | uri.c | 31 |
2 files changed, 37 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 5792c240..a4c22bf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,13 @@ ### Deprecated ### Removed ### Fixed + +* Out-of-bounds read when parsing URIs with malformed %-encoded + content ([#2353][2353]). + +[2353]: https://codeberg.org/dnkl/foot/issues/2353 + + ### Security ### Contributors @@ -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); +} |
