1- import contextlib
2- import contextvars
31from typing import Optional
42
53from ddtrace .appsec ._constants import IAST
64from ddtrace .appsec ._constants import IAST_SPAN_TAGS
75from ddtrace .appsec ._iast ._iast_env import IASTEnvironment
86from ddtrace .appsec ._iast ._iast_env import _get_iast_env
97from ddtrace .appsec ._iast ._overhead_control_engine import oce
10- from ddtrace .appsec ._iast ._taint_tracking . _context import debug_num_tainted_objects
8+ from ddtrace .appsec ._iast ._request_taint_state import _num_objects_tainted_in_request
119from ddtrace .appsec ._iast ._taint_tracking ._context import finish_request_context
1210from ddtrace .appsec ._iast ._taint_tracking ._context import start_request_context
1311from ddtrace .appsec ._iast .sampling .vulnerability_detection import update_global_vulnerability_limit
12+ from ddtrace .appsec ._iast_request_state import IAST_CONTEXT
13+ from ddtrace .appsec ._iast_request_state import _get_iast_context_id
14+ from ddtrace .appsec ._iast_request_state import iast_suppress_context as iast_suppress_context # noqa: F401
15+ from ddtrace .appsec ._iast_request_state import is_iast_request_enabled as is_iast_request_enabled
1416from ddtrace .internal import core
1517from ddtrace .internal .logger import get_logger
1618from ddtrace .internal .settings .asm import config as asm_config
1719
1820
1921log = get_logger (__name__ )
2022
21- # Stopgap module for providing ASM context for the blocking features wrapping some contextvars.
22-
23- IAST_CONTEXT : contextvars .ContextVar [Optional [int ]] = contextvars .ContextVar ("iast_var" , default = None )
24-
25- # Keep source suppression separate from IAST_CONTEXT. Clearing the
26- # request context id disables request-scoped taint queries and propagation and
27- # can send no-context queries through unsafe native fallback paths.
28- _IAST_TAINT_SOURCES_SUPPRESSED : contextvars .ContextVar [bool ] = contextvars .ContextVar (
29- "iast_taint_sources_suppressed" , default = False
30- )
31-
32-
33- @contextlib .contextmanager
34- def iast_suppress_context ():
35- """Temporarily disable IAST taint *source* generation for the current context."""
36- token = _IAST_TAINT_SOURCES_SUPPRESSED .set (True )
37- try :
38- yield
39- finally :
40- _IAST_TAINT_SOURCES_SUPPRESSED .reset (token )
41-
42-
43- def _is_iast_taint_source_enabled () -> bool :
44- return not _IAST_TAINT_SOURCES_SUPPRESSED .get ()
45-
4623
4724def _set_span_tag_iast_request_tainted (span ):
4825 total_objects_tainted = _num_objects_tainted_in_request ()
@@ -102,11 +79,6 @@ def _iast_start_request(span=None) -> Optional[int]:
10279 return context_id
10380
10481
105- def _get_iast_context_id () -> Optional [int ]:
106- """Retrieve the current IAST context identifier from the ContextVar."""
107- return IAST_CONTEXT .get ()
108-
109-
11082def _iast_finish_request (span = None , shoud_update_global_vulnerability_limit : bool = True ) -> bool :
11183 """Finalize the IAST request context and optionally update global limits.
11284
@@ -129,22 +101,6 @@ def _iast_finish_request(span=None, shoud_update_global_vulnerability_limit: boo
129101 return False
130102
131103
132- def is_iast_request_enabled () -> bool :
133- """Check whether IAST is currently operating within an active request context."""
134- return _get_iast_context_id () is not None
135-
136-
137- def _num_objects_tainted_in_request () -> int :
138- """Get the count of tainted objects tracked in the active IAST request context.
139-
140- Useful for span metrics and internal telemetry.
141- """
142- context_id = _get_iast_context_id ()
143- if context_id is not None :
144- return debug_num_tainted_objects (context_id )
145- return 0
146-
147-
148104def get_hash_object_tracking_len ():
149105 env = _get_iast_env ()
150106 if env :
0 commit comments