Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: primitive Windows batch handling in github-env #217

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/audit/github_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ use crate::models::Step;
use crate::state::AuditState;
use anyhow::Context;
use github_actions_models::workflow::job::StepBody;
use regex::Regex;
use std::cell::RefCell;
use std::ops::Deref;
use std::sync::LazyLock;
use tree_sitter::Parser;

static GITHUB_ENV_WRITE_CMD: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"(?mi)^.+\s*>>?\s*"?%GITHUB_ENV%"?.*$"#).unwrap());

pub(crate) struct GitHubEnv {
// NOTE: interior mutability used since Parser::parse requires &mut self
bash_parser: RefCell<Parser>,
Expand Down Expand Up @@ -52,9 +57,10 @@ impl GitHubEnv {
}

fn uses_github_env(&self, run_step_body: &str, shell: &str) -> anyhow::Result<bool> {
// TODO: handle `run:` bodies other than bash/sh.
match shell {
"bash" | "sh" => self.bash_uses_github_env(run_step_body),
"cmd" => Ok(GITHUB_ENV_WRITE_CMD.is_match(run_step_body)),
// TODO: handle pwsh/powershell/python.
&_ => {
log::warn!(
"'{}' shell not supported when evaluating usage of GITHUB_ENV",
Expand Down Expand Up @@ -129,7 +135,7 @@ impl WorkflowAudit for GitHubEnv {

#[cfg(test)]
mod tests {
use crate::audit::github_env::GitHubEnv;
use crate::audit::github_env::{GitHubEnv, GITHUB_ENV_WRITE_CMD};
use crate::audit::WorkflowAudit;
use crate::state::{AuditState, Caches};

Expand Down Expand Up @@ -180,4 +186,22 @@ mod tests {
assert_eq!(uses_github_env, *expected);
}
}

#[test]
fn test_exploitable_cmd_patterns() {
for (case, expected) in &[
// Common cases
("echo LIBRARY=%LIBRARY%>>%GITHUB_ENV%", true),
("echo LIBRARY=%LIBRARY%>> %GITHUB_ENV%", true),
("echo LIBRARY=%LIBRARY% >> %GITHUB_ENV%", true),
("echo LIBRARY=%LIBRARY% >> \"%GITHUB_ENV%\"", true),
("echo>>\"%GITHUB_ENV%\" %%a=%%b", true),
(
"echo SERVER=${{ secrets.SQL19SERVER }}>> %GITHUB_ENV%",
true,
),
] {
assert_eq!(GITHUB_ENV_WRITE_CMD.is_match(case), *expected);
}
}
}