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
//! Security control sites, sanitizers, tainted bindings, skipped callees, and
//! sink sites.
use super::*;
/// Family of a control pattern observed in a file on an import trace.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
serde::Serialize,
serde::Deserialize,
bitcode::Encode,
bitcode::Decode,
)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum SecurityControlKind {
/// Sanitization or escaping before a sink.
Sanitization,
/// Input validation or schema parsing.
Validation,
/// Authentication check or middleware.
Authentication,
/// Authorization or permission check.
Authorization,
}
/// An observed control call or guard pattern, without proof that it protects a sink.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, bitcode::Encode, bitcode::Decode)]
pub struct SecurityControlSite {
/// Control family.
pub kind: SecurityControlKind,
/// Flattened callee path or a stable synthetic name for guard-derived
/// controls.
pub callee_path: String,
/// Byte offset of the control span start.
pub span_start: u32,
/// Byte offset of the control span end.
pub span_end: u32,
}
/// Sanitizer output domain. Kept intentionally narrow so a sanitizer for one
/// domain cannot suppress a different sink family.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
serde::Serialize,
serde::Deserialize,
bitcode::Encode,
bitcode::Decode,
)]
pub enum SanitizerScope {
/// HTML markup sanitized by DOMPurify-compatible APIs.
Html,
/// URL or redirect target checked against a literal-backed allowlist.
Url,
/// Path value checked against a high-confidence containment guard.
Path,
/// SQL identifier quoted with a helper that doubles embedded identifier quotes.
SqlIdentifier,
}
/// A captured sink argument that is itself a recognized sanitizer call.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, bitcode::Encode, bitcode::Decode)]
pub struct SanitizedSinkArg {
/// Byte offset of the owning sink span start.
pub span_start: u32,
/// The positional argument index on the owning sink.
pub arg_index: u32,
/// The sanitizer output domain for this argument.
pub scope: SanitizerScope,
}
/// A local binding tied to the flattened member-access path it was initialized
/// from. The analyze layer matches `source_path` against the data-driven source
/// catalogue; when it matches, `local` is treated as carrying untrusted input.
///
/// Captured for two shapes: a direct assignment (`const id = req.query.id` ->
/// `{ local: "id", source_path: "req.query" }`, the literal-key tail dropped so
/// the path matches a catalogue prefix) and an object destructure
/// (`const { id } = req.query` -> `{ local: "id", source_path: "req.query" }`).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, bitcode::Encode, bitcode::Decode)]
pub struct TaintedBinding {
/// The local binding name introduced by the declarator.
pub local: String,
/// The flattened object member-access path the binding was sourced from.
pub source_path: String,
/// Byte offset of the source read (the member-access expression the binding
/// was sourced from), so the analyze layer can anchor a taint trace's source
/// node at the real read line instead of the module import line. Stored as a
/// `u32` (not `Span`) to stay bitcode-encodable for the cache. `0` when no
/// concrete read expression is available (synthetic framework-param /
/// helper-return bindings), in which case the analyze layer falls back to the
/// sink site rather than claiming a spurious line.
pub source_span_start: u32,
}
/// Why a sink-shaped callee could not be flattened into a static catalogue
/// path.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
serde::Serialize,
serde::Deserialize,
bitcode::Encode,
bitcode::Decode,
)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum SkippedSecurityCalleeReason {
/// A computed member access such as `client[method](input)`.
ComputedMember,
/// A dynamic non-member callee such as `(factory())(input)`.
DynamicDispatch,
/// An assignment target whose object could not be flattened.
UnsupportedAssignmentObject,
}
/// Syntactic expression shape for a skipped security callee.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
serde::Serialize,
serde::Deserialize,
bitcode::Encode,
bitcode::Decode,
)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case")]
pub enum SkippedSecurityCalleeExpressionKind {
/// `obj.prop(...)`.
StaticMemberExpression,
/// `obj[prop](...)`.
ComputedMemberExpression,
/// A bare identifier or private identifier callee.
Identifier,
/// Any other call-like expression that cannot be represented compactly.
Other,
}
/// Span-only diagnostic for a skipped security callee inside one module.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, bitcode::Encode, bitcode::Decode)]
pub struct SkippedSecurityCalleeSite {
/// Why the callee was skipped.
pub reason: SkippedSecurityCalleeReason,
/// Compact expression shape of the skipped callee.
pub expression_kind: SkippedSecurityCalleeExpressionKind,
/// Start byte offset of the skipped callee expression.
pub span_start: u32,
/// End byte offset of the skipped callee expression.
pub span_end: u32,
}
/// The syntactic shape of a captured security sink site. Category-blind: the
/// extractor records the shape and the dotted/bare callee path; the analyze
/// layer matches it against the data-driven catalogue. See
/// `crates/security/data/security_matchers.toml`.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
bitcode::Encode,
bitcode::Decode,
)]
pub enum SinkShape {
/// A call to a bare identifier (e.g. `eval(x)`).
Call,
/// A call to a dotted member path (e.g. `child_process.exec(x)`).
MemberCall,
/// An assignment to a member target (e.g. `el.innerHTML = x`).
MemberAssign,
/// A tagged template expression (e.g. ``sql`...${x}...` ``).
TaggedTemplate,
/// A JSX attribute value (e.g. `dangerouslySetInnerHTML={x}`).
JsxAttr,
/// A constructor call (e.g. `new Function("return x")`).
NewExpression,
/// A static string literal assigned to a secret-shaped identifier or known
/// provider credential prefix.
SecretLiteral,
}
/// The shape of the argument captured at a sink site. Category-blind like
/// [`SinkShape`], but finer-grained: it lets the catalogue matcher require or
/// exclude specific argument shapes. The discriminator is what distinguishes an
/// unsafe SQL string concatenation or template-into-`.execute()` from a
/// safely-parameterized `` sql`${x}` `` tagged template, an object-literal
/// `.execute({ sql, args })` argument, or a literal-aware sink argument.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
serde::Serialize,
serde::Deserialize,
bitcode::Encode,
bitcode::Decode,
)]
pub enum SinkArgKind {
/// A template literal with at least one `${...}` substitution (e.g.
/// `` `SELECT ${x}` ``). On a `tagged-template` shape this is the tag's
/// quasi; on a `call`/`member-call` shape it is the positional argument.
TemplateWithSubst,
/// A binary `+` string concatenation (e.g. `"SELECT " + x`).
Concat,
/// An object literal (e.g. `.execute({ sql, args })`, the parameterized form).
Object,
/// A call expression argument (e.g. `query(buildSql())`).
Call,
/// A literal argument admitted by a literal-aware security matcher.
Literal,
/// A zero-argument sink captured because the callee itself is the signal.
NoArg,
/// Any other non-literal expression (bare identifier, member access, etc.).
Other,
}
/// A captured sink site. The visitor records every existing non-literal call /
/// member-assign / member-call / tagged-template / jsx-attr sink site, and a
/// small allowlist of literal-aware sites where the literal value is the signal.
/// It knows nothing about CWE categories.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, bitcode::Encode, bitcode::Decode)]
pub struct SinkSite {
/// The syntactic shape of the sink site.
pub sink_shape: SinkShape,
/// The flattened dotted/bare callee or member path.
pub callee_path: String,
/// The positional argument index. For zero-argument captures this is 0.
pub arg_index: u32,
/// Whether the relevant argument is non-literal. Existing non-literal
/// catalogue rows require this to remain true.
pub arg_is_non_literal: bool,
/// The finer-grained shape of the captured argument. Lets the catalogue
/// require unsafe shapes (concat / template-with-substitution / literal /
/// no-arg) and exclude safe ones (object literal, the parameterized form).
/// See [`SinkArgKind`].
pub arg_kind: SinkArgKind,
/// Literal argument value for literal-aware rows.
pub arg_literal: Option<SinkLiteralValue>,
/// Risky regex fragment for structural ReDoS candidates.
pub regex_pattern: Option<String>,
/// Static object-literal properties for option-object rows.
pub object_properties: Vec<SinkObjectProperty>,
/// Static top-level object-literal keys, including keys whose values are not
/// literal. Used by missing-option rows that only need key presence.
pub object_property_keys: Vec<String>,
/// Whether [`object_property_keys`](Self::object_property_keys) is complete.
/// False for non-object arguments and object literals with spread or
/// non-static keys, where a missing-key claim would be speculative.
pub object_property_keys_complete: bool,
/// Identifier names referenced anywhere inside the captured non-literal sink
/// argument, or contextual names for zero-argument captures such as a
/// token-like `Math.random()` assignment target. Deduped in source order.
/// Used by the analyze layer to back-trace the sink argument to a known
/// untrusted source or to apply narrow context gates. Intra-module,
/// name-based, conservative; it is never a taint proof.
pub arg_idents: Vec<String>,
/// Flattened static member paths referenced inside the captured non-literal
/// sink argument. Includes both the full path and source-object path for
/// leaf reads (`process.env.SECRET` records `process.env.SECRET` and
/// `process.env`) so direct source expressions can be matched without an
/// intermediate local binding.
pub arg_source_paths: Vec<String>,
/// Byte offset of the sink span start. Stored as `u32` (not `Span`) so the
/// struct is bitcode-encodable and can be persisted directly in the cache.
pub span_start: u32,
/// Byte offset of the sink span end.
pub span_end: u32,
/// The arg-0 URL string literal of a network-shaped call (`fetch`, `axios.*`,
/// `got`, ...), captured so the `secret-to-network` category (#890) can carry
/// a destination-host signal on its candidate: `Some(literal)` when the
/// destination is a static string literal (almost always intended auth, e.g.
/// the credential's own provider), `None` when it is dynamic (the suspicious
/// case). `None` for non-call sinks and calls with no arg 0.
pub url_arg_literal: Option<String>,
/// URL construction shape for URL-like sink arguments when the extractor can
/// classify it syntactically. `None` for non-URL sinks and URL expressions
/// whose shape is not visible at the sink.
pub url_shape: Option<SecurityUrlShape>,
/// Options-object metadata of the call, on the command argument and the
/// argument array of a call whose options position the extractor knows. `None` for every other
/// sink, which means that the options are unknown.
pub call_options: Option<Box<SinkCallOptions>>,
}
impl SinkSite {
/// Reconstruct the source span from the stored byte offsets.
#[must_use]
pub fn span(&self) -> Span {
Span::new(self.span_start, self.span_end)
}
}