chore(appsec): fix unwrap after re-patching a class method DD_BPPZV3 DD_RXF8ZI DD_MQNBM0 DD_2UATV7 #22032
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #22032"
general
weight: 0.35
avara1986
Description
Related to https://datadoghq.atlassian.net/browse/APPSEC-71951
apply_patchinddtrace/internal/security/_patch_utils.py(shared by IAST and AAP) decides what to save as the original before wrapping, and had two flaws.It read the current value with
getattr.getattrgoes throughFunctionWrapper.__get__(None, cls)and returns aBoundFunctionWrapper. That isn't aFunctionWrapper, so a second patch of the same method replaced the saved original in_DD_ORIGINAL_ATTRIBUTESwith the wrapper.try_unwrapthen put that wrapper back. The hook kept running and the method could never be unwrapped for the rest of the process.resolve_path, which returns the raw class__dict__value.It treated any
FunctionWrapperas one of ours.setdefault, so a wrapper installed elsewhere is saved and restored on unwrap.The same-hook check now runs first: a replacement using the same hook as the current value is skipped, whether or not we recorded that value. This also covers a value inherited from a base class that was patched first.
try_unwrapsets the saved original directly instead of going back throughapply_patch, so a restore can't be skipped by that check; thepatch_builtinsfallback for immutable types is kept.Behavior changes:
cls. A staticmethod came back as a plain function, so calling it on an instance raisedTypeError.This helper was the underlying cause of the
test_weak_cipher_rc4_unpatchedflakes (py3.9 / 3.10 / 3.12 / 3.13):ARC4Cipher.encrypt, and every later unpatch on that xdist worker restored it.test_replace_aspect.pythroughtest_weak_cipher.pywithout work-stealing, which is why py3.9 was hit most.Customer impact
None. Production paths don't patch twice (IAST sinks skip a repeat patch outside testing, and AAP common modules have an
_is_patchedguard), and nothing outside tests unpatches.Testing
tests/internal/test_security_patch_utils.py(8 tests):- patches a method, a classmethod, a staticmethod and a module function twice, then unwraps each and checks that the original is restored, its entry is gone and the hook no longer runs;
- patches a method with two different hooks;
- patches a method and a module function that already hold a wrapper installed elsewhere;
- patches a base class and its subclass with the same hook.
test_weak_cipher_rc4_unpatchedfail on main with the same assertion as CI, and pass with this change.appsec_iast_default, full suite: py3.9 19917 passed, py3.12 19931 passed. This includes the weak-hash sink, which patches an immutable C type throughpatch_builtins.appsecsuite: py3.9 966 passed, py3.12 967 passed.scripts/lint checkspasses.Risks
Low. Only how
apply_patchandtry_unwrapread, record and restore the original changes, and production never patches twice or unpatches. The extra lookup runs once per patch call at startup, never per request, so I didn't run benchmarks.try_unwrapwhile our entry exists, a later patch and unwrap puts back the older original.try_unwrapwrites the saved original into the subclass's own__dict__instead of deleting it. This was already true on main, and it shows up in three ways:- Different hooks on base and subclass: after both are unwrapped, the base wrapper stays on the subclass.
- Same hook, subclass patched first: the hook is removed, but the subclass keeps its own copy of the original function.
- A single patch through an inheriting subclass, such as IAST's patch of
flask.Response.set_cookie, which flask inherits from werkzeug: after unwrap,flask.Responsekeeps its own copy.The last two leave no hook running, but the copy would hide any later patch on the base class. The follow-up is to delete the attribute in
try_unwrapwhen the subclass didn't define it, which fixes all three.Additional Notes
No release note: there is no customer-visible behavior change (see Customer impact).
tests/internal/test_security_patch_utils.pyis now co-owned by@DataDog/asm-python, which owns the code it tests.#21881 (closed) is superseded by this change.
DD_BPPZV3 DD_RXF8ZI DD_MQNBM0 DD_2UATV7
🤖 Generated with Claude Code