piton-cli 0.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- piton/__init__.py +5 -0
- piton/cli.py +716 -0
- piton/control_plane/__init__.py +11 -0
- piton/control_plane/server.py +402 -0
- piton/control_plane/store.py +500 -0
- piton/iac/__init__.py +36 -0
- piton/iac/agent_config_lint.py +436 -0
- piton/iac/lockfile.py +420 -0
- piton/iac/mcp_probe.py +582 -0
- piton/iac/poisoning_lint.py +197 -0
- piton/iac/replay.py +896 -0
- piton/iac/scanner.py +350 -0
- piton/iac/schema.py +256 -0
- piton_cli-0.4.2.dist-info/METADATA +192 -0
- piton_cli-0.4.2.dist-info/RECORD +19 -0
- piton_cli-0.4.2.dist-info/WHEEL +5 -0
- piton_cli-0.4.2.dist-info/entry_points.txt +3 -0
- piton_cli-0.4.2.dist-info/licenses/LICENSE +21 -0
- piton_cli-0.4.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Day-1 MCP Tool-Description & Schema Poisoning Linter (Audit-Hardened).
|
|
3
|
+
|
|
4
|
+
Prevents `piton init` and `piton lock` from blindly hashing and pinning
|
|
5
|
+
an already-poisoned third-party MCP server on Day 1.
|
|
6
|
+
Hardened against:
|
|
7
|
+
- NFKC Unicode homoglyph evasion
|
|
8
|
+
- Invisible zero-width, bidi-override, and Unicode tag characters
|
|
9
|
+
- ReDoS (bounded quantifiers only; no `.*` + DOTALL catastrophic backtracking)
|
|
10
|
+
- Poisoned `tool["name"]`, `annotations`, `examples`, `outputSchema`, and oversized `inputSchema`
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from enum import Enum
|
|
14
|
+
import json
|
|
15
|
+
import re
|
|
16
|
+
from typing import Any, Dict, List, Tuple
|
|
17
|
+
import unicodedata
|
|
18
|
+
from pydantic import BaseModel, Field
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class LintSeverity(str, Enum):
|
|
22
|
+
CRITICAL = "critical"
|
|
23
|
+
HIGH = "high"
|
|
24
|
+
MEDIUM = "medium"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PoisoningFinding(BaseModel):
|
|
28
|
+
server_name: str
|
|
29
|
+
tool_name: str
|
|
30
|
+
rule_id: str
|
|
31
|
+
severity: LintSeverity
|
|
32
|
+
field_path: str
|
|
33
|
+
message: str
|
|
34
|
+
matched_snippet: str = ""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class PoisoningLintReport(BaseModel):
|
|
38
|
+
passed: bool
|
|
39
|
+
findings: List[PoisoningFinding] = Field(default_factory=list)
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def critical_or_high_count(self) -> int:
|
|
43
|
+
return sum(1 for f in self.findings if f.severity in (LintSeverity.CRITICAL, LintSeverity.HIGH))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# Deterministic, ReDoS-safe rule catalog (bounded gaps `{0,120}` instead of unbounded `.*`)
|
|
47
|
+
POISONING_RULES: Tuple[Tuple[str, LintSeverity, str, str], ...] = (
|
|
48
|
+
(
|
|
49
|
+
"MCP_POISON_INSTRUCTION_OVERRIDE",
|
|
50
|
+
LintSeverity.CRITICAL,
|
|
51
|
+
r"(ignore\s+(all\s+)?(previous|prior|above)\s+instructions?|disregard\s+(all\s+)?(previous|prior)|new\s+system\s+prompt|override\s+system\s+instructions?)",
|
|
52
|
+
"Tool metadata contains prompt-override directives attempting to hijack agent instructions.",
|
|
53
|
+
),
|
|
54
|
+
(
|
|
55
|
+
"MCP_POISON_COVERT_CONCEALMENT",
|
|
56
|
+
LintSeverity.CRITICAL,
|
|
57
|
+
r"(do\s+not\s+(tell|inform|mention|show|notify)\s+the\s+user|never\s+mention\s+this|hide\s+this\s+from\s+the\s+user|silently\s+(send|forward|copy|bcc|exfiltrate|execute)|<\s*IMPORTANT\s*>|<\s*SYSTEM\s*>)",
|
|
58
|
+
"Tool metadata contains covert concealment or pseudo-system tags used in MCP tool-poisoning attacks.",
|
|
59
|
+
),
|
|
60
|
+
(
|
|
61
|
+
"MCP_POISON_CREDENTIAL_EXFIL",
|
|
62
|
+
LintSeverity.CRITICAL,
|
|
63
|
+
r"(~/\.ssh|id_rsa|id_ed25519|/etc/passwd|\.aws/credentials|\.env\b|\bexfiltrat\w*\b|webhook\.site|ngrok\.io|blind\s+carbon\s+copy|\bbcc\b\s+[^\s]{1,80}@|pass\s+(all|the)\s+(api\s+keys?|secrets?|tokens?|credentials?))",
|
|
64
|
+
"Tool metadata references sensitive credential paths or covert outbound exfiltration.",
|
|
65
|
+
),
|
|
66
|
+
(
|
|
67
|
+
"MCP_POISON_CROSS_TOOL_SHADOWING",
|
|
68
|
+
LintSeverity.HIGH,
|
|
69
|
+
r"((before|after|whenever|instead\s+of)\s+(calling|invoking|using)\s+[`'\"]?[a-zA-Z0-9_.-]{1,64}[`'\"]?[^.\n]{0,120}(also\s+call|must\s+first|redirect|override|replace|send\s+the\s+output)|redefine\s+the\s+behavior\s+of)",
|
|
70
|
+
"Tool metadata attempts cross-server tool shadowing by steering calls to adjacent tools.",
|
|
71
|
+
),
|
|
72
|
+
(
|
|
73
|
+
"MCP_POISON_SHELL_RCE_DIRECTIVE",
|
|
74
|
+
LintSeverity.HIGH,
|
|
75
|
+
r"(\bcurl\s+[^|\n]{1,120}\|\s*(ba)?sh\b|\bwget\s+[^|\n]{1,120}\|\s*(ba)?sh\b|bash\s+-c\s+['\"]|powershell\s+-enc)",
|
|
76
|
+
"Tool metadata contains embedded shell-execution / pipe-to-shell commands.",
|
|
77
|
+
),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
INVISIBLE_UNICODE_RE = re.compile(
|
|
81
|
+
r"[\u200b-\u200f\u202a-\u202e\u2060-\u206f\ufeff\U000e0000-\U000e007f]"
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
MAX_SAFE_DESCRIPTION_CHARS = 2_000
|
|
85
|
+
MAX_SAFE_SCHEMA_BYTES = 16_384
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _collect_all_strings(obj: Any, prefix: str) -> List[Tuple[str, str]]:
|
|
89
|
+
"""Recursively extracts string leaves from schemas, annotations, and examples."""
|
|
90
|
+
found: List[Tuple[str, str]] = []
|
|
91
|
+
if isinstance(obj, str):
|
|
92
|
+
found.append((prefix, obj))
|
|
93
|
+
elif isinstance(obj, dict):
|
|
94
|
+
for k, v in obj.items():
|
|
95
|
+
found.extend(_collect_all_strings(v, f"{prefix}.{k}"))
|
|
96
|
+
elif isinstance(obj, list):
|
|
97
|
+
for idx, item in enumerate(obj):
|
|
98
|
+
found.extend(_collect_all_strings(item, f"{prefix}[{idx}]"))
|
|
99
|
+
return found
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def lint_tool_definition(
|
|
103
|
+
server_name: str,
|
|
104
|
+
tool: Dict[str, Any],
|
|
105
|
+
) -> List[PoisoningFinding]:
|
|
106
|
+
"""Lints a single MCP `tools/list` tool object across name, description, schemas, and annotations."""
|
|
107
|
+
findings: List[PoisoningFinding] = []
|
|
108
|
+
tool_name = str(tool.get("name") or "unknown_tool")
|
|
109
|
+
description = str(tool.get("description") or "")
|
|
110
|
+
|
|
111
|
+
text_surfaces: List[Tuple[str, str]] = [
|
|
112
|
+
("name", tool_name),
|
|
113
|
+
("description", description),
|
|
114
|
+
]
|
|
115
|
+
for surface_key in ("inputSchema", "outputSchema", "annotations", "examples"):
|
|
116
|
+
if surface_key in tool and tool[surface_key] is not None:
|
|
117
|
+
text_surfaces.extend(_collect_all_strings(tool[surface_key], surface_key))
|
|
118
|
+
|
|
119
|
+
if len(description) > MAX_SAFE_DESCRIPTION_CHARS:
|
|
120
|
+
findings.append(
|
|
121
|
+
PoisoningFinding(
|
|
122
|
+
server_name=server_name,
|
|
123
|
+
tool_name=tool_name,
|
|
124
|
+
rule_id="MCP_POISON_DESCRIPTION_BLOAT",
|
|
125
|
+
severity=LintSeverity.MEDIUM,
|
|
126
|
+
field_path="description",
|
|
127
|
+
message=(
|
|
128
|
+
f"Tool description length ({len(description)} chars) exceeds {MAX_SAFE_DESCRIPTION_CHARS} "
|
|
129
|
+
"chars; oversized descriptions frequently conceal injected instructions."
|
|
130
|
+
),
|
|
131
|
+
matched_snippet=description[:120],
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
if isinstance(tool.get("inputSchema"), dict):
|
|
136
|
+
schema_bytes = len(json.dumps(tool["inputSchema"]).encode("utf-8"))
|
|
137
|
+
if schema_bytes > MAX_SAFE_SCHEMA_BYTES:
|
|
138
|
+
findings.append(
|
|
139
|
+
PoisoningFinding(
|
|
140
|
+
server_name=server_name,
|
|
141
|
+
tool_name=tool_name,
|
|
142
|
+
rule_id="MCP_POISON_SCHEMA_BLOAT",
|
|
143
|
+
severity=LintSeverity.MEDIUM,
|
|
144
|
+
field_path="inputSchema",
|
|
145
|
+
message=f"Tool inputSchema size ({schema_bytes} bytes) exceeds {MAX_SAFE_SCHEMA_BYTES} bytes.",
|
|
146
|
+
)
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
for field_path, text in text_surfaces:
|
|
150
|
+
if not text:
|
|
151
|
+
continue
|
|
152
|
+
|
|
153
|
+
if INVISIBLE_UNICODE_RE.search(text):
|
|
154
|
+
findings.append(
|
|
155
|
+
PoisoningFinding(
|
|
156
|
+
server_name=server_name,
|
|
157
|
+
tool_name=tool_name,
|
|
158
|
+
rule_id="MCP_POISON_INVISIBLE_UNICODE",
|
|
159
|
+
severity=LintSeverity.CRITICAL,
|
|
160
|
+
field_path=field_path,
|
|
161
|
+
message="Detected invisible zero-width, bidi-override, or Unicode tag characters used to hide prompt injections.",
|
|
162
|
+
matched_snippet=repr(text[:120]),
|
|
163
|
+
)
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
# NFKC normalize before lexical matching to defeat Unicode homoglyph/fullwidth obfuscation
|
|
167
|
+
normalized = unicodedata.normalize("NFKC", INVISIBLE_UNICODE_RE.sub("", text))
|
|
168
|
+
|
|
169
|
+
for rule_id, severity, pattern, msg in POISONING_RULES:
|
|
170
|
+
m = re.search(pattern, normalized, re.IGNORECASE)
|
|
171
|
+
if m:
|
|
172
|
+
findings.append(
|
|
173
|
+
PoisoningFinding(
|
|
174
|
+
server_name=server_name,
|
|
175
|
+
tool_name=tool_name,
|
|
176
|
+
rule_id=rule_id,
|
|
177
|
+
severity=severity,
|
|
178
|
+
field_path=field_path,
|
|
179
|
+
message=msg,
|
|
180
|
+
matched_snippet=m.group(0)[:140],
|
|
181
|
+
)
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
return findings
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def lint_tools_list(
|
|
188
|
+
server_name: str,
|
|
189
|
+
tools: List[Dict[str, Any]],
|
|
190
|
+
) -> PoisoningLintReport:
|
|
191
|
+
"""Runs Day-1 poisoning lint across an entire MCP server's `tools/list` output."""
|
|
192
|
+
all_findings: List[PoisoningFinding] = []
|
|
193
|
+
for tool in tools:
|
|
194
|
+
if isinstance(tool, dict):
|
|
195
|
+
all_findings.extend(lint_tool_definition(server_name, tool))
|
|
196
|
+
has_blocking = any(f.severity in (LintSeverity.CRITICAL, LintSeverity.HIGH) for f in all_findings)
|
|
197
|
+
return PoisoningLintReport(passed=not has_blocking, findings=all_findings)
|