Skip to content

Commit

Permalink
response: fix connect tunneling bug
Browse files Browse the repository at this point in the history
The response was emitting partial body data depending on how you
fed the parser with inbound and outbound data chunks. It seems the
intended behavior is to not emit body data if HTP_STREAM_TUNNEL
will eventually be entered.

The fix was to allow htp_connp_REQ_CONNECT_WAIT_RESPONSE to resume in
order to enter the HTP_STREAM_TUNNEL state or complete the request.

The tunneling transaction was also incomplete because the
request side wasn't being finalized after entering HTP_STREAM_TUNNEL.

See test case for example.
  • Loading branch information
cccs-sadugas committed Jun 6, 2024
1 parent 559667a commit 4d33275
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions htp/htp_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ htp_status_t htp_connp_REQ_CONNECT_PROBE_DATA(htp_connp_t *connp) {
#endif
connp->in_status = HTP_STREAM_TUNNEL;
connp->out_status = HTP_STREAM_TUNNEL;

// set the final state to eventually complete the transaction
connp->in_state = htp_connp_REQ_FINALIZE;
}

// not calling htp_connp_req_clear_buffer, we're not consuming the data
Expand Down
14 changes: 11 additions & 3 deletions htp/htp_response.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,9 +1166,17 @@ htp_status_t htp_connp_RES_FINALIZE(htp_connp_t *connp) {
}

if (htp_treat_response_line_as_body(data, bytes_left)) {
// Interpret remaining bytes as body data
htp_log(connp, HTP_LOG_MARK, HTP_LOG_WARNING, 0, "Unexpected response body");
htp_status_t rc = htp_tx_res_process_body_data_ex(connp->out_tx, data, bytes_left);
// Interpret remaining bytes as body data only if inbound processing
// was not suspended. Otherwise, yield back to inbound processing in
// case we've suspended because of a CONNECT transaction and are about
// to enter a tunneled state where we won't process body data.
htp_status_t rc = HTP_DATA_OTHER;

if (connp->in_status != HTP_STREAM_DATA_OTHER) {
htp_log(connp, HTP_LOG_MARK, HTP_LOG_WARNING, 0, "Unexpected response body");
rc = htp_tx_res_process_body_data_ex(connp->out_tx, data, bytes_left);
}

htp_connp_res_clear_buffer(connp);
return rc;
}
Expand Down
1 change: 1 addition & 0 deletions htp/htp_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,7 @@ char *htp_connp_in_state_as_string(htp_connp_t *connp) {
if (connp->in_state == htp_connp_REQ_HEADERS) return "REQ_HEADERS";
if (connp->in_state == htp_connp_REQ_CONNECT_CHECK) return "REQ_CONNECT_CHECK";
if (connp->in_state == htp_connp_REQ_CONNECT_WAIT_RESPONSE) return "REQ_CONNECT_WAIT_RESPONSE";
if (connp->in_state == htp_connp_REQ_CONNECT_PROBE_DATA) return "REQ_CONNECT_PROBE_DATA";
if (connp->in_state == htp_connp_REQ_BODY_DETERMINE) return "REQ_BODY_DETERMINE";
if (connp->in_state == htp_connp_REQ_BODY_IDENTITY) return "REQ_BODY_IDENTITY";
if (connp->in_state == htp_connp_REQ_BODY_CHUNKED_LENGTH) return "REQ_BODY_CHUNKED_LENGTH";
Expand Down

0 comments on commit 4d33275

Please sign in to comment.