Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/libhtp/htp/htp_cookies.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 * Copyright (c) 2009-2010 Open Information Security Foundation
3
 * Copyright (c) 2010-2013 Qualys, Inc.
4
 * All rights reserved.
5
 * 
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are
8
 * met:
9
 * 
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
13
 * - Redistributions in binary form must reproduce the above copyright
14
 *   notice, this list of conditions and the following disclaimer in the
15
 *   documentation and/or other materials provided with the distribution.
16
17
 * - Neither the name of the Qualys, Inc. nor the names of its
18
 *   contributors may be used to endorse or promote products derived from
19
 *   this software without specific prior written permission.
20
 * 
21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 ***************************************************************************/
33
34
/**
35
 * @file
36
 * @author Ivan Ristic <ivanr@webkreator.com>
37
 */
38
39
#include "htp_config_auto.h"
40
41
#include "htp_private.h"
42
43
/**
44
 * Parses a single v0 request cookie and places the results into tx->request_cookies.
45
 *
46
 * @param[in] connp
47
 * @param[in] data
48
 * @param[in] len
49
 * @return HTP_OK on success, HTP_ERROR on error.
50
 */
51
0
int htp_parse_single_cookie_v0(htp_connp_t *connp, unsigned char *data, size_t len) {
52
0
    if (len == 0) return HTP_OK;
53
    
54
0
    size_t pos = 0;
55
56
    // Look for '='.
57
0
    while ((pos < len) && (data[pos] != '=')) pos++;
58
0
    if (pos == 0) return HTP_OK; // Ignore a nameless cookie.
59
60
0
    bstr *name = bstr_dup_mem(data, pos);
61
0
    if (name == NULL) return HTP_ERROR;
62
63
0
    bstr *value = NULL;
64
0
    if (pos == len) {
65
        // The cookie is empty.
66
0
        value = bstr_dup_c("");
67
0
    } else {
68
        // The cookie is not empty.
69
0
        value = bstr_dup_mem(data + pos + 1, len - pos - 1);
70
0
    }
71
72
0
    if (value == NULL) {
73
0
        bstr_free(name);
74
0
        return HTP_ERROR;
75
0
    }
76
    
77
0
    htp_table_addn(connp->in_tx->request_cookies, name, value);
78
79
0
    return HTP_OK;
80
0
}
81
82
/**
83
 * Parses the Cookie request header in v0 format.
84
 *
85
 * @param[in] connp
86
 * @return HTP_OK on success, HTP_ERROR on error
87
 */
88
0
htp_status_t htp_parse_cookies_v0(htp_connp_t *connp) {
89
0
    htp_header_t *cookie_header = htp_table_get_c(connp->in_tx->request_headers, "cookie");
90
0
    if (cookie_header == NULL) return HTP_OK;
91
92
    // Create a new table to store cookies.
93
0
    connp->in_tx->request_cookies = htp_table_create(4);
94
0
    if (connp->in_tx->request_cookies == NULL) return HTP_ERROR;
95
96
0
    unsigned char *data = bstr_ptr(cookie_header->value);
97
0
    size_t len = bstr_len(cookie_header->value);
98
0
    size_t pos = 0;
99
100
0
    while (pos < len) {
101
        // Ignore whitespace at the beginning.
102
0
        while ((pos < len) && (isspace((int)data[pos]))) pos++;
103
0
        if (pos == len) return HTP_OK;
104
105
0
        size_t start = pos;
106
107
        // Find the end of the cookie.
108
0
        while ((pos < len) && (data[pos] != ';')) pos++;
109
110
0
        if (htp_parse_single_cookie_v0(connp, data + start, pos - start) != HTP_OK) {
111
0
            return HTP_ERROR;
112
0
        }
113
114
        // Go over the semicolon.
115
0
        if (pos < len) pos++;
116
0
    }
117
118
0
    return HTP_OK;
119
0
}