This post is the fourth in a series of posts about GitHub Actions security. Part 1, Part 2, Part 3
We recently implemented CodeQL support for GitHub Actions workflows. We ran these queries at scale to test them against diverse open source projects. Having triaged and reported numerous alerts, we have identified some new common patterns that often lead to vulnerabilities in GitHub workflows:
Misuse ofpull_request_target
trigger
The pull_request_target
event trigger, while offering powerful automation capabilities in GitHub Actions, harbors a dark side filled with potential security pitfalls. This event trigger, designed to execute workflows within the context of Pull Request’s base branch, presents special characteristics that severely increases the impact in case of any vulnerability. A workflow activated by pull_request_target
and triggered from a fork operates with significant privileges in contrast to the pull_request
event:
pull_request
events.When working with pull_request_triggered
workflows, we have to be very careful and pay special attention to the following scenarios:
pull_request_target
, unleashes havoc. This malicious code, running in the context of the target repository’s environment, could exfiltrate secrets or even tamper with repository contents and releases. The danger lies in the inadvertent checkout and execution of code from untrusted sources. Common mistakes include using the actions/checkout
action with a reference to the pull request’s head branch.head.ref
and head.sha
, can lead to vulnerabilities. head.ref
, pointing to the branch, is susceptible to manipulation by attackers. In contrast, head.sha
, referencing the specific commit, provides a reliable and immutable pointer to the reviewed and approved code. Using the incorrect variable can create an opening for attackers to inject malicious code after approval.permissions: {}
configuration can unexpectedly pave the way for cache poisoning attacks. This attack vector involves injecting malicious content into the cache, which can subsequently affect other workflows relying on the poisoned cache entries. Even if a workflow doesn’t have write access to the repository, it can still poison the cache, leading to potential code execution or data manipulation in other workflows that utilize the compromised cache.If we really need to use this trigger event, there are a few ways to harden the workflows to prevent any abuses:
github.event.pull_request.head.repo.owner.login == "myorg"
to restrict workflow execution.pull_request
events, handle the initial processing of pull requests without access to sensitive secrets or write permissions. Privileged workflows, activated by workflow_run
events, are invoked only after the unprivileged workflow has completed its checks. This separation ensures that potentially malicious code from forked pull requests never executes within a privileged context. The unprivileged workflow will generally need to communicate and pass information to the privileged one. This is a crucial security boundary, and as we will see in the next section, any data coming from the unprivileged workflow should be considered untrusted and potentially dangerous.workflow_run
event
The workflow_run
event trigger in GitHub Actions is designed to automate tasks based on the execution or completion of another workflow. It may grant write permissions and access to secrets even if the triggering workflow doesn’t have such privileges. While this is beneficial for tasks like labeling pull requests based on test results, it poses significant security risks if not used carefully.
The workflow_run
trigger poses a risk because it can often be initiated by an attacker. Some maintainers were surprised by this, believing that their triggering workflows, which were run on events such as release
, were safe. This assumption was based on the idea that since an attacker couldn’t trigger a new release, they shouldn’t be able to initiate the triggering workflow or the subsequent workflow_run
workflow.
The reality is that an attacker can submit a pull request that modifies the triggering workflow and even replace the triggering events. Since pull_request
workflows run in the context of the pull request’s HEAD
branch, the modified workflow will run and, upon completion, will be able to trigger an existing workflow_run
workflow. The danger arises from the fact that even if the triggering pull_request
workflow is not privileged, the triggered workflow_run
workflow will have access to secrets and write-scoped tokens, even if the initial workflow did not have those privileges. This enables privilege escalation attacks, allowing attackers to execute malicious code with elevated permissions within the CI/CD pipeline.
Another significant pitfall with the workflow_run
event trigger is artifact poisoning. Artifacts are files generated during a workflow run that can be shared with other workflows. Attackers can poison these artifacts by uploading malicious content through a pull request. When a workflow_run
workflow downloads and uses these poisoned artifacts, it can lead to arbitrary code execution or other malicious activities within the privileged workflow. The issue is that many workflow_run
workflows do not verify the contents of downloaded artifacts before using them, making them vulnerable to various attacks.
Securing workflow_run
workflows requires a multi-faceted approach. By understanding the inherent risks and implementing the recommended mitigations, developers can leverage the automation benefits of workflow_run
while minimizing the potential for security compromises.
workflow_run
workflows can be triggered using the branches
filter. This helps restrict the scope of potential attacks by preventing them from being triggered on branches from forks.github.event_name != 'pull_request'
to prevent workflow_run
workflows from being triggered by pull requests from forks. This adds an extra layer of protection by ensuring that the triggering workflow originates from a trusted source./tmp
to prevent potential file overwrites, i.e. the pollution of the workspace.github.repository_owner == 'myorg'
), which is not effective in mitigating workflow_run
risks since the workflow always run in the context of the default branch which belongs to the organization.The issue_comment
event trigger in GitHub Actions is a powerful tool for automating workflows based on comments on issues and pull requests. When applied in the context of IssueOps, it can streamline tasks like running commands in response to specific comments. However, this convenience comes with significant security risks that must be carefully considered.
pull_request_target
event trigger, workflows using issue_comment
can be vulnerable to TOCTOU attacks. If the workflow checks out and executes code from a pull request based on an issue comment, an attacker could exploit the time window between the comment and the workflow execution. An attacker might initially submit a harmless pull request, waiting for an administrator to review and approve the workflow by adding a comment. Once the approval is given, the attacker could quickly update the pull request with malicious code, which would then be executed by the workflow. \issue_comment
event trigger is not subject to the pull request approval mechanisms intended to prevent abuse. Even if the workflows triggered by pull request
require approvals, an attacker can trigger an issue_comment
workflow by simply adding a comment to the pull request, potentially executing malicious code without any review. \pull_request
trigger contains details about the latest commit SHA of the Pull Request, there is no need for workflow to resolve a Pull Request number into the latest commit SHA, as it is the casse with issue_comment
, and therefore there is no window for an attacker to modify the Pull Request. Remember to use the commit SHA rather than the HEAD reference to prevent TOCTOU vulnerabilities. \issue_comment
event triggers. The issue_comment
event always executes within the context of the target repository’s default branch, making repository checks redundant. \We discovered a variety of new patterns of potential supply chain attacks through insecure GitHub Actions workflows. The new CodeQL packs allow you to scan your repository for those patterns. Enable CodeQL for GitHub Actions in your open source project to protect the ecosystem we all depend on!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4