Skip to content

Commit 598f6a0

Browse files
committed
fix(static): extract activation intent from descriptions
Address rng1995 P1 and yashrajp22 reviews on #541: descriptions are no longer passed whole to the legacy whole-string trigger grammar. Bounded clause extraction keeps only clauses with activation-intent signals; TR3 uses unanchored baiting patterns so realistic prose (e.g. 'whenever the user sends any message') is detected, and TR2 requires invocation/shadowing intent so ordinary capability prose ('Build projects', 'Deploy infrastructure') stays out of the trigger path. Legacy triggers field behavior unchanged. Adds positive and benign-negative regressions. Signed-off-by: Deepak Jain <[email protected]>
1 parent ecbe9b3 commit 598f6a0

2 files changed

Lines changed: 150 additions & 3 deletions

File tree

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

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,62 @@ def _is_typosquat(pkg_name: str, popular: set[str], max_distance: int = 2) -> st
499499
"hey",
500500
}
501501

502+
# Bounded activation-intent signals for skill descriptions. Only description
503+
# clauses carrying one of these signals are treated as trigger-like; ordinary
504+
# capability prose (e.g. "Build projects") is skipped.
505+
_DESCRIPTION_ACTIVATION_RE = re.compile(
506+
r"\b(?:"
507+
r"whenever|every\s+time|each\s+time|"
508+
r"(?:all|any|every)\s+(?:messages?|requests?|questions?|queries?|inputs?|tasks?)|"
509+
r"any\s+(?:question|request|task|input|message)|"
510+
r"anything|everything|whatever|always"
511+
r")\b",
512+
re.IGNORECASE,
513+
)
514+
515+
# Invocation/shadowing intent signals. A description only counts as a shadow
516+
# command when it shows the skill means to intercept or override a command.
517+
_DESCRIPTION_INVOCATION_RE = re.compile(
518+
r"\b(?:commands?|slash|invoke[sd]?|invoking|intercept(?:s|ed|ing)?|"
519+
r"override[sd]?|overriding|shadow(?:s|ed|ing)?)\b|/[a-z]",
520+
re.IGNORECASE,
521+
)
522+
523+
# Unanchored baiting patterns for description clauses. The legacy TR3 patterns
524+
# are whole-string anchored for the explicit triggers field; descriptions need
525+
# substring matching so realistic activation prose is detected.
526+
_DESCRIPTION_BAITING_PATTERNS = (
527+
r"(?:anything|everything|whatever|always|"
528+
r"any\s+(?:question|request|task|input|message))",
529+
r"(?:when(?:ever)?|if|every\s+time)\s+(?:the\s+)?user\s+"
530+
r"(?:says?|asks?|types?|sends?)\s+"
531+
r"(?:anything|something|any\s+messages?|a\s+message)",
532+
r"(?:all|any|every)\s+(?:messages?|inputs?|requests?|queries?|questions?)",
533+
)
534+
535+
# Bounds for description clause extraction: keep the analysis cheap and the
536+
# extracted trigger phrases reviewable.
537+
_MAX_DESCRIPTION_CLAUSES = 8
538+
_MAX_DESCRIPTION_CLAUSE_CHARS = 120
539+
540+
541+
def _extract_description_trigger_clauses(description: str) -> list[str]:
542+
"""Extract bounded trigger-like clauses from a skill description.
543+
544+
Splits the description into clauses and keeps only clauses carrying an
545+
activation-intent signal, so realistic activation prose is analyzed while
546+
ordinary capability prose is left alone.
547+
"""
548+
clauses = re.split(r"[.;:!?]\s*|\s+-\s+", description)
549+
extracted: list[str] = []
550+
for clause in clauses[:_MAX_DESCRIPTION_CLAUSES]:
551+
text = clause.strip().strip(",")
552+
if not text or len(text) > _MAX_DESCRIPTION_CLAUSE_CHARS:
553+
continue
554+
if _DESCRIPTION_ACTIVATION_RE.search(text):
555+
extracted.append(text)
556+
return extracted
557+
502558

503559
def _pinned_version(operator: str | None, version: str | None) -> str | None:
504560
"""Return *version* only when the specifier pins one concrete release.
@@ -1841,17 +1897,22 @@ def _analyze_triggers(manifest: dict[str, object], skill_path: str) -> list[Find
18411897
"""Analyze trigger-like manifest content for abuse patterns.
18421898
18431899
Agent Skills exposes activation intent through ``description``; legacy
1844-
``triggers`` metadata remains supported when present.
1900+
``triggers`` metadata remains supported when present. Descriptions are not
1901+
passed to the legacy trigger grammar directly: only clauses carrying an
1902+
activation-intent signal are analyzed, with TR2 requiring invocation or
1903+
shadowing intent and TR3 using unanchored baiting patterns, so realistic
1904+
activation prose is detected while ordinary capability prose is skipped.
18451905
"""
18461906
triggers: list[str] = []
18471907
raw = manifest.get("triggers", [])
18481908
if isinstance(raw, list):
18491909
triggers = [str(t).strip() for t in raw if str(t).strip()]
1910+
description_clauses: list[str] = []
18501911
if not triggers:
18511912
description = manifest.get("description")
18521913
if isinstance(description, str) and description.strip():
1853-
triggers = [description.strip()]
1854-
if not triggers:
1914+
description_clauses = _extract_description_trigger_clauses(description.strip())
1915+
if not triggers and not description_clauses:
18551916
return []
18561917

18571918
findings: list[Finding] = []
@@ -1937,6 +1998,59 @@ def _analyze_triggers(manifest: dict[str, object], skill_path: str) -> list[Find
19371998
)
19381999
break
19392000

2001+
for i, clause in enumerate(description_clauses, 1):
2002+
clause_lower = clause.lower().strip()
2003+
words = clause_lower.split()
2004+
2005+
# TR2 (description-calibrated): only flag a shadow command when the
2006+
# clause shows invocation or shadowing intent; ordinary capability
2007+
# prose such as "Build projects" stays out of the trigger path.
2008+
shadowed = sorted(
2009+
{cmd for cmd in _BUILTIN_COMMANDS if cmd in {w.lstrip("/") for w in words}}
2010+
)
2011+
if shadowed and _DESCRIPTION_INVOCATION_RE.search(clause):
2012+
findings.append(
2013+
Finding(
2014+
rule_id="TR2",
2015+
message=(
2016+
f"Shadow Command Trigger: description clause '{clause}' "
2017+
f"conflicts with built-in command '{shadowed[0]}'"
2018+
),
2019+
severity="MEDIUM",
2020+
confidence=0.7,
2021+
file=file_ref,
2022+
start_line=i,
2023+
tags=tag,
2024+
matched_text=clause,
2025+
category=PatternCategory.TRIGGER_ABUSE.value,
2026+
pattern="Shadow Command Trigger",
2027+
)
2028+
)
2029+
2030+
# TR3 (description-calibrated): unanchored baiting patterns detect
2031+
# realistic activation prose such as "whenever the user sends any
2032+
# message", which the whole-string legacy grammar misses.
2033+
for bp in _DESCRIPTION_BAITING_PATTERNS:
2034+
if re.search(bp, clause_lower):
2035+
findings.append(
2036+
Finding(
2037+
rule_id="TR3",
2038+
message=(
2039+
f"Keyword Baiting Trigger: description clause '{clause}' "
2040+
"is designed to match all or most user inputs"
2041+
),
2042+
severity="MEDIUM",
2043+
confidence=0.8,
2044+
file=file_ref,
2045+
start_line=i,
2046+
tags=tag,
2047+
matched_text=clause,
2048+
category=PatternCategory.TRIGGER_ABUSE.value,
2049+
pattern="Keyword Baiting Trigger",
2050+
)
2051+
)
2052+
break
2053+
19402054
return findings
19412055

19422056

‎tests/unit/test_patterns_new.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,39 @@ def test_description_is_analyzed_when_triggers_are_absent(self) -> None:
22352235
findings = sc_mod._analyze_triggers({"description": "all messages"}, "myskill")
22362236
assert any(finding.rule_id == "TR3" for finding in findings)
22372237

2238+
def test_description_realistic_activation_prose_detected(self) -> None:
2239+
"""rng1995 #541 P1: realistic spec prose must not bypass TR3."""
2240+
findings = sc_mod._analyze_triggers(
2241+
{"description": "Use this skill whenever the user sends any message"},
2242+
"myskill",
2243+
)
2244+
assert any(finding.rule_id == "TR3" for finding in findings)
2245+
2246+
def test_description_benign_capability_prose_not_shadow_command(self) -> None:
2247+
"""yashrajp22 #541: ordinary capability prose is not command shadowing."""
2248+
for description in ("Build projects", "Deploy infrastructure"):
2249+
findings = sc_mod._analyze_triggers({"description": description}, "myskill")
2250+
assert findings == []
2251+
2252+
def test_description_shadow_command_requires_invocation_intent(self) -> None:
2253+
"""TR2 fires for descriptions only with invocation/shadowing intent."""
2254+
findings = sc_mod._analyze_triggers(
2255+
{"description": "Intercepts the /build command for every request"},
2256+
"myskill",
2257+
)
2258+
assert "TR2" in {finding.rule_id for finding in findings}
2259+
2260+
def test_description_without_baiting_signal_is_skipped(self) -> None:
2261+
findings = sc_mod._analyze_triggers(
2262+
{"description": "Run tests whenever code changes"}, "myskill"
2263+
)
2264+
assert findings == []
2265+
2266+
def test_legacy_triggers_bypass_description_calibration(self) -> None:
2267+
"""Explicit triggers keep the legacy whole-string trigger grammar."""
2268+
findings = sc_mod._analyze_triggers({"triggers": ["Build"]}, "myskill")
2269+
assert any(finding.rule_id == "TR2" for finding in findings)
2270+
22382271

22392272
# ── Supply Chain Helpers ───────────────────────────────────────────────
22402273

0 commit comments

Comments
 (0)