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

Request header invalid 6415 v3 #411

Closed
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
2 changes: 2 additions & 0 deletions htp/htp_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ htp_status_t htp_tx_urldecode_params_inplace(htp_tx_t *tx, bstr *input);

void htp_connp_destroy_decompressors(htp_connp_t *connp);

htp_status_t htp_header_has_token(const unsigned char *hvp, size_t hvlen, const unsigned char *value);

#ifndef HAVE_STRLCAT
size_t strlcat(char *dst, const char *src, size_t size);
#endif
Expand Down
7 changes: 2 additions & 5 deletions htp/htp_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ static htp_status_t htp_tx_process_request_headers(htp_tx_t *tx) {
// (2.2.22 on Ubuntu 12.04 LTS) instead errors out with "Unknown Transfer-Encoding: identity".
// And it behaves strangely, too, sending a 501 and proceeding to process the request
// (e.g., PHP is run), but without the body. It then closes the connection.
if (bstr_cmp_c_nocase(te->value, "chunked") != 0) {
if (htp_header_has_token(bstr_ptr(te->value), bstr_len(te->value), (unsigned char*) "chunked") != HTP_OK) {
// Invalid T-E header value.
tx->request_transfer_coding = HTP_CODING_INVALID;
tx->flags |= HTP_REQUEST_INVALID_T_E;
Expand Down Expand Up @@ -585,10 +585,7 @@ static htp_status_t htp_tx_process_request_headers(htp_tx_t *tx) {
rc = htp_hook_run_all(tx->connp->cfg->hook_request_headers, tx);
if (rc != HTP_OK) return rc;

// We cannot proceed if the request is invalid.
if (tx->flags & HTP_REQUEST_INVALID) {
return HTP_ERROR;
}
// We still proceed if the request is invalid.

return HTP_OK;
}
Expand Down
51 changes: 51 additions & 0 deletions htp/htp_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2536,3 +2536,54 @@ htp_uri_t *htp_uri_alloc(void) {
char *htp_get_version(void) {
return HTP_VERSION_STRING_FULL;
}

htp_status_t htp_header_has_token(const unsigned char *hvp, size_t hvlen, const unsigned char *value) {
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hvp is haystack, value is needle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and haystack is header value, value is token to look for

int state = 0;
// offset to compare in value
size_t v_off = 0;
for (size_t i = 0; i < hvlen; i++) {
switch (state) {
case 0:
if (hvp[i] == 0) {
// skip 0 char
continue;
}
if (v_off == 0 && hvp[i] == ' ') {
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
// skip leading space
continue;
}
if (tolower(hvp[i]) == tolower(value[v_off])) {
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
v_off++;
if (value[v_off] == 0) {
// finish validation if end of token
state = 2;
}
continue;
} else {
// wait for a new token
v_off = 0;
state = 1;
}
// fallthrough
case 1:
if (hvp[i] == ',') {
// start of next token
state = 0;
}
break;
case 2:
if (hvp[i] == ',') {
return HTP_OK;
}
if (hvp[i] != 0 && hvp[i] != ' ') {
// trailing junk in token, cait for a next one
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
v_off = 0;
state = 1;
}
}
}
if (state == 2) {
return HTP_OK;
}
return HTP_ERROR;
}
36 changes: 36 additions & 0 deletions test/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1801,3 +1801,39 @@ TEST_F(UrlencodedParser, UrlDecode1) {
ASSERT_EQ(0, bstr_cmp_c(s, "/one/two/three/%3"));
bstr_free(s);
}

TEST(UtilTest, HeaderHasToken) {
char data[100];

// Basic
strcpy(data, "chunked");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));

// Negative
strcpy(data, "notchunked");
EXPECT_EQ(HTP_ERROR, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "chunkednot");
EXPECT_EQ(HTP_ERROR, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "chunk,ed");
EXPECT_EQ(HTP_ERROR, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "chunk ed");
EXPECT_EQ(HTP_ERROR, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));

// Positive
strcpy(data, " notchunked , chunked , yetanother");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "chunked,yetanother");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "not,chunked");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "chunk,chunked");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, " chunked");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "chunked ");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, "chunked,");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
strcpy(data, ",chunked");
EXPECT_EQ(HTP_OK, htp_header_has_token((unsigned char*) data, strlen(data), (unsigned char *)"chunked"));
}