• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 22553697083

01 Mar 2026 09:58PM UTC coverage: 75.673% (+2.0%) from 73.687%
22553697083

Pull #14925

github

web-flow
Merge 288827f07 into 90823fa90
Pull Request #14925: hs: false positive coverity warning in a reference string v1

241615 of 319288 relevant lines covered (75.67%)

1333554.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.83
/src/detect-http-header.c
1
/* Copyright (C) 2007-2022 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \ingroup httplayer
20
 *
21
 * @{
22
 */
23

24

25
/**
26
 * \file
27
 *
28
 * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
29
 *
30
 * Implements support for http_header keyword.
31
 */
32

33
#include "suricata-common.h"
34
#include "threads.h"
35
#include "decode.h"
36

37
#include "detect.h"
38
#include "detect-parse.h"
39
#include "detect-engine.h"
40
#include "detect-engine-buffer.h"
41
#include "detect-engine-mpm.h"
42
#include "detect-engine-state.h"
43
#include "detect-engine-prefilter.h"
44
#include "detect-engine-content-inspection.h"
45
#include "detect-content.h"
46
#include "detect-pcre.h"
47

48
#include "util-debug.h"
49
#include "util-print.h"
50
#include "util-memcmp.h"
51
#include "util-profiling.h"
52
#include "util-validate.h"
53

54
#include "app-layer.h"
55
#include "app-layer-parser.h"
56

57
#include "app-layer-htp.h"
58
#include "detect-http-header.h"
59
#include "detect-http-header-common.h"
60

61
static int DetectHttpHeaderSetup(DetectEngineCtx *, Signature *, const char *);
62
#ifdef UNITTESTS
63
static void DetectHttpHeaderRegisterTests(void);
64
#endif
65
static int g_http_header_buffer_id = 0;
66
static int g_keyword_thread_id = 0;
67

68
#define BUFFER_SIZE_STEP    1024
69
static HttpHeaderThreadDataConfig g_td_config = { BUFFER_SIZE_STEP };
70

71
static uint8_t *GetBufferForTX(
72
        htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, uint8_t flags, uint32_t *buffer_len)
73
{
618✔
74
    *buffer_len = 0;
618✔
75

76
    HttpHeaderThreadData *hdr_td = NULL;
618✔
77
    HttpHeaderBuffer *buf = HttpHeaderGetBufferSpace(det_ctx, flags, g_keyword_thread_id, &hdr_td);
618✔
78
    if (unlikely(buf == NULL)) {
618✔
79
        return NULL;
×
80
    }
×
81

82
    const htp_headers_t *headers;
618✔
83
    if (flags & STREAM_TOSERVER) {
618✔
84
        if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
459✔
85
                HTP_REQUEST_PROGRESS_HEADERS)
459✔
86
            return NULL;
60✔
87
        headers = htp_tx_request_headers(tx);
399✔
88
    } else {
399✔
89
        if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
159✔
90
                HTP_RESPONSE_PROGRESS_HEADERS)
159✔
91
            return NULL;
43✔
92
        headers = htp_tx_response_headers(tx);
116✔
93
    }
116✔
94
    if (headers == NULL)
515✔
95
        return NULL;
×
96

97
    size_t i = 0;
515✔
98
    size_t no_of_headers = htp_headers_size(headers);
515✔
99
    for (; i < no_of_headers; i++) {
2,941✔
100
        const htp_header_t *h = htp_headers_get_index(headers, i);
2,426✔
101
        size_t size1 = htp_header_name_len(h);
2,426✔
102
        size_t size2 = htp_header_value_len(h);
2,426✔
103

104
        if (flags & STREAM_TOSERVER) {
2,426✔
105
            if (size1 == 6 && SCMemcmpLowercase("cookie", htp_header_name_ptr(h), 6) == 0) {
1,589✔
106
                continue;
105✔
107
            }
105✔
108
        } else {
1,589✔
109
            if (size1 == 10 && SCMemcmpLowercase("set-cookie", htp_header_name_ptr(h), 10) == 0) {
837✔
110
                continue;
1✔
111
            }
1✔
112
        }
837✔
113

114
        size_t size = size1 + size2 + 4;
2,320✔
115
#if 0
116
        if (i + 1 == no_of_headers)
117
            size += 2;
118
#endif
119
        if (size + buf->len > buf->size) {
2,320✔
120
            if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
×
121
                return NULL;
×
122
            }
×
123
        }
×
124

125
        memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
2,320✔
126
        buf->len += htp_header_name_len(h);
2,320✔
127
        buf->buffer[buf->len++] = ':';
2,320✔
128
        buf->buffer[buf->len++] = ' ';
2,320✔
129
        memcpy(buf->buffer + buf->len, htp_header_value_ptr(h), htp_header_value_len(h));
2,320✔
130
        buf->len += htp_header_value_len(h);
2,320✔
131
        buf->buffer[buf->len++] = '\r';
2,320✔
132
        buf->buffer[buf->len++] = '\n';
2,320✔
133
#if 0 // looks like this breaks existing rules
134
        if (i + 1 == no_of_headers) {
135
            buf->buffer[buf->len++] = '\r';
136
            buf->buffer[buf->len++] = '\n';
137
        }
138
#endif
139
    }
2,320✔
140

141
    *buffer_len = buf->len;
515✔
142
    return buf->buffer;
515✔
143
}
515✔
144

145
static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx,
146
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flow_flags, void *txv,
147
        const int list_id)
148
{
601✔
149
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
601✔
150
    if (buffer->inspect == NULL) {
601✔
151
        uint32_t b_len = 0;
599✔
152
        const uint8_t *b = NULL;
599✔
153

154
        if (SCHttp2TxGetHeaders(txv, flow_flags, &b, &b_len) != 1)
599✔
155
            return NULL;
326✔
156
        if (b == NULL || b_len == 0)
273✔
157
            return NULL;
×
158

159
        InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
273✔
160
    }
273✔
161

162
    return buffer;
275✔
163
}
601✔
164

165
static InspectionBuffer *GetData1(DetectEngineThreadCtx *det_ctx,
166
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flags, void *txv,
167
        const int list_id)
168
{
226✔
169
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
226✔
170
    if (buffer->inspect == NULL) {
226✔
171
        uint32_t data_len = 0;
12✔
172
        uint8_t *data = GetBufferForTX(txv, det_ctx, flags, &data_len);
12✔
173
        InspectionBufferSetupAndApplyTransforms(
12✔
174
                det_ctx, list_id, buffer, data, data_len, transforms);
12✔
175
    }
12✔
176

177
    return buffer;
226✔
178
}
226✔
179

180
typedef struct PrefilterMpmHttpHeaderCtx {
181
    int list_id;
182
    const MpmCtx *mpm_ctx;
183
    const DetectEngineTransforms *transforms;
184
} PrefilterMpmHttpHeaderCtx;
185

186
/** \brief Generic Mpm prefilter callback
187
 *
188
 *  \param det_ctx detection engine thread ctx
189
 *  \param p packet to inspect
190
 *  \param f flow to inspect
191
 *  \param txv tx to inspect
192
 *  \param pectx inspection context
193
 */
194
static void PrefilterMpmHttpHeader(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
195
        Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
196
{
611✔
197
    SCEnter();
611✔
198

199
    const PrefilterMpmHttpHeaderCtx *ctx = pectx;
611✔
200
    const MpmCtx *mpm_ctx = ctx->mpm_ctx;
611✔
201
    SCLogDebug("running on list %d", ctx->list_id);
611✔
202

203
    const int list_id = ctx->list_id;
611✔
204
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
611✔
205
    if (buffer->inspect == NULL) {
611✔
206
        uint32_t rawdata_len = 0;
606✔
207
        uint8_t *rawdata = GetBufferForTX(txv, det_ctx, flags, &rawdata_len);
606✔
208
        if (rawdata_len == 0)
606✔
209
            return;
196✔
210

211
        /* setup buffer and apply transforms */
212
        InspectionBufferSetupAndApplyTransforms(
410✔
213
                det_ctx, list_id, buffer, rawdata, rawdata_len, ctx->transforms);
410✔
214
    }
410✔
215

216
    const uint32_t data_len = buffer->inspect_len;
415✔
217
    const uint8_t *data = buffer->inspect;
415✔
218

219
    SCLogDebug("mpm'ing buffer:");
415✔
220
    //PrintRawDataFp(stdout, data, data_len);
221

222
    if (data != NULL && data_len >= mpm_ctx->minlen) {
415✔
223
        (void)mpm_table[mpm_ctx->mpm_type].Search(
412✔
224
                mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, data, data_len);
412✔
225
        PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
412✔
226
    }
412✔
227
}
415✔
228

229
static void PrefilterMpmHttpTrailer(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p,
230
        Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
231
{
481✔
232
    SCEnter();
481✔
233

234
    htp_tx_t *tx = txv;
481✔
235
    const HtpTxUserData *htud = (const HtpTxUserData *)htp_tx_get_user_data(tx);
481✔
236
    /* if the request wasn't flagged as having a trailer, we skip */
237
    if (((flags & STREAM_TOSERVER) && !htud->request_has_trailers) ||
481✔
238
            ((flags & STREAM_TOCLIENT) && !htud->response_has_trailers)) {
481✔
239
        SCReturn;
470✔
240
    }
470✔
241
    PrefilterMpmHttpHeader(det_ctx, pectx, p, f, txv, idx, _txd, flags);
11✔
242
    SCReturn;
11✔
243
}
481✔
244

245
static void PrefilterMpmHttpHeaderFree(void *ptr)
246
{
3,817✔
247
    SCFree(ptr);
3,817✔
248
}
3,817✔
249

250
static int PrefilterMpmHttpHeaderRequestRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
251
        MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
252
{
1,029✔
253
    SCEnter();
1,029✔
254

255
    /* header */
256
    PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
1,029✔
257
    if (pectx == NULL)
1,029✔
258
        return -1;
×
259
    pectx->list_id = list_id;
1,029✔
260
    pectx->mpm_ctx = mpm_ctx;
1,029✔
261
    pectx->transforms = &mpm_reg->transforms;
1,029✔
262

263
    int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader, mpm_reg->app_v2.alproto,
1,029✔
264
            HTP_REQUEST_PROGRESS_HEADERS, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
1,029✔
265
    if (r != 0) {
1,029✔
266
        SCFree(pectx);
×
267
        return r;
×
268
    }
×
269

270
    /* trailer */
271
    pectx = SCCalloc(1, sizeof(*pectx));
1,029✔
272
    if (pectx == NULL)
1,029✔
273
        return -1;
×
274
    pectx->list_id = list_id;
1,029✔
275
    pectx->mpm_ctx = mpm_ctx;
1,029✔
276
    pectx->transforms = &mpm_reg->transforms;
1,029✔
277

278
    r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer, mpm_reg->app_v2.alproto,
1,029✔
279
            HTP_REQUEST_PROGRESS_TRAILER, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
1,029✔
280
    if (r != 0) {
1,029✔
281
        SCFree(pectx);
×
282
    }
×
283
    return r;
1,029✔
284
}
1,029✔
285

286
static int PrefilterMpmHttpHeaderResponseRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
287
        MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
288
{
662✔
289
    SCEnter();
662✔
290

291
    /* header */
292
    PrefilterMpmHttpHeaderCtx *pectx = SCCalloc(1, sizeof(*pectx));
662✔
293
    if (pectx == NULL)
662✔
294
        return -1;
×
295
    pectx->list_id = list_id;
662✔
296
    pectx->mpm_ctx = mpm_ctx;
662✔
297
    pectx->transforms = &mpm_reg->transforms;
662✔
298

299
    int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpHeader, mpm_reg->app_v2.alproto,
662✔
300
            HTP_RESPONSE_PROGRESS_HEADERS, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
662✔
301
    if (r != 0) {
662✔
302
        SCFree(pectx);
×
303
        return r;
×
304
    }
×
305

306
    /* trailer */
307
    pectx = SCCalloc(1, sizeof(*pectx));
662✔
308
    if (pectx == NULL)
662✔
309
        return -1;
×
310
    pectx->list_id = list_id;
662✔
311
    pectx->mpm_ctx = mpm_ctx;
662✔
312
    pectx->transforms = &mpm_reg->transforms;
662✔
313

314
    r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpmHttpTrailer, mpm_reg->app_v2.alproto,
662✔
315
            HTP_RESPONSE_PROGRESS_TRAILER, pectx, PrefilterMpmHttpHeaderFree, mpm_reg->pname);
662✔
316
    if (r != 0) {
662✔
317
        SCFree(pectx);
×
318
    }
×
319
    return r;
662✔
320
}
662✔
321

322
/**
323
 * \brief The setup function for the http_header keyword for a signature.
324
 *
325
 * \param de_ctx Pointer to the detection engine context.
326
 * \param s      Pointer to signature for the current Signature being parsed
327
 *               from the rules.
328
 * \param m      Pointer to the head of the SigMatchs for the current rule
329
 *               being parsed.
330
 * \param arg    Pointer to the string holding the keyword value.
331
 *
332
 * \retval  0 On success.
333
 * \retval -1 On failure.
334
 */
335
static int DetectHttpHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
336
{
986✔
337
    return DetectEngineContentModifierBufferSetup(
986✔
338
            de_ctx, s, arg, DETECT_HTTP_HEADER_CM, g_http_header_buffer_id, ALPROTO_HTTP1);
986✔
339
}
986✔
340

341
/**
342
 * \brief this function setup the http.header keyword used in the rule
343
 *
344
 * \param de_ctx   Pointer to the Detection Engine Context
345
 * \param s        Pointer to the Signature to which the current keyword belongs
346
 * \param str      Should hold an empty string always
347
 *
348
 * \retval 0       On success
349
 */
350
static int DetectHttpHeaderSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
351
{
4,065✔
352
    if (SCDetectBufferSetActiveList(de_ctx, s, g_http_header_buffer_id) < 0)
4,065✔
353
        return -1;
22✔
354
    if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
4,043✔
355
        return -1;
176✔
356
    return 0;
3,867✔
357
}
4,043✔
358

359
/**
360
 * \brief Registers the keyword handlers for the "http_header" keyword.
361
 */
362
void DetectHttpHeaderRegister(void)
363
{
33✔
364
    /* http_header content modifier */
365
    sigmatch_table[DETECT_HTTP_HEADER_CM].name = "http_header";
33✔
366
    sigmatch_table[DETECT_HTTP_HEADER_CM].desc =
33✔
367
            "content modifier to match only on the HTTP header-buffer";
33✔
368
    sigmatch_table[DETECT_HTTP_HEADER_CM].url =
33✔
369
            "/rules/http-keywords.html#http-header-and-http-raw-header";
33✔
370
    sigmatch_table[DETECT_HTTP_HEADER_CM].Setup = DetectHttpHeaderSetup;
33✔
371
#ifdef UNITTESTS
3✔
372
    sigmatch_table[DETECT_HTTP_HEADER_CM].RegisterTests = DetectHttpHeaderRegisterTests;
3✔
373
#endif
3✔
374
    sigmatch_table[DETECT_HTTP_HEADER_CM].flags |= SIGMATCH_NOOPT;
33✔
375
    sigmatch_table[DETECT_HTTP_HEADER_CM].flags |= SIGMATCH_INFO_CONTENT_MODIFIER;
33✔
376
    sigmatch_table[DETECT_HTTP_HEADER_CM].alternative = DETECT_HTTP_HEADER;
33✔
377

378
    /* http.header sticky buffer */
379
    sigmatch_table[DETECT_HTTP_HEADER].name = "http.header";
33✔
380
    sigmatch_table[DETECT_HTTP_HEADER].desc = "sticky buffer to match on the normalized HTTP header-buffer";
33✔
381
    sigmatch_table[DETECT_HTTP_HEADER].url = "/rules/http-keywords.html#http-header-and-http-raw-header";
33✔
382
    sigmatch_table[DETECT_HTTP_HEADER].Setup = DetectHttpHeaderSetupSticky;
33✔
383
    sigmatch_table[DETECT_HTTP_HEADER].flags |= SIGMATCH_NOOPT;
33✔
384
    sigmatch_table[DETECT_HTTP_HEADER].flags |= SIGMATCH_INFO_STICKY_BUFFER;
33✔
385

386
    DetectAppLayerInspectEngineRegister("http_header", ALPROTO_HTTP1, SIG_FLAG_TOSERVER,
33✔
387
            HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetData1);
33✔
388
    DetectAppLayerMpmRegister("http_header", SIG_FLAG_TOSERVER, 2,
33✔
389
            PrefilterMpmHttpHeaderRequestRegister, NULL, ALPROTO_HTTP1,
33✔
390
            0); /* not used, registered twice: HEADERS/TRAILER */
33✔
391

392
    DetectAppLayerInspectEngineRegister("http_header", ALPROTO_HTTP1, SIG_FLAG_TOCLIENT,
33✔
393
            HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetData1);
33✔
394
    DetectAppLayerMpmRegister("http_header", SIG_FLAG_TOCLIENT, 2,
33✔
395
            PrefilterMpmHttpHeaderResponseRegister, NULL, ALPROTO_HTTP1,
33✔
396
            0); /* not used, registered twice: HEADERS/TRAILER */
33✔
397

398
    DetectAppLayerInspectEngineRegister("http_header", ALPROTO_HTTP2, SIG_FLAG_TOSERVER,
33✔
399
            HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
33✔
400
    DetectAppLayerMpmRegister("http_header", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
33✔
401
            GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
33✔
402

403
    DetectAppLayerInspectEngineRegister("http_header", ALPROTO_HTTP2, SIG_FLAG_TOCLIENT,
33✔
404
            HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
33✔
405
    DetectAppLayerMpmRegister("http_header", SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister,
33✔
406
            GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
33✔
407

408
    DetectBufferTypeSetDescriptionByName("http_header",
33✔
409
            "http headers");
33✔
410

411
    g_http_header_buffer_id = DetectBufferTypeGetByName("http_header");
33✔
412

413
    g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header",
33✔
414
            HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree);
33✔
415
}
33✔
416

417
static int g_http_request_header_buffer_id = 0;
418
static int g_http_response_header_buffer_id = 0;
419
static int g_request_header_thread_id = 0;
420
static int g_response_header_thread_id = 0;
421

422
typedef struct HttpMultiBufItem {
423
    uint8_t *buffer;
424
    uint32_t len;
425
} HttpMultiBufItem;
426

427
typedef struct HttpMultiBufHeaderThreadData {
428
    // array of items, being defined as a buffer with its length just above
429
    HttpMultiBufItem *items;
430
    // capacity of items (size of allocation)
431
    size_t cap;
432
    // length of items (number in use)
433
    size_t len;
434
} HttpMultiBufHeaderThreadData;
435

436
static void *HttpMultiBufHeaderThreadDataInit(void *data)
437
{
73,044✔
438
    HttpMultiBufHeaderThreadData *td = SCCalloc(1, sizeof(*td));
73,044✔
439

440
    /* This return value check to satisfy our Cocci malloc checks. */
441
    if (td == NULL) {
73,044✔
442
        SCLogError("failed to allocate %" PRIuMAX " bytes: %s", (uintmax_t)sizeof(*td),
×
443
                strerror(errno));
×
444
        return NULL;
×
445
    }
×
446
    return td;
73,044✔
447
}
73,044✔
448

449
static void HttpMultiBufHeaderThreadDataFree(void *data)
450
{
73,042✔
451
    HttpMultiBufHeaderThreadData *td = data;
73,042✔
452
    for (size_t i = 0; i < td->cap; i++) {
73,315✔
453
        SCFree(td->items[i].buffer);
273✔
454
    }
273✔
455
    SCFree(td->items);
73,042✔
456
    SCFree(td);
73,042✔
457
}
73,042✔
458

459
static bool GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx, const void *txv, const uint8_t flags,
460
        uint32_t local_id, const uint8_t **buf, uint32_t *buf_len)
461
{
1,651✔
462
    SCEnter();
1,651✔
463

464
    int kw_thread_id;
1,651✔
465
    if (flags & STREAM_TOSERVER) {
1,651✔
466
        kw_thread_id = g_request_header_thread_id;
1,541✔
467
    } else {
1,541✔
468
        kw_thread_id = g_response_header_thread_id;
110✔
469
    }
110✔
470
    HttpMultiBufHeaderThreadData *hdr_td =
1,651✔
471
            DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id);
1,651✔
472
    if (unlikely(hdr_td == NULL)) {
1,651✔
473
        return false;
×
474
    }
×
475

476
    htp_tx_t *tx = (htp_tx_t *)txv;
1,651✔
477
    const htp_headers_t *headers;
1,651✔
478
    if (flags & STREAM_TOSERVER) {
1,651✔
479
        headers = htp_tx_request_headers(tx);
1,541✔
480
    } else {
1,541✔
481
        headers = htp_tx_response_headers(tx);
110✔
482
    }
110✔
483
    size_t no_of_headers = htp_headers_size(headers);
1,651✔
484
    if (local_id == 0) {
1,651✔
485
        // We initialize a big buffer on first item
486
        // Then, we will just use parts of it
487
        hdr_td->len = 0;
345✔
488
        if (hdr_td->cap < no_of_headers) {
345✔
489
            void *new_buffer = SCRealloc(hdr_td->items, no_of_headers * sizeof(HttpMultiBufItem));
82✔
490
            if (unlikely(new_buffer == NULL)) {
82✔
491
                return NULL;
×
492
            }
×
493
            hdr_td->items = new_buffer;
82✔
494
            // zeroes the new part of the items
495
            memset(hdr_td->items + hdr_td->cap, 0,
82✔
496
                    (no_of_headers - hdr_td->cap) * sizeof(HttpMultiBufItem));
82✔
497
            hdr_td->cap = no_of_headers;
82✔
498
        }
82✔
499
        for (size_t i = 0; i < no_of_headers; i++) {
1,655✔
500
            const htp_header_t *h = htp_headers_get_index(headers, i);
1,310✔
501
            uint32_t size1 = (uint32_t)htp_header_name_len(h);
1,310✔
502
            uint32_t size2 = (uint32_t)htp_header_value_len(h);
1,310✔
503
            uint32_t size = size1 + size2 + 2;
1,310✔
504
            if (hdr_td->items[i].len < size) {
1,310✔
505
                // Use realloc, as this pointer is not freed until HttpMultiBufHeaderThreadDataFree
506
                void *tmp = SCRealloc(hdr_td->items[i].buffer, size);
426✔
507
                if (unlikely(tmp == NULL)) {
426✔
508
                    return NULL;
×
509
                }
×
510
                hdr_td->items[i].buffer = tmp;
426✔
511
            }
426✔
512
            memcpy(hdr_td->items[i].buffer, htp_header_name_ptr(h), size1);
1,310✔
513
            hdr_td->items[i].buffer[size1] = ':';
1,310✔
514
            hdr_td->items[i].buffer[size1 + 1] = ' ';
1,310✔
515
            memcpy(hdr_td->items[i].buffer + size1 + 2, htp_header_value_ptr(h), size2);
1,310✔
516
            hdr_td->items[i].len = size;
1,310✔
517
        }
1,310✔
518
        hdr_td->len = no_of_headers;
345✔
519
    }
345✔
520

521
    // cbdata->local_id is the index of the requested header buffer
522
    // hdr_td->len is the number of header buffers
523
    if (local_id < hdr_td->len) {
1,651✔
524
        // we have one valid header buffer
525
        *buf = hdr_td->items[local_id].buffer;
1,309✔
526
        *buf_len = hdr_td->items[local_id].len;
1,309✔
527
        return true;
1,309✔
528
    } // else there are no more header buffer to get
1,309✔
529
    return false;
342✔
530
}
1,651✔
531

532
static int DetectHTTPRequestHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
533
{
3,256✔
534
    if (SCDetectBufferSetActiveList(de_ctx, s, g_http_request_header_buffer_id) < 0)
3,256✔
535
        return -1;
30✔
536

537
    if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP) != 0)
3,226✔
538
        return -1;
19✔
539

540
    return 0;
3,207✔
541
}
3,226✔
542

543
void DetectHttpRequestHeaderRegister(void)
544
{
33✔
545
    sigmatch_table[DETECT_HTTP_REQUEST_HEADER].name = "http.request_header";
33✔
546
    sigmatch_table[DETECT_HTTP_REQUEST_HEADER].desc =
33✔
547
            "sticky buffer to match on only one HTTP header name and value";
33✔
548
    sigmatch_table[DETECT_HTTP_REQUEST_HEADER].url = "/rules/http-keywords.html#request_header";
33✔
549
    sigmatch_table[DETECT_HTTP_REQUEST_HEADER].Setup = DetectHTTPRequestHeaderSetup;
33✔
550
    sigmatch_table[DETECT_HTTP_REQUEST_HEADER].flags |=
33✔
551
            SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER | SIGMATCH_INFO_MULTI_BUFFER;
33✔
552

553
    DetectAppLayerMultiRegister("http_request_header", ALPROTO_HTTP2, SIG_FLAG_TOSERVER,
33✔
554
            HTTP2StateOpen, SCHttp2TxGetHeader, 2);
33✔
555
    DetectAppLayerMultiRegister("http_request_header", ALPROTO_HTTP1, SIG_FLAG_TOSERVER,
33✔
556
            HTP_REQUEST_PROGRESS_HEADERS, GetHttp1HeaderData, 2);
33✔
557

558
    DetectBufferTypeSetDescriptionByName("http_request_header", "HTTP header name and value");
33✔
559
    g_http_request_header_buffer_id = DetectBufferTypeGetByName("http_request_header");
33✔
560
    DetectBufferTypeSupportsMultiInstance("http_request_header");
33✔
561
    g_request_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_request_header",
33✔
562
            HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
33✔
563
}
33✔
564

565
static int DetectHTTPResponseHeaderSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
566
{
197✔
567
    if (SCDetectBufferSetActiveList(de_ctx, s, g_http_response_header_buffer_id) < 0)
197✔
568
        return -1;
1✔
569

570
    if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP) != 0)
196✔
571
        return -1;
1✔
572

573
    return 0;
195✔
574
}
196✔
575

576
void DetectHttpResponseHeaderRegister(void)
577
{
33✔
578
    sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].name = "http.response_header";
33✔
579
    sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].desc =
33✔
580
            "sticky buffer to match on only one HTTP header name and value";
33✔
581
    sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].url = "/rules/http2-keywords.html#response_header";
33✔
582
    sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].Setup = DetectHTTPResponseHeaderSetup;
33✔
583
    sigmatch_table[DETECT_HTTP_RESPONSE_HEADER].flags |=
33✔
584
            SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER | SIGMATCH_INFO_MULTI_BUFFER;
33✔
585

586
    DetectAppLayerMultiRegister("http_response_header", ALPROTO_HTTP2, SIG_FLAG_TOCLIENT,
33✔
587
            HTTP2StateOpen, SCHttp2TxGetHeader, 2);
33✔
588
    DetectAppLayerMultiRegister("http_response_header", ALPROTO_HTTP1, SIG_FLAG_TOCLIENT,
33✔
589
            HTP_RESPONSE_PROGRESS_HEADERS, GetHttp1HeaderData, 2);
33✔
590

591
    DetectBufferTypeSetDescriptionByName("http_response_header", "HTTP header name and value");
33✔
592
    g_http_response_header_buffer_id = DetectBufferTypeGetByName("http_response_header");
33✔
593
    DetectBufferTypeSupportsMultiInstance("http_response_header");
33✔
594
    g_response_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_response_header",
33✔
595
            HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree);
596
}
33✔
597

598
/************************************Unittests*********************************/
599

600
#ifdef UNITTESTS
601
#include "tests/detect-http-header.c"
602
#endif
603

604
/**
605
 * @}
606
 */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc