Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/src/app-layer-http2.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2020 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
 * \file
20
 *
21
 * \author Philippe Antoine <p.antoine@catenacyber.fr>
22
 *
23
 * Parser for HTTP2, RFC 7540
24
 */
25
26
#include "suricata-common.h"
27
#include "stream.h"
28
#include "conf.h"
29
30
#include "util-unittest.h"
31
32
#include "app-layer-detect-proto.h"
33
#include "app-layer-parser.h"
34
35
#include "app-layer-htp.h"
36
#include "app-layer-http2.h"
37
#include "rust.h"
38
39
static int HTTP2RegisterPatternsForProtocolDetection(void)
40
74
{
41
    /* Using the 24 bytes pattern makes AppLayerTest09 fail/leak
42
     * The complete pattern is "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
43
     */
44
74
    if (AppLayerProtoDetectPMRegisterPatternCI(IPPROTO_TCP, ALPROTO_HTTP2,
45
74
                                               "PRI * HTTP/2.0\r\n",
46
74
                                               16, 0, STREAM_TOSERVER) < 0)
47
0
    {
48
0
        return -1;
49
0
    }
50
74
    return 0;
51
74
}
52
53
static StreamingBufferConfig sbcfg = STREAMING_BUFFER_CONFIG_INITIALIZER;
54
static SuricataFileContext sfc = { &sbcfg };
55
56
void RegisterHTTP2Parsers(void)
57
74
{
58
74
    const char *proto_name = "http2";
59
60
74
    if (AppLayerProtoDetectConfProtoDetectionEnabledDefault("tcp", proto_name, true)) {
61
74
        AppLayerProtoDetectRegisterProtocol(ALPROTO_HTTP2, proto_name);
62
74
        if (HTTP2RegisterPatternsForProtocolDetection() < 0)
63
0
            return;
64
65
74
        rs_http2_init(&sfc);
66
74
        rs_http2_register_parser();
67
74
    }
68
69
#ifdef UNITTESTS
70
    //TODOask HTTP2ParserRegisterTests();
71
#endif
72
74
}
73
74
void HTTP2MimicHttp1Request(void *alstate_orig, void *h2s)
75
3
{
76
3
    htp_tx_t *h1tx = HtpGetTxForH2(alstate_orig);
77
3
    if (h2s == NULL || h1tx == NULL) {
78
0
        return;
79
0
    }
80
3
    if (h1tx->request_method == NULL) {
81
        // may happen if we only got the reply, not the HTTP1 request
82
0
        return;
83
0
    }
84
    // else
85
3
    rs_http2_tx_set_method(h2s, bstr_ptr(h1tx->request_method), bstr_len(h1tx->request_method));
86
3
    if (h1tx->request_uri != NULL) {
87
        // A request line without spaces gets interpreted as a request_method
88
        // and has request_uri=NULL
89
3
        rs_http2_tx_set_uri(h2s, bstr_ptr(h1tx->request_uri), bstr_len(h1tx->request_uri));
90
3
    }
91
3
    size_t nbheaders = htp_table_size(h1tx->request_headers);
92
18
    for (size_t i = 0; i < nbheaders; i++) {
93
15
        htp_header_t *h = htp_table_get_index(h1tx->request_headers, i, NULL);
94
15
        rs_http2_tx_add_header(
95
15
                h2s, bstr_ptr(h->name), bstr_len(h->name), bstr_ptr(h->value), bstr_len(h->value));
96
15
    }
97
3
}