Skip to content

Commit 639751b

Browse files
fix: dont raise a ValueError on set_user when no span [backport #5548 to 1.10] (#5566)
## Checklist - [X] Change(s) are motivated and described in the PR description. - [X] Testing strategy is described if automated tests are not included in the PR. - [X] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [X] Change is maintainable (easy to change, telemetry, documentation). - [X] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [X] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [X] PR description includes explicit acknowledgement/acceptance of the performance implications of this PR as reported in the benchmarks PR comment. ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. --------- Signed-off-by: Juanjo Alvarez <[email protected]> Co-authored-by: Emmett Butler <[email protected]>
1 parent 87ef06f commit 639751b

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

ddtrace/appsec/trace_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ def should_block_user(tracer, userid): # type: (Tracer, str) -> bool
146146

147147
# Early check to avoid calling the WAF if the request is already blocked
148148
span = tracer.current_root_span()
149+
if not span:
150+
log.warning(
151+
"No root span in the current execution. should_block_user returning False"
152+
"See https://docs.datadoghq.com/security_platform/application_security"
153+
"/setup_and_configure/"
154+
"?tab=set_user&code-lang=python for more information.",
155+
)
156+
return False
157+
149158
if _context.get_item(WAF_CONTEXT_NAMES.BLOCKED, span=span):
150159
return True
151160

ddtrace/contrib/trace_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,6 @@ def set_user(tracer, user_id, name=None, email=None, scope=None, role=None, sess
612612
https://docs.datadoghq.com/security_platform/application_security/setup_and_configure/?tab=set_tag&code-lang=python
613613
"""
614614

615-
if config._appsec_enabled:
616-
from ddtrace.appsec.trace_utils import block_request_if_user_blocked
617-
618-
block_request_if_user_blocked(tracer, user_id)
619-
620615
span = tracer.current_root_span()
621616
if span:
622617
# Required unique identifier of the user
@@ -636,6 +631,11 @@ def set_user(tracer, user_id, name=None, email=None, scope=None, role=None, sess
636631
span.set_tag_str(user.ROLE, role)
637632
if session_id:
638633
span.set_tag_str(user.SESSION_ID, session_id)
634+
635+
if config._appsec_enabled:
636+
from ddtrace.appsec.trace_utils import block_request_if_user_blocked
637+
638+
block_request_if_user_blocked(tracer, user_id)
639639
else:
640640
log.warning(
641641
"No root span in the current execution. Skipping set_user tags. "
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
ASM: fix calling `set_user` without a created span raising a `ValueError`.

0 commit comments

Comments
 (0)