Skip to content

Commit

Permalink
Merge branch '3.x-staging' into yunkim/langchain-flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Kim authored Feb 3, 2025
2 parents aec75ba + 362fa22 commit 23cfc9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
27 changes: 22 additions & 5 deletions ddtrace/appsec/_common_module_patches.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# This module must not import other modules inconditionnaly that
# require iast, ddwaf or any native optional module.
# This module must not import other modules unconditionally that require iast

import ctypes
import os
Expand All @@ -17,9 +16,6 @@
from ddtrace.appsec._asm_request_context import get_blocked
from ddtrace.appsec._constants import EXPLOIT_PREVENTION
from ddtrace.appsec._constants import WAF_ACTIONS
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
from ddtrace.appsec._iast.constants import VULN_PATH_TRAVERSAL
import ddtrace.contrib.internal.subprocess.patch as subprocess_patch
from ddtrace.internal import core
from ddtrace.internal._exceptions import BlockingException
Expand All @@ -29,6 +25,14 @@
from ddtrace.settings.asm import config as asm_config


if asm_config._iast_enabled:
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
else:

def is_iast_request_enabled() -> bool:
return False


log = get_logger(__name__)
_DD_ORIGINAL_ATTRIBUTES: Dict[Any, Any] = {}

Expand All @@ -42,6 +46,16 @@ def patch_common_modules():
global _is_patched
if _is_patched:
return
# for testing purposes, we need to update is_iast_request_enabled
if asm_config._iast_enabled:
global is_iast_request_enabled
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
else:
global is_iast_request_enabled

def is_iast_request_enabled() -> bool:
return False

try_wrap_function_wrapper("builtins", "open", wrapped_open_CFDDB7ABBA9081B6)
try_wrap_function_wrapper("urllib.request", "OpenerDirector.open", wrapped_open_ED4CF71136E15EBF)
try_wrap_function_wrapper("_io", "BytesIO.read", wrapped_read_F3E51D71B4EC16EF)
Expand All @@ -52,6 +66,9 @@ def patch_common_modules():
subprocess_patch.add_lst_callback(_RASP_POPEN, popen_FD233052260D8B4D)
core.on("asm.block.dbapi.execute", execute_4C9BAC8E228EB347)
if asm_config._iast_enabled:
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
from ddtrace.appsec._iast.constants import VULN_PATH_TRAVERSAL

_set_metric_iast_instrumented_sink(VULN_PATH_TRAVERSAL)
_is_patched = True

Expand Down
2 changes: 1 addition & 1 deletion ddtrace/appsec/_iast/_iast_request_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def set_iast_request_enabled(request_enabled) -> None:
log.debug("[IAST] Trying to set IAST reporter but no context is present")


def is_iast_request_enabled():
def is_iast_request_enabled() -> bool:
env = _get_iast_context()
if env:
return env.request_enabled
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
ASM: This fix resolves an issue where IAST modules could be loaded, even if disabled,
which could create an ImportError exception on Windows.

0 comments on commit 23cfc9b

Please sign in to comment.