Skip to content

Commit 3925d28

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

File tree

3 files changed

+162
-6
lines changed

3 files changed

+162
-6
lines changed

.github/workflows/issue-write.yml

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 # v4.1.1
22+
with:
23+
github-token: ${{ secrets.ISSUE_WRITE_DOWNLOAD_ARTIFACT }}
24+
run-id: ${{ github.event.workflow_run.id }}
25+
name: workflow-args
26+
27+
- name: 'Comment on PR'
28+
uses: actions/github-script@v3
29+
with:
30+
github-token: ${{ secrets.GITHUB_TOKEN }}
31+
script: |
32+
var fs = require('fs');
33+
const comments = JSON.parse(fs.readFileSync('./comments'));
34+
if (!comments) {
35+
return;
36+
}
37+
38+
let runInfo = await github.actions.getWorkflowRun({
39+
owner: context.repo.owner,
40+
repo: context.repo.repo,
41+
run_id: context.payload.workflow_run.id
42+
});
43+
44+
console.log(runInfo);
45+
46+
47+
// Query to find the number of the pull request that triggered this job.
48+
const gql_query = `
49+
query($repo_owner : String!, $repo_name : String!, $branch: String!) {
50+
repository(owner: $repo_owner, name: $repo_name) {
51+
ref (qualifiedName: $branch) {
52+
associatedPullRequests(first: 100) {
53+
nodes {
54+
baseRepository {
55+
owner {
56+
login
57+
}
58+
}
59+
number
60+
state
61+
}
62+
}
63+
}
64+
}
65+
}
66+
`
67+
const gql_variables = {
68+
repo_owner: runInfo.data.head_repository.owner.login,
69+
repo_name: runInfo.data.head_repository.name,
70+
branch: runInfo.data.head_branch
71+
}
72+
const gql_result = await github.graphql(gql_query, gql_variables);
73+
console.log(gql_result);
74+
console.log(gql_result.repository.ref.associatedPullRequests.nodes);
75+
76+
var pr_number = 0;
77+
gql_result.repository.ref.associatedPullRequests.nodes.forEach((pr) => {
78+
if (pr.baseRepository.owner.login = context.repo.owner && pr.state == 'OPEN') {
79+
pr_number = pr.number;
80+
}
81+
});
82+
if (pr_number == 0) {
83+
console.log("Error retrieving pull request number");
84+
return;
85+
}
86+
87+
await comments.forEach(function (comment) {
88+
if (comment.id) {
89+
// Security check: Ensure that this comment was created by
90+
// the github-actions bot, so a malisious input won't overwrite
91+
// a user's comment.
92+
github.issues.getComment({
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
comment_id: comment.id
96+
}).then((old_comment) => {
97+
console.log(old_comment);
98+
if (old_comment.data.user.login != "github-actions[bot]") {
99+
console.log("Invalid comment id: " + comment.id);
100+
return;
101+
}
102+
github.issues.updateComment({
103+
owner: context.repo.owner,
104+
repo: context.repo.repo,
105+
issue_number: pr_number,
106+
comment_id: comment.id,
107+
body: comment.body
108+
});
109+
});
110+
} else {
111+
github.issues.createComment({
112+
owner: context.repo.owner,
113+
repo: context.repo.repo,
114+
issue_number: pr_number,
115+
body: comment.body
116+
});
117+
}
118+
});
119+
120+
- name: Dump comments file
121+
if: always()
122+
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@26f96dfa697d77e81fd5907df203aa23a56210a8 #v4.3.0
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)