Compare commits

..

3 Commits

Author SHA1 Message Date
Ezra
3c8510cc0a fix(context_compressor): reduce default protect_last_n from 20 to 5
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 2s
The previous default of 20 protected messages was overly conservative,
preventing meaningful compression on long sessions. Reducing to 5
strikes a better balance between preserving recent conversational
context and allowing the compressor to actually reduce token pressure.

Updates both ContextCompressor default and AIAgent integration,
plus adds a regression test verifying the last 5 turns are never
summarized away.
2026-04-07 02:20:34 -04:00
Ezra
5a47056073 feat(skills_guard): V-011 obfuscation bypass detection
Adds defense-in-depth against obfuscated malicious skill code:
- normalize_input() with NFKC normalization, case folding, and zero-width
  character removal to defeat homoglyph and ZWSP evasion.
- PythonSecurityAnalyzer AST visitor detecting eval/exec/compile,
  getattr dunder access, and imports of base64/codecs/marshal/types/ctypes.
- Additional regex patterns for getattr builtins chains, __import__
  os/subprocess, and nested base64 decoding.
- Integrates all patterns into scan_file(); Python files now get both
  normalized regex scanning and AST-based analysis.

Includes full test coverage in tests/tools/test_skills_guard.py.
2026-04-07 02:20:31 -04:00
3fc47a0e2e [claw-code] [CONFIG] Add Kimi model to fallback chain for Allegro and Bezalel (#151) (#177)
Some checks failed
Forge CI / smoke-and-build (push) Failing after 2s
Co-authored-by: claw-code <claw-code@timmy.local>
Co-committed-by: claw-code <claw-code@timmy.local>
2026-04-07 04:14:19 +00:00
6 changed files with 308 additions and 7 deletions

View File

@@ -64,7 +64,7 @@ class ContextCompressor:
model: str,
threshold_percent: float = 0.50,
protect_first_n: int = 3,
protect_last_n: int = 20,
protect_last_n: int = 5,
summary_target_ratio: float = 0.20,
quiet_mode: bool = False,
summary_model_override: str = None,

View File

@@ -1180,7 +1180,7 @@ class AIAgent:
compression_enabled = str(_compression_cfg.get("enabled", True)).lower() in ("true", "1", "yes")
compression_summary_model = _compression_cfg.get("summary_model") or None
compression_target_ratio = float(_compression_cfg.get("target_ratio", 0.20))
compression_protect_last = int(_compression_cfg.get("protect_last_n", 20))
compression_protect_last = int(_compression_cfg.get("protect_last_n", 5))
# Read explicit context_length override from model config
_model_cfg = _agent_cfg.get("model", {})

View File

@@ -557,8 +557,41 @@ class TestSummaryTargetRatio:
assert c.threshold_percent == 0.50
assert c.threshold_tokens == 50_000
def test_default_protect_last_n_is_20(self):
"""Default protect_last_n should be 20."""
def test_default_protect_last_n_is_5(self):
"""Default protect_last_n should be 5 (Last 5 Turns protection)."""
with patch("agent.context_compressor.get_model_context_length", return_value=100_000):
c = ContextCompressor(model="test", quiet_mode=True)
assert c.protect_last_n == 20
assert c.protect_last_n == 5
def test_last_5_turns_protected_from_compression(self):
"""The most recent 5 messages must never be summarized away."""
with patch("agent.context_compressor.get_model_context_length", return_value=10_000):
c = ContextCompressor(
model="test",
quiet_mode=True,
protect_first_n=2,
protect_last_n=5,
threshold_percent=0.50,
)
# Build messages: system + 11 user/assistant exchanges
messages = [{"role": "system", "content": "sys"}]
for i in range(11):
messages.append({"role": "user", "content": f"user {i}"})
messages.append({"role": "assistant", "content": f"assistant {i}"})
c.last_prompt_tokens = 6_000
with patch.object(c, "_generate_summary", return_value="[SUMMARY]"):
result = c.compress(messages, current_tokens=6_000)
# The tail should preserve the last 5 raw messages from the original list.
# Original last 5 messages: assistant 8, user 9, assistant 9, user 10, assistant 10
# The summary may be merged into the first tail message to avoid role collision.
tail_roles = [m.get("role") for m in result[-5:]]
tail_contents = [m.get("content", "") for m in result[-5:]]
assert tail_roles == ["assistant", "user", "assistant", "user", "assistant"]
assert tail_contents[-4:] == [
"user 9", "assistant 9", "user 10", "assistant 10"
]
# First tail message has the original content preserved (possibly merged with summary)
assert "assistant 8" in tail_contents[0]

View File

@@ -514,3 +514,79 @@ class TestSymlinkPrefixConfusionRegression:
new_escapes = not resolved.is_relative_to(skill_dir_resolved)
assert old_escapes is False
assert new_escapes is False
# ---------------------------------------------------------------------------
# V-011 Obfuscation Bypass Tests
# ---------------------------------------------------------------------------
from tools.skills_guard_v011 import normalize_input, analyze_python_ast
class TestNormalizeInput:
def test_nfkc_homoglyphs(self):
assert normalize_input("") == "eval"
def test_case_folding(self):
assert normalize_input("EVaL") == "eval"
def test_zwsp_removal(self):
assert normalize_input("ev\u200bal") == "eval"
assert normalize_input("ex\u200ce\u200dc") == "exec"
assert normalize_input("get\ufeffattr") == "getattr"
def test_combined_obfuscation(self):
assert normalize_input("E\u200b") == "eval"
class TestPythonSecurityAnalyzer:
def test_detects_eval_call(self):
code = "eval('1+1')"
findings = analyze_python_ast(code, "test.py")
assert any(f.pattern_id == "dynamic_eval_exec" for f in findings)
def test_detects_exec_call(self):
code = "exec('print(1)')"
findings = analyze_python_ast(code, "test.py")
assert any(f.pattern_id == "dynamic_eval_exec" for f in findings)
def test_detects_compile_call(self):
code = "compile('pass', '<string>', 'exec')"
findings = analyze_python_ast(code, "test.py")
assert any(f.pattern_id == "dynamic_compile" for f in findings)
def test_detects_getattr_dunder(self):
code = 'getattr(os, "__import__")'
findings = analyze_python_ast(code, "test.py")
assert any(f.pattern_id == "getattr_dunder" for f in findings)
def test_detects_import_base64(self):
code = "import base64"
findings = analyze_python_ast(code, "test.py")
assert any(f.pattern_id == "import_base64" for f in findings)
def test_no_false_positives_on_safe_code(self):
code = "print('hello world')"
findings = analyze_python_ast(code, "test.py")
assert len(findings) == 0
class TestV011Integration:
def test_scan_file_catches_obfuscated_eval(self, tmp_path):
f = tmp_path / "bad.py"
f.write_text("e\u200b('1+1')")
findings = scan_file(f, "bad.py")
assert any("eval" in f.description.lower() for f in findings)
def test_scan_file_catches_dynamic_exec(self, tmp_path):
f = tmp_path / "bad.py"
f.write_text("exec('import os')")
findings = scan_file(f, "bad.py")
assert any(f.pattern_id == "dynamic_eval_exec" for f in findings)
def test_scan_file_catches_obfuscated_import(self, tmp_path):
f = tmp_path / "bad.py"
f.write_text("__import__('os')")
findings = scan_file(f, "bad.py")
# Should be caught by regex after normalization
assert any("__import__" in f.description.lower() for f in findings)

View File

@@ -32,6 +32,12 @@ from datetime import datetime, timezone
from pathlib import Path
from typing import List, Set, Tuple
from tools.skills_guard_v011 import (
normalize_input,
analyze_python_ast,
V011_OBFUSCATION_PATTERNS,
)
@@ -484,7 +490,7 @@ THREAT_PATTERNS = [
(r'(send|post|upload|transmit)\s+.*\s+(to|at)\s+https?://',
"send_to_url", "high", "exfiltration",
"instructs agent to send data to a URL"),
]
] + V011_OBFUSCATION_PATTERNS
# Structural limits for skill directories
MAX_FILE_COUNT = 50 # skills shouldn't have 50+ files
@@ -921,7 +927,7 @@ def scan_file(file_path: Path, rel_path: str = "") -> List[Finding]:
original_line = lines[i - 1] if i <= len(lines) else norm_line
matched_text = original_line.strip()
if len(matched_text) > 120:
matched_text = matched_text[:117] + "..."
matched_text = matched_text[:117] + '...'
findings.append(Finding(
pattern_id=pid,
severity=severity,

186
tools/skills_guard_v011.py Normal file
View File

@@ -0,0 +1,186 @@
"""
V-011 Skills Guard Bypass fix — Obfuscation detection.
Adds:
- normalize_input() with NFKC + case folding + ZWSP removal
- PythonSecurityAnalyzer AST visitor for dynamic execution patterns
- Additional obfuscation threat patterns
"""
import ast
import re
import unicodedata
from dataclasses import dataclass
from typing import List
@dataclass
class Finding:
"""Minimal Finding dataclass to avoid circular import with skills_guard.py."""
pattern_id: str
severity: str
category: str
file: str
line: int
match: str
description: str
def normalize_input(text: str) -> str:
"""
Normalize text to defeat common obfuscation bypasses.
1. Compatibility decomposition (NFKC) — homoglyphs, compat chars
2. Case folding — uppercase ↔ lowercase evasion
3. Zero-width space / joiner removal
"""
text = unicodedata.normalize("NFKC", text)
text = text.casefold()
# Remove zero-width and control characters used for hiding
zwsp_chars = "\u200b\u200c\u200d\ufeff\u2060\u180e"
for ch in zwsp_chars:
text = text.replace(ch, "")
return text
# ---------------------------------------------------------------------------
# AST-based Python security analysis
# ---------------------------------------------------------------------------
class PythonSecurityAnalyzer(ast.NodeVisitor):
"""AST visitor that detects obfuscated/dynamic execution in Python code."""
def __init__(self, rel_path: str):
self.rel_path = rel_path
self.findings: List[Finding] = []
self.seen = set()
def _add(self, pattern_id: str, severity: str, category: str, line: int, match: str, description: str):
key = (pattern_id, line, match)
if key in self.seen:
return
self.seen.add(key)
if len(match) > 120:
match = match[:117] + "..."
self.findings.append(Finding(
pattern_id=pattern_id,
severity=severity,
category=category,
file=self.rel_path,
line=line,
match=match,
description=description,
))
def visit_Call(self, node: ast.Call):
# Detect eval/exec/compile with non-literal args
if isinstance(node.func, ast.Name):
if node.func.id in ("eval", "exec"):
self._add(
"dynamic_eval_exec",
"high",
"obfuscation",
getattr(node, "lineno", 0),
ast.dump(node)[:120],
f"Dynamic {node.func.id}() call detected (possible obfuscation)",
)
elif node.func.id == "compile":
self._add(
"dynamic_compile",
"high",
"obfuscation",
getattr(node, "lineno", 0),
ast.dump(node)[:120],
"compile() call detected (possible code obfuscation)",
)
elif node.func.id == "getattr" and len(node.args) >= 2:
# getattr(..., "__import__") or similar
if isinstance(node.args[1], ast.Constant) and isinstance(node.args[1].value, str):
if node.args[1].value.startswith("__") and node.args[1].value.endswith("__"):
self._add(
"getattr_dunder",
"high",
"obfuscation",
getattr(node, "lineno", 0),
f'getattr(..., "{node.args[1].value}")',
"getattr used to access dunder attribute (possible sandbox escape)",
)
elif isinstance(node.func, ast.Attribute):
if node.func.attr in ("__import__", "_import"):
self._add(
"dynamic_import",
"high",
"obfuscation",
getattr(node, "lineno", 0),
ast.dump(node)[:120],
"Dynamic __import__ invocation detected",
)
self.generic_visit(node)
def visit_Import(self, node: ast.Import):
# Detect import of known obfuscation modules
obf_modules = {"base64", "codecs", "marshal", "types", "ctypes"}
for alias in node.names:
if alias.name in obf_modules:
self._add(
f"import_{alias.name}",
"medium",
"obfuscation",
getattr(node, "lineno", 0),
f"import {alias.name}",
f"{alias.name} import (possible encoding/obfuscation helper)",
)
self.generic_visit(node)
def visit_ImportFrom(self, node: ast.ImportFrom):
obf_modules = {"base64", "codecs", "marshal", "types", "ctypes"}
if node.module in obf_modules:
self._add(
f"import_from_{node.module}",
"medium",
"obfuscation",
getattr(node, "lineno", 0),
f"from {node.module} import ...",
f"{node.module} import (possible encoding/obfuscation helper)",
)
self.generic_visit(node)
def analyze_python_ast(content: str, rel_path: str) -> List[Finding]:
"""Run AST analysis on Python content and return findings."""
try:
tree = ast.parse(content)
except SyntaxError:
return []
analyzer = PythonSecurityAnalyzer(rel_path)
analyzer.visit(tree)
return analyzer.findings
# ---------------------------------------------------------------------------
# Additional obfuscation patterns for regex scanning
# ---------------------------------------------------------------------------
V011_OBFUSCATION_PATTERNS = [
# getattr/__import__ chains as strings
(r'getattr\s*\([^)]*__builtins__[^)]*\)',
"getattr_builtins_chain", "high", "obfuscation",
"getattr chain targeting __builtins__ (sandbox escape)"),
(r'__import__\s*\(\s*["\']os["\']',
"dunder_import_os", "high", "obfuscation",
"__import__ used to load os module (obfuscation)"),
(r'__import__\s*\(\s*["\']subprocess["\']',
"dunder_import_subprocess", "high", "obfuscation",
"__import__ used to load subprocess module (obfuscation)"),
# exec/eval with obfuscated wrappers
(r'\beval\s*\(\s*\+',
"eval_plus_concat", "high", "obfuscation",
"eval with string concatenation (obfuscation)"),
(r'\bexec\s*\(\s*\+',
"exec_plus_concat", "high", "obfuscation",
"exec with string concatenation (obfuscation)"),
# Base64/hex dynamic execution
(r'base64\.(b64decode|decode)\s*\([^)]*\)\s*\)\s*\)',
"base64_nested_decode", "high", "obfuscation",
"nested base64 decode followed by execution (obfuscation)"),
]