Skip to content

Find the holes before someone else does. A fast, 100% Rust vulnerability scanner: XSS, SQL injection, leaked secrets, dependency CVEs and AI agent setups.

fix(config): ignore commented workflow injection lines (#141)

Src-Bhaveshdd403d53e722
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f69d045..2a73167 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- Config: `config.gha-script-injection` skips a commented-out workflow line like
+ `# - run: echo "${{ github.event.pull_request.title }}"`. A `#` line inside a `run: |` or `script: |`
+ block is still reported, since the expression is filled in before the shell sees the comment.
- Config: `config.gha-script-injection` also checks `workflow_run` workflows, so
`github.event.workflow_run.head_branch`, `.display_title`, `.head_commit.message` and the head commit's
author and committer name and email pasted into a `run:` step are reported. Fields the triggering run
diff --git a/src/rules/config.rs b/src/rules/config.rs
index d8e7017..034a58d 100644
--- a/src/rules/config.rs
+++ b/src/rules/config.rs
@@ -21,6 +21,70 @@ fn shell(f: &FileInfo) -> bool {
fn workflow(f: &FileInfo) -> bool {
f.under(".github/workflows/") && f.ext_in(&["yml", "yaml"])
}
+
+// A `#` inside a block scalar is script text, not a YAML comment.
+fn block_scalar_indent(line: &str) -> Option<(usize, Option<usize>)> {
+ let trimmed = line.trim_start();
+ let content = trimmed.strip_prefix("- ").map_or(trimmed, str::trim_start);
+ let indent = line.len() - content.len();
+ let (key, value) = content.split_once(':')?;
+ if !matches!(key.trim(), "run" | "script") {
+ return None;
+ }
+ let value = value.trim_start();
+ if !value.starts_with('|') && !value.starts_with('>') {
+ return None;
+ }
+ let indicator: Vec<char> = value[1..].split('#').next()?.trim().chars().collect();
+ let valid = match indicator.as_slice() {
+ [] => true,
+ [one] => matches!(one, '+' | '-' | '1'..='9'),
+ [one, two] => {
+ (matches!(one, '+' | '-') && matches!(two, '1'..='9'))
+ || (matches!(one, '1'..='9') && matches!(two, '+' | '-'))
+ }
+ _ => false,
+ };
+ let explicit = indicator
+ .iter()
+ .find(|c| c.is_ascii_digit())
+ .and_then(|c| c.to_digit(10))
+ .map(|n| indent + n as usize);
+ valid.then_some((indent, explicit))
+}
+
+fn in_workflow_block_scalar(text: &Text, line_no: usize) -> bool {
+ let mut block: Option<(usize, Option<usize>)> = None;
+ for n in 1..=line_no {
+ let line = text.line(n);
+ let indent = line.len() - line.trim_start().len();
+ if let Some((header_indent, content_indent)) = block {
+ if line.trim().is_empty() {
+ if n == line_no {
+ return false;
+ }
+ continue;
+ }
+ if indent <= header_indent {
+ block = None;
+ } else {
+ let content_indent = content_indent.unwrap_or_else(|| {
+ block = Some((header_indent, Some(indent)));
+ indent
+ });
+ if n == line_no {
+ return indent >= content_indent;
+ }
+ continue;
+ }
+ }
+ if let Some((header_indent, explicit_content_indent)) = block_scalar_indent(line) {
+ block = Some((header_indent, explicit_content_indent));
+ }
+ }
+ false
+}
+
fn shell_or_workflow(f: &FileInfo) -> bool {
shell(f) || workflow(f)
}
@@ -397,9 +461,15 @@ pub fn keep(file: &FileInfo, text: &Text, f: &Finding, hcl: &mut HclWalk) -> boo
FROM.captures(text.line(f.line))
.is_none_or(|c| !stages.contains(&c[1].to_ascii_lowercase()))
}
- "config.gha-script-injection" if workflow(file) => YAML_KEY
- .captures(text.line(f.line))
- .is_none_or(|c| &c[1] == "run" || &c[1] == "script"),
+ "config.gha-script-injection" if workflow(file) => {
+ let line = text.line(f.line);
+ let yaml_comment =
+ line.trim_start().starts_with('#') && !in_workflow_block_scalar(text, f.line);
+ !yaml_comment
+ && YAML_KEY
+ .captures(line)
+ .is_none_or(|c| &c[1] == "run" || &c[1] == "script")
+ }
// In a workflow only a `run:` step (or a line of its block) runs; a step `name:` or a
// commented-out line doesn't.
"config.remote-script" if workflow(file) => {
diff --git a/tests/fixtures/config/.github/workflows/ci.yml b/tests/fixtures/config/.github/workflows/ci.yml
index 000b077..4d11855 100644
--- a/tests/fixtures/config/.github/workflows/ci.yml
+++ b/tests/fixtures/config/.github/workflows/ci.yml
@@ -29,3 +29,14 @@ jobs:
- run: echo "Run ${{ github.event.workflow_run.id }} ${{ github.event.workflow_run.conclusion }}"
- run: echo "By ${{ github.event.workflow_run.head_commit.author.email }}"
- run: echo "Repo ${{ github.event.workflow_run.repository.name }}"
+ # - run: echo "${{ github.event.pull_request.title }}"
+ - run: |
+ # ${{ github.event.pull_request.title }}
+ echo "keep checking script comments"
+ # ${{ github.event.pull_request.title }}
+ - run: |2
+ # ${{ github.event.pull_request.body }}
+ - uses: actions/github-script@v7
+ with:
+ script: |
+ # ${{ github.event.pull_request.body }}
diff --git a/tests/fixtures/config/EXPECTED b/tests/fixtures/config/EXPECTED
index ecb8003..7a093ef 100644
--- a/tests/fixtures/config/EXPECTED
+++ b/tests/fixtures/config/EXPECTED
@@ -55,3 +55,5 @@ k8s/deploy.yaml:5 config.privileged-container
k8s/deploy.yaml:6 config.privileged-container
nginx.conf:2 config.nginx-autoindex
nginx.conf:3 config.weak-tls-version
+.github/workflows/ci.yml:34 config.gha-script-injection
+.github/workflows/ci.yml:42 config.gha-script-injection