Skip to content

Commit 2408e32

Browse files
committed
Reapply [workflows] Split pr-code-format into two parts to make it more secure (llvm#78215)
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 llvm#77142
1 parent 06c14c0 commit 2408e32

File tree

3 files changed

+164
-6
lines changed

3 files changed

+164
-6
lines changed

.github/workflows/issue-write.yml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: Comment on an issue
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Check code formatting"]
6+
types:
7+
- completed
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
pr-comment:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
pull-requests: write
17+
if: >
18+
github.event.workflow_run.event == 'pull_request'
19+
steps:
20+
- name: 'Download artifact'
21+
# v7.0.1
22+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
23+
with:
24+
script: |
25+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
26+
owner: context.repo.owner,
27+
repo: context.repo.repo,
28+
run_id: context.payload.workflow_run.id,
29+
});
30+
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
31+
return artifact.name == "workflow-args"
32+
})[0];
33+
let download = await github.rest.actions.downloadArtifact({
34+
owner: context.repo.owner,
35+
repo: context.repo.repo,
36+
artifact_id: matchArtifact.id,
37+
archive_format: 'zip',
38+
});
39+
let fs = require('fs');
40+
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/workflow-args.zip`, Buffer.from(download.data));
41+
42+
- run: unzip workflow-args.zip
43+
44+
- name: 'Comment on PR'
45+
uses: actions/github-script@v3
46+
with:
47+
github-token: ${{ secrets.GITHUB_TOKEN }}
48+
script: |
49+
var fs = require('fs');
50+
const comments = JSON.parse(fs.readFileSync('./comments'));
51+
if (!comments) {
52+
return;
53+
}
54+
55+
let runInfo = await github.actions.getWorkflowRun({
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
run_id: context.payload.workflow_run.id
59+
});
60+
61+
console.log(runInfo);
62+
const head_sha = runInfo.data.head_sha
63+
const search_query = "type:pr repo:llvm/llvm-project commit:" + head_sha
64+
const gql_query = `
65+
query($repo_owner : String!, $repo_name : String!, $branch: String!) {
66+
repository(owner: $repo_owner, name: $repo_name) {
67+
ref (qualifiedName: $branch) {
68+
associatedPullRequests(first: 100) {
69+
nodes {
70+
baseRepository {
71+
owner {
72+
login
73+
}
74+
}
75+
number
76+
state
77+
}
78+
}
79+
}
80+
}
81+
}
82+
`
83+
const gql_variables = {
84+
repo_owner: runInfo.data.head_repository.owner.login,
85+
repo_name: runInfo.data.head_repository.name,
86+
branch: runInfo.data.head_branch
87+
}
88+
const gql_result = await github.graphql(gql_query, gql_variables);
89+
console.log(gql_result);
90+
console.log(gql_result.repository.ref.associatedPullRequests.nodes);
91+
92+
var pr_number = 0;
93+
gql_result.repository.ref.associatedPullRequests.nodes.forEach((pr) => {
94+
if (pr.baseRepository.owner.login = context.repo.owner && pr.state == 'OPEN') {
95+
pr_number = pr.number;
96+
}
97+
});
98+
if (pr_number == 0) {
99+
console.log("Error retrieving pull request number");
100+
return;
101+
}
102+
103+
await comments.forEach(function (comment) {
104+
if (comment.id) {
105+
github.issues.updateComment({
106+
owner: context.repo.owner,
107+
repo: context.repo.repo,
108+
issue_number: pr_number,
109+
comment_id: comment.id,
110+
body: comment.body
111+
});
112+
} else {
113+
github.issues.createComment({
114+
owner: context.repo.owner,
115+
repo: context.repo.repo,
116+
issue_number: pr_number,
117+
body: comment.body
118+
});
119+
}
120+
});
121+
122+
- name: Dump comments file
123+
if: always()
124+
run: cat comments

.github/workflows/pr-code-format.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
name: "Check code formatting"
2-
on: pull_request_target
3-
permissions:
4-
pull-requests: write
2+
on: pull_request
53

64
jobs:
75
code_formatter:
@@ -27,12 +25,14 @@ jobs:
2725
separator: ","
2826
skip_initial_fetch: true
2927

30-
# We need to make sure that we aren't executing/using any code from the
31-
# PR for security reasons as we're using pull_request_target. Checkout
32-
# the target branch with the necessary files.
28+
# We need to pull the script from the main branch, so that we ensure
29+
# we get a version of the script that supports the --wirte-comment-to-file
30+
# option.
3331
- name: Fetch code formatting utils
3432
uses: actions/checkout@v4
3533
with:
34+
reository: ${{ github.repository }}
35+
ref: ${{ github.base_ref }}
3636
sparse-checkout: |
3737
llvm/utils/git/requirements_formatting.txt
3838
llvm/utils/git/code-format-helper.py
@@ -73,8 +73,16 @@ jobs:
7373
# the merge base.
7474
run: |
7575
python ./code-format-tools/llvm/utils/git/code-format-helper.py \
76+
--write-comment-to-file \
7677
--token ${{ secrets.GITHUB_TOKEN }} \
7778
--issue-number $GITHUB_PR_NUMBER \
7879
--start-rev $(git merge-base $START_REV $END_REV) \
7980
--end-rev $END_REV \
8081
--changed-files "$CHANGED_FILES"
82+
83+
- uses: actions/upload-artifact@v2
84+
if: always()
85+
with:
86+
name: workflow-args
87+
path: |
88+
comments

llvm/utils/git/code-format-helper.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class FormatArgs:
4444
token: str = None
4545
verbose: bool = True
4646
issue_number: int = 0
47+
write_comment_to_file: bool = False
4748

4849
def __init__(self, args: argparse.Namespace = None) -> None:
4950
if not args is None:
@@ -53,12 +54,14 @@ def __init__(self, args: argparse.Namespace = None) -> None:
5354
self.token = args.token
5455
self.changed_files = args.changed_files
5556
self.issue_number = args.issue_number
57+
self.write_comment_to_file = args.write_comment_to_file
5658

5759

5860
class FormatHelper:
5961
COMMENT_TAG = "<!--LLVM CODE FORMAT COMMENT: {fmt}-->"
6062
name: str
6163
friendly_name: str
64+
comment: dict = None
6265

6366
@property
6467
def comment_tag(self) -> str:
@@ -119,6 +122,13 @@ def update_pr(self, comment_text: str, args: FormatArgs, create_new: bool) -> No
119122
comment_text = self.comment_tag + "\n\n" + comment_text
120123

121124
existing_comment = self.find_comment(pr)
125+
126+
if args.write_comment_to_file:
127+
self.comment = {"body": comment_text}
128+
if existing_comment:
129+
self.comment["id"] = existing_comment.id
130+
return
131+
122132
if existing_comment:
123133
existing_comment.edit(comment_text)
124134
elif create_new:
@@ -309,6 +319,8 @@ def hook_main():
309319
if fmt.has_tool():
310320
if not fmt.run(args.changed_files, args):
311321
failed_fmts.append(fmt.name)
322+
if fmt.comment:
323+
comments.append(fmt.comment)
312324
else:
313325
print(f"Couldn't find {fmt.name}, can't check " + fmt.friendly_name.lower())
314326

@@ -349,6 +361,11 @@ def hook_main():
349361
type=str,
350362
help="Comma separated list of files that has been changed",
351363
)
364+
parser.add_argument(
365+
"--write-comment-to-file",
366+
action="store_true",
367+
help="Don't post comments on the PR, instead write the comments and metadata a file called 'comment'",
368+
)
352369

353370
args = FormatArgs(parser.parse_args())
354371

@@ -357,9 +374,18 @@ def hook_main():
357374
changed_files = args.changed_files.split(",")
358375

359376
failed_formatters = []
377+
comments = []
360378
for fmt in ALL_FORMATTERS:
361379
if not fmt.run(changed_files, args):
362380
failed_formatters.append(fmt.name)
381+
if fmt.comment:
382+
comments.append(fmt.comment)
383+
384+
if len(comments):
385+
with open("comments", "w") as f:
386+
import json
387+
388+
json.dump(comments, f)
363389

364390
if len(failed_formatters) > 0:
365391
print(f"error: some formatters failed: {' '.join(failed_formatters)}")

0 commit comments

Comments
 (0)