Your GitHub Actions Workflow Has More Power Than Most Developers Realize
Most developers think of GitHub Actions as automation. Build the app. Run tests. Deploy. Publish a package. Maybe send a notification. But a workflow is not just a script runner. Depending on how you configure it, a GitHub Actions job may be able to: read your repository modify code create releases publish packages access secrets talk to cloud providers deploy production comment on pull requests…
Most developers perceive GitHub Actions as a mere automation tool, capable of executing tasks like building applications, running tests, deploying code, and publishing packages. However, a GitHub Actions workflow is not limited to these functions. Depending on its configuration, a workflow can perform additional actions such as reading repository data, modifying code, creating releases, publishing packages, accessing secrets, interacting with cloud providers, and altering repository state.
This means that a GitHub Actions pipeline is not just automation but also a part of the security boundary of a project. Many workflows possess more authority than developers realize.
The GITHUB_TOKEN, an automatic creation by GitHub for every job, dictates the permissions of a workflow within a repository. If developers do not explicitly consider permissions, workflows can inadvertently be granted more authority than necessary. It is advisable to provide each workflow with the minimum permissions essential for its operation. For instance, a job that only checks out code and runs tests may not require write access.
When defining a job's permissions, it is crucial to think of them as belonging to the job, not to the general trust in GitHub Actions. A common misconception is that permissions pertain to the overarching trust in GitHub Actions. Instead, permissions belong to the job itself.
One trigger that developers should scrutinize carefully is the pull_request_target trigger. Although it appears similar to the pull_request trigger, the security model is fundamentally different. A pull_request trigger from a fork is restricted, with GitHub withholding repository secrets and confining the workflow to a read-only token.
Conversely, the pull_request_target trigger operates with the trust of the base repository, receiving repository secrets and a read/write GITHUB_TOKEN. This functionality is beneficial for tasks like labeling pull requests and authenticated checks. However, combining it with untrusted code can pose a significant risk.
The "Pwn Request" pattern exemplifies this risk. This pattern involves a pull_request_target workflow that checks out code from a pull request and then executes commands like npm install. Although this may appear reasonable at first, the untrusted code running inside a privileged workflow can potentially modify various components such as package.json, install scripts, test scripts, build scripts, Makefiles, and configuration files.
GitHub explicitly warns against executing fork code within a privileged pull_request_target workflow. The issue lies not in the checkout process itself but in the execution of the checked-out code.
Another often-overlooked aspect is that running npm install may execute install scripts within dependencies, which could involve running code controlled by the pull request. This same concept extends beyond npm; any build tools or configuration management scripts can also pose a risk when executed within a privileged job.
Secrets significantly escalate the risk involved. If a workflow contains environment variables defined by secrets, any step executed within that job could potentially interact with these secrets. To mitigate this risk, it is recommended to employ least-privilege credentials, ensuring that only the necessary secrets are accessible to each job or action.
Developers should ask themselves a critical question: If this step were compromised, what could it potentially steal or modify? This query should guide the structuring of jobs and actions.
Separating untrusted code from privileged operations is a prudent strategy. For instance, testing a pull request should be isolated from operations that require privileged access, such as publishing packages or deploying to production. This separation helps in managing trust boundaries and minimizes the risk associated with untrusted code.
Third-party actions, which are essentially dependencies of workflows, deserve careful consideration. Developers often review npm packages but may unknowingly execute third-party actions without realizing the potential risks. These actions can execute within the scope of your workflow, potentially accessing repository secrets or utilizing the workflow's GITHUB_TOKEN.
To mitigate this risk, it is advisable to pin third-party actions to a specific commit SHA rather than relying solely on version tags. A full commit SHA provides immutability and ensures that the action remains consistent and controlled.
Furthermore, it is essential to recognize that workflows have a dependency tree, not just the application code. This dependency tree includes various GitHub Actions, such as actions/checkout, setup-node, deployment actions, security scanners, and custom third-party actions. These actions may run with elevated privileges, potentially posing a security risk. Therefore, it is crucial to review and manage the privileges associated with these actions.
Lastly, the use of long-lived cloud credentials in workflows should be approached with caution. Many deployment setups involve storing AWS access keys and secret keys as secrets. While this approach is convenient, it introduces a significant security risk, as long-lived credentials can be compromised and used to access cloud resources.
Developers should consider rotating these credentials frequently and implementing more secure credential management practices to minimize the risk associated with long-lived cloud credentials.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.