|
1 | 1 | import json |
2 | 2 |
|
3 | 3 | from ddtrace.internal import _context |
| 4 | +from ddtrace.internal.compat import urlencode |
4 | 5 | from tests.appsec.test_processor import RULES_GOOD_PATH |
5 | 6 | from tests.contrib.flask import BaseFlaskTestCase |
6 | 7 | from tests.utils import override_env |
| 8 | +from tests.utils import override_global_config |
7 | 9 |
|
8 | 10 |
|
9 | 11 | class FlaskAppSecTestCase(BaseFlaskTestCase): |
@@ -109,3 +111,63 @@ def test_flask_cookie(self): |
109 | 111 | assert query == {"testingcookie_key": "testingcookie_value"} or query == { |
110 | 112 | "testingcookie_key": ["testingcookie_value"] |
111 | 113 | } |
| 114 | + |
| 115 | + def test_flask_body_urlencoded(self): |
| 116 | + with override_global_config(dict(_appsec_enabled=True)): |
| 117 | + self.tracer._appsec_enabled = True |
| 118 | + # Hack: need to pass an argument to configure so that the processors are recreated |
| 119 | + self.tracer.configure(api_version="v0.4") |
| 120 | + payload = urlencode({"mytestingbody_key": "mytestingbody_value"}) |
| 121 | + self.client.post("/", data=payload, content_type="application/x-www-form-urlencoded") |
| 122 | + root_span = self.pop_spans()[0] |
| 123 | + query = dict(_context.get_item("http.request.body", span=root_span)) |
| 124 | + |
| 125 | + assert root_span.get_tag("_dd.appsec.json") is None |
| 126 | + assert query == {"mytestingbody_key": "mytestingbody_value"} |
| 127 | + |
| 128 | + def test_flask_body_urlencoded_appsec_disabled_then_no_body(self): |
| 129 | + with override_global_config(dict(_appsec_enabled=False)): |
| 130 | + self.tracer._appsec_enabled = True |
| 131 | + # Hack: need to pass an argument to configure so that the processors are recreated |
| 132 | + self.tracer.configure(api_version="v0.4") |
| 133 | + payload = urlencode({"mytestingbody_key": "mytestingbody_value"}) |
| 134 | + self.client.post("/", data=payload, content_type="application/x-www-form-urlencoded") |
| 135 | + root_span = self.pop_spans()[0] |
| 136 | + assert not _context.get_item("http.request.body", span=root_span) |
| 137 | + |
| 138 | + def test_flask_request_body_urlencoded_attack(self): |
| 139 | + with override_global_config(dict(_appsec_enabled=True)): |
| 140 | + self.tracer._appsec_enabled = True |
| 141 | + # Hack: need to pass an argument to configure so that the processors are recreated |
| 142 | + self.tracer.configure(api_version="v0.4") |
| 143 | + payload = urlencode({"attack": "1' or '1' = '1'"}) |
| 144 | + self.client.post("/", data=payload, content_type="application/x-www-form-urlencoded") |
| 145 | + root_span = self.pop_spans()[0] |
| 146 | + query = dict(_context.get_item("http.request.body", span=root_span)) |
| 147 | + assert "triggers" in json.loads(root_span.get_tag("_dd.appsec.json")) |
| 148 | + assert query == {"attack": "1' or '1' = '1'"} |
| 149 | + |
| 150 | + def test_flask_body_json(self): |
| 151 | + with override_global_config(dict(_appsec_enabled=True)): |
| 152 | + self.tracer._appsec_enabled = True |
| 153 | + # Hack: need to pass an argument to configure so that the processors are recreated |
| 154 | + self.tracer.configure(api_version="v0.4") |
| 155 | + payload = {"mytestingbody_key": "mytestingbody_value"} |
| 156 | + self.client.post("/", json=payload, content_type="application/json") |
| 157 | + root_span = self.pop_spans()[0] |
| 158 | + query = dict(_context.get_item("http.request.body", span=root_span)) |
| 159 | + |
| 160 | + assert root_span.get_tag("_dd.appsec.json") is None |
| 161 | + assert query == {"mytestingbody_key": "mytestingbody_value"} |
| 162 | + |
| 163 | + def test_flask_body_json_attack(self): |
| 164 | + with override_global_config(dict(_appsec_enabled=True)): |
| 165 | + self.tracer._appsec_enabled = True |
| 166 | + # Hack: need to pass an argument to configure so that the processors are recreated |
| 167 | + self.tracer.configure(api_version="v0.4") |
| 168 | + payload = {"attack": "1' or '1' = '1'"} |
| 169 | + self.client.post("/", json=payload, content_type="application/json") |
| 170 | + root_span = self.pop_spans()[0] |
| 171 | + query = dict(_context.get_item("http.request.body", span=root_span)) |
| 172 | + assert "triggers" in json.loads(root_span.get_tag("_dd.appsec.json")) |
| 173 | + assert query == {"attack": "1' or '1' = '1'"} |
0 commit comments