|
1 | 1 | from typing import TYPE_CHECKING |
2 | 2 |
|
3 | 3 | from ddtrace import tracer |
| 4 | +from ddtrace.appsec.iast import oce |
| 5 | +from ddtrace.appsec.iast.overhead_control_engine import Operation |
| 6 | +from ddtrace.appsec.iast.reporter import Evidence |
| 7 | +from ddtrace.appsec.iast.reporter import IastSpanReporter |
| 8 | +from ddtrace.appsec.iast.reporter import Location |
| 9 | +from ddtrace.appsec.iast.reporter import Vulnerability |
| 10 | +from ddtrace.appsec.iast.stacktrace import get_info_frame |
| 11 | +from ddtrace.constants import IAST_CONTEXT_KEY |
| 12 | +from ddtrace.internal import _context |
4 | 13 | from ddtrace.internal.logger import get_logger |
| 14 | +from ddtrace.vendor.wrapt import wrap_function_wrapper |
5 | 15 |
|
6 | 16 |
|
7 | 17 | if TYPE_CHECKING: # pragma: no cover |
8 | 18 | from typing import Any |
9 | 19 | from typing import Callable |
| 20 | + from typing import Text |
10 | 21 |
|
11 | 22 | log = get_logger(__name__) |
12 | 23 |
|
13 | 24 |
|
14 | | -def inject_span(func): |
15 | | - # type: (Callable) -> Callable |
16 | | - """Get the current root Span and attach it to the wrapped function. We need the span to report the vulnerability |
17 | | - and update the context with the report information. |
18 | | - """ |
| 25 | +class VulnerabilityBase(Operation): |
| 26 | + vulnerability_type = "" |
| 27 | + evidence_type = "" |
19 | 28 |
|
20 | | - def wrapper(wrapped, instance, args, kwargs): |
21 | | - # type: (Callable, Any, Any, Any) -> Any |
22 | | - span = tracer.current_root_span() |
23 | | - if span: |
24 | | - return func(wrapped, span, instance, args, kwargs) |
25 | | - log.warning("No root span in the current execution. Skipping IAST Taint sink.") |
26 | | - return wrapped(*args, **kwargs) |
| 29 | + @classmethod |
| 30 | + def wrap(cls, func): |
| 31 | + # type: (Callable) -> Callable |
| 32 | + def wrapper(wrapped, instance, args, kwargs): |
| 33 | + # type: (Callable, Any, Any, Any) -> Any |
| 34 | + """Get the current root Span and attach it to the wrapped function. We need the span to report the vulnerability |
| 35 | + and update the context with the report information. |
| 36 | + """ |
| 37 | + if oce.request_has_quota and cls.has_quota(): |
| 38 | + return func(wrapped, instance, args, kwargs) |
| 39 | + else: |
| 40 | + log.debug("IAST: no vulnerability quota to analyze more sink points") |
| 41 | + return wrapped(*args, **kwargs) |
27 | 42 |
|
28 | | - return wrapper |
| 43 | + return wrapper |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def report(cls, evidence_value=""): |
| 47 | + # type: (Text) -> None |
| 48 | + """Build a IastSpanReporter instance to report it in the `AppSecIastSpanProcessor` as a string JSON |
| 49 | +
|
| 50 | + TODO: check deduplications if DD_IAST_DEDUPLICATION_ENABLED is true |
| 51 | + """ |
| 52 | + if cls.acquire_quota(): |
| 53 | + span = tracer.current_root_span() |
| 54 | + if not span: |
| 55 | + log.debug("No root span in the current execution. Skipping IAST taint sink.") |
| 56 | + return None |
| 57 | + |
| 58 | + report = _context.get_item(IAST_CONTEXT_KEY, span=span) |
| 59 | + file_name, line_number = get_info_frame() |
| 60 | + if report: |
| 61 | + report.vulnerabilities.add( |
| 62 | + Vulnerability( |
| 63 | + type=cls.vulnerability_type, |
| 64 | + evidence=Evidence(type=cls.evidence_type, value=evidence_value), |
| 65 | + location=Location(path=file_name, line=line_number), |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + else: |
| 70 | + report = IastSpanReporter( |
| 71 | + vulnerabilities={ |
| 72 | + Vulnerability( |
| 73 | + type=cls.vulnerability_type, |
| 74 | + evidence=Evidence(type=cls.evidence_type, value=evidence_value), |
| 75 | + location=Location(path=file_name, line=line_number), |
| 76 | + ) |
| 77 | + } |
| 78 | + ) |
| 79 | + _context.set_item(IAST_CONTEXT_KEY, report, span=span) |
| 80 | + |
| 81 | + |
| 82 | +def _wrap_function_wrapper_exception(module, name, wrapper): |
| 83 | + try: |
| 84 | + wrap_function_wrapper(module, name, wrapper) |
| 85 | + except (ImportError, AttributeError): |
| 86 | + log.debug("IAST patching. Module %s.%s not exists", module, name) |
0 commit comments