pinner 0.0.13

Secure CI/CD workflows by pinning mutable tags to immutable SHA-1 hashes. A high-performance Rust CLI that preserves YAML formatting and comments. Supports GitHub, GitLab, Bitbucket, Forgejo, and Docker image pinning.
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
use crate::error::PinnerError;
use crate::patcher::Patcher;
use crate::resolver::Resolver;
use crate::scanner::Scanner;
use colored::Colorize;
use std::path::PathBuf;

pub mod init;
pub mod pr;
pub mod sbom;
pub mod scan;

/// The central orchestration point for the Pinner pipeline.
///
/// It connects the three phases of execution:
/// 1. **Scanner**: Find dependencies in the file system.
/// 2. **Resolver**: Fetch immutable hashes from remote APIs.
/// 3. **Patcher**: Apply changes to files on disk.
pub struct Pipeline {
    scanner: Scanner,
    resolver: Resolver,
    patcher: Patcher,
}

impl Pipeline {
    /// Creates a new `Pipeline`.
    pub fn new(scanner: Scanner, resolver: Resolver, patcher: Patcher) -> Self {
        Self {
            scanner,
            resolver,
            patcher,
        }
    }

    /// Returns a reference to the pipeline's scanner.
    pub fn scanner(&self) -> &Scanner {
        &self.scanner
    }

    /// Returns a reference to the pipeline's resolver.
    pub fn resolver(&self) -> &Resolver {
        &self.resolver
    }

    /// Returns a reference to the pipeline's patcher.
    pub fn patcher(&self) -> &Patcher {
        &self.patcher
    }

    /// Automatically pins all symbolic action tags and image tags to hashes.
    pub async fn pin(&self, paths: &[PathBuf]) -> Result<(), PinnerError> {
        let (tasks, file_contents) = self.scanner.collect_tasks(paths).await?;
        let results = self.resolver.resolve_tasks(tasks, true).await?;
        self.patcher.apply_changes(results, file_contents).await
    }

    /// Upgrades dependencies to newer versions based on the configured strategy.
    pub async fn upgrade(&self, paths: &[PathBuf], interactive: bool) -> Result<(), PinnerError> {
        let (tasks, file_contents) = self.scanner.collect_tasks(paths).await?;
        let mut results = self.resolver.resolve_tasks(tasks, false).await?;

        if interactive {
            results = self.patcher.ui.prompt_upgrade(results)?;
        }

        self.patcher.apply_changes(results, file_contents).await
    }

    /// Verifies that all dependencies in the provided paths are pinned to an immutable hash.
    pub async fn verify(
        &self,
        paths: &[PathBuf],
        check_osv: bool,
        strict: bool,
    ) -> Result<crate::core::VerificationResult, PinnerError> {
        let (tasks, _) = self.scanner.collect_tasks(paths).await?;
        let mut unpinned = Vec::new();
        let mut compromised = Vec::new();
        let mut non_vetted = Vec::new();

        if !self.patcher.formatter.quiet
            && self.patcher.formatter.format == crate::cli::OutputFormat::Text
        {
            eprintln!("{}", "Verifying workflow dependencies...".bold());
        }

        let mut junit_cases = Vec::new();

        for task in tasks {
            let is_pinned = if let Some(tag) = &task.current_tag {
                (tag.len() == 40 && tag.chars().all(|c| c.is_ascii_hexdigit()))
                    || (tag.starts_with("sha256:") && tag.len() == 71)
                    || (task.key == "orbs" && !tag.is_empty())
            } else {
                false
            };

            let action_name = task.action.to_string();
            let file_path = task.path.display().to_string();

            if !is_pinned {
                unpinned.push(crate::core::UnpinnedDependency {
                    path: task.path.clone(),
                    action: task.action.clone(),
                    tag: task.current_tag.clone(),
                    line: task.line,
                    column: task.column,
                });

                if !self.patcher.formatter.quiet {
                    if self.patcher.formatter.format == crate::cli::OutputFormat::Text {
                        let display_tag = task.current_tag.as_deref().unwrap_or("latest");
                        eprintln!(
                            "  {} {}@{} in {}:{}:{} [✗ unpinned]",
                            "✗".red().bold(),
                            task.action.to_string().yellow(),
                            display_tag.yellow(),
                            task.path.display().to_string().cyan(),
                            task.line.to_string().magenta(),
                            task.column.to_string().magenta(),
                        );
                    } else if self.patcher.formatter.format == crate::cli::OutputFormat::Github {
                        let display_tag = task.current_tag.as_deref().unwrap_or("latest");
                        println!(
                            "::error file={},line={},col={}::Dependency {} is not pinned to a SHA (found tag: {})",
                            file_path, task.line, task.column, action_name, display_tag
                        );
                    }
                }

                if self.patcher.formatter.format == crate::cli::OutputFormat::Junit {
                    let display_tag = task.current_tag.as_deref().unwrap_or("latest");
                    junit_cases.push(format!(
                        "    <testcase name=\"{}\" classname=\"{}\" time=\"0.0\">\n      <failure message=\"Dependency is not pinned\">Dependency {} is not pinned to a SHA (found tag: {}) in {}:{}:{}</failure>\n    </testcase>",
                        action_name, file_path, action_name, display_tag, file_path, task.line, task.column
                    ));
                }
            } else {
                let tag = task.current_tag.as_deref().unwrap_or("");
                let mut status = self
                    .patcher
                    .formatter
                    .check_hash_security(&task.action.to_string(), tag);

                if status == crate::patcher::formatter::HashSecurityStatus::NotChecked && check_osv
                {
                    let action_str = task.action.to_string();
                    let is_git_sha = tag.len() == 40 && tag.chars().all(|c| c.is_ascii_hexdigit());

                    if !is_git_sha {
                        let image_name =
                            action_str.strip_prefix("docker://").unwrap_or(&action_str);
                        match self
                            .resolver
                            .registry
                            .verify_provenance(image_name, tag)
                            .await
                        {
                            Ok(true) => {}
                            Ok(false) => {
                                status = crate::patcher::formatter::HashSecurityStatus::Compromised;
                            }
                            Err(e) => {
                                if !self.patcher.formatter.quiet {
                                    eprintln!(
                                        "Warning: Could not verify OCI provenance for {}@{} due to error: {}",
                                        image_name, tag, e
                                    );
                                }
                            }
                        }
                    } else if let Ok(Some(body)) = self.resolver.check_vulnerabilities(tag).await {
                        #[derive(serde::Deserialize)]
                        struct OsvResponse {
                            vulns: Option<Vec<serde_json::Value>>,
                        }

                        if let Ok(osv_resp) = serde_json::from_str::<OsvResponse>(&body) {
                            if let Some(vulns) = osv_resp.vulns {
                                if !vulns.is_empty() {
                                    status =
                                        crate::patcher::formatter::HashSecurityStatus::Compromised;
                                }
                            }
                        }
                    }
                }

                match status {
                    crate::patcher::formatter::HashSecurityStatus::Compromised => {
                        compromised.push(crate::core::CompromisedDependency {
                            path: task.path.clone(),
                            action: task.action.clone(),
                            hash: tag.to_string(),
                            line: task.line,
                            column: task.column,
                        });

                        if !self.patcher.formatter.quiet {
                            if self.patcher.formatter.format == crate::cli::OutputFormat::Text {
                                eprintln!(
                                    "  {} {}@{} in {}:{}:{} [✗ compromised]",
                                    "✗".red().bold(),
                                    task.action.to_string().yellow(),
                                    tag.red(),
                                    task.path.display().to_string().cyan(),
                                    task.line.to_string().magenta(),
                                    task.column.to_string().magenta(),
                                );
                            } else if self.patcher.formatter.format
                                == crate::cli::OutputFormat::Github
                            {
                                println!(
                                    "::error file={},line={},col={}::Dependency {}@{} is COMPROMISED (Supply Chain Attack)!",
                                    file_path, task.line, task.column, action_name, tag
                                );
                            }
                        }

                        if self.patcher.formatter.format == crate::cli::OutputFormat::Junit {
                            junit_cases.push(format!(
                                "    <testcase name=\"{}\" classname=\"{}\" time=\"0.0\">\n      <failure message=\"Dependency is compromised\">Dependency {}@{} is COMPROMISED (Supply Chain Attack)! in {}:{}:{}</failure>\n    </testcase>",
                                action_name, file_path, action_name, tag, file_path, task.line, task.column
                            ));
                        }
                    }
                    crate::patcher::formatter::HashSecurityStatus::NotChecked => {
                        if strict {
                            non_vetted.push(crate::core::NonVettedDependency {
                                path: task.path.clone(),
                                action: task.action.clone(),
                                tag: task.current_tag.clone(),
                                line: task.line,
                                column: task.column,
                            });

                            if !self.patcher.formatter.quiet {
                                if self.patcher.formatter.format == crate::cli::OutputFormat::Text {
                                    eprintln!(
                                        "  {} {}@{} in {}:{}:{} [✗ not vetted]",
                                        "✗".red().bold(),
                                        task.action.to_string().yellow(),
                                        tag.yellow(),
                                        task.path.display().to_string().cyan(),
                                        task.line.to_string().magenta(),
                                        task.column.to_string().magenta(),
                                    );
                                } else if self.patcher.formatter.format
                                    == crate::cli::OutputFormat::Github
                                {
                                    println!(
                                        "::error file={},line={},col={}::Dependency {}@{} is pinned but not vetted (strict mode enabled)",
                                        file_path, task.line, task.column, action_name, tag
                                    );
                                }
                            }

                            if self.patcher.formatter.format == crate::cli::OutputFormat::Junit {
                                junit_cases.push(format!(
                                    "    <testcase name=\"{}\" classname=\"{}\" time=\"0.0\">\n      <failure message=\"Dependency is not vetted\">Dependency {}@{} is pinned but not vetted in {}:{}:{}</failure>\n    </testcase>",
                                    action_name, file_path, action_name, tag, file_path, task.line, task.column
                                ));
                            }
                        } else if self.patcher.formatter.format == crate::cli::OutputFormat::Junit {
                            junit_cases.push(format!(
                                "    <testcase name=\"{}\" classname=\"{}\" time=\"0.0\"/>",
                                action_name, file_path
                            ));
                        }
                    }
                    crate::patcher::formatter::HashSecurityStatus::Vetted => {
                        if self.patcher.formatter.format == crate::cli::OutputFormat::Junit {
                            junit_cases.push(format!(
                                "    <testcase name=\"{}\" classname=\"{}\" time=\"0.0\"/>",
                                action_name, file_path
                            ));
                        }
                    }
                }
            }
        }

        let is_success = unpinned.is_empty() && compromised.is_empty() && non_vetted.is_empty();

        if !self.patcher.formatter.quiet {
            if self.patcher.formatter.format == crate::cli::OutputFormat::Text {
                if is_success {
                    eprintln!(
                        "\n{} Verification successful! All dependencies are pinned and secure.",
                        "✔".green().bold()
                    );
                } else {
                    eprintln!(
                        "\n{} Verification failed! Some dependencies are not pinned, are compromised, or are not vetted.",
                        "✗".red().bold()
                    );
                }
            } else if self.patcher.formatter.format == crate::cli::OutputFormat::Junit {
                let mut xml = String::new();
                xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

                let total_tests = junit_cases.len();
                let total_failures = unpinned.len() + compromised.len() + non_vetted.len();

                xml.push_str(&format!(
                    "<testsuites name=\"Pinner Verification\" tests=\"{}\" failures=\"{}\" errors=\"0\" time=\"0.0\">\n",
                    total_tests, total_failures
                ));
                xml.push_str(&format!(
                    "  <testsuite name=\"pinner.verify\" tests=\"{}\" failures=\"{}\" errors=\"0\" time=\"0.0\">\n",
                    total_tests, total_failures
                ));
                for case in junit_cases {
                    xml.push_str(&case);
                    xml.push('\n');
                }
                xml.push_str("  </testsuite>\n");
                xml.push_str("</testsuites>\n");

                print!("{}", xml);
            }
        }

        Ok(crate::core::VerificationResult {
            unpinned,
            compromised,
            non_vetted,
        })
    }

    /// Forcibly sets a specific action to a provided hash across all files.
    pub async fn set(
        &self,
        paths: &[PathBuf],
        action: &str,
        hash: &str,
    ) -> Result<(), PinnerError> {
        let (tasks, file_contents) = self.scanner.collect_tasks(paths).await?;
        let mut results = Vec::new();

        for task in tasks {
            if task.action.0 == action {
                results.push(crate::core::UpdateResult {
                    action: task.action.clone(),
                    path: task.path.clone(),
                    old_tag: task.current_tag.clone(),
                    task: task.clone(),
                    new_sha: crate::core::DependencyRef::from(hash.to_string()),
                    new_tag: None,
                });
            }
        }

        self.patcher.apply_changes(results, file_contents).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::UpgradeStrategy;
    use crate::patcher::Formatter;
    use crate::resolver::provider::MockRemoteProvider;
    use crate::resolver::registry::MockRegistryProvider;
    use std::fs;
    use std::sync::Arc;
    use std::time::Duration;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_pipeline_verify_github_format() {
        let dir = tempdir().unwrap();
        let f = dir.path().join("f.yml");
        fs::write(&f, "uses: actions/checkout@v3").unwrap();

        let scanner = Scanner::new(vec![]);
        let osv_client = Arc::new(crate::resolver::OsvClient::new(
            None,
            false,
            Duration::from_secs(0),
        ));
        let resolver = Resolver::new(
            Arc::new(MockRemoteProvider::new()),
            Arc::new(MockRegistryProvider::new()),
            osv_client,
            UpgradeStrategy::Latest,
            1,
        );
        let ui = Arc::new(crate::patcher::ui::TestUi { response: true });

        let patcher = Patcher::new(
            Formatter::new(
                crate::cli::OutputFormat::Github,
                false,
                vec![],
                vec![],
                true,
            ),
            ui,
            false,
        );
        let pipeline = Pipeline::new(scanner, resolver, patcher);

        let res = pipeline
            .verify(std::slice::from_ref(&f), false, false)
            .await
            .unwrap();
        assert!(!res.is_success());
    }

    #[tokio::test]
    async fn test_pipeline_verify_junit_format() {
        let dir = tempdir().unwrap();
        let f = dir.path().join("f.yml");
        fs::write(&f, "uses: actions/checkout@v3").unwrap();

        let scanner = Scanner::new(vec![]);
        let osv_client = Arc::new(crate::resolver::OsvClient::new(
            None,
            false,
            Duration::from_secs(0),
        ));
        let resolver = Resolver::new(
            Arc::new(MockRemoteProvider::new()),
            Arc::new(MockRegistryProvider::new()),
            osv_client,
            UpgradeStrategy::Latest,
            1,
        );
        let ui = Arc::new(crate::patcher::ui::TestUi { response: true });

        let patcher = Patcher::new(
            Formatter::new(crate::cli::OutputFormat::Junit, false, vec![], vec![], true),
            ui,
            false,
        );
        let pipeline = Pipeline::new(scanner, resolver, patcher);

        let res = pipeline
            .verify(std::slice::from_ref(&f), false, false)
            .await
            .unwrap();
        assert!(!res.is_success());
    }

    #[tokio::test]
    async fn test_pipeline_verify_all_formats_and_statuses() {
        let dir = tempdir().unwrap();
        let f = dir.path().join("f.yml");
        fs::write(&f, "uses: actions/checkout@v3\nuses: actions/setup-node@1111111111111111111111111111111111111111\nuses: docker://node:18").unwrap();

        let ui = Arc::new(crate::patcher::ui::TestUi { response: true });

        // Test with OutputFormat::Github
        {
            let scanner = Scanner::new(vec![]);
            let osv_client = Arc::new(crate::resolver::OsvClient::new(
                None,
                false,
                Duration::from_secs(0),
            ));
            let resolver = Resolver::new(
                Arc::new(MockRemoteProvider::new()),
                Arc::new(MockRegistryProvider::new()),
                osv_client,
                UpgradeStrategy::Latest,
                1,
            );
            let patcher_github = Patcher::new(
                Formatter::new(
                    crate::cli::OutputFormat::Github,
                    false,
                    vec![],
                    vec![],
                    true,
                ),
                ui.clone(),
                false,
            );
            let pipeline_github = Pipeline::new(scanner, resolver, patcher_github);
            let res_github = pipeline_github
                .verify(std::slice::from_ref(&f), false, true)
                .await
                .unwrap();
            assert!(!res_github.is_success());
        }

        // Test with OutputFormat::Junit
        {
            let scanner = Scanner::new(vec![]);
            let osv_client = Arc::new(crate::resolver::OsvClient::new(
                None,
                false,
                Duration::from_secs(0),
            ));
            let resolver = Resolver::new(
                Arc::new(MockRemoteProvider::new()),
                Arc::new(MockRegistryProvider::new()),
                osv_client,
                UpgradeStrategy::Latest,
                1,
            );
            let patcher_junit = Patcher::new(
                Formatter::new(crate::cli::OutputFormat::Junit, false, vec![], vec![], true),
                ui,
                false,
            );
            let pipeline_junit = Pipeline::new(scanner, resolver, patcher_junit);
            let res_junit = pipeline_junit
                .verify(std::slice::from_ref(&f), false, true)
                .await
                .unwrap();
            assert!(!res_junit.is_success());
        }
    }
}