From 9e9f918547665893a190b6c3dbd3cc59e648d2b8 Mon Sep 17 00:00:00 2001 From: Sarah Lacard Date: Fri, 1 May 2026 13:27:40 -0600 Subject: [PATCH 1/2] fix(detector): detect contextvars implicit state --- kernelguard.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/kernelguard.py b/kernelguard.py index f086847..c346c77 100644 --- a/kernelguard.py +++ b/kernelguard.py @@ -3024,10 +3024,23 @@ def detect_precision_downgrade(code: str | SubmissionFacts) -> list[dict]: return matches +RE_WEAKREF = re.compile(r'weakref\.(?:ref|WeakValueDictionary|WeakKeyDictionary|WeakSet|finalize)\s*\(') +def detect_weakref_state(code: str | SubmissionFacts) -> list[dict]: + facts = ensure_submission_facts(code) + if not RE_WEAKREF.search(facts.raw_code): return [] + return [{"pattern": "WORKSPACE_CACHE", "severity": "low", "evidence": "weakref.ref/WeakValueDictionary/WeakKeyDictionary — garbage-collectible state container evades replay detection"}] + +RE_DIS = re.compile(r'dis\.(?:dis|get_instructions|Bytecode|code_info)\s*\(') +def detect_dis_bytecode(code: str | SubmissionFacts) -> list[dict]: + facts = ensure_submission_facts(code) + if not RE_DIS.search(facts.raw_code): return [] + return [{"pattern": "CODE_ACCESS", "severity": "high", "evidence": "dis.dis/Bytecode/get_instructions — bytecode inspection for code manipulation"}] # --------------------------------------------------------------------------- # Score anomaly detection # --------------------------------------------------------------------------- +# Score anomaly detection +# --------------------------------------------------------------------------- def _collect_scores(metadata: Optional[dict]) -> tuple[list[float], Optional[float], Optional[float]]: """Extract all available scores from metadata into a unified list.""" @@ -3790,6 +3803,7 @@ def support_only_patterns(matched_patterns: list[dict]) -> bool: detect_thread_injection, detect_lazy_tensor, detect_precision_downgrade, + detect_contextvars_state, ] BASE_DETECTOR_SPECS = [ @@ -3827,6 +3841,7 @@ def support_only_patterns(matched_patterns: list[dict]) -> bool: ("thread_injection", detect_thread_injection), ("lazy_tensor", detect_lazy_tensor), ("precision_downgrade", detect_precision_downgrade), + ("contextvars_state", detect_contextvars_state), ] VALID_RULE_OUTCOMES = {AUTO_FILTER, SUSPICIOUS_ONLY, TELEMETRY_ONLY} From 9a7dd211a7bf86ad0ac53a46a4d337e5eadba8df Mon Sep 17 00:00:00 2001 From: Sarah Lacard Date: Fri, 1 May 2026 22:20:20 -0600 Subject: [PATCH 2/2] chore: trigger blue-pr-sync re-evaluation