Skip to content

Conversation

santunioni
Copy link
Contributor

@santunioni santunioni commented Oct 10, 2022

Fix documentation related to tokens. The secrets.GITHUB_TOKEN provided by the GitHub Actions App can do everything related to the repo if we elevate its permissions, including calling workflow_dispatch and repository_dispatch.

Some people in my organization are using PAT's instead of the secrets.GITHUB_TOKEN when using this action because of the README suggestion, even when they are calling the same repository. Using PATs in that context provides unnecessary security risks.

Fix documentation related to tokens. The secrets.GITHUB_TOKEN provided by the GitHub Actions App can do everything related to the repo if we elevate its permissions, including calling workflow_dispatch and repository_dispatch.

Some people in my organization are using PAT's instead of the secrets.GITHUB_TOKEN when using this action because of this README suggestion, even when they are calling the same repository. Using PATs in that contexts provides unnecessary security risks.
@santunioni
Copy link
Contributor Author

santunioni commented Oct 10, 2022

Testing

  1. Create two workflows:

trigger.yaml

name: Trigger

on:
  workflow_dispatch:

permissions:
  actions: write

jobs:
  repositorydispatch:
    runs-on: ubuntu-latest
    steps:
      - name: Dispatch workflow
        uses: peter-evans/repository-dispatch@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          event-type: test-repo-dispatch
          client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'

triggered.yaml

name: Triggered

on:
  repository_dispatch:
    types: [test-repo-dispatch]

jobs:
  read:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          result-encoding: string
          script: |
            const fs = require("fs")
            const eventPath = '${{ github.event_path }}' 
            fs.readFile(eventPath, "utf-8", (err, data) => {
              console.log(data)
              console.error(err)
            })
            return eventPath

  1. Manually run the Trigger workflow
  2. See the Triggered workflow will also run

@peter-evans
Copy link
Owner

@santunioni Thank you for this contribution! I wasn't aware of this token change.

I'm going to merge to a feature branch so I can make some further documentation and workflow changes. Then I'll merge to main.

@peter-evans peter-evans changed the base branch from main to token-update October 19, 2022 07:07
@peter-evans peter-evans merged commit 8e295bb into peter-evans:token-update Oct 19, 2022
@peter-evans
Copy link
Owner

I've updated the repo with your contribution. Thank you!

I also made GITHUB_TOKEN the default for input token. So you don't need to specify it anymore if your dispatch is local.

permissions:
  actions: write

jobs:
  repositorydispatch:
    runs-on: ubuntu-latest
    steps:
      - name: Repository Dispatch
        uses: peter-evans/repository-dispatch@v2
        with:
          event-type: my-event

@santunioni santunioni deleted the patch-1 branch October 19, 2022 16:29
@peter-evans
Copy link
Owner

@santunioni I don't think elevating the token permissions is necessary. It seems to work without this:

permissions:
  actions: write

@santunioni
Copy link
Contributor Author

santunioni commented Oct 21, 2022

@peter-evans great! I confirmed you are right for personal accounts.

However you do need the workflow to have actions: write permission. GitHub assigns it by default for personal accounts repository workflows.

@santunioni
Copy link
Contributor Author

santunioni commented Oct 21, 2022

As an example, try this:

permissions:
  actions: none

which is the default for GitHub Teams plan.

You will see the trigger.yaml fails.

@santunioni
Copy link
Contributor Author

What approach do you think suits better the action? Telling the user the permission the workflow needs explicitly, without relying on GitHub defaults, or rely on the defaults?

@peter-evans
Copy link
Owner

Ah, interesting. In that case I will make a note of it in the readme in the same way I've done for other actions I maintain.

cb18cc8

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants