Skip to content

Commit 2662f8e

Browse files
fix(asm): make sure iast is not loaded by exploit prevention if disabled [backport 2.19] (#12352)
backport #12198 to 2.19 Make sure, if iast is disabled, that we don't load any iast modules in the common module mechanism used both by iast and exploit prevention. APPSEC-56659 Co-authored-by: Ramy Elkest <[email protected]> (cherry picked from commit 362fa22) ## 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) --------- Co-authored-by: Christophe Papazian <[email protected]>
1 parent 8eee423 commit 2662f8e

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

ddtrace/appsec/_common_module_patches.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# This module must not import other modules inconditionnaly that
2-
# require iast, ddwaf or any native optional module.
1+
# This module must not import other modules unconditionally that require iast
32

43
import ctypes
54
import os
@@ -14,8 +13,6 @@
1413
import ddtrace
1514
from ddtrace.appsec._asm_request_context import get_blocked
1615
from ddtrace.appsec._constants import WAF_ACTIONS
17-
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
18-
from ddtrace.appsec._iast.constants import VULN_PATH_TRAVERSAL
1916
from ddtrace.internal import core
2017
from ddtrace.internal._exceptions import BlockingException
2118
from ddtrace.internal._unpatched import _gc as gc
@@ -24,6 +21,14 @@
2421
from ddtrace.settings.asm import config as asm_config
2522

2623

24+
if asm_config._iast_enabled:
25+
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
26+
else:
27+
28+
def is_iast_request_enabled() -> bool:
29+
return False
30+
31+
2732
log = get_logger(__name__)
2833
_DD_ORIGINAL_ATTRIBUTES: Dict[Any, Any] = {}
2934

@@ -34,13 +39,26 @@ def patch_common_modules():
3439
global _is_patched
3540
if _is_patched:
3641
return
42+
# for testing purposes, we need to update is_iast_request_enabled
43+
if asm_config._iast_enabled:
44+
global is_iast_request_enabled
45+
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
46+
else:
47+
global is_iast_request_enabled
48+
49+
def is_iast_request_enabled() -> bool:
50+
return False
51+
3752
try_wrap_function_wrapper("builtins", "open", wrapped_open_CFDDB7ABBA9081B6)
3853
try_wrap_function_wrapper("urllib.request", "OpenerDirector.open", wrapped_open_ED4CF71136E15EBF)
3954
try_wrap_function_wrapper("_io", "BytesIO.read", wrapped_read_F3E51D71B4EC16EF)
4055
try_wrap_function_wrapper("_io", "StringIO.read", wrapped_read_F3E51D71B4EC16EF)
4156
try_wrap_function_wrapper("os", "system", wrapped_system_5542593D237084A7)
4257
core.on("asm.block.dbapi.execute", execute_4C9BAC8E228EB347)
4358
if asm_config._iast_enabled:
59+
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
60+
from ddtrace.appsec._iast.constants import VULN_PATH_TRAVERSAL
61+
4462
_set_metric_iast_instrumented_sink(VULN_PATH_TRAVERSAL)
4563
_is_patched = True
4664

ddtrace/appsec/_iast/_iast_request_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def set_iast_request_enabled(request_enabled) -> None:
113113
log.debug("[IAST] Trying to set IAST reporter but no context is present")
114114

115115

116-
def is_iast_request_enabled():
116+
def is_iast_request_enabled() -> bool:
117117
env = _get_iast_context()
118118
if env:
119119
return env.request_enabled

ddtrace/appsec/_iast/_patch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from wrapt import FunctionWrapper
66

7-
from ddtrace.appsec._common_module_patches import wrap_object
87
from ddtrace.internal.logger import get_logger
98

109
from ._taint_utils import taint_structure
@@ -35,6 +34,8 @@ def set_module_unpatched(module_str: Text, default_attr: Text = "_datadog_patch"
3534

3635

3736
def try_wrap_function_wrapper(module: Text, name: Text, wrapper: Callable):
37+
from ddtrace.appsec._common_module_patches import wrap_object
38+
3839
try:
3940
wrap_object(module, name, FunctionWrapper, (wrapper,))
4041
except (ImportError, AttributeError):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
ASM: This fix resolves an issue where IAST modules could be loaded, even if disabled,
5+
which could create an ImportError exception on Windows.

0 commit comments

Comments
 (0)