fallow_types/results_trace_hop.rs
1//! The hops of the structural trace of a security finding.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6
7use crate::serde_path;
8
9/// The role a hop plays in a security finding's structural import trace.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
12#[serde(rename_all = "kebab-case")]
13pub enum TraceHopRole {
14 /// The `"use client"` boundary file the finding is anchored on.
15 ClientBoundary,
16 /// A module that reads an untrusted input source such as request data,
17 /// where the candidate's sink argument actually traces back to that read in
18 /// the same statement (arg-level, the strong intra-module association).
19 UntrustedSource,
20 /// A module that merely contains an untrusted-input source somewhere and is
21 /// import-reachable to the sink module (module-level, issue #885). This is a
22 /// reachability signal, NOT a proven value path: the specific source value
23 /// is not shown to reach the sink argument. Labeled distinctly from
24 /// `UntrustedSource` so a consumer never reads a module-level hop as a
25 /// value-flow proof.
26 ModuleSource,
27 /// An intermediate module on the transitive import path.
28 Intermediate,
29 /// The module that reads the secret.
30 SecretSource,
31 /// The syntactic sink site of a catalogue-driven `tainted-sink` candidate
32 /// (the single hop the `tainted_sink` detector emits). Distinct from
33 /// `SecretSource`, which is specific to the `client-server-leak` rule.
34 Sink,
35}
36
37/// One hop in a security finding's structural trace. Stored as an absolute path
38/// internally; JSON serialization strips the project root via
39/// `serde_path::serialize`.
40#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
41#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
42pub struct TraceHop {
43 /// File on this hop of the import chain.
44 #[serde(serialize_with = "serde_path::serialize")]
45 pub path: PathBuf,
46 /// 1-based line number. Import-chain hops point at the import site. A
47 /// `secret-source` hop points at the first secret read of the module, and
48 /// a `client-server-leak` sink hop at its first server-only import.
49 pub line: u32,
50 /// 0-based byte column offset.
51 pub col: u32,
52 /// Role of this hop in the chain.
53 pub role: TraceHopRole,
54 /// Every non-public env read that makes this module a secret source, in
55 /// source order. Present only on a `secret-source` hop; the hop `line` and
56 /// `col` equal the first entry.
57 #[serde(default, skip_serializing_if = "Vec::is_empty")]
58 pub secret_reads: Vec<TraceSecretRead>,
59}
60
61/// One non-public env read on a `secret-source` trace hop.
62#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
63#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
64pub struct TraceSecretRead {
65 /// The read path, for example `process.env.SESSION_SECRET`.
66 pub name: String,
67 /// 1-based line number of the read.
68 pub line: u32,
69 /// 0-based byte column offset of the read.
70 pub col: u32,
71}
72
73impl TraceHop {
74 /// A hop with no secret reads.
75 #[must_use]
76 pub const fn new(path: PathBuf, line: u32, col: u32, role: TraceHopRole) -> Self {
77 Self {
78 path,
79 line,
80 col,
81 role,
82 secret_reads: Vec::new(),
83 }
84 }
85}