Skip to content

Commit 9d4d417

Browse files
authored
chore(iast): more sqli redaction tests (#12242)
## 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 4b74d31 commit 9d4d417

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

Diff for: tests/appsec/integrations/django_tests/conftest.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
from ddtrace.appsec._iast import enable_iast_propagation
88
from ddtrace.appsec._iast._patch_modules import patch_iast
9-
from ddtrace.contrib.internal.django.patch import patch
9+
from ddtrace.contrib.internal.django.patch import patch as django_patch
1010
from ddtrace.trace import Pin
1111
from tests.appsec.iast.conftest import _end_iast_context_and_oce
1212
from tests.appsec.iast.conftest import _start_iast_context_and_oce
1313
from tests.utils import DummyTracer
1414
from tests.utils import TracerSpanContainer
15+
from tests.utils import override_env
1516
from tests.utils import override_global_config
1617

1718

@@ -26,10 +27,10 @@ def pytest_configure():
2627
_iast_deduplication_enabled=False,
2728
_iast_request_sampling=100.0,
2829
)
29-
):
30+
), override_env(dict(_DD_IAST_PATCH_MODULES="tests.appsec.integrations")):
3031
settings.DEBUG = False
3132
patch_iast()
32-
patch()
33+
django_patch()
3334
enable_iast_propagation()
3435
django.setup()
3536

Diff for: tests/appsec/integrations/django_tests/django_app/urls.py

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def shutdown(request):
4646
views.sqli_http_request_parameter_name_post,
4747
name="sqli_http_request_parameter_name_post",
4848
),
49+
handler(
50+
"appsec/sqli_query_no_redacted/$",
51+
views.sqli_query_no_redacted,
52+
name="sqli_query_no_redacted",
53+
),
4954
handler(
5055
"appsec/sqli_http_request_header_name/$",
5156
views.sqli_http_request_header_name,

Diff for: tests/appsec/integrations/django_tests/django_app/views.py

+8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ def sqli_http_request_parameter_name_post(request):
129129
return HttpResponse(request.META["HTTP_USER_AGENT"], status=200)
130130

131131

132+
def sqli_query_no_redacted(request):
133+
obj = request.GET["q"]
134+
with connection.cursor() as cursor:
135+
# label sqli_query_no_redacted
136+
cursor.execute(f"SELECT * FROM {obj} ORDER BY name")
137+
return HttpResponse("OK", status=200)
138+
139+
132140
def sqli_http_request_header_name(request):
133141
key = [x for x in request.META.keys() if x == "master"][0]
134142

Diff for: tests/appsec/integrations/django_tests/test_django_appsec_iast.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_django_tainted_user_agent_iast_disabled(client, test_spans, tracer):
168168

169169
@pytest.mark.django_db()
170170
@pytest.mark.skipif(not asm_config._iast_supported, reason="Python version not supported by IAST")
171-
def test_django_tainted_user_agent_iast_enabled_sqli_http_request_parameter(client, test_spans, tracer):
171+
def test_django_sqli_http_request_parameter(client, test_spans, tracer):
172172
root_span, response = _aux_appsec_get_root_span(
173173
client,
174174
test_spans,
@@ -309,6 +309,46 @@ def test_django_sqli_http_request_parameter_name_post(client, test_spans, tracer
309309
assert loaded["vulnerabilities"][0]["hash"] == hash_value
310310

311311

312+
@pytest.mark.django_db()
313+
@pytest.mark.skipif(not asm_config._iast_supported, reason="Python version not supported by IAST")
314+
def test_django_sqli_query_no_redacted(client, test_spans, tracer):
315+
root_span, response = _aux_appsec_get_root_span(
316+
client,
317+
test_spans,
318+
tracer,
319+
url="/appsec/sqli_query_no_redacted/?q=sqlite_master",
320+
)
321+
322+
vuln_type = "SQL_INJECTION"
323+
324+
assert response.status_code == 200
325+
assert response.content == b"OK"
326+
327+
loaded = json.loads(root_span.get_tag(IAST.JSON))
328+
329+
line, hash_value = get_line_and_hash("sqli_query_no_redacted", vuln_type, filename=TEST_FILE)
330+
331+
assert loaded["sources"] == [
332+
{
333+
"name": "q",
334+
"origin": "http.request.parameter",
335+
"value": "sqlite_master",
336+
}
337+
]
338+
339+
assert loaded["vulnerabilities"][0]["type"] == vuln_type
340+
assert loaded["vulnerabilities"][0]["evidence"] == {
341+
"valueParts": [
342+
{"value": "SELECT * FROM "},
343+
{"source": 0, "value": "sqlite_master"},
344+
{"value": " ORDER BY name"},
345+
]
346+
}
347+
assert loaded["vulnerabilities"][0]["location"]["path"] == TEST_FILE
348+
assert loaded["vulnerabilities"][0]["location"]["line"] == line
349+
assert loaded["vulnerabilities"][0]["hash"] == hash_value
350+
351+
312352
@pytest.mark.django_db()
313353
@pytest.mark.skipif(not asm_config._iast_supported, reason="Python version not supported by IAST")
314354
def test_django_sqli_http_request_header_value(client, test_spans, tracer):

0 commit comments

Comments
 (0)