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

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

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

92.97
/src/detect-http-header-names.c
1
/* Copyright (C) 2007-2017 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 Victor Julien <victor@inliniac.net>
29
 *
30
 * Implements support http_header_names
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
#include "detect-http-header-common.h"
48
#include "detect-http-header-names.h"
49

50
#include "flow.h"
51
#include "flow-var.h"
52
#include "flow-util.h"
53

54
#include "util-debug.h"
55
#include "util-unittest.h"
56
#include "util-unittest-helper.h"
57
#include "util-spm.h"
58
#include "util-print.h"
59

60
#include "app-layer.h"
61
#include "app-layer-parser.h"
62

63
#include "app-layer-htp.h"
64
#include "detect-http-header.h"
65
#include "stream-tcp.h"
66

67
#define KEYWORD_NAME "http.header_names"
6✔
68
#define KEYWORD_NAME_LEGACY "http_header_names"
3✔
69
#define KEYWORD_DOC "http-keywords.html#http-header-names"
3✔
70
#define BUFFER_NAME "http_header_names"
33✔
71
#define BUFFER_DESC "http header names"
3✔
72
static int g_buffer_id = 0;
73
static int g_keyword_thread_id = 0;
74
static int g_http2_thread_id = 0;
75

76
#define BUFFER_SIZE_STEP    256
77
static HttpHeaderThreadDataConfig g_td_config = { BUFFER_SIZE_STEP };
78

79
static uint8_t *GetBufferForTX(
80
        htp_tx_t *tx, DetectEngineThreadCtx *det_ctx, uint8_t flags, uint32_t *buffer_len)
81
{
115✔
82
    *buffer_len = 0;
115✔
83

84
    HttpHeaderThreadData *hdr_td = NULL;
115✔
85
    HttpHeaderBuffer *buf = HttpHeaderGetBufferSpace(det_ctx, flags, g_keyword_thread_id, &hdr_td);
115✔
86
    if (unlikely(buf == NULL)) {
115✔
87
        return NULL;
×
88
    }
×
89

90
    const htp_headers_t *headers;
115✔
91
    if (flags & STREAM_TOSERVER) {
115✔
92
        if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
85✔
93
                HTP_REQUEST_PROGRESS_HEADERS)
85✔
94
            return NULL;
17✔
95
        headers = htp_tx_request_headers(tx);
68✔
96
    } else {
68✔
97
        if (AppLayerParserGetStateProgress(IPPROTO_TCP, ALPROTO_HTTP1, tx, flags) <=
30✔
98
                HTP_RESPONSE_PROGRESS_HEADERS)
30✔
99
            return NULL;
14✔
100
        headers = htp_tx_response_headers(tx);
16✔
101
    }
16✔
102
    if (headers == NULL)
84✔
103
        return NULL;
×
104

105
    /* fill the buffer. \r\nName1\r\nName2\r\n\r\n */
106
    size_t i = 0;
84✔
107
    size_t no_of_headers = htp_headers_size(headers);
84✔
108
    for (; i < no_of_headers; i++) {
419✔
109
        const htp_header_t *h = htp_headers_get_index(headers, i);
335✔
110
        size_t size = htp_header_name_len(h) + 2; // for \r\n
335✔
111
        if (i == 0)
335✔
112
            size += 2;
77✔
113
        if (i + 1 == no_of_headers)
335✔
114
            size += 2;
77✔
115

116
        SCLogDebug("size %"PRIuMAX" + buf->len %u vs buf->size %u",
335✔
117
                (uintmax_t)size, buf->len, buf->size);
335✔
118
        if (size + buf->len > buf->size) {
335✔
UNCOV
119
            if (HttpHeaderExpandBuffer(hdr_td, buf, size) != 0) {
×
120
                return NULL;
×
121
            }
×
UNCOV
122
        }
×
123

124
        /* start with a \r\n */
125
        if (i == 0) {
335✔
126
            buf->buffer[buf->len++] = '\r';
77✔
127
            buf->buffer[buf->len++] = '\n';
77✔
128
        }
77✔
129

130
        memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
335✔
131
        buf->len += htp_header_name_len(h);
335✔
132
        buf->buffer[buf->len++] = '\r';
335✔
133
        buf->buffer[buf->len++] = '\n';
335✔
134

135
        /* end with an extra \r\n */
136
        if (i + 1 == no_of_headers) {
335✔
137
            buf->buffer[buf->len++] = '\r';
77✔
138
            buf->buffer[buf->len++] = '\n';
77✔
139
        }
77✔
140
    }
335✔
141

142
    *buffer_len = buf->len;
84✔
143
    return buf->buffer;
84✔
144
}
84✔
145

146
static InspectionBuffer *GetBuffer1ForTX(DetectEngineThreadCtx *det_ctx,
147
        const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
148
        const int list_id)
149
{
115✔
150
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
115✔
151
    if (buffer->inspect == NULL) {
115✔
152
        uint32_t rawdata_len = 0;
115✔
153
        uint8_t *rawdata = GetBufferForTX(txv, det_ctx, flow_flags, &rawdata_len);
115✔
154
        if (rawdata_len == 0)
115✔
155
            return NULL;
38✔
156

157
        InspectionBufferSetupAndApplyTransforms(
77✔
158
                det_ctx, list_id, buffer, rawdata, rawdata_len, transforms);
77✔
159
    }
77✔
160

161
    return buffer;
77✔
162
}
115✔
163

164
static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx,
165
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t flow_flags, void *txv,
166
        const int list_id)
167
{
635✔
168
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
635✔
169
    if (buffer->inspect == NULL) {
635✔
170
        uint32_t b_len = 0;
549✔
171
        const uint8_t *b = NULL;
549✔
172

173
        void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id);
549✔
174
        if (thread_buf == NULL)
549✔
175
            return NULL;
×
176

177
        if (SCHttp2TxGetHeaderNames(txv, flow_flags, &b, &b_len, thread_buf) != 1)
549✔
178
            return NULL;
295✔
179
        if (b == NULL || b_len == 0)
254✔
180
            return NULL;
×
181

182
        InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
254✔
183
    }
254✔
184

185
    return buffer;
340✔
186
}
635✔
187

188
/**
189
 * \brief The setup function for the http.header_names keyword for a signature.
190
 *
191
 * \param de_ctx Pointer to the detection engine context.
192
 * \param s      Pointer to signature for the current Signature being parsed
193
 *               from the rules.
194
 * \param m      Pointer to the head of the SigMatchs for the current rule
195
 *               being parsed.
196
 * \param arg    Pointer to the string holding the keyword value.
197
 *
198
 * \retval  0 On success.
199
 * \retval -1 On failure.
200
 */
201
static int DetectHttpHeaderNamesSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
202
{
3,804✔
203
    if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
3,804✔
204
        return -1;
2✔
205

206
    if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
3,802✔
207
        return -1;
1✔
208

209
    return 0;
3,801✔
210
}
3,802✔
211

212
/**
213
 * \brief Registers the keyword handlers for the "http.header_names" keyword.
214
 */
215
void DetectHttpHeaderNamesRegister(void)
216
{
3✔
217
    sigmatch_table[DETECT_HTTP_HEADER_NAMES].name = KEYWORD_NAME;
3✔
218
    sigmatch_table[DETECT_HTTP_HEADER_NAMES].alias = KEYWORD_NAME_LEGACY;
3✔
219
    sigmatch_table[DETECT_HTTP_HEADER_NAMES].desc = BUFFER_NAME " sticky buffer";
3✔
220
    sigmatch_table[DETECT_HTTP_HEADER_NAMES].url = "/rules/" KEYWORD_DOC;
3✔
221
    sigmatch_table[DETECT_HTTP_HEADER_NAMES].Setup = DetectHttpHeaderNamesSetup;
3✔
222

223
    sigmatch_table[DETECT_HTTP_HEADER_NAMES].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
3✔
224

225
    /* http1 */
226
    DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
3✔
227
            GetBuffer1ForTX, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_HEADERS);
3✔
228
    DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister,
3✔
229
            GetBuffer1ForTX, ALPROTO_HTTP1, HTP_RESPONSE_PROGRESS_HEADERS);
3✔
230

231
    DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_HTTP1, SIG_FLAG_TOSERVER,
3✔
232
            HTP_REQUEST_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
3✔
233
    DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_HTTP1, SIG_FLAG_TOCLIENT,
3✔
234
            HTP_RESPONSE_PROGRESS_HEADERS, DetectEngineInspectBufferGeneric, GetBuffer1ForTX);
3✔
235

236
    /* http2 */
237
    DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
3✔
238
            GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataClient);
3✔
239
    DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister,
3✔
240
            GetBuffer2ForTX, ALPROTO_HTTP2, HTTP2StateDataServer);
3✔
241

242
    DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_HTTP2, SIG_FLAG_TOSERVER,
3✔
243
            HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
3✔
244
    DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_HTTP2, SIG_FLAG_TOCLIENT,
3✔
245
            HTTP2StateDataServer, DetectEngineInspectBufferGeneric, GetBuffer2ForTX);
3✔
246

247
    DetectBufferTypeSetDescriptionByName(BUFFER_NAME,
3✔
248
            BUFFER_DESC);
3✔
249

250
    g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
3✔
251

252
    g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME,
3✔
253
            HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree);
3✔
254

255
    g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs(
3✔
256
            "http2.header_names", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree);
3✔
257

258
    SCLogDebug("keyword %s registered. Thread id %d. "
3✔
259
            "Buffer %s registered. Buffer id %d",
3✔
260
            KEYWORD_NAME, g_keyword_thread_id,
3✔
261
            BUFFER_NAME, g_buffer_id);
3✔
262
}
3✔
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