/.github/workflows/dependency-test.yml: improve security hygiene#3454
/.github/workflows/dependency-test.yml: improve security hygiene#3454coffeegoddd wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Updates the dependency-test GitHub Actions workflow to tighten default token permissions and make it work for both pull_request and issue_comment triggers by resolving the PR SHA/number in a dedicated step.
Changes:
- Adds explicit workflow
permissionsfor improved security hygiene. - Normalizes PR SHA/number resolution across
pull_requestandissue_commentevents and reuses those outputs in subsequent API calls. - Disables persisted Git credentials during checkout.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: read |
There was a problem hiding this comment.
The workflow now sets explicit permissions, but later steps call the Issues API (/issues/.../comments). With the current permissions (contents: read, pull-requests: read), GITHUB_TOKEN may not be allowed to read issue comments, causing those curl calls to fail with 403. Add issues: read (or broaden to read-all if preferred) to match the API usage in this workflow.
| pull-requests: read | |
| pull-requests: read | |
| issues: read |
| jobs: | ||
| test-integration: | ||
| if: github.event_name == 'issue_comment' && github.event.issue.pull_request != '' || github.event_name == 'pull_request' | ||
| if: github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request != '') |
There was a problem hiding this comment.
The job-level if condition uses github.event.issue.pull_request != '' to detect PR comments. For issue_comment events on regular issues, github.event.issue.pull_request is typically missing/null, and null != '' can evaluate truthy, unintentionally running this job for non-PR issues (and then the API calls will hit /pulls/<issue_number> and fail). Use an existence check like github.event.issue.pull_request or an explicit null comparison instead of comparing to the empty string.
| if: github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request != '') | |
| if: github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request != null) |
| PR_DATA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | ||
| https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.issue.number }}) | ||
| SHA=$(echo "$PR_DATA" | jq -r '.head.sha') |
There was a problem hiding this comment.
Resolve PR ref relies on curl -s and then blindly parses .head.sha with jq. If the API request returns a non-2xx (e.g., 404/403 due to permissions) curl can still exit 0 and jq will produce null, which would propagate into actions/checkout as an invalid ref. Make the API call fail on HTTP errors (e.g., curl -f) and validate that the parsed SHA is non-empty/non-null before writing to $GITHUB_OUTPUT.
| PR_DATA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
| https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.issue.number }}) | |
| SHA=$(echo "$PR_DATA" | jq -r '.head.sha') | |
| PR_DATA=$(curl -fsS -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
| https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.issue.number }}) | |
| SHA=$(echo "$PR_DATA" | jq -r '.head.sha // empty') | |
| if [ -z "$SHA" ]; then | |
| echo "Failed to resolve PR head SHA from GitHub API response." >&2 | |
| exit 1 | |
| fi |
| with: | ||
| ref: ${{ github.event.pull_request.head.sha }} | ||
| ref: ${{ steps.resolve_pr.outputs.ref }} | ||
| persist-credentials: false |
There was a problem hiding this comment.
actions/checkout is configured with persist-credentials: false, but a later step runs git fetch --all --unshallow / merges origin/main. With credentials removed, those subsequent git network operations can fail on private repos (and can be flaky if GitHub ever requires auth). Either keep credentials persisted for this job, or adjust the later fetch/merge to authenticate explicitly without relying on persisted checkout credentials.
| persist-credentials: false | |
| persist-credentials: true |
No description provided.