Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: prepare libhtp.rs aliases for htp opaque htp_header_t #12430

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/app-layer-htp-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include "suricata-common.h"
#include "app-layer-htp-file.h"
#include "app-layer-htp-libhtp.h"
#include "app-layer-htp-range.h"
#include "app-layer-events.h"
#include "util-validate.h"
Expand Down
11 changes: 11 additions & 0 deletions src/app-layer-htp-libhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@
#define htp_tx_response_progress(tx) tx->response_progress
#define htp_tx_response_protocol_number(tx) tx->response_protocol_number

#define htp_tx_request_header(tx, header) htp_table_get_c(tx->request_headers, header)
#define htp_tx_response_header(tx, header) htp_table_get_c(tx->response_headers, header)

// Functions introduced to handle opaque htp_header_t
#define htp_header_name_len(h) bstr_len(h->name)
#define htp_header_name_ptr(h) bstr_ptr(h->name)
#define htp_header_name(h) h->name
#define htp_header_value_len(h) bstr_len(h->value)
#define htp_header_value_ptr(h) bstr_ptr(h->value)
#define htp_header_value(h) h->value

bstr *SCHTPGenerateNormalizedUri(htp_tx_t *tx, htp_uri_t *uri, bool uri_include_all);

#endif /* SURICATA_APP_LAYER_HTP_LIBHTP__H */
3 changes: 1 addition & 2 deletions src/app-layer-htp-xff.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#include "app-layer-parser.h"
#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "app-layer-htp-xff.h"

#ifndef HAVE_MEMRCHR
Expand Down Expand Up @@ -142,7 +141,7 @@ int HttpXFFGetIPFromTx(const Flow *f, uint64_t tx_id, HttpXFFCfg *xff_cfg,

htp_header_t *h_xff = NULL;
if (htp_tx_request_headers(tx) != NULL) {
h_xff = htp_table_get_c(htp_tx_request_headers(tx), xff_cfg->header);
h_xff = htp_tx_request_header(tx, xff_cfg->header);
}

if (h_xff != NULL && bstr_len(h_xff->value) >= XFF_CHAIN_MINLEN &&
Expand Down
43 changes: 21 additions & 22 deletions src/app-layer-htp.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,7 @@ static AppLayerResult HTPHandleResponseData(Flow *f, void *htp_state, AppLayerPa
case HTP_STREAM_STATE_TUNNEL:
tx = htp_connp_get_out_tx(hstate->connp);
if (tx != NULL && htp_tx_response_status_number(tx) == 101) {
htp_header_t *h =
(htp_header_t *)htp_table_get_c(htp_tx_response_headers(tx), "Upgrade");
htp_header_t *h = (htp_header_t *)htp_tx_response_header(tx, "Upgrade");
if (h == NULL) {
break;
}
Expand All @@ -984,7 +983,7 @@ static AppLayerResult HTPHandleResponseData(Flow *f, void *htp_state, AppLayerPa
dp = (uint16_t)htp_tx_request_port_number(tx);
}
consumed = (uint32_t)htp_connp_res_data_consumed(hstate->connp);
if (bstr_cmp_c(h->value, "h2c") == 0) {
if (bstr_cmp_c(htp_header_value(h), "h2c") == 0) {
if (AppLayerProtoDetectGetProtoName(ALPROTO_HTTP2) == NULL) {
// if HTTP2 is disabled, keep the HTP_STREAM_STATE_TUNNEL mode
break;
Expand All @@ -1000,7 +999,7 @@ static AppLayerResult HTPHandleResponseData(Flow *f, void *htp_state, AppLayerPa
SCReturnStruct(APP_LAYER_INCOMPLETE(consumed, input_len - consumed));
}
SCReturnStruct(APP_LAYER_OK);
} else if (bstr_cmp_c_nocase(h->value, "WebSocket") == 0) {
} else if (bstr_cmp_c_nocase(htp_header_value(h), "WebSocket") == 0) {
if (AppLayerProtoDetectGetProtoName(ALPROTO_WEBSOCKET) == NULL) {
// if WS is disabled, keep the HTP_STREAM_STATE_TUNNEL mode
break;
Expand Down Expand Up @@ -1141,9 +1140,10 @@ static int HTTPParseContentDispositionHeader(uint8_t *name, size_t name_len,
*/
static int HtpRequestBodySetupMultipart(htp_tx_t *tx, HtpTxUserData *htud)
{
htp_header_t *h = (htp_header_t *)htp_table_get_c(htp_tx_request_headers(tx), "Content-Type");
if (h != NULL && bstr_len(h->value) > 0) {
htud->mime_state = SCMimeStateInit(bstr_ptr(h->value), (uint32_t)bstr_len(h->value));
htp_header_t *h = (htp_header_t *)htp_tx_request_header(tx, "Content-Type");
if (h != NULL && htp_header_value_len(h) > 0) {
htud->mime_state =
SCMimeStateInit(htp_header_value_ptr(h), (uint32_t)htp_header_value_len(h));
if (htud->mime_state) {
htud->tsflags |= HTP_BOUNDARY_SET;
SCReturnInt(1);
Expand Down Expand Up @@ -1362,12 +1362,12 @@ static int HtpResponseBodyHandle(HtpState *hstate, HtpTxUserData *htud,
size_t filename_len = 0;

/* try Content-Disposition header first */
htp_header_t *h =
(htp_header_t *)htp_table_get_c(htp_tx_response_headers(tx), "Content-Disposition");
if (h != NULL && bstr_len(h->value) > 0) {
htp_header_t *h = (htp_header_t *)htp_tx_response_header(tx, "Content-Disposition");
if (h != NULL && htp_header_value_len(h) > 0) {
/* parse content-disposition */
(void)HTTPParseContentDispositionHeader((uint8_t *)"filename=", 9,
(uint8_t *) bstr_ptr(h->value), bstr_len(h->value), &filename, &filename_len);
(uint8_t *)htp_header_value_ptr(h), htp_header_value_len(h), &filename,
&filename_len);
}

/* fall back to name from the uri */
Expand All @@ -1381,8 +1381,7 @@ static int HtpResponseBodyHandle(HtpState *hstate, HtpTxUserData *htud,

if (filename != NULL) {
// set range if present
htp_header_t *h_content_range =
htp_table_get_c(htp_tx_response_headers(tx), "content-range");
htp_header_t *h_content_range = htp_tx_response_header(tx, "content-range");
if (filename_len > SC_FILENAME_MAX) {
// explicitly truncate the file name if too long
filename_len = SC_FILENAME_MAX;
Expand Down Expand Up @@ -2946,7 +2945,7 @@ static int HTPParserTest01(void)
htp_header_t *h = htp_table_get_index(htp_tx_request_headers(tx), 0, NULL);
FAIL_IF_NULL(h);

FAIL_IF(strcmp(bstr_util_strdup_to_c(h->value), "Victor/1.0"));
FAIL_IF(strcmp(bstr_util_strdup_to_c(htp_header_value(h)), "Victor/1.0"));
FAIL_IF(htp_tx_request_method_number(tx) != HTP_METHOD_POST);
FAIL_IF(htp_tx_request_protocol_number(tx) != HTP_PROTOCOL_V1_0);

Expand Down Expand Up @@ -2990,7 +2989,7 @@ static int HTPParserTest01b(void)
htp_header_t *h = htp_table_get_index(htp_tx_request_headers(tx), 0, NULL);
FAIL_IF_NULL(h);

FAIL_IF(strcmp(bstr_util_strdup_to_c(h->value), "Victor/1.0"));
FAIL_IF(strcmp(bstr_util_strdup_to_c(htp_header_value(h)), "Victor/1.0"));
FAIL_IF(htp_tx_request_method_number(tx) != HTP_METHOD_POST);
FAIL_IF(htp_tx_request_protocol_number(tx) != HTP_PROTOCOL_V1_0);

Expand Down Expand Up @@ -3045,7 +3044,7 @@ static int HTPParserTest01c(void)
htp_header_t *h = htp_table_get_index(htp_tx_request_headers(tx), 0, NULL);
FAIL_IF_NULL(h);

FAIL_IF(strcmp(bstr_util_strdup_to_c(h->value), "Victor/1.0"));
FAIL_IF(strcmp(bstr_util_strdup_to_c(htp_header_value(h)), "Victor/1.0"));
FAIL_IF(htp_tx_request_method_number(tx) != HTP_METHOD_POST);
FAIL_IF(htp_tx_request_protocol_number(tx) != HTP_PROTOCOL_V1_0);

Expand Down Expand Up @@ -3101,7 +3100,7 @@ static int HTPParserTest01a(void)
htp_header_t *h = htp_table_get_index(htp_tx_request_headers(tx), 0, NULL);
FAIL_IF_NULL(h);

FAIL_IF(strcmp(bstr_util_strdup_to_c(h->value), "Victor/1.0"));
FAIL_IF(strcmp(bstr_util_strdup_to_c(htp_header_value(h)), "Victor/1.0"));
FAIL_IF(htp_tx_request_method_number(tx) != HTP_METHOD_POST);
FAIL_IF(htp_tx_request_protocol_number(tx) != HTP_PROTOCOL_V1_0);

Expand Down Expand Up @@ -3642,11 +3641,11 @@ static int HTPParserTest10(void)
htp_header_t *h = htp_table_get_index(htp_tx_request_headers(tx), 0, NULL);
FAIL_IF_NULL(h);

char *name = bstr_util_strdup_to_c(h->name);
char *name = bstr_util_strdup_to_c(htp_header_name(h));
FAIL_IF_NULL(name);
FAIL_IF(strcmp(name, "Host") != 0);
FAIL_IF(strcmp(htp_header_name(h), "Host") != 0);

char *value = bstr_util_strdup_to_c(h->value);
char *value = bstr_util_strdup_to_c(htp_header_value(h));
FAIL_IF_NULL(value);
FAIL_IF(strcmp(value, "www.google.com") != 0);

Expand Down Expand Up @@ -3820,11 +3819,11 @@ static int HTPParserTest13(void)
htp_header_t *h = htp_table_get_index(htp_tx_request_headers(tx), 0, NULL);
FAIL_IF_NULL(h);

char *name = bstr_util_strdup_to_c(h->name);
char *name = bstr_util_strdup_to_c(htp_header_name(h));
FAIL_IF_NULL(name);
FAIL_IF(strcmp(name, "Host") != 0);

char *value = bstr_util_strdup_to_c(h->value);
char *value = bstr_util_strdup_to_c(htp_header_value(h));
FAIL_IF_NULL(value);
FAIL_IF(strcmp(value, "www.google.com\rName: Value") != 0);

Expand Down
4 changes: 4 additions & 0 deletions src/app-layer-htp.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "app-layer-frames.h"

#include <htp/htp.h>
// Temporary include directly app-layer-htp-libhtp.h
// This helps libhtp.rs transition by making small steps
// app-layer-htp-libhtp.h will be removed with libhtp.rs final merge
#include "app-layer-htp-libhtp.h"

/* default request body limit */
#define HTP_CONFIG_DEFAULT_REQUEST_BODY_LIMIT 4096U
Expand Down
5 changes: 2 additions & 3 deletions src/app-layer-http2.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "app-layer-parser.h"

#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "app-layer-http2.h"
#include "rust.h"

Expand Down Expand Up @@ -94,7 +93,7 @@ void HTTP2MimicHttp1Request(void *alstate_orig, void *h2s)
size_t nbheaders = htp_table_size(htp_tx_request_headers(h1tx));
for (size_t i = 0; i < nbheaders; i++) {
htp_header_t *h = htp_table_get_index(htp_tx_request_headers(h1tx), i, NULL);
rs_http2_tx_add_header(h2s, bstr_ptr(h->name), (uint32_t)bstr_len(h->name),
bstr_ptr(h->value), (uint32_t)bstr_len(h->value));
rs_http2_tx_add_header(h2s, htp_header_name_ptr(h), (uint32_t)htp_header_name_len(h),
htp_header_value_ptr(h), (uint32_t)htp_header_value_len(h));
}
}
1 change: 0 additions & 1 deletion src/detect-file-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#include "app-layer.h"
#include "app-layer-parser.h"
#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "app-layer-smtp.h"

#include "flow.h"
Expand Down
1 change: 0 additions & 1 deletion src/detect-http-client-body.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
#include "app-layer.h"
#include "app-layer-parser.h"
#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "detect-http-client-body.h"
#include "stream-tcp.h"
#include "util-profiling.h"
Expand Down
18 changes: 8 additions & 10 deletions src/detect-http-cookie.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
#include "app-layer-parser.h"

#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "detect-http-cookie.h"
#include "stream-tcp.h"

Expand Down Expand Up @@ -181,14 +180,14 @@ static InspectionBuffer *GetRequestData(DetectEngineThreadCtx *det_ctx,
if (htp_tx_request_headers(tx) == NULL)
return NULL;

htp_header_t *h = (htp_header_t *)htp_table_get_c(htp_tx_request_headers(tx), "Cookie");
if (h == NULL || h->value == NULL) {
htp_header_t *h = (htp_header_t *)htp_tx_request_header(tx, "Cookie");
if (h == NULL || htp_header_value(h) == NULL) {
SCLogDebug("HTTP cookie header not present in this request");
return NULL;
}

const uint32_t data_len = bstr_len(h->value);
const uint8_t *data = bstr_ptr(h->value);
const uint32_t data_len = htp_header_value_len(h);
const uint8_t *data = htp_header_value_ptr(h);

InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
InspectionBufferApplyTransforms(buffer, transforms);
Expand All @@ -208,15 +207,14 @@ static InspectionBuffer *GetResponseData(DetectEngineThreadCtx *det_ctx,
if (htp_tx_response_headers(tx) == NULL)
return NULL;

htp_header_t *h =
(htp_header_t *)htp_table_get_c(htp_tx_response_headers(tx), "Set-Cookie");
if (h == NULL || h->value == NULL) {
htp_header_t *h = (htp_header_t *)htp_tx_response_header(tx, "Set-Cookie");
if (h == NULL || htp_header_value(h) == NULL) {
SCLogDebug("HTTP cookie header not present in this request");
return NULL;
}

const uint32_t data_len = bstr_len(h->value);
const uint8_t *data = bstr_ptr(h->value);
const uint32_t data_len = htp_header_value_len(h);
const uint8_t *data = htp_header_value_ptr(h);

InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
InspectionBufferApplyTransforms(buffer, transforms);
Expand Down
7 changes: 3 additions & 4 deletions src/detect-http-header-names.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#include "app-layer-parser.h"

#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "detect-http-header.h"
#include "stream-tcp.h"

Expand Down Expand Up @@ -107,7 +106,7 @@ static uint8_t *GetBufferForTX(
size_t no_of_headers = htp_table_size(headers);
for (; i < no_of_headers; i++) {
htp_header_t *h = htp_table_get_index(headers, i, NULL);
size_t size = bstr_size(h->name) + 2; // for \r\n
size_t size = htp_header_name_len(h) + 2; // for \r\n
if (i == 0)
size += 2;
if (i + 1 == no_of_headers)
Expand All @@ -127,8 +126,8 @@ static uint8_t *GetBufferForTX(
buf->buffer[buf->len++] = '\n';
}

memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
buf->len += bstr_size(h->name);
memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
buf->len += htp_header_name_len(h);
buf->buffer[buf->len++] = '\r';
buf->buffer[buf->len++] = '\n';

Expand Down
27 changes: 12 additions & 15 deletions src/detect-http-header.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#include "app-layer-parser.h"

#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "detect-http-header.h"
#include "detect-http-header-common.h"

Expand Down Expand Up @@ -99,17 +98,15 @@ static uint8_t *GetBufferForTX(
size_t no_of_headers = htp_table_size(headers);
for (; i < no_of_headers; i++) {
htp_header_t *h = htp_table_get_index(headers, i, NULL);
size_t size1 = bstr_size(h->name);
size_t size2 = bstr_size(h->value);
size_t size1 = htp_header_name_len(h);
size_t size2 = htp_header_value_len(h);

if (flags & STREAM_TOSERVER) {
if (size1 == 6 &&
SCMemcmpLowercase("cookie", bstr_ptr(h->name), 6) == 0) {
if (size1 == 6 && SCMemcmpLowercase("cookie", htp_header_name_ptr(h), 6) == 0) {
continue;
}
} else {
if (size1 == 10 &&
SCMemcmpLowercase("set-cookie", bstr_ptr(h->name), 10) == 0) {
if (size1 == 10 && SCMemcmpLowercase("set-cookie", htp_header_name_ptr(h), 10) == 0) {
continue;
}
}
Expand All @@ -125,12 +122,12 @@ static uint8_t *GetBufferForTX(
}
}

memcpy(buf->buffer + buf->len, bstr_ptr(h->name), bstr_size(h->name));
buf->len += bstr_size(h->name);
memcpy(buf->buffer + buf->len, htp_header_name_ptr(h), htp_header_name_len(h));
buf->len += htp_header_name_len(h);
buf->buffer[buf->len++] = ':';
buf->buffer[buf->len++] = ' ';
memcpy(buf->buffer + buf->len, bstr_ptr(h->value), bstr_size(h->value));
buf->len += bstr_size(h->value);
memcpy(buf->buffer + buf->len, htp_header_value_ptr(h), htp_header_value_len(h));
buf->len += htp_header_value_len(h);
buf->buffer[buf->len++] = '\r';
buf->buffer[buf->len++] = '\n';
#if 0 // looks like this breaks existing rules
Expand Down Expand Up @@ -577,8 +574,8 @@ static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx,
}
for (size_t i = 0; i < no_of_headers; i++) {
htp_header_t *h = htp_table_get_index(headers, i, NULL);
size_t size1 = bstr_size(h->name);
size_t size2 = bstr_size(h->value);
size_t size1 = htp_header_name_len(h);
size_t size2 = htp_header_value_len(h);
size_t size = size1 + size2 + 2;
if (hdr_td->items[i].len < size) {
// Use realloc, as this pointer is not freed until HttpMultiBufHeaderThreadDataFree
Expand All @@ -588,10 +585,10 @@ static InspectionBuffer *GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx,
}
hdr_td->items[i].buffer = tmp;
}
memcpy(hdr_td->items[i].buffer, bstr_ptr(h->name), size1);
memcpy(hdr_td->items[i].buffer, htp_header_name_ptr(h), size1);
hdr_td->items[i].buffer[size1] = ':';
hdr_td->items[i].buffer[size1 + 1] = ' ';
memcpy(hdr_td->items[i].buffer + size1 + 2, bstr_ptr(h->value), size2);
memcpy(hdr_td->items[i].buffer + size1 + 2, htp_header_value_ptr(h), size2);
hdr_td->items[i].len = size;
}
hdr_td->len = no_of_headers;
Expand Down
Loading
Loading