Unlike pull_request
, pull_request_target
run the workflow in the context of the target repository, so you have access to the secrets. You can reduce this vulnerability by adding labeled
type, however it doesn't really make this a safe approach
From Keeping your GitHub Actions and workflows secure
As such this approach should only be used as a temporary solution,
until a proper fix from the options above is applied. Since external
users do not have the permission to assign labels, this effectively
requires repository owners to manually review changes first and is
also prone to human error.
Note that there is an important “gotcha” to any remediation put in
place for a vulnerable workflow. All PRs that were opened before a fix
was made to the vulnerable workflow will use the version of the
workflow as it existed at the time the PR was opened. That means that
if there is a pending PR, any updates to the PR may still abuse the
vulnerable workflow. It is advisable to either close or rebase such
PRs if untrusted commits may be added to them after a vulnerable
workflow is fixed.
You may ask yourself: if the pull_request_target workflow only checks
out and builds the PR, i.e. runs untrusted code but doesn’t reference
any secrets, is it still vulnerable?
Yes it is, because a workflow triggered on pull_request_target still
has the read/write repository token in memory that is potentially
available to any running program. If the workflow uses
actions/checkout and does not pass the optional parameter
persist-credentials as false, it makes it even worse. The default for
the parameter is true. It means that in any subsequent steps any
running code can simply read the stored repository token from the
disk. If you don’t need a repository write access or secrets, just
stick to the pull_request trigger.
If you still want to go that way add the pull_request_target
trigger with labeled
type
on:
pull_request_target:
types: [labeled]
Create a label via Pull requests -> Labels -> new label
and apply it to the pull request from Labels section in the right side menu when you are ready to merge the PR, this will trigger the workflow.