|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/security/detect" |
| 10 | +) |
| 11 | + |
| 12 | +// CapabilityMismatch is a SOFT check (FR-009, US2) that flags a gap between what |
| 13 | +// a tool *declares* it does and what it *implies* it touches: |
| 14 | +// |
| 15 | +// - Declared-vs-implied: a tool whose declared purpose is pure computation or |
| 16 | +// string manipulation (name/lead like "add", "to_uppercase") that |
| 17 | +// nevertheless references a sensitive resource it has no business touching |
| 18 | +// (~/.ssh, /etc/passwd, an external URL, a shell). A calculator reading |
| 19 | +// id_rsa is a classic capability-mismatch exfiltration tell. |
| 20 | +// - Unexplained data-sink param: a free-form input named like an exfiltration |
| 21 | +// channel ("sidenote", "scratchpad") that the description never explains — |
| 22 | +// the model is steered to stuff stolen data into it. |
| 23 | +// |
| 24 | +// The declared category is taken from the tool NAME and its leading sentence, |
| 25 | +// NOT the full description, so an attacker's benign cover sentence still anchors |
| 26 | +// the declaration while the smuggled access in the rest of the text is treated |
| 27 | +// as implied. Tools that legitimately declare file/network/system access are |
| 28 | +// therefore NOT flagged for touching those resources (FR-009 MUST-NOT). |
| 29 | +// |
| 30 | +// Being soft, a hit raises a finding for review and never auto-quarantines. |
| 31 | +type CapabilityMismatch struct{} |
| 32 | + |
| 33 | +// ID implements detect.Check. |
| 34 | +func (*CapabilityMismatch) ID() string { return "capability.mismatch" } |
| 35 | + |
| 36 | +const ( |
| 37 | + mismatchConfidence = 0.55 |
| 38 | + dataSinkConfidence = 0.5 |
| 39 | +) |
| 40 | + |
| 41 | +// Category keyword sets. IO categories (file/network/system) take precedence so |
| 42 | +// a tool that genuinely declares resource access is never flagged for using it. |
| 43 | +var ( |
| 44 | + fileWords = []string{"file", "path", "dir", "folder", "read", "write", "load", "save", "open", "document", "filesystem"} |
| 45 | + networkWords = []string{"http", "url", "fetch", "download", "upload", "request", "web", "api", "curl", "wget"} |
| 46 | + systemWords = []string{"exec", "shell", "command", "process", "terminal", "spawn", "subprocess", "script"} |
| 47 | + computeWords = []string{"add", "sum", "subtract", "minus", "multiply", "divide", "calc", "math", "arithmetic", "average", "count", "modulo", "power", "sqrt", "mean", "round", "compute"} |
| 48 | + stringWords = []string{"string", "upper", "lower", "concat", "reverse", "trim", "replace", "encode", "decode", "length", "substring", "split", "join", "format", "case", "slug"} |
| 49 | +) |
| 50 | + |
| 51 | +// sensitiveMarkers are concrete resource references a pure compute/string tool |
| 52 | +// has no reason to touch. Written to match NORMALIZED text (lowercased, lightly |
| 53 | +// stemmed — e.g. ".aws/credentials" → ".aws/credential"). |
| 54 | +var sensitiveMarkers = []string{ |
| 55 | + ".ssh", "id_rsa", "id_ed25519", "/etc/passwd", "/etc/shadow", ".aws/credential", |
| 56 | + ".aws", "private key", "keychain", ".netrc", ".npmrc", ".git-credential", |
| 57 | + "authorized_key", ".pgpass", "kube/config", "/.config/gcloud", |
| 58 | + "http://", "https://", "/bin/sh", "/bin/bash", "subprocess", "exfiltrat", |
| 59 | +} |
| 60 | + |
| 61 | +// sinkParamNames are input parameter names that read as free-form exfiltration |
| 62 | +// channels rather than genuine tool inputs. |
| 63 | +var sinkParamNames = map[string]struct{}{ |
| 64 | + "sidenote": {}, "side_note": {}, "scratchpad": {}, "scratch": {}, |
| 65 | + "thoughts": {}, "thought": {}, "reasoning": {}, "memo": {}, "exfil": {}, |
| 66 | + "secret_note": {}, "debug_info": {}, "extra_context": {}, "notes_to_self": {}, |
| 67 | + "hidden_note": {}, "annotation": {}, "annotations": {}, |
| 68 | +} |
| 69 | + |
| 70 | +// Inspect implements detect.Check. It emits at most one signal per tool, |
| 71 | +// preferring the capability-mismatch signal over an unexplained data-sink. |
| 72 | +func (c *CapabilityMismatch) Inspect(tool detect.ToolView, _ detect.RegistryView) []detect.Signal { |
| 73 | + declared := declaredCategory(tool) |
| 74 | + text := tool.NormalizedText |
| 75 | + |
| 76 | + // Declared-vs-implied mismatch: a compute/string tool touching a sensitive |
| 77 | + // resource. |
| 78 | + if declared == "compute" || declared == "string" { |
| 79 | + if marker, ok := firstMarker(text); ok { |
| 80 | + return []detect.Signal{{ |
| 81 | + CheckID: c.ID(), |
| 82 | + Tier: detect.TierSoft, |
| 83 | + ThreatType: detect.ThreatExfiltration, |
| 84 | + Confidence: mismatchConfidence, |
| 85 | + Evidence: detect.CapEvidence(marker), |
| 86 | + Detail: fmt.Sprintf("Tool declares a %s capability yet references %q — a resource it has no declared reason to access.", |
| 87 | + declared, marker), |
| 88 | + }} |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Unexplained data-sink parameter. |
| 93 | + if param, ok := unexplainedSinkParam(tool); ok { |
| 94 | + return []detect.Signal{{ |
| 95 | + CheckID: c.ID(), |
| 96 | + Tier: detect.TierSoft, |
| 97 | + ThreatType: detect.ThreatExfiltration, |
| 98 | + Confidence: dataSinkConfidence, |
| 99 | + Evidence: detect.CapEvidence(param), |
| 100 | + Detail: fmt.Sprintf("Input parameter %q reads as a free-form data sink and is never explained in the description — a likely exfiltration channel.", |
| 101 | + param), |
| 102 | + }} |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +// declaredCategory infers the tool's declared purpose from its name first, then |
| 109 | +// its leading sentence. Returns "" when unknown. |
| 110 | +func declaredCategory(tool detect.ToolView) string { |
| 111 | + if cat := categoryFromText(strings.ToLower(tool.Name)); cat != "" { |
| 112 | + return cat |
| 113 | + } |
| 114 | + lead := strings.ToLower(tool.Description) |
| 115 | + if i := strings.IndexByte(lead, '.'); i > 0 { |
| 116 | + lead = lead[:i] |
| 117 | + } |
| 118 | + return categoryFromText(lead) |
| 119 | +} |
| 120 | + |
| 121 | +// categoryFromText classifies free text into a capability category. IO |
| 122 | +// categories are checked first so they win over an incidental compute word. |
| 123 | +func categoryFromText(s string) string { |
| 124 | + switch { |
| 125 | + case containsAny(s, fileWords): |
| 126 | + return "file" |
| 127 | + case containsAny(s, networkWords): |
| 128 | + return "network" |
| 129 | + case containsAny(s, systemWords): |
| 130 | + return "system" |
| 131 | + case containsAny(s, computeWords): |
| 132 | + return "compute" |
| 133 | + case containsAny(s, stringWords): |
| 134 | + return "string" |
| 135 | + default: |
| 136 | + return "" |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func containsAny(hay string, subs []string) bool { |
| 141 | + for _, s := range subs { |
| 142 | + if strings.Contains(hay, s) { |
| 143 | + return true |
| 144 | + } |
| 145 | + } |
| 146 | + return false |
| 147 | +} |
| 148 | + |
| 149 | +// firstMarker returns the first sensitive marker present in text, scanning in |
| 150 | +// declaration order for determinism. |
| 151 | +func firstMarker(text string) (string, bool) { |
| 152 | + for _, m := range sensitiveMarkers { |
| 153 | + if strings.Contains(text, m) { |
| 154 | + return m, true |
| 155 | + } |
| 156 | + } |
| 157 | + return "", false |
| 158 | +} |
| 159 | + |
| 160 | +// unexplainedSinkParam returns the first (alphabetically) input parameter whose |
| 161 | +// name reads as a data sink AND is not mentioned in the description. Parsing is |
| 162 | +// total: a malformed schema yields no parameters rather than an error. |
| 163 | +func unexplainedSinkParam(tool detect.ToolView) (string, bool) { |
| 164 | + if len(tool.InputSchema) == 0 { |
| 165 | + return "", false |
| 166 | + } |
| 167 | + var doc struct { |
| 168 | + Properties map[string]json.RawMessage `json:"properties"` |
| 169 | + } |
| 170 | + if err := json.Unmarshal(tool.InputSchema, &doc); err != nil { |
| 171 | + return "", false |
| 172 | + } |
| 173 | + names := make([]string, 0, len(doc.Properties)) |
| 174 | + for name := range doc.Properties { |
| 175 | + names = append(names, name) |
| 176 | + } |
| 177 | + sort.Strings(names) |
| 178 | + |
| 179 | + desc := strings.ToLower(tool.Description) |
| 180 | + for _, name := range names { |
| 181 | + if _, isSink := sinkParamNames[strings.ToLower(name)]; !isSink { |
| 182 | + continue |
| 183 | + } |
| 184 | + // "Explained" = the description references the param name. Checked against |
| 185 | + // the description only (NOT the schema, which always contains the name). |
| 186 | + if strings.Contains(desc, strings.ToLower(name)) { |
| 187 | + continue |
| 188 | + } |
| 189 | + return name, true |
| 190 | + } |
| 191 | + return "", false |
| 192 | +} |
0 commit comments