Skip to content

Commit 55d531a

Browse files
authored
chore(iast): avoid exception when no relevant frame is found [2.21] (#13348)
1 parent 135b12c commit 55d531a

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

ddtrace/appsec/_iast/_stacktrace.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ _get_result_tuple(PyFrameObject* frame)
157157
PyObject* result = NULL;
158158
PyObject* filename_o = NULL;
159159
PyObject* line_o = NULL;
160-
PyObject* funcname_o = NULL;
161-
PyObject* classname_o = NULL;
162160

163161
filename_o = GET_FILENAME(frame);
164162
if (!filename_o) {
@@ -227,6 +225,9 @@ get_file_and_line(PyObject* Py_UNUSED(module), PyObject* Py_UNUSED(args))
227225

228226
exit:
229227
FRAME_XDECREF(frame);
228+
if (!result) {
229+
result = PyTuple_Pack(2, Py_None, Py_None);
230+
}
230231
return result;
231232
}
232233

ddtrace/appsec/_iast/taint_sinks/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def report(cls, evidence_value: Text = "", dialect: Optional[Text] = None) -> No
129129
return None
130130

131131
file_name, line_number = frame_info
132+
if not file_name:
133+
return
132134

133135
file_name = cls._rel_path(file_name)
134136
if not file_name:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
import pytest
6+
7+
from ddtrace.appsec._iast._stacktrace import get_info_frame
8+
9+
10+
def test_stacktrace():
11+
file, line = get_info_frame()
12+
import traceback
13+
14+
traceback.print_stack()
15+
assert file is not None
16+
assert file.endswith("test_stacktrace.py")
17+
assert line is not None
18+
19+
20+
async def test_stacktrace_async():
21+
async def _inner():
22+
return get_info_frame()
23+
24+
file, line = await _inner()
25+
assert file is not None
26+
assert file.endswith("test_stacktrace.py")
27+
assert line is not None
28+
29+
30+
@pytest.mark.skipif(sys.version_info < (3, 9, 0), reason="Test compatible with Python 3.9+")
31+
async def test_stacktrace_async_no_relevant_frame():
32+
"""
33+
In the absence of any non-ddtrace and non-stdlib frame in the stacktrace, no frame is returned.
34+
(And no exception is raised).
35+
"""
36+
import asyncio
37+
38+
file, line = await asyncio.to_thread(get_info_frame)
39+
assert file is None
40+
assert line is None

0 commit comments

Comments
 (0)