Skip to content

Commit 5d56f69

Browse files
florentinlalyshawang
authored andcommitted
fix(appsec): text/plain response bodies must not be parsed (#13897)
Quick fix to remove parsing of plain text bodies: matching failing system test: https://github.com/DataDog/system-tests/blob/9a0d53da8e422e7ff84e2690038daa0ceaeaa0f0/tests/appsec/test_blocking_addresses.py#L479-L493 ```python def setup_non_blocking_plain_text(self): self.setup_blocking() self.rm_req_nonblock_plain_text = weblog.post( "/waf", data=b'{"value4": "bsldhkuqwgervf"}', headers={"content-type": "text/plain"} ) @Irrelevant( context.weblog_variant in ("akka-http", "play", "jersey-grizzly2", "resteasy-netty3"), reason="Blocks on text/plain if parsed to a String", ) def test_non_blocking_plain_text(self): self.test_blocking() # TODO: This test is pending a better definition of when text/plain is considered parsed body, # which depends on application logic. assert self.rm_req_nonblock_plain_text.status_code == 200 ``` ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent 6180246 commit 5d56f69

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

ddtrace/appsec/_http_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def parse_http_body(
5757
elif content_type.startswith("multipart/form-data"):
5858
return http_utils.parse_form_multipart(body, normalized_headers)
5959
elif content_type == "text/plain":
60-
return body
60+
return None
6161
else:
6262
return None
6363

tests/appsec/appsec/test_appsec_http_utils.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,16 @@ def test_normalize_headers(input_headers, expected):
2323
[
2424
# Body is None
2525
({}, None, False, None),
26-
# Base64 encoded body - text/plain
27-
(
28-
{"content-type": "text/plain"},
29-
"dGV4dCBib2R5",
30-
True,
31-
"text body",
32-
),
3326
# Base64 encoded body - application/json
3427
(
3528
{"content-type": "application/json"},
3629
"eyJrZXkiOiAidmFsdWUifQ==",
3730
True,
3831
{"key": "value"},
3932
),
40-
# Base64 decoding failure - text/plain
33+
# Base64 decoding failure - application/json
4134
(
42-
{"content-type": "text/plain"},
35+
{"content-type": "application/json"},
4336
"invalid_base64_string",
4437
True,
4538
None,
@@ -59,7 +52,7 @@ def test_normalize_headers(input_headers, expected):
5952
({"content-type": "application/xml"}, "<root><key>value</key></root>", False, {"root": {"key": "value"}}),
6053
({"content-type": "text/xml"}, "<root><key>value</key></root>", False, {"root": {"key": "value"}}),
6154
# Text plain
62-
({"content-type": "text/plain"}, "simple text body", False, "simple text body"),
55+
({"content-type": "text/plain"}, "simple text body", False, None),
6356
# Unsupported content type
6457
({"content-type": "application/octet-stream"}, "binary data", False, None),
6558
# No content type provided

0 commit comments

Comments
 (0)