pipechecker 0.3.2

CI/CD Pipeline Auditor - Catch errors before you push
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Secrets auditor - detects hardcoded secrets and secret usage patterns
//!
//! This module scans pipeline configurations for:
//! - Hardcoded secrets in environment variables at all levels (pipeline, job, step)
//! - Secret references in `run:` blocks (`${{ secrets.X }}`)
//! - Secret references in `with:` input blocks
//! - Undeclared environment variable references (`${{ env.X }}`)
//! - Hardcoded values in `env:` fields that look like secrets

use crate::error::Result;
use crate::models::{rule_codes, Issue, Pipeline, Severity};
use regex::Regex;
use std::sync::OnceLock;
static SECRET_REGEX: OnceLock<Regex> = OnceLock::new();
static ENV_REGEX: OnceLock<Regex> = OnceLock::new();
use std::collections::HashSet;

/// Patterns that indicate hardcoded secrets
const SECRET_VALUE_PATTERNS: &[&str] = &[
    "api_key",
    "apikey",
    "api-key",
    "api_secret",
    "apisecret",
    "api-secret",
    "secret_key",
    "secretkey",
    "secret-key",
    "access_key",
    "accesskey",
    "access-key",
    "auth_token",
    "authtoken",
    "auth-token",
    "private_key",
    "privatekey",
    "private-key",
    "password",
    "passwd",
    "token",
];

/// Audit a pipeline for secret-related issues
pub fn audit(pipeline: &Pipeline) -> Result<Vec<Issue>> {
    let mut issues = Vec::new();

    let secret_pattern =
        SECRET_REGEX.get_or_init(|| Regex::new(r"\$\{\{\s*secrets\.(\w+)\s*\}\}").unwrap());
    let env_pattern = ENV_REGEX.get_or_init(|| Regex::new(r"\$\{\{\s*env\.(\w+)\s*\}\}").unwrap());

    // Check for pull_request_target security risk (GitHub Actions only)
    if pipeline.provider == crate::models::Provider::GitHubActions {
        check_pull_request_target(pipeline, &mut issues);
    }

    let mut declared_env: HashSet<String> = pipeline.env.iter().map(|e| e.key.clone()).collect();

    // Check pipeline-level env vars for hardcoded secrets
    for env_var in &pipeline.env {
        if is_potential_secret(&env_var.key, &env_var.value) {
            let (line, col) = pipeline.find_line(&format!("  {}", env_var.key));
            issues.push(Issue::for_job_with_code(
                Severity::Warning,
                &format!(
                    "Pipeline env '{}' may contain a hardcoded secret",
                    env_var.key
                ),
                "",
                line,
                col,
                Some(format!(
                    "Use secrets.{} instead of hardcoding",
                    env_var.key.to_lowercase().replace("_", "-")
                )),
                rule_codes::HARDCODED_SECRET,
            ));
        }
    }

    for job in &pipeline.jobs {
        // Add job-level env vars to declared set
        for env_var in &job.env {
            declared_env.insert(env_var.key.clone());
        }

        // Check job-level env vars for hardcoded secrets
        for env_var in &job.env {
            if is_potential_secret(&env_var.key, &env_var.value) {
                let (line, col) = pipeline.find_job_line(&job.id, "env");
                issues.push(Issue::for_job_with_code(
                    Severity::Warning,
                    &format!(
                        "Job '{}' env '{}' may contain a hardcoded secret",
                        job.id, env_var.key
                    ),
                    &job.id,
                    line,
                    col,
                    Some("Use repository secrets or workflow-level env vars instead".to_string()),
                    rule_codes::HARDCODED_SECRET,
                ));
            }
        }

        for step in &job.steps {
            // Check step-level env vars for hardcoded secrets
            for env_var in &step.env {
                if is_potential_secret(&env_var.key, &env_var.value) {
                    let (line, col) = pipeline.find_job_line(&job.id, "env");
                    issues.push(Issue::for_job_with_code(
                        Severity::Warning,
                        &format!(
                            "Job '{}' step env '{}' may contain a hardcoded secret",
                            job.id, env_var.key
                        ),
                        &job.id,
                        line,
                        col,
                        Some("Use repository secrets instead of hardcoded values".to_string()),
                        rule_codes::HARDCODED_SECRET,
                    ));
                }
                declared_env.insert(env_var.key.clone());
            }

            // Check run commands for secret references
            if let Some(run) = &step.run {
                for cap in secret_pattern.captures_iter(run) {
                    let secret_name = &cap[1];
                    let (line, col) = pipeline.find_job_line(&job.id, "run");
                    issues.push(Issue::for_job_with_code(
                        Severity::Info,
                        &format!("Job '{}' uses secret: {}", job.id, secret_name),
                        &job.id,
                        line,
                        col,
                        Some("Ensure this secret is configured in repository settings".to_string()),
                        rule_codes::SECRET_REFERENCE,
                    ));
                }

                for cap in env_pattern.captures_iter(run) {
                    let env_name = &cap[1];
                    if !declared_env.contains(env_name) {
                        let (line, col) = pipeline.find_job_line(&job.id, "run");
                        issues.push(Issue::for_job_with_code(
                            Severity::Warning,
                            &format!(
                                "Job '{}' references undeclared env var: {}",
                                job.id, env_name
                            ),
                            &job.id,
                            line,
                            col,
                            Some(format!("Declare '{}' in env section", env_name)),
                            rule_codes::UNDECLARED_ENV_VAR,
                        ));
                    }
                }
            }

            // Check 'with:' input blocks for secret references
            if let Some(with_val) = &step.with_inputs {
                scan_yaml_for_secrets(
                    with_val,
                    &job.id,
                    secret_pattern,
                    env_pattern,
                    &declared_env,
                    pipeline,
                    &mut issues,
                );
            }
        }
    }

    Ok(issues)
}

/// Recursively scan a YAML value for secret patterns
fn scan_yaml_for_secrets(
    val: &serde_yaml::Value,
    job_id: &str,
    secret_pattern: &Regex,
    env_pattern: &Regex,
    declared_env: &HashSet<String>,
    pipeline: &Pipeline,
    issues: &mut Vec<Issue>,
) {
    match val {
        serde_yaml::Value::String(s) => {
            for cap in secret_pattern.captures_iter(s) {
                let secret_name = &cap[1];
                let (line, col) = pipeline.find_job_line(job_id, "with");
                issues.push(Issue::for_job_with_code(
                    Severity::Info,
                    &format!("Job '{}' uses secret: {}", job_id, secret_name),
                    job_id,
                    line,
                    col,
                    Some("Ensure this secret is configured in repository settings".to_string()),
                    rule_codes::SECRET_REFERENCE,
                ));
            }
            for cap in env_pattern.captures_iter(s) {
                let env_name = &cap[1];
                if !declared_env.contains(env_name) {
                    let (line, col) = pipeline.find_job_line(job_id, "with");
                    issues.push(Issue::for_job_with_code(
                        Severity::Warning,
                        &format!(
                            "Job '{}' references undeclared env var: {}",
                            job_id, env_name
                        ),
                        job_id,
                        line,
                        col,
                        Some(format!("Declare '{}' in env section", env_name)),
                        rule_codes::UNDECLARED_ENV_VAR,
                    ));
                }
            }
        }
        serde_yaml::Value::Mapping(m) => {
            for (_k, v) in m {
                scan_yaml_for_secrets(
                    v,
                    job_id,
                    secret_pattern,
                    env_pattern,
                    declared_env,
                    pipeline,
                    issues,
                );
            }
        }
        serde_yaml::Value::Sequence(seq) => {
            for item in seq {
                scan_yaml_for_secrets(
                    item,
                    job_id,
                    secret_pattern,
                    env_pattern,
                    declared_env,
                    pipeline,
                    issues,
                );
            }
        }
        _ => {}
    }
}

/// Check if a key-value pair looks like it might be a hardcoded secret
fn is_potential_secret(key: &str, value: &str) -> bool {
    // Skip GitHub Actions secret references — these are the correct way to use secrets
    if value.contains("${{ secrets.") || value.contains("${{secrets.") {
        return false;
    }

    let lower_key = key.to_lowercase();
    let lower_val = value.to_lowercase();

    // Check if the key name suggests it's a secret
    for pattern in SECRET_VALUE_PATTERNS {
        if lower_key.contains(pattern) {
            // If the key is suspicious, any non-empty value that isn't a reference is suspect
            return !value.trim().is_empty();
        }
    }

    // Check if the value contains suspicious patterns
    for pattern in SECRET_VALUE_PATTERNS {
        if lower_val.contains(pattern) {
            return true;
        }
    }

    // Check if value looks like a key/token (long alphanumeric string)
    if value.len() > 20
        && value
            .chars()
            .all(|c| c.is_alphanumeric() || c == '_' || c == '-')
    {
        return true;
    }

    // Check for base64-like strings that might be encoded secrets
    if value.len() > 40
        && value
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '/' || c == '=')
    {
        return true;
    }

    false
}

/// Check for `on: pull_request_target` combined with `actions/checkout` (security risk)
fn check_pull_request_target(pipeline: &Pipeline, issues: &mut Vec<Issue>) {
    let source = &pipeline.source;

    // Detect pull_request_target trigger
    let has_pr_target = source.contains("pull_request_target");
    if !has_pr_target {
        return;
    }

    // Check if any job uses actions/checkout without ref: restriction
    let mut has_unrestricted_checkout = false;
    for job in &pipeline.jobs {
        for step in &job.steps {
            if let Some(uses) = &step.uses {
                if uses.starts_with("actions/checkout") {
                    // Check if there's a 'ref:' restriction in with_inputs
                    let has_ref = step
                        .with_inputs
                        .as_ref()
                        .and_then(|v| v.get("ref"))
                        .is_some();
                    if !has_ref {
                        has_unrestricted_checkout = true;
                        let (line, col) = pipeline.find_job_line(&job.id, "uses");
                        issues.push(Issue::for_job_with_code(
                            Severity::Warning,
                            &format!(
                                "Job '{}' uses actions/checkout in a pull_request_target workflow without ref: restriction",
                                job.id
                            ),
                            &job.id,
                            line,
                            col,
                            Some(
                                "pull_request_target runs in the context of the base branch. \
                                 Using actions/checkout without ref: may expose secrets to untrusted code. \
                                 Add 'ref: ${{ github.event.pull_request.head.sha }}' or restrict the checkout."
                                    .to_string(),
                            ),
                            rule_codes::PR_TARGET_INSECURE,
                        ));
                    }
                }
            }
        }
    }

    // If no checkout found at all but pull_request_target is used, still warn
    if !has_unrestricted_checkout && !has_pr_target {
        return;
    }

    // General warning about pull_request_target if no specific checkout issue was flagged
    if has_pr_target && !has_unrestricted_checkout {
        // Check if there's any checkout at all
        let has_any_checkout = pipeline.jobs.iter().any(|job| {
            job.steps.iter().any(|s| {
                s.uses
                    .as_deref()
                    .is_some_and(|u| u.starts_with("actions/checkout"))
            })
        });
        if !has_any_checkout {
            let (line, col) = pipeline.find_line("pull_request_target");
            issues.push(Issue::with_code(
                Severity::Warning,
                "Workflow uses 'pull_request_target' trigger — ensure secrets are not exposed to untrusted code",
                Some(
                    "pull_request_target runs with the base branch's secrets. \
                     Review all steps to ensure they cannot be manipulated by the PR author."
                        .to_string(),
                ),
                rule_codes::PR_TARGET_INSECURE,
            ));
            let _ = (line, col);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::Provider;

    #[test]
    fn test_secret_reference_is_not_secret() {
        // Secret references are the correct way, not hardcoded
        assert!(!is_potential_secret("API_KEY", "${{ secrets.API_KEY }}"));
        assert!(!is_potential_secret("token", "${{secrets.TOKEN}}"));
    }

    #[test]
    fn test_keyword_patterns_detected() {
        assert!(is_potential_secret("API_KEY", "abc123"));
        assert!(is_potential_secret("some_var", "password=hunter2"));
    }

    #[test]
    fn test_keyword_case_insensitive() {
        assert!(is_potential_secret("api_key", "abc"));
        assert!(is_potential_secret("Password", "123"));
    }

    #[test]
    fn test_long_alphanumeric_detected_as_secret() {
        assert!(is_potential_secret("var", "aB3dEf6hIjKlMnOpQrStUvWx"));
    }

    #[test]
    fn test_short_alphanumeric_not_secret() {
        assert!(!is_potential_secret("var", "hello"));
    }

    #[test]
    fn test_base64_like_detected() {
        assert!(is_potential_secret(
            "var",
            "abcDEF123+/xyzABC456===GHJklmno789pqrSTUV"
        ));
    }

    #[test]
    fn test_normal_values_not_flagged() {
        assert!(!is_potential_secret("image", "node:18-alpine"));
        assert!(!is_potential_secret("run", "echo hello world"));
    }

    #[test]
    fn test_values_with_special_chars_not_long_enough() {
        // Contains spaces, so not all alphanumeric
        assert!(!is_potential_secret(
            "run",
            "hello world this is a test string"
        ));
    }

    #[test]
    fn test_empty_string_not_secret() {
        assert!(!is_potential_secret("var", ""));
    }

    // --- pull_request_target tests ---

    #[test]
    fn test_pr_target_with_unrestricted_checkout_warns() {
        let source = "name: CI\non:\n  pull_request_target:\njobs:\n  build:\n    runs-on: ubuntu\n    steps:\n      - uses: actions/checkout@v4\n";
        let pipeline = Pipeline {
            provider: Provider::GitHubActions,
            jobs: vec![crate::models::Job {
                id: "build".to_string(),
                name: None,
                depends_on: vec![],
                steps: vec![crate::models::Step {
                    name: None,
                    uses: Some("actions/checkout@v4".to_string()),
                    run: None,
                    env: vec![],
                    with_inputs: None,
                }],
                env: vec![],
                container_image: None,
                service_images: vec![],
                timeout_minutes: None,
                rules: vec![],
            }],
            env: vec![],
            source: source.to_string(),
            is_reusable: false,
            workflow_call_inputs: vec![],
            workflow_call_secrets: vec![],
            workflow_rules: vec![],
        };
        let issues = audit(&pipeline).unwrap();
        assert!(issues
            .iter()
            .any(|i| i.rule_code == Some(rule_codes::PR_TARGET_INSECURE)));
    }

    #[test]
    fn test_pr_target_with_ref_checkout_ok() {
        let source = "name: CI\non:\n  pull_request_target:\njobs:\n  build:\n    runs-on: ubuntu\n    steps:\n      - uses: actions/checkout@v4\n        with:\n          ref: ${{ github.event.pull_request.head.sha }}\n";
        let pipeline = Pipeline {
            provider: Provider::GitHubActions,
            jobs: vec![crate::models::Job {
                id: "build".to_string(),
                name: None,
                depends_on: vec![],
                steps: vec![crate::models::Step {
                    name: None,
                    uses: Some("actions/checkout@v4".to_string()),
                    run: None,
                    env: vec![],
                    with_inputs: Some(
                        serde_yaml::from_str("ref: ${{ github.event.pull_request.head.sha }}")
                            .unwrap(),
                    ),
                }],
                env: vec![],
                container_image: None,
                service_images: vec![],
                timeout_minutes: None,
                rules: vec![],
            }],
            env: vec![],
            source: source.to_string(),
            is_reusable: false,
            workflow_call_inputs: vec![],
            workflow_call_secrets: vec![],
            workflow_rules: vec![],
        };
        let issues = audit(&pipeline).unwrap();
        assert!(!issues
            .iter()
            .any(|i| i.rule_code == Some(rule_codes::PR_TARGET_INSECURE)));
    }

    #[test]
    fn test_pr_target_without_checkout_warns() {
        let source = "name: CI\non:\n  pull_request_target:\njobs:\n  build:\n    runs-on: ubuntu\n    steps:\n      - run: echo hi\n";
        let pipeline = Pipeline {
            provider: Provider::GitHubActions,
            jobs: vec![crate::models::Job {
                id: "build".to_string(),
                name: None,
                depends_on: vec![],
                steps: vec![crate::models::Step {
                    name: None,
                    uses: None,
                    run: Some("echo hi".to_string()),
                    env: vec![],
                    with_inputs: None,
                }],
                env: vec![],
                container_image: None,
                service_images: vec![],
                timeout_minutes: None,
                rules: vec![],
            }],
            env: vec![],
            source: source.to_string(),
            is_reusable: false,
            workflow_call_inputs: vec![],
            workflow_call_secrets: vec![],
            workflow_rules: vec![],
        };
        let issues = audit(&pipeline).unwrap();
        assert!(issues
            .iter()
            .any(|i| i.rule_code == Some(rule_codes::PR_TARGET_INSECURE)));
    }

    #[test]
    fn test_no_pr_target_no_issue() {
        let source = "name: CI\non: push:\njobs:\n  build:\n    runs-on: ubuntu\n    steps:\n      - uses: actions/checkout@v4\n";
        let pipeline = Pipeline {
            provider: Provider::GitHubActions,
            jobs: vec![crate::models::Job {
                id: "build".to_string(),
                name: None,
                depends_on: vec![],
                steps: vec![crate::models::Step {
                    name: None,
                    uses: Some("actions/checkout@v4".to_string()),
                    run: None,
                    env: vec![],
                    with_inputs: None,
                }],
                env: vec![],
                container_image: None,
                service_images: vec![],
                timeout_minutes: None,
                rules: vec![],
            }],
            env: vec![],
            source: source.to_string(),
            is_reusable: false,
            workflow_call_inputs: vec![],
            workflow_call_secrets: vec![],
            workflow_rules: vec![],
        };
        let issues = audit(&pipeline).unwrap();
        assert!(!issues
            .iter()
            .any(|i| i.rule_code == Some(rule_codes::PR_TARGET_INSECURE)));
    }
}