简体   繁体   English

如何安全地允许 Github Actions 检查 PR 并在评论中发布结果

[英]How to securely allow Github Actions to check PR and post results in comment

I'm trying to understand the security model in Github Actions (GHA).我正在尝试了解 Github 操作 (GHA) 中的安全性 model。 Let's say I have the following requirements on a public repo:假设我对公共回购有以下要求:

  1. Allow pull requests from forked repos to be opened允许打开来自分叉回购的拉取请求
  2. GHA should run unit tests on pull requests GHA 应该对拉取请求运行单元测试
  3. GHA should post unit test results as a PR comment GHA 应该将单元测试结果作为 PR 评论发布

In order for the third requirement to work, the pull request needs access to the GITHUB_TOKEN with repo write permissions.为了使第三个要求起作用,拉取请求需要访问具有回购写入权限的 GITHUB_TOKEN。 This will require the following two permissions:这将需要以下两个权限:

  • Run Workflows from fork pull requests.从分支拉取请求运行工作流。
  • Send write tokens to Workflows from fork pull requests从分叉拉取请求向工作流发送写入令牌

首先,从分叉拉取请求运行工作流。其次,将写入令牌从 fork 拉取请求发送到工作流

Now if write tokens are sent to the Workflows in fork PRs, what's to prevent a hacker from changing the Workflow in the PR and using it for any number of malicious purposes (creating a malicious release in the original repo or exfiltrating repo secrets)?现在,如果写令牌被发送到分叉 PR 中的工作流,如何防止黑客更改 PR 中的工作流并将其用于任何数量的恶意目的(在原始回购中创建恶意版本或泄露回购秘密)? I understand you can limit the permissions of the token , but this is done within the workflow;我知道您可以限制令牌的权限,但这是在工作流程中完成的; the hacker can just as easily remove the limitations as part of the PR.黑客可以像 PR 的一部分一样轻松地删除限制。

Is there any way to accomplish the three requirements without this security hole?有没有办法在没有这个安全漏洞的情况下完成这三个要求?

The must-read for this question is: Keeping your GitHub Actions and workflows secure Part 1: Preventing pwn requests这个问题的必读是: Keeping your GitHub Actions and workflows secure Part 1: Preventing pwn requests

GH Action provides the event type " pull_request_target ", which has write permissions and can comment the PR. GH Action 提供事件类型“ pull_request_target ”,具有写入权限,可以对 PR 进行评论。 Do not use this without being careful!请勿在不小心的情况下使用它! The PR's code is untrusted - if you build it, it might inject malicious code and compromise your repository and might steal your secrets. PR 的代码不受信任 - 如果您构建它,它可能会注入恶意代码并破坏您的存储库并可能窃取您的秘密。

The proposed solution for this is:为此提出的解决方案是:

  1. Have a workflow triggered by " pull_request " event.有一个由“ pull_request ”事件触发的工作流。 This runs with read-only GITHUB_TOKEN.这使用只读 GITHUB_TOKEN 运行。 Here you can run the unit tests.您可以在这里运行单元测试。 At the end of this workflow, the unit test results are uploaded as a build artifact.在此工作流结束时,单元测试结果作为构建工件上传。

  2. Have another workflow, that is triggered by the event " workflow_run ".有另一个工作流,它由事件“ workflow_run ”触发。 It runs when the PR-workflow has completed.它在 PR 工作流完成时运行。 This second workflow runs in the context of the base repository with write-access GITHUB_TOKEN and all other configured secrets.第二个工作流在具有写访问权限 GITHUB_TOKEN 和所有其他配置的秘密的基础存储库的上下文中运行。 It can download the artifact from the first workflow run and use this build result to create a comment to the PR.它可以从第一次运行的工作流中下载工件,并使用这个构建结果来创建对 PR 的评论。

Important: The incoming artifact data from the first workflow run must still be considered untrusted.重要提示:来自第一次工作流运行的传入工件数据仍必须被视为不受信任。 But:但:

When used in a safe manner, like reading PR numbers or reading a code coverage text to comment on the PR, it is safe to use such untrusted data in the privileged workflow context.当以安全的方式使用时,例如阅读 PR 编号或阅读代码覆盖文本以评论 PR,在特权工作流上下文中使用此类不受信任的数据是安全的。

See https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ for a complete usage example.有关完整的使用示例,请参阅https://securitylab.github.com/research/github-actions-preventing-pwn-requests/

Note: Your screenshot about the configuration for "Fork pull request workflows" has meanwhile been renamed to "Fork pull request workflows in private repositories ":注意:您关于“Fork pull request workflows”配置的屏幕截图已同时重命名为“Fork pull request workflows in private repositories ”: 私人仓库中分叉工作流的配置 With private repos, you are in control with whom you share your code.使用私有存储库,您可以控制与谁共享您的代码。 So you might decide to trust by default.所以你可能决定默认信任。 But with public repository, anyone can fork the repo.但是有了公共存储库,任何人都可以 fork 存储库。

Update: All links to the github actions blog post series:更新:github 操作博文系列的所有链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM