@coreguard/agent-commerce 0.1.5 → 0.2.1
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.
- package/README.md +31 -2
- package/bin/trust-card.mjs +12 -0
- package/mcp.mjs +34 -3
- package/observations.mjs +24 -3
- package/package.json +10 -4
- package/server.json +3 -3
- package/trust-card-cli.mjs +289 -0
- package/trust-card.mjs +252 -0
- package/wave3.mjs +1173 -0
package/trust-card.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CoreGuard trust cards (CG-TC/1) — turn a server record and a tools/list into
|
|
3
|
+
* one publishable page.
|
|
4
|
+
*
|
|
5
|
+
* This module is the product surface behind `coreguard_audit_server_manifest`
|
|
6
|
+
* and `coreguard_scan_tool_descriptions`: a deterministic composition of both
|
|
7
|
+
* checks plus the inventory, digests and the honesty boundary a listing needs.
|
|
8
|
+
*
|
|
9
|
+
* It performs no I/O, reads no wall clock and calls no network. The CLI wraps
|
|
10
|
+
* it; the MCP tools stay available for agents that want the raw structures.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { auditServerManifest, scanToolDescriptions, digestOf } from "./wave3.mjs";
|
|
14
|
+
|
|
15
|
+
export const TRUST_CARD_SPEC = "CG-TC/1";
|
|
16
|
+
|
|
17
|
+
const str = (v) => (typeof v === "string" && v.trim() ? v.trim() : null);
|
|
18
|
+
|
|
19
|
+
/** Accept an array, {tools}, a JSON-RPC tools/list response, or {result:{tools}}. */
|
|
20
|
+
export function parseToolsList(text) {
|
|
21
|
+
let parsed;
|
|
22
|
+
try {
|
|
23
|
+
parsed = typeof text === "string" ? JSON.parse(text) : text;
|
|
24
|
+
} catch (err) {
|
|
25
|
+
throw new Error(`tools/list is not valid JSON: ${err?.message ?? err}`);
|
|
26
|
+
}
|
|
27
|
+
const tools = Array.isArray(parsed)
|
|
28
|
+
? parsed
|
|
29
|
+
: Array.isArray(parsed?.tools)
|
|
30
|
+
? parsed.tools
|
|
31
|
+
: Array.isArray(parsed?.result?.tools)
|
|
32
|
+
? parsed.result.tools
|
|
33
|
+
: null;
|
|
34
|
+
if (!tools) throw new Error("tools/list must be an array, {tools:[...]} or a JSON-RPC tools/list response");
|
|
35
|
+
if (tools.some((t) => !t || typeof t !== "object" || !str(t.name))) {
|
|
36
|
+
throw new Error("every tool needs a non-empty name");
|
|
37
|
+
}
|
|
38
|
+
return tools;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Detect the JSON-RPC envelope so the card can state what the bytes were. */
|
|
42
|
+
export function toolsListShape(text) {
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
parsed = typeof text === "string" ? JSON.parse(text) : text;
|
|
46
|
+
} catch {
|
|
47
|
+
return "unknown";
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(parsed)) return "array";
|
|
50
|
+
if (Array.isArray(parsed?.tools)) return "object";
|
|
51
|
+
if (Array.isArray(parsed?.result?.tools)) return "json-rpc";
|
|
52
|
+
return "unknown";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Structural verdict. Deliberately conservative:
|
|
57
|
+
* BLOCKED — a HIGH manifest finding or a DANGEROUS description scan.
|
|
58
|
+
* REVIEW — anything medium/suspicious, plus any manifest-only card.
|
|
59
|
+
* PASS — no structural finding at all. PASS is not SAFE.
|
|
60
|
+
*/
|
|
61
|
+
export function verdictFor(manifestAudit, scan, coverage) {
|
|
62
|
+
const reasons = [];
|
|
63
|
+
if (!coverage.tools) reasons.push("TOOLS_LIST_MISSING: descriptions were not scanned, so the surface is not covered");
|
|
64
|
+
if (manifestAudit.risk === "high") reasons.push("MANIFEST_HIGH: at least one HIGH-severity manifest finding");
|
|
65
|
+
if (scan.verdict === "DANGEROUS") reasons.push("TOOLS_DANGEROUS: at least one HIGH-severity description finding");
|
|
66
|
+
if (reasons.length > 0) return { verdict: "BLOCKED", reasons };
|
|
67
|
+
if (manifestAudit.risk === "medium" || scan.verdict === "SUSPICIOUS" || !coverage.manifest) reasons.push("REVIEW_REQUIRED: medium-severity findings or missing manifest");
|
|
68
|
+
if (manifestAudit.findings.length > 0) reasons.push(`${manifestAudit.findings.length} informational/low manifest finding(s)`);
|
|
69
|
+
return { verdict: reasons.length > 0 ? "REVIEW" : "PASS", reasons };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const esc = (s) => String(s ?? "").replace(/\|/g, "\\|").replace(/\n/g, " ");
|
|
73
|
+
const short = (s, n = 96) => {
|
|
74
|
+
const v = String(s ?? "");
|
|
75
|
+
return v.length <= n ? v : v.slice(0, n - 1) + "…";
|
|
76
|
+
};
|
|
77
|
+
const code = (v) => "`" + String(v).replace(/`/g, "'") + "`";
|
|
78
|
+
|
|
79
|
+
function findingsTable(findings) {
|
|
80
|
+
if (findings.length === 0) return "_No manifest findings._";
|
|
81
|
+
const rows = findings.map(
|
|
82
|
+
(f) => `| ${code(f.id)} | ${f.severity} | ${esc(f.detail)}${f.evidence === undefined || f.evidence === null ? "" : ` — ${code(short(JSON.stringify(f.evidence), 120))}`} |`,
|
|
83
|
+
);
|
|
84
|
+
return ["| Finding | Severity | Detail |", "|---|---|---|", ...rows].join("\n");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Build the card.
|
|
89
|
+
*
|
|
90
|
+
* @param {object} input
|
|
91
|
+
* @param {object} input.manifest server.json record
|
|
92
|
+
* @param {Array} [input.tools] tools/list entries
|
|
93
|
+
* @param {object} [input.sources] { manifest: label, tools: label }
|
|
94
|
+
* @param {string} [input.date] YYYY-MM-DD, caller-supplied (no wall clock here)
|
|
95
|
+
* @param {string} [input.url] published URL, if any
|
|
96
|
+
* @param {string} [input.toolsShape] array | object | json-rpc
|
|
97
|
+
*/
|
|
98
|
+
export function buildTrustCard(input = {}) {
|
|
99
|
+
const manifest = input.manifest;
|
|
100
|
+
if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {
|
|
101
|
+
throw new Error("server.json must be a JSON object");
|
|
102
|
+
}
|
|
103
|
+
const tools = Array.isArray(input.tools) ? input.tools : null;
|
|
104
|
+
if (!tools) throw new Error("tools/list is required: a card that has not scanned the tool surface is not a trust card");
|
|
105
|
+
if (input.date !== undefined && input.date !== null && !/^\d{4}-\d{2}-\d{2}$/.test(String(input.date))) {
|
|
106
|
+
throw new Error("--date must be YYYY-MM-DD");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const coverage = { manifest: true, tools: true };
|
|
110
|
+
const manifestAudit = auditServerManifest({ manifest });
|
|
111
|
+
const scan = scanToolDescriptions({ tools });
|
|
112
|
+
const { verdict, reasons } = verdictFor(manifestAudit, scan, coverage);
|
|
113
|
+
|
|
114
|
+
const serverName = str(input.name) ?? manifestAudit.serverName ?? str(manifest.name) ?? "unnamed server";
|
|
115
|
+
const version = manifest.version ?? "unpinned";
|
|
116
|
+
const toolsDigest = digestOf(tools);
|
|
117
|
+
const sources = { manifest: input.sources?.manifest ?? "server.json", tools: input.sources?.tools ?? "tools/list" };
|
|
118
|
+
const cardDigest = digestOf({
|
|
119
|
+
spec: TRUST_CARD_SPEC,
|
|
120
|
+
server: serverName,
|
|
121
|
+
version,
|
|
122
|
+
verdict,
|
|
123
|
+
manifestDigest: manifestAudit.manifestDigest,
|
|
124
|
+
toolsDigest,
|
|
125
|
+
toolsCount: tools.length,
|
|
126
|
+
date: input.date ?? null,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const byTool = new Map();
|
|
130
|
+
for (const f of scan.findings) {
|
|
131
|
+
const list = byTool.get(f.tool) ?? [];
|
|
132
|
+
list.push(f);
|
|
133
|
+
byTool.set(f.tool, list);
|
|
134
|
+
}
|
|
135
|
+
const inventory = tools
|
|
136
|
+
.map((t, i) => {
|
|
137
|
+
const hits = byTool.get(String(t.name)) ?? [];
|
|
138
|
+
const status = hits.length === 0 ? "clean" : hits.some((h) => h.severity === "HIGH") ? "**FLAGGED**" : "review";
|
|
139
|
+
return `| ${i + 1} | ${code(short(t.name, 48))} | ${short(t.description, 80) || "—"} | ${status} |`;
|
|
140
|
+
})
|
|
141
|
+
.join("\n");
|
|
142
|
+
|
|
143
|
+
const flagged = scan.findings.length === 0
|
|
144
|
+
? "_No hidden characters, HTML comments or imperative phrases were found in any tool description._"
|
|
145
|
+
: scan.findings
|
|
146
|
+
.map((f) => `- ${code(f.tool)} — ${f.kind}${f.codepoint ? ` ${f.codepoint}` : f.phrase ? ` "${esc(f.phrase)}"` : ""} at offset ${f.index} (${f.severity})`)
|
|
147
|
+
.join("\n");
|
|
148
|
+
|
|
149
|
+
const lines = [
|
|
150
|
+
`# Trust card — ${serverName} ${version}`,
|
|
151
|
+
"",
|
|
152
|
+
`> **Verdict: ${verdict}** · manifest risk \`${manifestAudit.risk}\` · tool descriptions \`${scan.verdict}\` · ${tools.length} tools`,
|
|
153
|
+
">",
|
|
154
|
+
"> Generated by CoreGuard (`CG-TC/1`) from the published server record and a live `tools/list`. Structural audit only: it executes nothing and contacts no network. **PASS ≠ SAFE; absence of findings is not proof of safety.**",
|
|
155
|
+
"",
|
|
156
|
+
"| Field | Value |",
|
|
157
|
+
"|---|---|",
|
|
158
|
+
`| Server | ${esc(serverName)} |`,
|
|
159
|
+
`| Version | ${code(version)} |`,
|
|
160
|
+
`| Transport | ${manifestAudit.transport} |`,
|
|
161
|
+
`| Tools | ${tools.length} — ${code(toolsDigest)} |`,
|
|
162
|
+
`| Manifest digest | ${code(manifestAudit.manifestDigest)} |`,
|
|
163
|
+
`| Card digest | ${code(cardDigest)} |`,
|
|
164
|
+
`| Sources | record: ${code(sources.manifest)} · tools: ${code(sources.tools)} |`,
|
|
165
|
+
...(input.date ? [`| Card date | ${code(input.date)} |`] : []),
|
|
166
|
+
"",
|
|
167
|
+
"## Why this verdict",
|
|
168
|
+
"",
|
|
169
|
+
...(reasons.length === 0 ? ["_No structural finding was raised. That is all this card can say._"] : reasons.map((r) => `- ${r}`)),
|
|
170
|
+
"",
|
|
171
|
+
"## Server-record findings",
|
|
172
|
+
"",
|
|
173
|
+
findingsTable(manifestAudit.findings),
|
|
174
|
+
"",
|
|
175
|
+
"## Tool-description scan",
|
|
176
|
+
"",
|
|
177
|
+
`Scanned ${tools.length} descriptions. Findings: ${scan.counts.findings} (${scan.counts.high} high, ${scan.counts.medium} medium).`,
|
|
178
|
+
"",
|
|
179
|
+
flagged,
|
|
180
|
+
"",
|
|
181
|
+
"## Tool inventory",
|
|
182
|
+
"",
|
|
183
|
+
"| # | Tool | Description (truncated) | Scan |",
|
|
184
|
+
"|---|---|---|---|",
|
|
185
|
+
inventory,
|
|
186
|
+
"",
|
|
187
|
+
"## What was checked",
|
|
188
|
+
"",
|
|
189
|
+
"- Server record: pinned version and package, declared distribution, transport, repository provenance, secret-shaped environment variables.",
|
|
190
|
+
"- Tool descriptions: bidi overrides, Unicode tag/smuggling characters, zero-width and soft-hyphen characters, HTML comments, and a fixed imperative-phrase list.",
|
|
191
|
+
"- Binding: both digests are embedded above, so a changed record or a single changed description changes the card digest.",
|
|
192
|
+
"",
|
|
193
|
+
"## What this does not prove",
|
|
194
|
+
"",
|
|
195
|
+
"- It does not execute the server, its package, or its remote endpoint.",
|
|
196
|
+
"- It does not review source code, permissions, data handling, licensing, or the payment behaviour of any tool.",
|
|
197
|
+
"- It does not detect every poisoning technique; the phrase list is fixed and the scanner is intentionally narrow.",
|
|
198
|
+
"- A clean card is a starting point for review, not an approval.",
|
|
199
|
+
"",
|
|
200
|
+
"## Reproduce",
|
|
201
|
+
"",
|
|
202
|
+
"```bash",
|
|
203
|
+
`coreguard-trust-card \\`,
|
|
204
|
+
` --server-json ${sources.manifest === "server.json" ? "server.json" : sources.manifest} \\`,
|
|
205
|
+
` --tools-list ${sources.tools === "tools/list" ? "tools.json" : sources.tools} \\`,
|
|
206
|
+
` --out trust-card.md`,
|
|
207
|
+
"```",
|
|
208
|
+
"",
|
|
209
|
+
"Same inputs and same `--date` produce the same card digest. Verify it by re-running and comparing the digest line.",
|
|
210
|
+
"",
|
|
211
|
+
"<!-- paste-ready summary for a listing page -->",
|
|
212
|
+
"",
|
|
213
|
+
`> **Trust card:** ${verdict} · manifest risk \`${manifestAudit.risk}\` · tool descriptions \`${scan.verdict}\` · ${tools.length} tools · card \`${cardDigest}\``,
|
|
214
|
+
`> Structural audit by CoreGuard (CG-TC/1) from the server record and a live \`tools/list\`. PASS ≠ SAFE.${input.url ? ` Full card: ${input.url}` : ""}`,
|
|
215
|
+
"",
|
|
216
|
+
];
|
|
217
|
+
|
|
218
|
+
const json = {
|
|
219
|
+
spec: TRUST_CARD_SPEC,
|
|
220
|
+
server: { name: serverName, version, transport: manifestAudit.transport },
|
|
221
|
+
verdict,
|
|
222
|
+
reasons,
|
|
223
|
+
coverage,
|
|
224
|
+
manifest: {
|
|
225
|
+
digest: manifestAudit.manifestDigest,
|
|
226
|
+
risk: manifestAudit.risk,
|
|
227
|
+
findings: manifestAudit.findings,
|
|
228
|
+
counts: manifestAudit.counts,
|
|
229
|
+
},
|
|
230
|
+
tools: {
|
|
231
|
+
count: tools.length,
|
|
232
|
+
digest: toolsDigest,
|
|
233
|
+
shape: input.toolsShape ?? "unknown",
|
|
234
|
+
scan: { verdict: scan.verdict, counts: scan.counts, findings: scan.findings },
|
|
235
|
+
},
|
|
236
|
+
sources,
|
|
237
|
+
cardDigest,
|
|
238
|
+
...(input.date ? { date: input.date } : {}),
|
|
239
|
+
...(input.url ? { url: input.url } : {}),
|
|
240
|
+
boundary: "structural audit only; executes nothing; PASS ≠ SAFE",
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
return { markdown: lines.join("\n"), json, verdict, cardDigest, manifestAudit, scan };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Exit status for the gate: 0 below the threshold, 1 at/above it. */
|
|
247
|
+
export function exitCodeFor(verdict, failOn = "high") {
|
|
248
|
+
const rank = { PASS: 0, REVIEW: 1, BLOCKED: 2 };
|
|
249
|
+
const threshold = { none: 99, high: 2, medium: 1 }[failOn];
|
|
250
|
+
if (threshold === undefined) throw new Error("--fail-on must be none, medium or high");
|
|
251
|
+
return rank[verdict] >= threshold ? 1 : 0;
|
|
252
|
+
}
|