-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[workflows] Split pr-code-format into two parts to make it more secure (
#78216) Actions triggered by pull_request_target events have access to all repository secrets, so it is unsafe to use them when executing untrusted code. The pr-code-format workflow does not execute any untrusted code, but it passes untrused input into clang-format. An attacker could use this to exploit a flaw in clang-format and potentially gain access to the repository secrets. By splitting the workflow, we can use the pull_request target which is more secure and isolate the issue write permissions in a separate job. The pull_request target also makes it easier to test changes to the code-format-helepr.py script, because the version of the script from the pull request will be used rather than the version of the script from main. Fixes #77142
- Loading branch information
Showing
3 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
name: Comment on an issue | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["Check code formatting"] | ||
types: | ||
- completed | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
pr-comment: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
pull-requests: write | ||
if: > | ||
github.event.workflow_run.event == 'pull_request' | ||
steps: | ||
- name: 'Download artifact' | ||
# v7.0.1 | ||
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea | ||
with: | ||
script: | | ||
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
run_id: context.payload.workflow_run.id, | ||
}); | ||
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { | ||
return artifact.name == "workflow-args" | ||
})[0]; | ||
let download = await github.rest.actions.downloadArtifact({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
artifact_id: matchArtifact.id, | ||
archive_format: 'zip', | ||
}); | ||
let fs = require('fs'); | ||
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/workflow-args.zip`, Buffer.from(download.data)); | ||
- run: unzip workflow-args.zip | ||
|
||
- name: 'Comment on PR' | ||
uses: actions/github-script@v3 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
var fs = require('fs'); | ||
const comments = JSON.parse(fs.readFileSync('./comments')); | ||
if (!comments) { | ||
return; | ||
} | ||
let runInfo = await github.actions.getWorkflowRun({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
run_id: context.payload.workflow_run.id | ||
}); | ||
if (runInfo.data.pull_requests.length != 1) { | ||
console.log("Error retrieving pull request number"); | ||
console.log(runInfo); | ||
return; | ||
} | ||
const prNumber = runInfo.data.pull_requests[0].number; | ||
await comments.forEach(function (comment) { | ||
if (comment.id) { | ||
github.issues.updateComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: prNumber, | ||
comment_id: comment.id, | ||
body: comment.body | ||
}); | ||
} else { | ||
github.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: prNumber, | ||
body: comment.body | ||
}); | ||
} | ||
}); | ||
- name: Dump comments file | ||
if: always() | ||
run: cat comments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters