Skip to content

Commit 13b5f77

Browse files
fix(asm): make sure iast is not loaded by exploit prevention if disabled [backport 2.20] (#12271)
Co-authored-by: Christophe Papazian <[email protected]>
1 parent f216948 commit 13b5f77

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

ddtrace/appsec/_common_module_patches.py

Lines changed: 22 additions & 5 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
@@ -17,9 +16,6 @@
1716
from ddtrace.appsec._asm_request_context import get_blocked
1817
from ddtrace.appsec._constants import EXPLOIT_PREVENTION
1918
from ddtrace.appsec._constants import WAF_ACTIONS
20-
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
21-
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
22-
from ddtrace.appsec._iast.constants import VULN_PATH_TRAVERSAL
2319
import ddtrace.contrib.internal.subprocess.patch as subprocess_patch
2420
from ddtrace.internal import core
2521
from ddtrace.internal._exceptions import BlockingException
@@ -29,6 +25,14 @@
2925
from ddtrace.settings.asm import config as asm_config
3026

3127

28+
if asm_config._iast_enabled:
29+
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
30+
else:
31+
32+
def is_iast_request_enabled() -> bool:
33+
return False
34+
35+
3236
log = get_logger(__name__)
3337
_DD_ORIGINAL_ATTRIBUTES: Dict[Any, Any] = {}
3438

@@ -42,6 +46,16 @@ def patch_common_modules():
4246
global _is_patched
4347
if _is_patched:
4448
return
49+
# for testing purposes, we need to update is_iast_request_enabled
50+
if asm_config._iast_enabled:
51+
global is_iast_request_enabled
52+
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
53+
else:
54+
global is_iast_request_enabled
55+
56+
def is_iast_request_enabled() -> bool:
57+
return False
58+
4559
try_wrap_function_wrapper("builtins", "open", wrapped_open_CFDDB7ABBA9081B6)
4660
try_wrap_function_wrapper("urllib.request", "OpenerDirector.open", wrapped_open_ED4CF71136E15EBF)
4761
try_wrap_function_wrapper("_io", "BytesIO.read", wrapped_read_F3E51D71B4EC16EF)
@@ -52,6 +66,9 @@ def patch_common_modules():
5266
subprocess_patch.add_lst_callback(_RASP_POPEN, popen_FD233052260D8B4D)
5367
core.on("asm.block.dbapi.execute", execute_4C9BAC8E228EB347)
5468
if asm_config._iast_enabled:
69+
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
70+
from ddtrace.appsec._iast.constants import VULN_PATH_TRAVERSAL
71+
5572
_set_metric_iast_instrumented_sink(VULN_PATH_TRAVERSAL)
5673
_is_patched = True
5774

ddtrace/appsec/_iast/_iast_request_context.py

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

106106

107-
def is_iast_request_enabled():
107+
def is_iast_request_enabled() -> bool:
108108
env = _get_iast_context()
109109
if env:
110110
return env.request_enabled
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)