Skip to content

Commit b162eef

Browse files
fixes
1 parent ea3923a commit b162eef

File tree

3 files changed

+11
-25
lines changed

3 files changed

+11
-25
lines changed

tests/appsec/contrib_appsec/utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def test_request_suspicious_request_block_match_uri(
711711
assert self.status(response) == 403
712712
assert get_entry_span_tag(http.STATUS_CODE) == "403"
713713
block_id = self.check_single_rule_triggered("tst-037-002", entry_span)
714-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
714+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
715715
assert (
716716
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
717717
== "application/json"
@@ -774,7 +774,7 @@ def test_request_suspicious_request_block_match_path_params(
774774
assert self.status(response) == 403
775775
assert get_entry_span_tag(http.STATUS_CODE) == "403"
776776
block_id = self.check_single_rule_triggered("tst-037-007", entry_span)
777-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
777+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
778778
assert (
779779
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
780780
== "application/json"
@@ -818,7 +818,7 @@ def test_request_suspicious_request_block_match_query_params(
818818
assert self.status(response) == 403
819819
assert get_entry_span_tag(http.STATUS_CODE) == "403"
820820
block_id = self.check_single_rule_triggered("tst-037-001", entry_span)
821-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
821+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
822822
assert (
823823
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
824824
== "application/json"
@@ -857,7 +857,7 @@ def test_request_suspicious_request_block_match_request_headers(
857857
assert self.status(response) == 403
858858
assert get_entry_span_tag(http.STATUS_CODE) == "403"
859859
block_id = self.check_single_rule_triggered("tst-037-004", entry_span)
860-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
860+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
861861
assert (
862862
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
863863
== "application/json"
@@ -896,7 +896,7 @@ def test_request_suspicious_request_block_match_request_cookies(
896896
assert self.status(response) == 403
897897
assert get_entry_span_tag(http.STATUS_CODE) == "403"
898898
block_id = self.check_single_rule_triggered("tst-037-008", entry_span)
899-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
899+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
900900
assert (
901901
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
902902
== "application/json"
@@ -939,7 +939,7 @@ def test_request_suspicious_request_block_match_response_status(
939939
assert self.status(response) == 403
940940
assert get_entry_span_tag(http.STATUS_CODE) == "403"
941941
block_id = self.check_single_rule_triggered(blocked, entry_span)
942-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
942+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
943943
assert (
944944
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
945945
== "application/json"
@@ -992,7 +992,7 @@ def test_request_suspicious_request_block_match_response_headers(
992992
assert self.status(response) == 403
993993
assert get_entry_span_tag(http.STATUS_CODE) == "403"
994994
block_id = self.check_single_rule_triggered(blocked, entry_span)
995-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
995+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
996996
assert (
997997
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
998998
== "application/json"
@@ -1063,7 +1063,7 @@ def test_request_suspicious_request_block_match_request_body(
10631063
assert self.status(response) == 403
10641064
assert get_entry_span_tag(http.STATUS_CODE) == "403"
10651065
block_id = self.check_single_rule_triggered(blocked, entry_span)
1066-
assert self.body(response) == constants.BLOCKED_RESPONSE_JSON.replace(SECID, f'"{block_id}"')
1066+
assert self.body(response) == _format_template(constants.BLOCKED_RESPONSE_JSON, block_id)
10671067
assert (
10681068
get_entry_span_tag(asm_constants.SPAN_DATA_NAMES.RESPONSE_HEADERS_NO_COOKIES + ".content-type")
10691069
== "application/json"

tests/contrib/django/test_django_appsec_snapshots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ def test_request_ipblock_match_403():
199199
},
200200
)
201201
assert result.status_code == 403
202-
as_bytes = bytes(constants.BLOCKED_RESPONSE_HTML, "utf-8")
203-
204-
assert result.content == as_bytes
202+
body = result.content.decode()
203+
body_parsed = re.sub(r"Response ID: [-0-9a-z]+", r"Response ID: [security_response_id]", body)
204+
assert body_parsed == constants.BLOCKED_RESPONSE_HTML
205205

206206

207207
@pytest.mark.skipif(

tests/contrib/flask/test_appsec_flask_snapshot.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
from ddtrace.appsec._constants import APPSEC
1414
from ddtrace.contrib.internal.flask.patch import flask_version
15-
from ddtrace.internal.constants import BLOCKED_RESPONSE_HTML
16-
from ddtrace.internal.constants import BLOCKED_RESPONSE_JSON
1715
from ddtrace.internal.utils.retry import RetryError
1816
import tests.appsec.rules as rules
1917
from tests.webclient import Client
@@ -141,10 +139,6 @@ def flask_client(flask_command, flask_port, flask_wsgi_application, flask_env_ar
141139
def test_flask_ipblock_match_403(flask_client):
142140
resp = flask_client.get("/", headers={"X-Real-Ip": rules._IP.BLOCKED, "ACCEPT": "text/html"})
143141
assert resp.status_code == 403
144-
if hasattr(resp, "text"):
145-
assert resp.text == BLOCKED_RESPONSE_HTML
146-
else:
147-
assert resp.data == BLOCKED_RESPONSE_HTML.encode("utf-8")
148142

149143

150144
@pytest.mark.snapshot(
@@ -177,10 +171,6 @@ def test_flask_ipblock_match_403(flask_client):
177171
def test_flask_ipblock_match_403_json(flask_client):
178172
resp = flask_client.get("/", headers={"X-Real-Ip": rules._IP.BLOCKED})
179173
assert resp.status_code == 403
180-
if hasattr(resp, "text"):
181-
assert resp.text == BLOCKED_RESPONSE_JSON
182-
else:
183-
assert resp.data == BLOCKED_RESPONSE_JSON.encode("utf-8")
184174

185175

186176
@pytest.mark.snapshot(
@@ -212,10 +202,6 @@ def test_flask_ipblock_match_403_json(flask_client):
212202
def test_flask_userblock_match_403_json(flask_client):
213203
resp = flask_client.get("/checkuser/%s" % _BLOCKED_USER)
214204
assert resp.status_code == 403
215-
if hasattr(resp, "text"):
216-
assert resp.text == BLOCKED_RESPONSE_JSON
217-
else:
218-
assert resp.data == BLOCKED_RESPONSE_JSON.encode("utf-8")
219205

220206

221207
@pytest.mark.snapshot(

0 commit comments

Comments
 (0)