Skip to content

Commit

Permalink
Merge branch 'main' into zachg/deprecate_patch_all
Browse files Browse the repository at this point in the history
  • Loading branch information
ZStriker19 authored Feb 7, 2025
2 parents 4aef9d3 + 122caa6 commit 470f077
Show file tree
Hide file tree
Showing 78 changed files with 468 additions and 2,075 deletions.
23 changes: 0 additions & 23 deletions .riot/requirements/1761702.txt

This file was deleted.

25 changes: 0 additions & 25 deletions .riot/requirements/1cc7b0e.txt

This file was deleted.

25 changes: 0 additions & 25 deletions .riot/requirements/1f1e9b4.txt

This file was deleted.

25 changes: 0 additions & 25 deletions .riot/requirements/9b8251b.txt

This file was deleted.

25 changes: 0 additions & 25 deletions .riot/requirements/fda8aa6.txt

This file was deleted.

1 change: 0 additions & 1 deletion ddtrace/_monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"pyodbc": True,
"fastapi": True,
"dogpile_cache": True,
"yaaredis": True,
"asyncpg": True,
"aws_lambda": True, # patch only in AWS Lambda environments
"azure_functions": True,
Expand Down
7 changes: 5 additions & 2 deletions ddtrace/appsec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# this module must not load any other unsafe appsec module directly

from ddtrace.internal import core
from ddtrace.settings.asm import config as asm_config


_APPSEC_TO_BE_LOADED = True
Expand Down Expand Up @@ -28,7 +29,9 @@ def load_iast():

def load_common_appsec_modules():
"""Lazily load the common module patches."""
if (asm_config._ep_enabled and asm_config._asm_enabled) or asm_config._iast_enabled:
from ddtrace.settings.asm import config as asm_config

if asm_config._load_modules:
from ddtrace.appsec._common_module_patches import patch_common_modules

patch_common_modules()
Expand Down
13 changes: 10 additions & 3 deletions ddtrace/appsec/_asm_request_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
from ddtrace.appsec._constants import APPSEC
from ddtrace.appsec._constants import EXPLOIT_PREVENTION
from ddtrace.appsec._constants import SPAN_DATA_NAMES
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
from ddtrace.appsec._iast._taint_tracking import OriginType
from ddtrace.appsec._iast._taint_tracking._taint_objects import taint_pyobject
from ddtrace.appsec._utils import add_context_log
from ddtrace.appsec._utils import get_triggers
from ddtrace.internal import core
Expand All @@ -28,6 +25,16 @@
from ddtrace.trace import Span


if asm_config._iast_enabled:
from ddtrace.appsec._iast._iast_request_context import is_iast_request_enabled
from ddtrace.appsec._iast._taint_tracking import OriginType
from ddtrace.appsec._iast._taint_tracking._taint_objects import taint_pyobject
else:

def is_iast_request_enabled() -> bool:
return False


if TYPE_CHECKING:
from ddtrace.appsec._ddwaf import DDWaf_info
from ddtrace.appsec._ddwaf import DDWaf_result
Expand Down
8 changes: 4 additions & 4 deletions ddtrace/appsec/_common_module_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def is_iast_request_enabled() -> bool:

def patch_common_modules():
global _is_patched
# ensure that the subprocess patch is applied even after one click activation
subprocess_patch.patch()
subprocess_patch.add_str_callback(_RASP_SYSTEM, wrapped_system_5542593D237084A7)
subprocess_patch.add_lst_callback(_RASP_POPEN, popen_FD233052260D8B4D)
if _is_patched:
return
# for testing purposes, we need to update is_iast_request_enabled
Expand All @@ -60,10 +64,6 @@ def is_iast_request_enabled() -> bool:
try_wrap_function_wrapper("urllib.request", "OpenerDirector.open", wrapped_open_ED4CF71136E15EBF)
try_wrap_function_wrapper("_io", "BytesIO.read", wrapped_read_F3E51D71B4EC16EF)
try_wrap_function_wrapper("_io", "StringIO.read", wrapped_read_F3E51D71B4EC16EF)
# ensure that the subprocess patch is applied even after one click activation
subprocess_patch.patch()
subprocess_patch.add_str_callback(_RASP_SYSTEM, wrapped_system_5542593D237084A7)
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
Expand Down
2 changes: 2 additions & 0 deletions ddtrace/appsec/_constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# this module must not load any other unsafe appsec module directly

import os
from re import Match
import sys
Expand Down
10 changes: 9 additions & 1 deletion ddtrace/appsec/_iast/taint_sinks/xss.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ def patch():
)

_set_metric_iast_instrumented_sink(VULN_XSS)
_set_metric_iast_instrumented_sink(VULN_XSS)
# Even when starting the application with `ddtrace-run ddtrace-run`, `jinja2.FILTERS` is created before this patch
# function executes. Therefore, we update the in-memory object with the newly patched version.
try:
from jinja2.filters import FILTERS
from jinja2.filters import do_mark_safe

FILTERS["safe"] = do_mark_safe
except (ImportError, KeyError):
pass


def unpatch():
Expand Down
4 changes: 3 additions & 1 deletion ddtrace/appsec/_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# this module must not load any other unsafe appsec module directly

import logging
import sys
from typing import Any
import uuid

from ddtrace.appsec._constants import API_SECURITY
from ddtrace.appsec._constants import APPSEC
from ddtrace.appsec._constants import SPAN_DATA_NAMES
from ddtrace.internal._unpatched import unpatched_json_loads
from ddtrace.internal.compat import to_unicode
from ddtrace.internal.logger import get_logger
Expand All @@ -21,7 +24,6 @@ def parse_response_body(raw_body):
import xmltodict

from ddtrace.appsec import _asm_request_context
from ddtrace.appsec._constants import SPAN_DATA_NAMES
from ddtrace.contrib.internal.trace_utils import _get_header_value_case_insensitive

if not raw_body:
Expand Down
67 changes: 0 additions & 67 deletions ddtrace/contrib/_yaaredis.py

This file was deleted.

5 changes: 3 additions & 2 deletions ddtrace/contrib/internal/httplib/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import wrapt

from ddtrace import config
from ddtrace.appsec._common_module_patches import wrapped_request_D8CB81E472AF98A2 as _wrap_request_asm
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.constants import SPAN_KIND
from ddtrace.contrib import trace_utils
Expand Down Expand Up @@ -77,12 +76,14 @@ def _wrap_getresponse(func, instance, args, kwargs):


def _call_asm_wrap(func, instance, *args, **kwargs):
from ddtrace.appsec._common_module_patches import wrapped_request_D8CB81E472AF98A2 as _wrap_request_asm

_wrap_request_asm(func, instance, args, kwargs)


def _wrap_request(func, instance, args, kwargs):
# Use any attached tracer if available, otherwise use the global tracer
if asm_config._iast_enabled or asm_config._asm_enabled:
if asm_config._iast_enabled or (asm_config._asm_enabled and asm_config._ep_enabled):
func_to_call = functools.partial(_call_asm_wrap, func, instance)
else:
func_to_call = func
Expand Down
23 changes: 0 additions & 23 deletions ddtrace/contrib/internal/langchain/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,29 +1090,6 @@ def unpatch():
delattr(langchain, "_datadog_integration")


def taint_outputs(instance, inputs, outputs):
from ddtrace.appsec._iast._metrics import _set_iast_error_metric
from ddtrace.appsec._iast._taint_tracking._taint_objects import get_tainted_ranges
from ddtrace.appsec._iast._taint_tracking._taint_objects import taint_pyobject

try:
ranges = None
for key in filter(lambda x: x in inputs, instance.input_keys):
input_val = inputs.get(key)
if input_val:
ranges = get_tainted_ranges(input_val)
if ranges:
break

if ranges:
source = ranges[0].source
for key in filter(lambda x: x in outputs, instance.output_keys):
output_value = outputs[key]
outputs[key] = taint_pyobject(output_value, source.name, source.value, source.origin)
except Exception as e:
_set_iast_error_metric("IAST propagation error. langchain taint_outputs. {}".format(e))


def taint_parser_output(func, instance, args, kwargs):
from ddtrace.appsec._iast._metrics import _set_iast_error_metric
from ddtrace.appsec._iast._taint_tracking._taint_objects import get_tainted_ranges
Expand Down
5 changes: 3 additions & 2 deletions ddtrace/contrib/internal/mysql/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import wrapt

from ddtrace import config
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
from ddtrace.appsec._iast.constants import VULN_SQL_INJECTION
from ddtrace.contrib.dbapi import TracedConnection
from ddtrace.contrib.internal.trace_utils import _convert_to_string
from ddtrace.ext import db
Expand Down Expand Up @@ -51,6 +49,9 @@ def patch():
mysql.connector.Connect = mysql.connector.connect

if asm_config._iast_enabled:
from ddtrace.appsec._iast._metrics import _set_metric_iast_instrumented_sink
from ddtrace.appsec._iast.constants import VULN_SQL_INJECTION

_set_metric_iast_instrumented_sink(VULN_SQL_INJECTION)
mysql.connector._datadog_patch = True

Expand Down
Loading

0 comments on commit 470f077

Please sign in to comment.