|
| 1 | +use std::ops::Deref; |
| 2 | + |
| 3 | +use github_actions_models::{ |
| 4 | + common::Expression, |
| 5 | + workflow::{ |
| 6 | + job::{Container, DockerCredentials}, |
| 7 | + Job, |
| 8 | + }, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + finding::{Confidence, Severity}, |
| 13 | + models::AuditConfig, |
| 14 | +}; |
| 15 | + |
| 16 | +use super::WorkflowAudit; |
| 17 | + |
| 18 | +pub(crate) struct HardcodedContainerCredentials<'a> { |
| 19 | + pub(crate) _config: AuditConfig<'a>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<'a> WorkflowAudit<'a> for HardcodedContainerCredentials<'a> { |
| 23 | + fn ident() -> &'static str |
| 24 | + where |
| 25 | + Self: Sized, |
| 26 | + { |
| 27 | + "hardcoded-container-credentials" |
| 28 | + } |
| 29 | + |
| 30 | + fn new(config: crate::models::AuditConfig<'a>) -> anyhow::Result<Self> |
| 31 | + where |
| 32 | + Self: Sized, |
| 33 | + { |
| 34 | + Ok(Self { _config: config }) |
| 35 | + } |
| 36 | + |
| 37 | + fn audit<'w>( |
| 38 | + &self, |
| 39 | + workflow: &'w crate::models::Workflow, |
| 40 | + ) -> anyhow::Result<Vec<crate::finding::Finding<'w>>> { |
| 41 | + let mut findings = vec![]; |
| 42 | + |
| 43 | + for job in workflow.jobs() { |
| 44 | + let Job::NormalJob(normal) = job.deref() else { |
| 45 | + continue; |
| 46 | + }; |
| 47 | + |
| 48 | + if let Some(Container::Container { |
| 49 | + image: _, |
| 50 | + credentials: |
| 51 | + Some(DockerCredentials { |
| 52 | + username: _, |
| 53 | + password: Some(password), |
| 54 | + }), |
| 55 | + .. |
| 56 | + }) = &normal.container |
| 57 | + { |
| 58 | + // If the password doesn't parse as an expression, it's hardcoded. |
| 59 | + if Expression::from_curly(password.into()).is_none() { |
| 60 | + findings.push( |
| 61 | + Self::finding() |
| 62 | + .severity(Severity::High) |
| 63 | + .confidence(Confidence::High) |
| 64 | + .add_location( |
| 65 | + job.location() |
| 66 | + .annotated("container registry password is hard-coded"), |
| 67 | + ) |
| 68 | + .build(workflow)?, |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for (service, config) in normal.services.iter() { |
| 74 | + if let Container::Container { |
| 75 | + image: _, |
| 76 | + credentials: |
| 77 | + Some(DockerCredentials { |
| 78 | + username: _, |
| 79 | + password: Some(password), |
| 80 | + }), |
| 81 | + .. |
| 82 | + } = &config |
| 83 | + { |
| 84 | + if Expression::from_curly(password.into()).is_none() { |
| 85 | + findings.push( |
| 86 | + Self::finding() |
| 87 | + .severity(Severity::High) |
| 88 | + .confidence(Confidence::High) |
| 89 | + .add_location(job.location().annotated(format!( |
| 90 | + "service {service}: container registry password is hard-coded" |
| 91 | + ))) |
| 92 | + .build(workflow)?, |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + Ok(findings) |
| 100 | + } |
| 101 | +} |
0 commit comments