Skip to content

Commit 8e4f63f

Browse files
committed
Emit warning when used with pull_request_target trigger
1 parent d28176c commit 8e4f63f

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,13 @@ However, there are a couple of ways to use this Action in Workflows that should
356356
> [!CAUTION]
357357
> The following section explains how you can use git-auto-commit in combination with the `pull_request_target` trigger.
358358
> **Using `pull_request_target` in your workflows can lead to repository compromise as [mentioned](https://securitylab.github.com/research/github-actions-preventing-pwn-requests/) by GitHub's own security team. This means, that a bad actor could potentially leak/steal your GitHub Actions repository secrets.**
359-
> Please be aware of this risk when using `pull_request_target` in your workflows.
359+
> Please be aware of this risk when using `pull_request_target` in your workflows. See [GitHub's documentation](https://docs.github.com/en/actions/reference/security/securely-using-pull_request_target) for more information.
360360
>
361361
> If your workflow runs code-fixing tools, consider running the workflow on your default branch by listening to the `push` event or use a third-party tool like [autofix.ci](https://autofix.ci/).
362362
> We keep this documentation around, as many questions came in over the years, on how to use this action for public forks.
363+
>
364+
> To remind users of this risk, git-auto-commit emits a warning annotation whenever it detects it is running on a `pull_request_target` event.
365+
> If you have evaluated the risk and want to silence the warning, set the `disable_pull_request_target_trigger_warning` input to `true`.
363366

364367
The workflow below runs whenever a commit is pushed to the `main`-branch or when activity on a pull request happens, by listening to the [`pull_request_target`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target) event.
365368

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ inputs:
8484
description: Perform a clean git tag and push, without commiting anything
8585
required: false
8686
default: false
87+
disable_pull_request_target_trigger_warning:
88+
description: Suppress the security warning emitted when the action runs on a `pull_request_target` event.
89+
required: false
90+
default: false
8791
internal_git_binary:
8892
description: Internal use only! Path to git binary used to check if git is available. (Don't change this!)
8993
required: false

entrypoint.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ _main() {
3535

3636
_check_if_repository_is_in_detached_state
3737

38+
_check_for_pull_request_target_trigger
39+
3840
if "$INPUT_CREATE_GIT_TAG_ONLY"; then
3941
_log "debug" "Create git tag only";
4042
_set_github_output "create_git_tag_only" "true"
@@ -107,6 +109,16 @@ _check_if_is_git_repository() {
107109
fi
108110
}
109111
112+
_check_for_pull_request_target_trigger() {
113+
if "$INPUT_DISABLE_PULL_REQUEST_TARGET_TRIGGER_WARNING"; then
114+
return
115+
fi
116+
117+
if [ "${GITHUB_EVENT_NAME:-}" = "pull_request_target" ]; then
118+
_log "warning" "git-auto-commit is running on a 'pull_request_target' event. This trigger can be a security risk: a malicious pull request could potentially exfiltrate your repository secrets. See https://docs.github.com/en/actions/reference/security/securely-using-pull_request_target and the 'pull_request_target' section in the git-auto-commit README (https://github.com/stefanzweifel/git-auto-commit-action#using-the-action-in-a-pull_request_target-workflow) for safer usage. Set 'disable_pull_request_target_trigger_warning: true' to suppress this warning.";
119+
fi
120+
}
121+
110122
_check_if_repository_is_in_detached_state() {
111123
if [ -z "$(git symbolic-ref HEAD)" ]
112124
then

tests/git-auto-commit.bats

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ setup() {
4141
export INPUT_SKIP_PUSH=false
4242
export INPUT_DISABLE_GLOBBING=false
4343
export INPUT_CREATE_BRANCH=false
44+
export INPUT_DISABLE_PULL_REQUEST_TARGET_TRIGGER_WARNING=false
4445
export INPUT_INTERNAL_GIT_BINARY=git
4546

47+
# Unset the GitHub event name by default so tests do not pick up
48+
# pull_request_target from the environment of the shell running BATS.
49+
unset GITHUB_EVENT_NAME
50+
4651
# Set GitHub environment variables used by the GitHub Action
4752
temp_github_output_file=$(mktemp -t github_output_test.XXXXX)
4853
export GITHUB_OUTPUT="${temp_github_output_file}"
@@ -1521,3 +1526,49 @@ END
15211526

15221527
assert_equal $current_sha $remote_sha
15231528
}
1529+
1530+
@test "It emits a warning when running on a pull_request_target event" {
1531+
export GITHUB_EVENT_NAME="pull_request_target"
1532+
1533+
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-1.txt
1534+
1535+
run git_auto_commit
1536+
1537+
assert_success
1538+
assert_output --partial "::warning::git-auto-commit is running on a 'pull_request_target' event."
1539+
assert_output --partial "disable_pull_request_target_trigger_warning"
1540+
}
1541+
1542+
@test "It does not emit the pull_request_target warning when the event is pull_request" {
1543+
export GITHUB_EVENT_NAME="pull_request"
1544+
1545+
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-1.txt
1546+
1547+
run git_auto_commit
1548+
1549+
assert_success
1550+
refute_output --partial "::warning::git-auto-commit is running on a 'pull_request_target' event."
1551+
}
1552+
1553+
@test "It does not emit the pull_request_target warning when GITHUB_EVENT_NAME is unset" {
1554+
unset GITHUB_EVENT_NAME
1555+
1556+
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-1.txt
1557+
1558+
run git_auto_commit
1559+
1560+
assert_success
1561+
refute_output --partial "::warning::git-auto-commit is running on a 'pull_request_target' event."
1562+
}
1563+
1564+
@test "It does not emit the pull_request_target warning when disable_pull_request_target_trigger_warning is true" {
1565+
export GITHUB_EVENT_NAME="pull_request_target"
1566+
export INPUT_DISABLE_PULL_REQUEST_TARGET_TRIGGER_WARNING=true
1567+
1568+
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-1.txt
1569+
1570+
run git_auto_commit
1571+
1572+
assert_success
1573+
refute_output --partial "::warning::git-auto-commit is running on a 'pull_request_target' event."
1574+
}

0 commit comments

Comments
 (0)