1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Artifact graph for representing relationships between scanned assets.
use crate::findings::ArtifactKind;
use serde::{Deserialize, Serialize};
/// Capability exposed or requested by an artifact.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactCapability {
BrowserAccess,
NetworkAccess,
InstallExecution,
ExposesBinary,
PrivilegedRuntime,
HostFilesystemAccess,
ProcessExecution,
SecretAccess,
PersistenceSurface,
FilesystemWrite,
IdentityAccess,
InboundNetworkSurface,
}
/// Origin of a capability assessment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactCapabilitySource {
Declared,
Observed,
}
/// A capability attached to an artifact, including how it was derived.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactCapabilityFact {
pub capability: ArtifactCapability,
pub source: ArtifactCapabilitySource,
}
/// A node in the scanned artifact graph.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArtifactNode {
pub path: String,
pub kind: ArtifactKind,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub capabilities: Vec<ArtifactCapabilityFact>,
}
/// Describes the network endpoint category for a ConnectsTo or Downloads edge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EndpointKind {
/// Publicly addressable remote endpoint (attacker-controlled or external service).
Remote,
/// Known package registry (npm, PyPI, crates.io, …). Downloads from these are lower risk.
Registry,
/// Ephemeral or tunneled endpoint (ngrok, trycloudflare, …).
Transient,
/// Cloud provider metadata/control-plane endpoint (169.254.169.254, …).
ControlPlane,
/// Loopback or LAN-local endpoint.
Local,
}
/// A directed edge between two artifacts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArtifactEdge {
pub from: String,
pub to: String,
pub relation: ArtifactRelation,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub endpoint_kind: Option<EndpointKind>,
}
/// Relationship between two artifacts.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactRelation {
References,
Contains,
Locks,
Downloads,
Executes,
Loads,
Persists,
Mounts,
ConnectsTo,
Reads,
Writes,
AccessesSecrets,
}
/// Lightweight graph describing scanned artifacts and their relationships.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ArtifactGraph {
pub nodes: Vec<ArtifactNode>,
pub edges: Vec<ArtifactEdge>,
}
impl ArtifactGraph {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn add_node(&mut self, path: impl Into<String>, kind: ArtifactKind) {
let path = path.into();
self.add_node_with_capabilities(path, kind, Vec::new());
}
pub fn add_node_with_capabilities(
&mut self,
path: impl Into<String>,
kind: ArtifactKind,
capabilities: Vec<ArtifactCapabilityFact>,
) {
let path = path.into();
if let Some(existing) = self.nodes.iter_mut().find(|node| node.path == path) {
// Promote `kind` to the more specific classification. Pipeline
// ordering is not deterministic, so a "first wins" rule would
// silently lose more-specific kinds discovered later (e.g.
// `McpServerManifest` arriving after a generic `AgentInstruction`
// pre-classification). See `ArtifactKind::specificity` for the
// tier ordering and the tests in this module that pin the
// contract.
if kind.specificity() > existing.kind.specificity() {
existing.kind = kind;
}
for capability in capabilities {
if !existing.capabilities.iter().any(|fact| {
fact.capability == capability.capability && fact.source == capability.source
}) {
existing.capabilities.push(capability);
}
}
return;
}
self.nodes.push(ArtifactNode {
path,
kind,
capabilities,
});
}
pub fn add_edge(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
relation: ArtifactRelation,
) {
self.add_edge_with_endpoint(from, to, relation, None);
}
pub fn add_edge_with_endpoint(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
relation: ArtifactRelation,
endpoint_kind: Option<EndpointKind>,
) {
let edge = ArtifactEdge {
from: from.into(),
to: to.into(),
relation,
endpoint_kind,
};
// Edge identity is `(from, to, relation)` — `endpoint_kind` is an
// *annotation* on the edge, not a discriminator. Pre-fix the dedup
// also required `existing.endpoint_kind == edge.endpoint_kind`,
// so two calls adding the same `(from, to, relation)` triple with
// different annotations (e.g. `Some(Remote)` vs `Some(Registry)`)
// both survived and produced two distinct edges. The taint engine
// and capability scoring then double-counted the same logical edge.
// The fix dedupes on the triple and *upgrades* the existing
// annotation to the more-adversarial value when a duplicate fires.
if let Some(existing) = self.edges.iter_mut().find(|existing| {
existing.from == edge.from
&& existing.to == edge.to
&& std::mem::discriminant(&existing.relation)
== std::mem::discriminant(&edge.relation)
}) {
existing.endpoint_kind =
upgrade_endpoint_kind(existing.endpoint_kind, edge.endpoint_kind);
return;
}
self.edges.push(edge);
}
}
/// Pick the higher-priority annotation when two edges with the same
/// `(from, to, relation)` triple converge. Used by
/// [`ArtifactGraph::add_edge_with_endpoint`] to keep edge identity stable
/// while preserving the most-adversarial annotation. Priority (highest
/// first):
///
/// 1. [`EndpointKind::ControlPlane`] — cloud metadata (IMDS), highest signal.
/// 2. [`EndpointKind::Transient`] — ngrok / trycloudflare tunnels.
/// 3. [`EndpointKind::Remote`] — public attacker-controlled.
/// 4. [`EndpointKind::Local`] — loopback / LAN.
/// 5. [`EndpointKind::Registry`] — known package registry, lowest concern.
/// 6. `None` — unknown, lowest priority.
fn upgrade_endpoint_kind(
existing: Option<EndpointKind>,
incoming: Option<EndpointKind>,
) -> Option<EndpointKind> {
fn rank(kind: Option<EndpointKind>) -> u8 {
match kind {
Some(EndpointKind::ControlPlane) => 5,
Some(EndpointKind::Transient) => 4,
Some(EndpointKind::Remote) => 3,
Some(EndpointKind::Local) => 2,
Some(EndpointKind::Registry) => 1,
None => 0,
}
}
if rank(incoming) > rank(existing) {
incoming
} else {
existing
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Contract: re-inserting the same path with a more specific
/// `ArtifactKind` upgrades the recorded kind. Without this,
/// pipeline-ordering randomness silently shadowed
/// `McpServerManifest` (specificity 4) behind an earlier
/// `AgentInstruction` (specificity 3) classification.
#[test]
fn add_node_promotes_to_more_specific_kind() {
let mut g = ArtifactGraph::new();
g.add_node("/pkg/manifest", ArtifactKind::AgentInstruction);
g.add_node("/pkg/manifest", ArtifactKind::McpServerManifest);
let node = g
.nodes
.iter()
.find(|n| n.path == "/pkg/manifest")
.expect("node must exist");
assert_eq!(
node.kind,
ArtifactKind::McpServerManifest,
"More specific kind MUST replace less specific one"
);
}
/// Inverse direction: a less specific later insertion does NOT demote.
#[test]
fn add_node_does_not_demote_kind() {
let mut g = ArtifactGraph::new();
g.add_node("/pkg/manifest", ArtifactKind::McpServerManifest);
g.add_node("/pkg/manifest", ArtifactKind::GenericArtifact);
let node = g.nodes.iter().find(|n| n.path == "/pkg/manifest").unwrap();
assert_eq!(
node.kind,
ArtifactKind::McpServerManifest,
"Less specific kind MUST NOT demote a more specific one"
);
}
/// Idempotent: same kind twice doesn't change anything.
#[test]
fn add_node_is_idempotent_for_same_kind() {
let mut g = ArtifactGraph::new();
g.add_node("/pkg/x", ArtifactKind::PackageManifest);
g.add_node("/pkg/x", ArtifactKind::PackageManifest);
assert_eq!(
g.nodes.iter().filter(|n| n.path == "/pkg/x").count(),
1,
"Re-inserting the same path must NOT duplicate the node"
);
}
/// Equal-specificity insertions keep the first one (stable behaviour
/// within a tier; only cross-tier upgrades fire).
#[test]
fn add_node_keeps_first_within_same_specificity_tier() {
let mut g = ArtifactGraph::new();
g.add_node("/pkg/x", ArtifactKind::PackageManifest); // tier 4
g.add_node("/pkg/x", ArtifactKind::McpServerManifest); // tier 4
let node = g.nodes.iter().find(|n| n.path == "/pkg/x").unwrap();
assert_eq!(node.kind, ArtifactKind::PackageManifest);
}
/// # Contract
///
/// Two `add_edge_with_endpoint` calls with the same
/// `(from, to, relation)` triple but different `endpoint_kind`
/// annotations MUST collapse to a single edge whose annotation is
/// upgraded to the more-adversarial value. Pre-fix the dedup
/// included `endpoint_kind` in the equality, so the same logical
/// download edge appeared twice when one detector annotated it
/// `Some(Remote)` and another annotated it `Some(Registry)`,
/// inflating taint-engine path counts and capability scoring.
#[test]
fn add_edge_dedupes_on_triple_and_upgrades_endpoint_annotation() {
let mut g = ArtifactGraph::new();
g.add_edge_with_endpoint(
"a",
"b",
ArtifactRelation::Downloads,
Some(EndpointKind::Registry),
);
g.add_edge_with_endpoint(
"a",
"b",
ArtifactRelation::Downloads,
Some(EndpointKind::Remote),
);
assert_eq!(
g.edges.len(),
1,
"duplicate (from,to,relation) MUST NOT produce two edges; got {:?}",
g.edges
);
assert_eq!(
g.edges[0].endpoint_kind,
Some(EndpointKind::Remote),
"annotation must upgrade to the more-adversarial value (Remote > Registry)"
);
}
/// # Contract (priority order)
///
/// Higher-priority annotations win regardless of insertion order.
/// ControlPlane (IMDS) is the highest priority; Transient (ngrok),
/// Remote, Local, and Registry follow in descending order. `None`
/// is the lowest priority.
#[test]
fn add_edge_endpoint_priority_order_preserves_highest() {
let mut g = ArtifactGraph::new();
// Insert in reverse priority — the final annotation must still
// be ControlPlane.
g.add_edge_with_endpoint("a", "b", ArtifactRelation::Downloads, None);
g.add_edge_with_endpoint(
"a",
"b",
ArtifactRelation::Downloads,
Some(EndpointKind::Registry),
);
g.add_edge_with_endpoint(
"a",
"b",
ArtifactRelation::Downloads,
Some(EndpointKind::Local),
);
g.add_edge_with_endpoint(
"a",
"b",
ArtifactRelation::Downloads,
Some(EndpointKind::Remote),
);
g.add_edge_with_endpoint(
"a",
"b",
ArtifactRelation::Downloads,
Some(EndpointKind::Transient),
);
g.add_edge_with_endpoint(
"a",
"b",
ArtifactRelation::Downloads,
Some(EndpointKind::ControlPlane),
);
assert_eq!(g.edges.len(), 1);
assert_eq!(
g.edges[0].endpoint_kind,
Some(EndpointKind::ControlPlane),
"ControlPlane (IMDS) MUST be the surviving annotation"
);
}
/// # Contract (negative)
///
/// Edges with different `relation` MUST stay distinct even when
/// `(from, to)` matches — a `Downloads` and a `Reads` edge between
/// the same pair encode different semantics and the taint engine
/// distinguishes them.
#[test]
fn add_edge_keeps_different_relations_distinct() {
let mut g = ArtifactGraph::new();
g.add_edge("a", "b", ArtifactRelation::Downloads);
g.add_edge("a", "b", ArtifactRelation::Reads);
assert_eq!(g.edges.len(), 2);
}
}