Skip to content

Commit 7da29d0

Browse files
authored
Merge pull request #2499 from DataDog/glopes/server-req-body
Support json/xml request bodies
2 parents 8aa2892 + 498fae5 commit 7da29d0

26 files changed

+831
-423
lines changed

appsec/cmake/clang-format.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ add_custom_target(headers_fix
3333
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
3434

3535
add_custom_target(format_fix_chg
36-
COMMAND bash -c "git status --porcelain=1 :/ | grep -E '\.(c|h|cpp|hpp)$' | awk '{ print \"${CMAKE_SOURCE_DIR}/\" $NF }' | xargs echo '${CLANG_FORMAT}' -i"
37-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
36+
COMMAND bash -c "git status --porcelain=1 :/appsec/ | grep -E '\.(c|h|cpp|hpp)$$' | awk '{ print \"${CMAKE_SOURCE_DIR}/../\" $NF }' | xargs '${CLANG_FORMAT}' --dry-run"
37+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
38+
VERBATIM)

appsec/src/extension/commands/request_init.c

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "../string_helpers.h"
2020
#include "request_init.h"
2121
#include <mpack.h>
22-
#include <zend_string.h>
2322

2423
static dd_result _request_pack(mpack_writer_t *nonnull w, void *nonnull ctx);
2524
static void _init_autoglobals(void);
@@ -31,6 +30,8 @@ static void _pack_files_field_names(
3130
mpack_writer_t *nonnull w, const zend_array *nonnull files);
3231
static void _pack_path_params(
3332
mpack_writer_t *nonnull w, const zend_string *nullable uri_raw);
33+
static void _pack_request_body(mpack_writer_t *nonnull w,
34+
struct req_info_init *nonnull ctx, const zend_array *nonnull server);
3435

3536
static const dd_command_spec _spec = {
3637
.name = "request_init",
@@ -101,8 +102,7 @@ static dd_result _request_pack(mpack_writer_t *nonnull w, void *nonnull _ctx)
101102

102103
// 6.
103104
dd_mpack_write_lstr(w, "server.request.body");
104-
dd_mpack_write_array(w, dd_get_superglob_or_equiv(ZEND_STRL("_POST"),
105-
TRACK_VARS_POST, ctx->superglob_equiv));
105+
_pack_request_body(w, ctx, server);
106106

107107
// 7.
108108
const zend_array *nonnull files = dd_get_superglob_or_equiv(
@@ -123,12 +123,9 @@ static dd_result _request_pack(mpack_writer_t *nonnull w, void *nonnull _ctx)
123123
dd_mpack_write_nullable_zstr(w, ctx->req_info.client_ip);
124124

125125
// 11.
126-
if (send_raw_body && !ctx->superglob_equiv) {
126+
if (send_raw_body && ctx->entity) {
127127
dd_mpack_write_lstr(w, "server.request.body.raw");
128-
zend_string *nonnull req_body =
129-
dd_request_body_buffered(get_DD_APPSEC_MAX_BODY_BUFF_SIZE());
130-
dd_mpack_write_zstr(w, req_body);
131-
zend_string_release(req_body);
128+
dd_mpack_write_zstr(w, ctx->entity);
132129
}
133130

134131
mpack_finish_map(w);
@@ -175,7 +172,13 @@ static void _pack_headers(
175172
continue;
176173
}
177174

178-
if (_is_relevant_header(key)) {
175+
if (zend_string_equals_literal(key, "CONTENT_TYPE")) {
176+
dd_mpack_write_lstr(w, "content-type");
177+
dd_mpack_write_zval(w, val);
178+
} else if (zend_string_equals_literal(key, "CONTENT_LENGTH")) {
179+
dd_mpack_write_lstr(w, "content-length");
180+
dd_mpack_write_zval(w, val);
181+
} else if (_is_relevant_header(key)) {
179182
zend_string *transf_header_name = _transform_header_name(key);
180183
dd_mpack_write_zstr(w, transf_header_name);
181184
zend_string_efree(transf_header_name);
@@ -274,3 +277,31 @@ static void _pack_path_params(
274277
efree(uri_work_zstr);
275278
mpack_complete_array(w);
276279
}
280+
281+
static void _pack_request_body(mpack_writer_t *nonnull w,
282+
struct req_info_init *nonnull ctx, const zend_array *nonnull server)
283+
{
284+
const zend_array *post = dd_get_superglob_or_equiv(
285+
ZEND_STRL("_POST"), TRACK_VARS_POST, ctx->superglob_equiv);
286+
if (zend_hash_num_elements(post) != 0) {
287+
dd_mpack_write_array(w, post);
288+
} else {
289+
bool written = false;
290+
if (ctx->entity) {
291+
zend_string *ct =
292+
dd_php_get_string_elem_cstr(server, ZEND_STRL("CONTENT_TYPE"));
293+
if (ct) {
294+
zval body_zv = dd_entity_body_convert(
295+
ZSTR_VAL(ct), ZSTR_LEN(ct), ctx->entity);
296+
if (Z_TYPE(body_zv) != IS_NULL) {
297+
dd_mpack_write_zval(w, &body_zv);
298+
zval_ptr_dtor(&body_zv);
299+
written = true;
300+
}
301+
}
302+
}
303+
if (!written) {
304+
dd_mpack_write_array(w, &zend_empty_array);
305+
}
306+
}
307+
}

appsec/src/extension/commands/request_init.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
struct req_info_init {
1212
struct req_info req_info;
1313
zend_array *nullable superglob_equiv;
14+
zend_string *nullable entity;
1415
};
1516
dd_result dd_request_init(
1617
dd_conn *nonnull conn, struct req_info_init *nonnull ctx);

0 commit comments

Comments
 (0)