Skip to content

Commit

Permalink
Improve security of github action
Browse files Browse the repository at this point in the history
  • Loading branch information
bookernath committed Dec 31, 2024
1 parent 1c9b880 commit 9db1094
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions .github/workflows/translations-changeset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@ jobs:
create-translations-patch:
if: github.actor == 'bc-svc-local'
runs-on: ubuntu-latest
# Add permissions block to limit token scope
permissions:
contents: write
pull-requests: write

steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Validate inputs
id: validate
run: |
# Validate ref name against allowed pattern (alphanumeric, dash, underscore, and forward slash only)
if ! [[ "${{ github.event.pull_request.head.ref }}" =~ ^[a-zA-Z0-9/_-]+$ ]]; then
echo "Error: Invalid branch name format"
exit 1
fi
echo "ref=${{ github.event.pull_request.head.ref }}" >> $GITHUB_OUTPUT
- name: Use commit SHA for filename
id: generate-sha
run: |
Expand All @@ -44,8 +58,16 @@ jobs:
git add .changeset/translations-patch-$SHORT_SHA.md
git commit -m "chore(core): create translations patch"
- name: Push changeset
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} HEAD:${{ github.event.pull_request.head.ref }}
- name: Push changes
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { repo, owner } = context.repo;
const ref = '${{ steps.validate.outputs.ref }}';
await exec.exec('git', [
'push',
`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${owner}/${repo}`,
`HEAD:${ref}`
]);

0 comments on commit 9db1094

Please sign in to comment.