Skip to content

Commit fcff223

Browse files
authored
fix(iast): handle value error thrown by numpy [backport 2.5] (#8164)
Backport b8ca6e9 from #8088 to 2.5. IAST: Fixes an issue where a ValueError is thrown by numpy while checking the existence of an array. Regression test added. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] 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) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that.
1 parent 2901112 commit fcff223

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

ddtrace/appsec/_iast/taint_sinks/ast_taint.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@ def ast_function(
2020
*args, # type: Any
2121
**kwargs, # type: Any
2222
): # type: (...) -> Any
23-
cls = getattr(func, "__self__", None)
23+
instance = getattr(func, "__self__", None)
2424
func_name = getattr(func, "__name__", None)
2525
cls_name = ""
26-
if cls and func_name:
26+
if instance is not None and func_name:
2727
try:
28-
cls_name = cls.__class__.__name__
28+
cls_name = instance.__class__.__name__
2929
except AttributeError:
3030
pass
3131

3232
if flag_added_args > 0:
3333
args = args[flag_added_args:]
3434

35-
if cls.__class__.__module__ == "random" and cls_name == "Random" and func_name in DEFAULT_WEAK_RANDOMNESS_FUNCTIONS:
35+
if (
36+
instance.__class__.__module__ == "random"
37+
and cls_name == "Random"
38+
and func_name in DEFAULT_WEAK_RANDOMNESS_FUNCTIONS
39+
):
3640
# Weak, run the analyzer
3741
increment_iast_span_metric(IAST_SPAN_TAGS.TELEMETRY_EXECUTED_SINK, WeakRandomness.vulnerability_type)
3842
_set_metric_iast_executed_sink(WeakRandomness.vulnerability_type)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Vulnerability Management for Code-level (IAST): Fix an unhandled ValueError in ``ast_function`` thrown in some cases (i.e. Numpy arrays when converted to bool).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
3+
from ddtrace.appsec._iast.taint_sinks.ast_taint import ast_function
4+
5+
6+
class MyArray:
7+
def __init__(self, values):
8+
self.values = values
9+
10+
def copy(self):
11+
return self.values.copy()
12+
13+
def __bool__(self):
14+
if len(self.values) > 0:
15+
raise ValueError("Array is not empty")
16+
return False
17+
18+
19+
def test_ast_function_with_valueerror_on_bool():
20+
values = MyArray([7, 19, 20, 35, 10, 42, 8])
21+
values_copy = ast_function(values.copy, False)
22+
assert values_copy == values.values

0 commit comments

Comments
 (0)