Skip to content

Commit 5680c2c

Browse files
authored
Scope the locality guard to the namespace (#365)
* fix(yara): scope locality guard to agent namespace Signed-off-by: Narendran Raghavan <[email protected]> * Preserve built-in YARA namespace precedence Signed-off-by: Narendran Raghavan <[email protected]> --------- Signed-off-by: Narendran Raghavan <[email protected]>
1 parent 81ff688 commit 5680c2c

2 files changed

Lines changed: 65 additions & 5 deletions

File tree

‎src/skillspector/nodes/analyzers/static_yara.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
_DEFAULT_RULE_ID = "YR4"
6363
_DEFAULT_SEVERITY = Severity.MEDIUM
6464
_DEFAULT_CONFIDENCE = 0.7
65+
_DESTRUCTIVE_AUTONOMY_NAMESPACE = "agent_skills"
6566
_DESTRUCTIVE_AUTONOMY_RULE = "agent_skill_destructive_autonomous_actions"
6667
_MAX_DESTRUCTIVE_AUTONOMY_LINE_DISTANCE = 3
6768

@@ -71,14 +72,20 @@
7172

7273

7374
def _collect_rule_files(*dirs: Path) -> list[Path]:
74-
"""Collect all YARA rule files under one or more directories, sorted for determinism."""
75-
files: set[Path] = set()
75+
"""Collect YARA files deterministically while preserving directory precedence."""
76+
files: list[Path] = []
77+
seen: set[Path] = set()
7678
for d in dirs:
7779
if not d.is_dir():
7880
continue
81+
directory_files: set[Path] = set()
7982
for ext in _RULE_EXTENSIONS:
80-
files.update(d.rglob(ext))
81-
return sorted(files)
83+
directory_files.update(d.rglob(ext))
84+
for rule_file in sorted(directory_files):
85+
if rule_file not in seen:
86+
seen.add(rule_file)
87+
files.append(rule_file)
88+
return files
8289

8390

8491
def _content_hash(rule_files: list[Path]) -> str:
@@ -278,7 +285,8 @@ def _match_file(rules: yara.Rules, content: str, file_path: str) -> list[Analyze
278285
findings: list[AnalyzerFinding] = []
279286
for match in matches:
280287
if (
281-
match.rule == _DESTRUCTIVE_AUTONOMY_RULE
288+
match.namespace == _DESTRUCTIVE_AUTONOMY_NAMESPACE
289+
and match.rule == _DESTRUCTIVE_AUTONOMY_RULE
282290
and not _has_local_destructive_autonomy_evidence(match, data)
283291
):
284292
logger.debug(

‎tests/nodes/analyzers/test_static_yara.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,58 @@ def test_destructive_root_delete_remains_blocking_without_autonomy_phrase(self):
491491
findings = _run_builtin("rm -rf /\n", "setup.sh")
492492
assert _has_rule(findings, "agent_skill_destructive_autonomous_actions")
493493

494+
def test_user_rule_with_destructive_rule_name_is_not_post_filtered(self, tmp_path):
495+
_write_rule(
496+
tmp_path,
497+
"agent_skill_destructive_autonomous_actions",
498+
category="hack_tool",
499+
severity="MEDIUM",
500+
strings={"custom": "CUSTOM_DESTRUCTIVE_MARKER"},
501+
)
502+
503+
findings = _run("CUSTOM_DESTRUCTIVE_MARKER", "custom.txt", str(tmp_path))
504+
505+
assert _has_rule(findings, "agent_skill_destructive_autonomous_actions")
506+
507+
def test_user_agent_skills_file_cannot_claim_builtin_namespace(self, tmp_path, monkeypatch):
508+
builtin_dir = tmp_path / "z_builtin"
509+
user_dir = tmp_path / "a_user"
510+
builtin_dir.mkdir()
511+
user_dir.mkdir()
512+
(builtin_dir / "agent_skills.yar").write_text(
513+
"""
514+
rule agent_skill_destructive_autonomous_actions {
515+
strings:
516+
$destructive_action = "DELETE_MARKER"
517+
$autonomy_action = "AUTONOMY_MARKER"
518+
condition:
519+
all of them
520+
}
521+
"""
522+
)
523+
(user_dir / "agent_skills.yar").write_text(
524+
"""
525+
rule agent_skill_destructive_autonomous_actions {
526+
strings:
527+
$custom = "CUSTOM_DESTRUCTIVE_MARKER"
528+
condition:
529+
$custom
530+
}
531+
"""
532+
)
533+
monkeypatch.setattr(static_yara, "_BUILTIN_RULES_DIR", builtin_dir)
534+
535+
intervening_lines = "\n".join(f"review step {index}" for index in range(6))
536+
content = (
537+
f"DELETE_MARKER\n{intervening_lines}\nAUTONOMY_MARKER\nCUSTOM_DESTRUCTIVE_MARKER\n"
538+
)
539+
540+
findings = _run(content, "custom.txt", str(user_dir))
541+
542+
assert len(findings) == 1
543+
assert _has_rule(findings, "agent_skill_destructive_autonomous_actions")
544+
assert "[a_user/agent_skills]" in findings[0].message
545+
494546
def test_credential_webhook_requires_collection_and_transmission(self):
495547
content = """
496548
# Document how to rotate OPENAI_API_KEY.

0 commit comments

Comments
 (0)