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

OISF / suricata / 22553492142

01 Mar 2026 09:48PM UTC coverage: 70.74% (-2.9%) from 73.687%
22553492142

Pull #14920

github

web-flow
Merge e15a765bc into 90823fa90
Pull Request #14920: draft: rust based configuration file parser and loader - v4

38209 of 77306 branches covered (49.43%)

Branch coverage included in aggregate %.

533 of 779 new or added lines in 5 files covered. (68.42%)

11924 existing lines in 491 files now uncovered.

252429 of 333548 relevant lines covered (75.68%)

2403268.06 hits per line

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

79.86
/src/detect-http-method.c
1
/* Copyright (C) 2007-2019 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 Brian Rectanus <brectanu@gmail.com>
29
 *
30
 * Implements the http_method keyword
31
 */
32

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

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-prefilter.h"
43
#include "detect-content.h"
44
#include "detect-pcre.h"
45

46
#include "flow.h"
47
#include "flow-var.h"
48
#include "flow-util.h"
49

50
#include "util-debug.h"
51
#include "util-unittest.h"
52
#include "util-unittest-helper.h"
53
#include "util-spm.h"
54

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

58
#include "app-layer-htp.h"
59
#include "detect-http-method.h"
60
#include "stream-tcp.h"
61

62
static int g_http_method_buffer_id = 0;
63
static int DetectHttpMethodSetup(DetectEngineCtx *, Signature *, const char *);
64
static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str);
65
#ifdef UNITTESTS
66
void DetectHttpMethodRegisterTests(void);
67
#endif
68
void DetectHttpMethodFree(void *);
69
static bool DetectHttpMethodValidateCallback(
70
        const Signature *s, const char **sigerror, const DetectBufferType *dbt);
71
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
72
        const DetectEngineTransforms *transforms, Flow *_f,
73
        const uint8_t _flow_flags, void *txv, const int list_id);
74
static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
75
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
76
        const int list_id);
77

78
/**
79
 * \brief Registration function for keyword: http_method
80
 */
81
void DetectHttpMethodRegister(void)
82
{
2,159✔
83
    /* http_method content modifier */
84
    sigmatch_table[DETECT_HTTP_METHOD_CM].name = "http_method";
2,159✔
85
    sigmatch_table[DETECT_HTTP_METHOD_CM].desc =
2,159✔
86
            "content modifier to match only on the HTTP method-buffer";
2,159✔
87
    sigmatch_table[DETECT_HTTP_METHOD_CM].url = "/rules/http-keywords.html#http-method";
2,159✔
88
    sigmatch_table[DETECT_HTTP_METHOD_CM].Match = NULL;
2,159✔
89
    sigmatch_table[DETECT_HTTP_METHOD_CM].Setup = DetectHttpMethodSetup;
2,159✔
90
#ifdef UNITTESTS
3✔
91
    sigmatch_table[DETECT_HTTP_METHOD_CM].RegisterTests = DetectHttpMethodRegisterTests;
3✔
92
#endif
3✔
93
    sigmatch_table[DETECT_HTTP_METHOD_CM].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_CONTENT_MODIFIER;
2,159✔
94
    sigmatch_table[DETECT_HTTP_METHOD_CM].alternative = DETECT_HTTP_METHOD;
2,159✔
95

96
    /* http.method sticky buffer */
97
    sigmatch_table[DETECT_HTTP_METHOD].name = "http.method";
2,159✔
98
    sigmatch_table[DETECT_HTTP_METHOD].desc = "sticky buffer to match specifically and only on the HTTP method buffer";
2,159✔
99
    sigmatch_table[DETECT_HTTP_METHOD].url = "/rules/http-keywords.html#http-method";
2,159✔
100
    sigmatch_table[DETECT_HTTP_METHOD].Setup = DetectHttpMethodSetupSticky;
2,159✔
101
    sigmatch_table[DETECT_HTTP_METHOD].flags |= SIGMATCH_NOOPT|SIGMATCH_INFO_STICKY_BUFFER;
2,159✔
102

103
    DetectAppLayerInspectEngineRegister("http_method", ALPROTO_HTTP1, SIG_FLAG_TOSERVER,
2,159✔
104
            HTP_REQUEST_PROGRESS_LINE, DetectEngineInspectBufferGeneric, GetData);
2,159✔
105

106
    DetectAppLayerMpmRegister("http_method", SIG_FLAG_TOSERVER, 4, PrefilterGenericMpmRegister,
2,159✔
107
            GetData, ALPROTO_HTTP1, HTP_REQUEST_PROGRESS_LINE);
2,159✔
108

109
    DetectAppLayerInspectEngineRegister("http_method", ALPROTO_HTTP2, SIG_FLAG_TOSERVER,
2,159✔
110
            HTTP2StateDataClient, DetectEngineInspectBufferGeneric, GetData2);
2,159✔
111

112
    DetectAppLayerMpmRegister("http_method", SIG_FLAG_TOSERVER, 4, PrefilterGenericMpmRegister,
2,159✔
113
            GetData2, ALPROTO_HTTP2, HTTP2StateDataClient);
2,159✔
114

115
    DetectBufferTypeSetDescriptionByName("http_method",
2,159✔
116
            "http request method");
2,159✔
117

118
    DetectBufferTypeRegisterValidateCallback("http_method",
2,159✔
119
            DetectHttpMethodValidateCallback);
2,159✔
120

121
    g_http_method_buffer_id = DetectBufferTypeGetByName("http_method");
2,159✔
122

123
    SCLogDebug("registering http_method rule option");
2,159!
124
}
2,159✔
125

126
/**
127
 * \brief This function is used to add the parsed "http_method" option
128
 *        into the current signature.
129
 *
130
 * \param de_ctx Pointer to the Detection Engine Context.
131
 * \param s      Pointer to the Current Signature.
132
 * \param str    Pointer to the user provided option string.
133
 *
134
 * \retval  0 on Success.
135
 * \retval -1 on Failure.
136
 */
137
static int DetectHttpMethodSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
138
{
128✔
139
    return DetectEngineContentModifierBufferSetup(
128✔
140
            de_ctx, s, str, DETECT_HTTP_METHOD_CM, g_http_method_buffer_id, ALPROTO_HTTP1);
128✔
141
}
128✔
142

143
/**
144
 * \brief this function setup the http.method keyword used in the rule
145
 *
146
 * \param de_ctx   Pointer to the Detection Engine Context
147
 * \param s        Pointer to the Signature to which the current keyword belongs
148
 * \param str      Should hold an empty string always
149
 *
150
 * \retval 0       On success
151
 */
152
static int DetectHttpMethodSetupSticky(DetectEngineCtx *de_ctx, Signature *s, const char *str)
153
{
7,301✔
154
    if (SCDetectBufferSetActiveList(de_ctx, s, g_http_method_buffer_id) < 0)
7,301!
UNCOV
155
        return -1;
×
156

157
    if (SCDetectSignatureSetAppProto(s, ALPROTO_HTTP) < 0)
7,301!
UNCOV
158
        return -1;
×
159

160
    return 0;
7,301✔
161
}
7,301✔
162

163
/**
164
 *  \retval 1 valid
165
 *  \retval 0 invalid
166
 */
167
static bool DetectHttpMethodValidateCallback(
168
        const Signature *s, const char **sigerror, const DetectBufferType *dbt)
169
{
7,357✔
170
    for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
29,612✔
171
        if (s->init_data->buffers[x].id != (uint32_t)dbt->id)
22,255✔
172
            continue;
14,898✔
173
        const SigMatch *sm = s->init_data->buffers[x].head;
7,357✔
174
        for (; sm != NULL; sm = sm->next) {
14,755✔
175
            if (sm->type != DETECT_CONTENT)
7,398✔
176
                continue;
44✔
177
            const DetectContentData *cd = (const DetectContentData *)sm->ctx;
7,354✔
178
            if (cd->content && cd->content_len) {
7,354!
179
                if (cd->content[cd->content_len - 1] == 0x20) {
7,354!
UNCOV
180
                    *sigerror = "http_method pattern with trailing space";
×
UNCOV
181
                    SCLogError("%s", *sigerror);
×
UNCOV
182
                    return false;
×
183
                } else if (cd->content[0] == 0x20) {
7,354!
UNCOV
184
                    *sigerror = "http_method pattern with leading space";
×
UNCOV
185
                    SCLogError("%s", *sigerror);
×
UNCOV
186
                    return false;
×
187
                } else if (cd->content[cd->content_len - 1] == 0x09) {
7,354!
UNCOV
188
                    *sigerror = "http_method pattern with trailing tab";
×
UNCOV
189
                    SCLogError("%s", *sigerror);
×
UNCOV
190
                    return false;
×
191
                } else if (cd->content[0] == 0x09) {
7,354!
UNCOV
192
                    *sigerror = "http_method pattern with leading tab";
×
UNCOV
193
                    SCLogError("%s", *sigerror);
×
UNCOV
194
                    return false;
×
UNCOV
195
                }
×
196
            }
7,354✔
197
        }
7,354✔
198
    }
7,357✔
199
    return true;
7,357✔
200
}
7,357✔
201

202
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
203
        const DetectEngineTransforms *transforms, Flow *_f,
204
        const uint8_t _flow_flags, void *txv, const int list_id)
205
{
671✔
206
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
671✔
207
    if (buffer->inspect == NULL) {
671✔
208
        htp_tx_t *tx = (htp_tx_t *)txv;
367✔
209

210
        if (htp_tx_request_method(tx) == NULL)
367!
211
            return NULL;
1✔
212

213
        const uint32_t data_len = (uint32_t)bstr_len(htp_tx_request_method(tx));
366✔
214
        const uint8_t *data = bstr_ptr(htp_tx_request_method(tx));
366✔
215

216
        InspectionBufferSetupAndApplyTransforms(
366✔
217
                det_ctx, list_id, buffer, data, data_len, transforms);
366✔
218
    }
366✔
219

220
    return buffer;
670✔
221
}
671✔
222

223
static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx,
224
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
225
        const int list_id)
226
{
8✔
227
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
8✔
228
    if (buffer->inspect == NULL) {
8✔
229
        uint32_t b_len = 0;
6✔
230
        const uint8_t *b = NULL;
6✔
231

232
        if (SCHttp2TxGetMethod(txv, &b, &b_len) != 1)
6✔
233
            return NULL;
4✔
234
        if (b == NULL || b_len == 0)
2!
235
            return NULL;
×
236

237
        InspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
2✔
238
    }
2✔
239

240
    return buffer;
4✔
241
}
8✔
242

243
#ifdef UNITTESTS
244
#include "tests/detect-http-method.c"
245
#endif
246

247
/**
248
 * @}
249
 */
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