Using user-controlled input in GitHub Actions may lead to code injection in contexts like run: or script:.

Code injection in GitHub Actions may allow an attacker to exfiltrate the temporary GitHub repository authorization token. The token might have write access to the repository, allowing an attacker to use the token to make changes to the repository.

The best practice to avoid code injection vulnerabilities in GitHub workflows is to set the untrusted input value of the expression to an intermediate environment variable.

It is also recommended to limit the permissions of any tokens used by a workflow such as the GITHUB_TOKEN.

The following example lets a user inject an arbitrary shell command:

The following example uses shell syntax to read the environment variable and will prevent the attack:

  • GitHub Security Lab Research: Keeping your GitHub Actions and workflows secure: Untrusted input.
  • GitHub Docs: Security hardening for GitHub Actions.
  • GitHub Docs: Permissions for the GITHUB_TOKEN.