Skip to content
Open
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
11 changes: 7 additions & 4 deletions apache2/msc_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
*
* \retval rval On Success
*/
char *utf8_unicode_inplace_ex(apr_pool_t *mp, unsigned char *input, long int input_len, int *changed) {

Check failure on line 106 in apache2/msc_util.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 51 to the 25 allowed.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaE3PwM2xyjeyssf-DDp&open=AaE3PwM2xyjeyssf-DDp&pullRequest=3685
int unicode_len = 0;
unsigned int d = 0;
unsigned char c, *utf;
Expand All @@ -120,7 +120,8 @@
data = rval = apr_palloc(mp, len);
if (rval == NULL) return NULL;

for (i = 0; i < bytes_left;) {
for (i = 0; i < input_len;) {
bytes_left = input_len - i;
unicode_len = 0; d = 0;
utf = (unsigned char *)&input[i];
c = *utf;
Expand All @@ -129,7 +130,7 @@
if ((c & 0x80) == 0) {
if (c == 0) {
unicode_len = 2;
d = utf[1];
d = (bytes_left >= 2) ? utf[1] : 0;
}
}
/* If first byte begins with binary 110 it is two byte encoding*/
Expand Down Expand Up @@ -178,6 +179,8 @@
}
/* invalid UTF-8 character number range (RFC 3629) */
if ((d >= 0xD800) && (d <= 0xDFFF)) unicode_len = UNICODE_ERROR_RESTRICTED_CHARACTER;
/* restrict characters to UTF-8 range (U+0000 - U+10FFFF) */
if ((unicode_len == 4) && (d > 0x10FFFF)) unicode_len = UNICODE_ERROR_RESTRICTED_CHARACTER;
/* check for overlong */
if ((unicode_len == 4) && (d < 0x010000)) unicode_len = UNICODE_ERROR_OVERLONG_CHARACTER;
/* three byte could be represented with less bytes */
Expand All @@ -187,8 +190,8 @@

if (unicode_len > 0) {
i += unicode_len;
sprintf(data, "%%u%04x", d);
data += 6;
/* code points above U+FFFF need five hex digits */
data += sprintf(data, "%%u%04x", d);

Check failure on line 194 in apache2/msc_util.c

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure use of "sprintf" function is safe here or replace it with a call to "snprintf".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaE3PwM2xyjeyssf-DDq&open=AaE3PwM2xyjeyssf-DDq&pullRequest=3685
*changed = 1;
}
else {
Expand Down
32 changes: 32 additions & 0 deletions tests/tfn/utf8toUnicode.t
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,35 @@
output => " %u27f0 %u27f1 %u27f2 %u27f3 %u27f4 %u27f5 %u27f6 %u27f7 %u27f8 %u27f9 %u27fa %u27fb %u27fc %u27fd %u27fe %u27ff",
ret => 1,
},
# Supplementary planes (four-byte sequences, five hex digits)
{
type => "tfn",
name => "utf8toUnicode",
input => "😀 𝒜 𠀀 􏿿",
output => "%u1f600 %u1d49c %u20000 %u10ffff",
ret => 1,
},
# Four-byte sequence followed by ASCII must not lose its last digit
{
type => "tfn",
name => "utf8toUnicode",
input => "😀A",
output => "%u1f600A",
ret => 1,
},
# Code point above U+10FFFF is not valid UTF-8 and is passed through
{
type => "tfn",
name => "utf8toUnicode",
input => "\xf4\x90\x80\x80",
output => "\xf4\x90\x80\x80",
ret => 0,
},
# Truncated sequence at the end of the input is passed through
{
type => "tfn",
name => "utf8toUnicode",
input => "a\xe2\x82",
output => "a\xe2\x82",
ret => 0,
},
Loading