Skip to main content

fallow_types/
results_security_types.rs

1//! Security candidates, sinks, boundaries, taint flows, runtime context,
2//! defensive controls, and the attack surface.
3
4use super::*;
5
6/// The kind of security candidate. Findings are candidates for downstream agent
7/// verification, NOT verified vulnerabilities.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
10#[serde(rename_all = "kebab-case")]
11pub enum SecurityFindingKind {
12    /// A `"use client"` file transitively imports a module that reads a
13    /// non-public `process.env` secret (graph-structural; bespoke, not catalogue).
14    ClientServerLeak,
15    /// A syntactic sink site matched against the data-driven catalogue
16    /// (`security_matchers.toml`). Serializes `"tainted-sink"`; the CWE class is
17    /// carried in `category` + `cwe`. ONE variant covers all catalogue categories.
18    TaintedSink,
19}
20
21/// How strongly the untrusted-source signal is associated with the sink, a
22/// structured discriminator so a consumer can tier candidates without parsing
23/// the human `evidence` prose. Present only when
24/// [`SecurityReachability::reachable_from_untrusted_source`] is true. Neither
25/// value proves exploitability; both are ranking signals (issue #885 doctrine:
26/// rank, never gate).
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
28#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
29#[serde(rename_all = "kebab-case")]
30pub enum TaintConfidence {
31    /// The sink's argument traces back to a known untrusted-source read in the
32    /// same statement / module (the intra-module back-trace, issue #859). The
33    /// strong, high-value candidate: a specific source expression is implicated.
34    ArgLevel,
35    /// The sink merely lives in a module that is import-reachable from a module
36    /// containing an untrusted source (issue #885). The weak candidate: only the
37    /// module is implicated, not a specific value path to the sink argument.
38    ModuleLevel,
39}
40
41/// Graph-derived reachability ranking signal for a security candidate. Computed
42/// from the existing module graph after detection, never proven exploitable.
43/// Used to surface candidates that sit on a request/runtime-reachable surface,
44/// receive same-module source evidence, or are import-reachable from an
45/// untrusted-source module above isolated helpers or scripts.
46///
47/// This is a relative-ordering signal, NOT a `confidence` or `signal_strength`
48/// score: fallow does not prove the path is exploitable.
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
51pub struct SecurityReachability {
52    /// Whether the anchor module is reachable from a runtime/application entry
53    /// point (route handlers, server entry, framework runtime roots), the
54    /// closest graph proxy for an external/request input surface. Code reachable
55    /// only from test entry points does not count.
56    pub reachable_from_entry: bool,
57    /// Whether the anchor module is reachable over value imports from a module
58    /// that reads a known untrusted input source. Module-level only: this does
59    /// not prove a specific source value reaches the sink argument.
60    #[serde(default)]
61    pub reachable_from_untrusted_source: bool,
62    /// Structured tier of the untrusted-source association: `arg-level` when the
63    /// sink argument traces to a same-module source read (strong), `module-level`
64    /// when only the module is import-reachable from a source (weak). Present
65    /// exactly when `reachable_from_untrusted_source` is true, so a consumer can
66    /// separate strong from weak candidates from this field alone without parsing
67    /// the `evidence` string. Not an exploitability proof.
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    pub taint_confidence: Option<TaintConfidence>,
70    /// Number of value-import hops from the untrusted-source module to the sink
71    /// module when `reachable_from_untrusted_source` is true.
72    #[serde(default, skip_serializing_if = "Option::is_none")]
73    pub untrusted_source_hop_count: Option<u32>,
74    /// Module-level import path from the untrusted-source module to the sink
75    /// anchor. Empty when no source module reaches this candidate. The path is a
76    /// ranking explanation, not a value-flow proof.
77    #[serde(default, skip_serializing_if = "Vec::is_empty")]
78    pub untrusted_source_trace: Vec<TraceHop>,
79    /// Number of distinct modules that transitively depend on the anchor module
80    /// (fan-in via the graph's reverse-dependency index). A higher value means a
81    /// wider surface: more call sites could route untrusted input into the sink.
82    pub blast_radius: u32,
83    /// Whether the anchor module participates in an architecture-boundary
84    /// violation found in the same run (as the importing or imported file).
85    /// Optional pairing: a candidate that also crosses a declared boundary is a
86    /// stronger review target.
87    pub crosses_boundary: bool,
88}
89
90/// Dead-code cross-link attached to a security candidate when fallow's dead-code
91/// pass reports the same anchor as removable code.
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
93#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
94pub struct SecurityDeadCodeContext {
95    /// Dead-code issue kind that matched the security candidate.
96    pub kind: SecurityDeadCodeKind,
97    /// Unused export name when `kind` is `unused-export`.
98    #[serde(default, skip_serializing_if = "Option::is_none")]
99    pub export_name: Option<String>,
100    /// Dead-code finding line when available.
101    #[serde(default, skip_serializing_if = "Option::is_none")]
102    pub line: Option<u32>,
103    /// Agent-facing guidance for deciding between deletion and hardening.
104    pub guidance: String,
105}
106
107/// Dead-code issue kind linked to a security candidate.
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
109#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
110#[serde(rename_all = "kebab-case")]
111pub enum SecurityDeadCodeKind {
112    /// The candidate's anchor file is also reported as an unused file.
113    UnusedFile,
114    /// The candidate's anchor sits on an unused export declaration.
115    UnusedExport,
116}
117
118/// Internal row for a security sink-shaped callee that extraction could not
119/// flatten to a static catalogue path.
120#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
121#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
122pub struct SecurityUnresolvedCalleeDiagnostic {
123    /// File containing the skipped callee. Absolute internally.
124    #[serde(serialize_with = "serde_path::serialize")]
125    pub path: PathBuf,
126    /// 1-based line of the skipped callee.
127    pub line: u32,
128    /// 0-based byte column of the skipped callee.
129    pub col: u32,
130    /// Why the callee could not be flattened.
131    pub reason: SkippedSecurityCalleeReason,
132    /// Compact syntax shape of the skipped callee.
133    pub expression_kind: SkippedSecurityCalleeExpressionKind,
134}
135
136/// The sink slot of a [`SecurityCandidate`]: a self-contained description of the
137/// matched sink site. Echoes the finding's own span (`path`/`line`/`col`) plus
138/// the catalogue `category`/`cwe` and the captured `callee`, so an agent can act
139/// on `candidate.sink` in isolation (e.g. after fanning a finding out to a
140/// sub-agent) without reading the parent finding.
141#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
142#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
143pub struct SecurityCandidateSink {
144    /// File of the sink site. Absolute internally; JSON strips the project root
145    /// via `serde_path::serialize`.
146    #[serde(serialize_with = "serde_path::serialize")]
147    pub path: PathBuf,
148    /// 1-based line of the sink site.
149    pub line: u32,
150    /// 0-based byte column of the sink site.
151    pub col: u32,
152    /// Catalogue category id of the sink (e.g. `"dangerous-html"`). For
153    /// `client-server-leak` this is `"client-server-leak"` for the secret-leak
154    /// finding, and `"server-only-import"` when a `"use client"` cone reaches
155    /// server-only code. Older releases left it absent for the secret leak.
156    #[serde(default, skip_serializing_if = "Option::is_none")]
157    pub category: Option<String>,
158    /// CWE number declared by the catalogue entry. `None` for
159    /// `client-server-leak`; never fabricated beyond the catalogue's value.
160    #[serde(default, skip_serializing_if = "Option::is_none")]
161    pub cwe: Option<u32>,
162    /// The sink callee (the dangerous function or member path, e.g.
163    /// `"el.innerHTML"`, `"child_process.exec"`) captured by the catalogue match.
164    /// `None` for `client-server-leak` and matches that name no callee.
165    #[serde(default, skip_serializing_if = "Option::is_none")]
166    pub callee: Option<String>,
167    /// URL construction shape for SSRF and open-redirect style candidates when
168    /// fallow can classify whether the origin is fixed or dynamic. Absent for
169    /// non-URL sinks and unclassified URL expressions.
170    #[serde(default, skip_serializing_if = "Option::is_none")]
171    pub url_shape: Option<SecurityUrlShape>,
172}
173
174/// A declared architecture-zone crossing, recovered by correlating a finding's
175/// anchor against the run's architecture-boundary violations.
176#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
177#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
178pub struct SecurityZoneCrossing {
179    /// Zone the importing side belongs to.
180    pub from: String,
181    /// Zone the imported side belongs to.
182    pub to: String,
183}
184
185/// The boundary slot of a [`SecurityCandidate`]: which structural boundaries the
186/// candidate's flow crosses. A flow that crosses a client/server or module
187/// boundary is a stronger review target than a self-contained one; the boundary
188/// is fallow's structural signal over a pure source-sink match.
189///
190/// Two further boundary kinds are reserved for a follow-up and are deliberately
191/// absent here rather than emitted as always-false: `export_visibility` (is the
192/// sink on a publicly-exported symbol?) and a package boundary (does the flow
193/// cross an npm-package edge?). Both need new graph derivation that does not
194/// exist today; emitting them as `false` would misreport "we checked and it does
195/// not cross" when fallow has not checked at all.
196#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
197#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
198pub struct SecurityCandidateBoundary {
199    /// Whether the finding crosses a client/server boundary (a `"use client"`
200    /// file appears in the trace). True only for `client-server-leak` today;
201    /// `tainted-sink` candidates carry no client/server marker.
202    pub client_server: bool,
203    /// Whether an untrusted source reaches the sink across one or more
204    /// value-import (module) hops. Derived from the reachability hop count.
205    pub cross_module: bool,
206    /// The architecture-zone crossing when the anchor participates in a declared
207    /// boundary-rule violation in the same run. `None` when it crosses no
208    /// declared zone boundary.
209    #[serde(default, skip_serializing_if = "Option::is_none")]
210    pub architecture_zone: Option<SecurityZoneCrossing>,
211}
212
213/// Network-destination context for a `secret-to-network` candidate (#890): where
214/// the secret-bearing network call sends its data. Present only on
215/// network-category candidates. A consuming agent uses it to triage exfil
216/// (dynamic / untrusted destination) from intended auth (a literal provider
217/// host) without re-reading source.
218#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
219#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
220pub struct SecurityNetworkContext {
221    /// The network call's destination as a static URL string literal, or absent
222    /// when the destination is dynamic (not a literal). A dynamic destination is
223    /// the higher-signal exfil case; a literal provider host is usually intended
224    /// auth.
225    #[serde(default, skip_serializing_if = "Option::is_none")]
226    pub destination: Option<String>,
227}
228
229/// An agent-actionable candidate record on a [`SecurityFinding`]. fallow fills
230/// `source_kind`, `sink`, and `boundary`. The exploitability impact is
231/// deliberately NOT a field: `severity` on the parent finding is only a
232/// review-priority tier, while deciding exploitability remains the consuming
233/// agent's job. A perpetually-null `impact` key would only train consumers to
234/// ignore it. The agent reads this record, then writes its own impact verdict
235/// downstream.
236#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
237#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
238pub struct SecurityCandidate {
239    /// The kind of untrusted input that reaches the sink, as a stable catalogue
240    /// source id (`"http-request-input"`, `"process-env"`, `"process-argv"`,
241    /// `"message-event-data"`, `"location-input"`, ...). `None`/absent when no
242    /// untrusted source was matched (always `None` for `client-server-leak`).
243    /// This is an open string set, driven by the data-driven source catalogue; a
244    /// consumer should treat an unknown id as "untrusted source of unknown kind"
245    /// and never drop the candidate on that basis.
246    #[serde(default, skip_serializing_if = "Option::is_none")]
247    pub source_kind: Option<String>,
248    /// The sink the candidate fires on, self-contained so the record is
249    /// actionable without reading the parent finding.
250    pub sink: SecurityCandidateSink,
251    /// The structural boundary the flow crosses.
252    pub boundary: SecurityCandidateBoundary,
253    /// Network-destination context, present only on `secret-to-network` (#890)
254    /// candidates: the host the secret-bearing call targets, so an agent can
255    /// triage exfil from intended auth. Absent for every other category.
256    #[serde(default, skip_serializing_if = "Option::is_none")]
257    pub network: Option<SecurityNetworkContext>,
258}
259
260/// One endpoint (source or sink node) of a [`SecurityTaintFlow`].
261#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
262#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
263pub struct TaintEndpoint {
264    /// File of the endpoint. Absolute internally; JSON strips the project root.
265    #[serde(serialize_with = "serde_path::serialize")]
266    pub path: PathBuf,
267    /// 1-based line of the endpoint.
268    pub line: u32,
269    /// 0-based byte column of the endpoint.
270    pub col: u32,
271}
272
273/// Compact taint-flow path shape. The ordered per-hop trace is NOT duplicated
274/// here: it lives on [`SecurityReachability::untrusted_source_trace`]. This
275/// carries only the flow's structural summary (intra-module flow plus the
276/// cross-module hop count) so consumers do not parse two copies of the hops.
277#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
278#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
279pub struct TaintPath {
280    /// Whether the source and sink sit in the same module (no import hop between
281    /// them); the source-to-sink association is intra-module.
282    pub intra_module: bool,
283    /// Number of value-import hops from the untrusted-source module to the sink
284    /// module. Zero for an intra-module flow.
285    pub cross_module_hops: u32,
286}
287
288/// A source-to-sink taint-flow triple, emitted only when an untrusted source is
289/// import-reachable to the sink (`reachability.reachable_from_untrusted_source`).
290/// The `{ source, sink, path }` shape matches the model agent SAST tooling
291/// expects (cf. Semgrep `taint_source` / `taint_sink`, SARIF `threadFlows`).
292#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
293#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
294pub struct SecurityTaintFlow {
295    /// The untrusted-source endpoint (first hop of the reachability trace).
296    pub source: TaintEndpoint,
297    /// The sink endpoint (terminal hop of the reachability trace / the anchor).
298    pub sink: TaintEndpoint,
299    /// Compact flow shape: same-module flag plus module hop count. The full
300    /// ordered path is `reachability.untrusted_source_trace`.
301    pub path: TaintPath,
302}
303
304/// Runtime coverage state for the function enclosing a security sink.
305/// This is production-observation evidence, not an exploitability verdict.
306#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
307#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
308#[serde(rename_all = "kebab-case")]
309pub enum SecurityRuntimeState {
310    /// The sink sits inside a runtime hot path.
311    RuntimeHot,
312    /// The sink sits inside a tracked function with zero production invocations.
313    RuntimeCold,
314    /// The sink sits inside a tracked function the runtime layer marked as safe
315    /// to delete because it was never executed.
316    NeverExecuted,
317    /// The sink sits inside a function that executed, but below the low-traffic
318    /// threshold.
319    LowTraffic,
320    /// Runtime coverage could not classify the enclosing function.
321    CoverageUnavailable,
322    /// A static enclosing function was found, but the runtime report carried no
323    /// matching evidence for it.
324    RuntimeUnknown,
325}
326
327/// Runtime coverage context attached to a security candidate when
328/// `fallow security --runtime-coverage` is supplied.
329#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
330#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
331pub struct SecurityRuntimeContext {
332    /// Runtime state for the enclosing function.
333    pub state: SecurityRuntimeState,
334    /// Enclosing function name from static extraction.
335    pub function: String,
336    /// 1-based line where the enclosing function starts.
337    pub line: u32,
338    /// Observed invocation count when the runtime report provides it.
339    #[serde(default, skip_serializing_if = "Option::is_none")]
340    pub invocations: Option<u64>,
341    /// Runtime coverage stable function id, when available.
342    #[serde(default, skip_serializing_if = "Option::is_none")]
343    pub stable_id: Option<String>,
344    /// Short candidate-framed explanation of the runtime evidence.
345    #[serde(default, skip_serializing_if = "Option::is_none")]
346    pub evidence: Option<String>,
347}
348
349/// Verification-priority tier for a security candidate. This is ranking, not an
350/// exploitability verdict.
351#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
352#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
353#[serde(rename_all = "lowercase")]
354pub enum SecuritySeverity {
355    /// Highest-priority candidate based on reachability, boundary, or runtime-hot signals.
356    High,
357    /// Candidate has source-reachability evidence but no high-priority signal.
358    Medium,
359    /// Candidate has no source-reachability or boundary signal.
360    Low,
361}
362
363/// Control pattern observed in a file on an attack-surface import trace.
364/// Its presence does not prove that it executes before the sink or protects
365/// the same input.
366#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
367#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
368pub struct SecurityDefensiveControl {
369    /// Control family.
370    pub kind: SecurityControlKind,
371    /// File of the control site. Absolute internally; JSON strips the project root.
372    #[serde(serialize_with = "serde_path::serialize")]
373    pub path: PathBuf,
374    /// 1-based line of the control site.
375    pub line: u32,
376    /// 0-based byte column of the control site.
377    pub col: u32,
378    /// Flattened callee path or a stable synthetic guard name.
379    pub callee: String,
380}
381
382/// Agent-facing defensive-boundary verification context for one surface path.
383#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
384#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
385pub struct SecurityDefensiveBoundary {
386    /// Control patterns observed in files on this import trace. These are
387    /// verification hints, not proof of sink protection or value-level data flow.
388    pub controls: Vec<SecurityDefensiveControl>,
389    /// Verification question for the consuming agent. It is a prompt, not a
390    /// missing-guard verdict.
391    pub verification_prompt: String,
392}
393
394/// One untrusted entry to reachable sink path for `fallow security --surface`.
395#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
396#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
397pub struct SecurityAttackSurfaceEntry {
398    /// The untrusted-source endpoint.
399    pub source: TaintEndpoint,
400    /// The reachable sink endpoint and catalogue metadata.
401    pub sink: SecurityCandidateSink,
402    /// Ordered source to sink path. Same shape as the reachability trace so
403    /// consumers can reuse existing path handling.
404    pub path: Vec<TraceHop>,
405    /// Defensive-boundary context detected on this path.
406    pub defensive_boundary: SecurityDefensiveBoundary,
407}