Skip to content

Commit 982e953

Browse files
authored
chore(appsec): skip user block when auto mode resolves to disabled (#17580)
## Summary `block_request_if_user_blocked` has a guard-ordering bug: it checks `mode == DISABLED` **before** resolving `AUTO` to the configured mode. When called with `mode="auto"` (via `set_user_for_asm`) and `DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE=disabled`, the guard reads `"auto" == "disabled"` → False and proceeds, calling `should_block_user` unnecessarily. That WAF call returns `keep=True` and causes the tracer to force-keep the trace via `_asm_manual_keep` — even though no automated user event was generated. Fix: resolve `AUTO` to the configured mode first, then apply the disabled guard. ```python # Before (buggy) if not asm_config._asm_enabled or mode == LOGIN_EVENTS_MODE.DISABLED: return if mode == LOGIN_EVENTS_MODE.AUTO: mode = asm_config._user_event_mode # too late — guard already passed # After if mode == LOGIN_EVENTS_MODE.AUTO: mode = asm_config._user_event_mode # resolve first if not asm_config._asm_enabled or mode == LOGIN_EVENTS_MODE.DISABLED: return # now correctly catches disabled ``` ## How the bug was found While writing end-to-end tests for the `APPSEC_AUTO_EVENTS_TRACKING=disabled` scenario in [system-tests](DataDog/system-tests#6750), login-success traces were unexpectedly force-kept (`_sampling_priority_v1=2`, `_dd.p.dm=-5`) even though no user event tags were emitted. Debug tracing confirmed `_asm_manual_keep` was called from `_processor.py:399` via `should_block_user` → `call_waf_callback(usr.id=...)`, bypassing the disabled guard due to the ordering bug described above. ## Release notes > **Note:** Release notes still needed before this can be merged. Co-authored-by: romain.marcadier <romain.marcadier@datadoghq.com>
1 parent 80d6ce3 commit 982e953

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

ddtrace/appsec/_trace_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,14 @@ def block_request_if_user_blocked(userid: str, mode: str = "sdk", session_id: Op
360360
:param userid: the ID of the user as registered by `set_user`
361361
:param mode: the mode of the login event ("sdk" by default, "auto" to simulate auto instrumentation)
362362
"""
363-
if not asm_config._asm_enabled or mode == LOGIN_EVENTS_MODE.DISABLED:
364-
log.warning("should_block_user call requires ASM to be enabled")
363+
if not asm_config._asm_enabled:
364+
if mode != LOGIN_EVENTS_MODE.AUTO:
365+
log.warning("should_block_user call requires ASM to be enabled")
365366
return
366367
if mode == LOGIN_EVENTS_MODE.AUTO:
367368
mode = asm_config._user_event_mode
369+
if mode == LOGIN_EVENTS_MODE.DISABLED:
370+
return
368371
entry_span = _asm_request_context.get_entry_span()
369372
if entry_span:
370373
entry_span._set_attribute(APPSEC.AUTO_LOGIN_EVENTS_COLLECTION_MODE, mode)

0 commit comments

Comments
 (0)