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
// CB-950 through CB-954 YAML best practices detectors.
// Included by yaml_best_practices.rs — do NOT add `use` imports or `#!` attributes.
// =============================================================================
// CB-950: Truthy String Ambiguity
// =============================================================================
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
/// Detect cb950 truthy ambiguity.
pub fn detect_cb950_truthy_ambiguity(project_path: &Path) -> Vec<CbPatternViolation> {
let files = walkdir_yaml_files(project_path);
let mut violations = Vec::new();
for file_path in &files {
let content = match fs::read_to_string(file_path) {
Ok(c) => c,
Err(_) => continue,
};
let raw_lines: Vec<&str> = content.lines().collect();
let prod_lines = compute_yaml_production_lines(&content);
let rel = file_path
.strip_prefix(project_path)
.unwrap_or(file_path)
.display()
.to_string();
for (line_num, line) in &prod_lines {
// Honor inline # pmat:ignore directives
if let Some(raw) = raw_lines.get(line_num.wrapping_sub(1)) {
if raw.contains("# pmat:ignore") {
continue;
}
}
// Pattern: key: value where value is an unquoted truthy string
if let Some(colon_pos) = line.find(": ") {
let key = line[..colon_pos].trim();
let value = line[colon_pos + 2..].trim();
// Skip CI/CD keys that require native booleans
if NATIVE_BOOLEAN_KEYS.contains(&key) {
continue;
}
// Check if value is an unquoted truthy string
if !value.starts_with('"')
&& !value.starts_with('\'')
&& TRUTHY_STRINGS.contains(&value)
{
violations.push(CbPatternViolation {
pattern_id: "CB-950".to_string(),
file: rel.clone(),
line: *line_num,
description: format!(
"Unquoted truthy string `{}` — quote to prevent implicit boolean conversion",
value
),
severity: Severity::Warning,
});
}
}
}
}
violations
}
// =============================================================================
// CB-951: Excessive Nesting
// =============================================================================
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
/// Detect cb951 excessive nesting.
pub fn detect_cb951_excessive_nesting(project_path: &Path) -> Vec<CbPatternViolation> {
let files = walkdir_yaml_files(project_path);
let mut violations = Vec::new();
for file_path in &files {
let content = match fs::read_to_string(file_path) {
Ok(c) => c,
Err(_) => continue,
};
let rel = file_path
.strip_prefix(project_path)
.unwrap_or(file_path)
.display()
.to_string();
let mut max_depth = 0;
let mut max_depth_line = 1;
for (i, line) in content.lines().enumerate() {
if line.trim().is_empty() || line.trim().starts_with('#') {
continue;
}
// Calculate indentation depth (spaces / 2)
let indent = line.len() - line.trim_start().len();
let depth = indent / 2;
if depth > max_depth {
max_depth = depth;
max_depth_line = i + 1;
}
}
// Contract YAML files legitimately nest to 12+ levels
// (proof_obligations → items → constraints → sub-fields)
let threshold = if rel.starts_with("contracts/") || rel.contains("/contracts/") {
14
} else {
8
};
if max_depth > threshold {
violations.push(CbPatternViolation {
pattern_id: "CB-951".to_string(),
file: rel.clone(),
line: max_depth_line,
description: format!(
"Excessive nesting depth {} (threshold: {}) — consider restructuring",
max_depth, threshold
),
severity: Severity::Info,
});
}
}
violations
}
// =============================================================================
// CB-952: Missing Required Fields (GitHub Actions specific)
// =============================================================================
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
/// Detect cb952 missing required fields.
pub fn detect_cb952_missing_required_fields(project_path: &Path) -> Vec<CbPatternViolation> {
let files = walkdir_yaml_files(project_path);
let mut violations = Vec::new();
for file_path in &files {
// Only check GitHub Actions workflows
let rel_path = file_path.strip_prefix(project_path).unwrap_or(file_path);
let rel = rel_path.display().to_string();
if !rel.contains(".github/workflows") {
continue;
}
let content = match fs::read_to_string(file_path) {
Ok(c) => c,
Err(_) => continue,
};
let has_name = content.lines().any(|l| l.starts_with("name:"));
let has_on = content
.lines()
.any(|l| l.starts_with("on:") || l.starts_with("on ") || l.trim() == "on:");
let has_jobs = content.lines().any(|l| l.starts_with("jobs:"));
if !has_name {
violations.push(CbPatternViolation {
pattern_id: "CB-952".to_string(),
file: rel.clone(),
line: 1,
description: "GitHub Actions workflow missing `name:` field".to_string(),
severity: Severity::Warning,
});
}
if !has_on {
violations.push(CbPatternViolation {
pattern_id: "CB-952".to_string(),
file: rel.clone(),
line: 1,
description: "GitHub Actions workflow missing `on:` trigger".to_string(),
severity: Severity::Warning,
});
}
if !has_jobs {
violations.push(CbPatternViolation {
pattern_id: "CB-952".to_string(),
file: rel.clone(),
line: 1,
description: "GitHub Actions workflow missing `jobs:` section".to_string(),
severity: Severity::Warning,
});
}
}
violations
}
// =============================================================================
// CB-953: Unpinned Action Version
// =============================================================================
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
/// Detect cb953 unpinned action.
pub fn detect_cb953_unpinned_action(project_path: &Path) -> Vec<CbPatternViolation> {
let files = walkdir_yaml_files(project_path);
let mut violations = Vec::new();
for file_path in &files {
let rel_path = file_path.strip_prefix(project_path).unwrap_or(file_path);
let rel = rel_path.display().to_string();
if !rel.contains(".github/workflows") && !rel.contains(".github/actions") {
continue;
}
let content = match fs::read_to_string(file_path) {
Ok(c) => c,
Err(_) => continue,
};
for (i, line) in content.lines().enumerate() {
let trimmed = line.trim();
if !trimmed.starts_with("uses:") && !trimmed.starts_with("- uses:") {
continue;
}
// Extract the action reference
let action_ref = trimmed
.trim_start_matches("- ")
.trim_start_matches("uses:")
.trim()
.trim_matches('"')
.trim_matches('\'');
// Check if pinned to branch instead of tag/SHA
if let Some(at_pos) = action_ref.find('@') {
let action_name = &action_ref[..at_pos];
let version = &action_ref[at_pos + 1..];
// Org-internal reusable workflows pinned to main are acceptable
// (the org controls the workflow source)
let is_org_internal = action_name.contains("/.github/");
if !is_org_internal
&& (version == "main"
|| version == "master"
|| version == "latest"
|| version == "dev")
{
violations.push(CbPatternViolation {
pattern_id: "CB-953".to_string(),
file: rel.clone(),
line: i + 1,
description: format!(
"Action `{}` pinned to branch `{}` — use version tag or SHA",
action_name, version
),
severity: Severity::Warning,
});
}
} else if !action_ref.starts_with("./") && !action_ref.is_empty() {
// No @ at all (not a local action)
violations.push(CbPatternViolation {
pattern_id: "CB-953".to_string(),
file: rel.clone(),
line: i + 1,
description: format!(
"Action `{}` has no version pin — add @vX or @SHA",
action_ref
),
severity: Severity::Warning,
});
}
}
}
violations
}
// =============================================================================
// CB-954: Secret in Plain Text
// =============================================================================
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
/// Detect cb954 plaintext secret.
pub fn detect_cb954_plaintext_secret(project_path: &Path) -> Vec<CbPatternViolation> {
let files = walkdir_yaml_files(project_path);
let mut violations = Vec::new();
for file_path in &files {
let rel = file_path
.strip_prefix(project_path)
.unwrap_or(file_path)
.display()
.to_string();
// Only scan CI/CD and config YAML for secrets, not docs/roadmaps/contracts
let is_ci_or_config = rel.contains(".github/")
|| rel.contains("docker")
|| rel.contains("deploy")
|| rel.ends_with(".pmat.yaml")
|| rel.ends_with(".pmat-gates.toml")
|| rel.contains("ci/")
|| rel.contains("config");
if !is_ci_or_config {
continue;
}
let content = match fs::read_to_string(file_path) {
Ok(c) => c,
Err(_) => continue,
};
for (i, line) in content.lines().enumerate() {
let trimmed = line.trim();
if trimmed.starts_with('#') || trimmed.is_empty() {
continue;
}
// Honor inline # pmat:ignore directives
if line.contains("# pmat:ignore") {
continue;
}
if let Some(colon_pos) = trimmed.find(": ") {
let key = trimmed[..colon_pos].trim().to_lowercase();
let value = trimmed[colon_pos + 2..].trim();
// Check if key matches secret patterns but is not in allowlist
let is_allowlisted = SECRET_KEY_ALLOWLIST.iter().any(|a| key == *a);
let is_secret_key =
!is_allowlisted && SECRET_KEY_PATTERNS.iter().any(|p| key.contains(p));
if is_secret_key {
// Allow references to env vars, secrets, and GHA inherit
if value.starts_with("${{")
|| value.starts_with("${")
|| value.starts_with("$")
|| value == "\"\""
|| value == "''"
|| value.is_empty()
|| value == "null"
|| value == "~"
|| value == "inherit" // GHA: `secrets: inherit` is standard
{
continue;
}
violations.push(CbPatternViolation {
pattern_id: "CB-954".to_string(),
file: rel.clone(),
line: i + 1,
description: format!(
"Possible plaintext secret in `{}` — use environment variable or secret reference",
key
),
severity: Severity::Error,
});
}
}
}
}
violations
}