Skip to main content

Research

GitHub Actions Security Risks

Understanding the security risks of GitHub Actions workflows with pull_request_target event

Pentrova Research Pentrova Research
7 min read

Reading mode

Understanding the Risks of pull_request_target#

The pull_request_target event in GitHub Actions workflows poses significant security risks, including elevated trust and access to repository secrets. This event allows workflows to run with the base repository’s GITHUB_TOKEN and access to repository and organization secrets, making it a potential target for code injection attacks. To understand the risks of pull_request_target, it is essential to consider the differences between the pull_request_target and pull_request events. The pull_request event runs the workflow file from the merge commit of the pull request, which can be controlled by someone without write access to the base repository. In contrast, the pull_request_target event runs the workflow file from the base repository, giving it elevated trust and access to secrets. This means that the pull_request_target event has the potential to access sensitive data and perform actions that could compromise the security of the repository. To mitigate these risks, it is recommended to use the pull_request event instead of pull_request_target, unless absolutely necessary. Additionally, it is crucial to implement least privilege access in workflows and to limit access to secrets and sensitive data. By understanding the risks of pull_request_target and implementing measures to mitigate them, developers can ensure that their GitHub Actions workflows are secure and that the risk of security breaches is minimized.

Comparing pull_request_target and pull_request Events#

The pull_request_target event differs from the pull_request event in terms of the level of trust and access to secrets. The pull_request event runs the workflow file from the merge commit of the pull request, which can be controlled by someone without write access to the base repository, as noted in GitHub’s documentation on securely using pull_request_target. In contrast, the pull_request_target event runs the workflow file from the base repository, giving it elevated trust and access to secrets.

Preventing Script Injections in Workflows#

To prevent script injections in GitHub Actions workflows, it is essential to sanitize user input and protect secrets, as advised in GitHub’s documentation on script injections. This can be achieved by validating and sanitizing user input, using secrets for sensitive information, and masking sensitive data. For example, when using the github.event.issue.title context, ensure that the value is sanitized and validated to prevent script injections.

Using the GITHUB_TOKEN Securely#

To use the GITHUB_TOKEN securely in GitHub Actions workflows, it is essential to follow best practices that minimize the risk of token exposure and unauthorized access. The GITHUB_TOKEN should be granted the least privileges required to perform the necessary actions, and sensitive data should be masked using ::add-mask::VALUE to prevent exposure. This can be achieved by setting the minimum required permissions for the GITHUB_TOKEN and limiting access to secrets. For example, when using the GITHUB_TOKEN to access repository secrets, ensure that the token has the minimum required permissions, such as read access only, and that sensitive data is masked. Additionally, it is crucial to validate and sanitize user input to prevent script injections and exposure of sensitive data. By following these best practices, developers can ensure that the GITHUB_TOKEN is used securely and minimize the risk of security breaches. Furthermore, it is recommended to regularly review and update the permissions granted to the GITHUB_TOKEN to ensure that they are still necessary and aligned with the workflow’s requirements. This can be done by using the permissions keyword in the workflow file to specify the required permissions. By implementing these measures, developers can ensure that the GITHUB_TOKEN is used securely and that the risk of security breaches is minimized.

Securing Workflow Inputs and Outputs#

Securing workflow inputs and outputs is crucial to preventing script injections and exposure of sensitive data in GitHub Actions workflows. To achieve this, it is essential to validate and sanitize user input using techniques such as whitelisting and escaping. This can be done by using validation and sanitization libraries or frameworks that provide built-in support for input validation. Additionally, sensitive data should be protected by using secrets for sensitive information and masking sensitive data. For example, when using the github.event.issue.title context, ensure that the value is sanitized and validated to prevent script injections. It is also recommended to use environment variables to store sensitive data, such as secrets and tokens, and to limit access to these variables to only the necessary jobs and steps within the workflow. By implementing these measures, developers can ensure that workflow inputs and outputs are secure and that the risk of security breaches is minimized. Furthermore, it is crucial to regularly review and update the workflow file to ensure that it is aligned with the latest security best practices and that any potential vulnerabilities are addressed. This can be done by using tools and services that provide security scanning and vulnerability detection for GitHub Actions workflows.

Implementing Least Privilege Access in Workflows#

Implementing least privilege access in GitHub Actions workflows involves granting the minimum required permissions to the GITHUB_TOKEN and limiting access to secrets. This can be achieved by setting the default permission for the GITHUB_TOKEN to read access only and increasing permissions as required for individual jobs within the workflow file. For example, when using the GITHUB_TOKEN to access repository secrets, ensure that the token has the minimum required permissions, such as read access only, and that sensitive data is masked. Additionally, it is crucial to limit access to secrets and sensitive data by using environment variables to store sensitive data and limiting access to these variables to only the necessary jobs and steps within the workflow. By implementing least privilege access, developers can ensure that the GITHUB_TOKEN is used securely and that the risk of security breaches is minimized. Furthermore, it is recommended to regularly review and update the workflow file to ensure that it is aligned with the latest security best practices and that any potential vulnerabilities are addressed. This can be done by using tools and services that provide security scanning and vulnerability detection for GitHub Actions workflows. By implementing least privilege access and regularly reviewing and updating the workflow file, developers can ensure that their GitHub Actions workflows are secure and that the risk of security breaches is minimized.

FAQ#

  • What are the security risks of using the pull_request_target event in GitHub Actions workflows? The security risks include elevated trust and access to repository secrets, making it a potential target for code injection attacks.
  • How can I prevent script injections in my GitHub Actions workflows? Prevent script injections by sanitizing user input and protecting secrets, using secrets for sensitive information, and masking sensitive data.
  • What are the best practices for using the GITHUB_TOKEN in GitHub Actions workflows? Best practices include setting the minimum required permissions and masking sensitive data, granting the GITHUB_TOKEN the least privileges required, and limiting access to secrets.

Written by

Pentrova Research Pentrova Research

Pentrova Research writes about deterministic offensive-security proof, LLM-driven pentest chains, and how to ship exploit-grade evidence into engineering pipelines.

Deterministic Authorization Testing

Catch BOLA flaws that return HTTP 200 OK

Traditional scanners miss logic flaws in valid JSON responses. Pentrova maps multi-tenant object access across roles to prove BOLA before merge.

Test API Authorization →

Keep reading

Site search

↑↓ navigateEnter openEsc close