]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output/http: log content-type like other headers
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 30 Oct 2025 10:43:27 +0000 (11:43 +0100)
committerVictor Julien <vjulien@oisf.net>
Wed, 5 Nov 2025 06:24:52 +0000 (07:24 +0100)
Ticket: 8056

Avoid stack allocation.
Do not handle null and ; especially

(cherry picked from commit b8411fcc8dfc16910c3080d4d8c03a9a64c3a1f7)

src/output-json-http.c
src/util-byte.c
src/util-byte.h

index 85d813f275b5cf3a8318ab0672ce73b0bddc1681..b6d6fcc997600718ba5b5cce6568ab1d71f0c67d 100644 (file)
@@ -238,14 +238,12 @@ static void EveHttpLogJSONBasic(SCJsonBuilder *js, htp_tx_t *tx)
     if (htp_tx_response_headers(tx) != NULL) {
         const htp_header_t *h_content_type = htp_tx_response_header(tx, "content-type");
         if (h_content_type != NULL) {
-            const size_t size = htp_header_value_len(h_content_type) * 2 + 1;
-            char string[size];
-            BytesToStringBuffer(htp_header_value_ptr(h_content_type),
-                    htp_header_value_len(h_content_type), string, size);
-            char *p = strchr(string, ';');
+            uint32_t len = (uint32_t)htp_header_value_len(h_content_type);
+            const uint8_t *p = memchr(htp_header_value_ptr(h_content_type), ';', len);
             if (p != NULL)
-                *p = '\0';
-            SCJbSetString(js, "http_content_type", string);
+                len = (uint32_t)(p - htp_header_value_ptr(h_content_type));
+            SCJbSetStringFromBytes(
+                    js, "http_content_type", htp_header_value_ptr(h_content_type), len);
         }
         const htp_header_t *h_content_range = htp_tx_response_header(tx, "content-range");
         if (h_content_range != NULL) {
index a0cb18946039f67d0b294fd14175edf6aa8c6354..a08b54fd5e561f67d61f257af147b8974fcb96de 100644 (file)
@@ -72,53 +72,6 @@ char *BytesToString(const uint8_t *bytes, size_t nbytes)
     return string;
 }
 
-/** \brief Turn byte array into string.
- *
- *  All non-printables are copied over, except for '\0', which is
- *  turned into literal \0 in the string.
- *
- *  \param bytes byte array
- *  \param nbytes number of bytes
- *  \param outstr[out] buffer to fill
- *  \param outlen size of outstr. Must be at least 2 * nbytes + 1 in size
- */
-void BytesToStringBuffer(const uint8_t *bytes, size_t nbytes, char *outstr, size_t outlen)
-{
-    DEBUG_VALIDATE_BUG_ON(outlen < (nbytes * 2 + 1));
-
-    size_t n = nbytes + 1;
-    size_t nulls = 0;
-
-    size_t u;
-    for (u = 0; u < nbytes; u++) {
-        if (bytes[u] == '\0')
-            nulls++;
-    }
-    n += nulls;
-
-    char string[n];
-
-    if (nulls == 0) {
-        /* no nulls */
-        memcpy(string, bytes, nbytes);
-        string[nbytes] = '\0';
-    } else {
-        /* nulls present */
-        char *dst = string;
-        for (u = 0; u < nbytes; u++) {
-            if (bytes[u] == '\0') {
-                *dst++ = '\\';
-                *dst++ = '0';
-            } else {
-                *dst++ = bytes[u];
-            }
-        }
-        *dst = '\0';
-    }
-
-    strlcpy(outstr, string, outlen);
-}
-
 int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes)
 {
     uint64_t i64;
index eca7b47f9edf7ae3b76e4773f4bc49bbeec10150..43452094f748e890980fee8bc9f691f44fd60ebe 100644 (file)
@@ -81,7 +81,6 @@
  *  \return string nul-terminated string or NULL on error
  */
 char *BytesToString(const uint8_t *bytes, size_t nbytes);
-void BytesToStringBuffer(const uint8_t *bytes, size_t nbytes, char *outstr, size_t outlen);
 
 /**
  * Extract bytes from a byte string and convert to a unint64_t.