Conversation
| runs-on: ubuntu-latest | ||
| needs: | ||
| - test | ||
| - test2 | ||
| if: ${{ failure() }} | ||
| steps: | ||
| - run: | | ||
| echo "ran after failure" |
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 months ago
Generally, to fix this class of problem you explicitly declare a permissions: block either at the workflow root (applies to all jobs) or on individual jobs, granting only the minimal scopes required. For jobs that only run shell commands and do not need to write to the repo or PRs, permissions: contents: read (or even permissions: {}) is usually sufficient; contents: read is a clear, documented minimal baseline.
For this specific workflow, none of the jobs perform GitHub write operations. The least invasive, non‑functional change is to add a permissions: block at the top level so it applies uniformly to all jobs. Place it after the on: block (commonly used position) and set contents: read. This documents that the jobs only need read access to repository contents and constrains the GITHUB_TOKEN accordingly. No imports or additional methods are required; this is a pure YAML configuration change in .github/workflows/test.yml.
Concretely: in .github/workflows/test.yml, insert a new permissions: section between the on: block (line 2–3) and the jobs: key (line 5). The content should be:
permissions:
contents: readThis will satisfy CodeQL and enforce least-privilege without altering the behavior of the existing steps.
| @@ -2,6 +2,9 @@ | ||
| on: | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest |
| runs-on: ubuntu-latest | ||
| steps: | ||
| - run: | | ||
| echo "this is a test" | ||
| exit 1 | ||
| test2: |
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 months ago
In general, this issue is fixed by adding an explicit permissions block that defines the least privileges needed. For workflows that don’t use GITHUB_TOKEN at all, the safest option is permissions: { contents: read } or even permissions: {} at the workflow or job level. Defining it at the workflow (root) level applies to all jobs that do not override it.
For this specific workflow, none of the jobs perform any GitHub operations; they only run shell commands. The best fix that doesn’t change behavior is to add a root-level permissions: block granting read-only access to repository contents (or even no permissions). A common minimal baseline is:
permissions:
contents: readThis documents that the workflow only needs read access and prevents it from accidentally acquiring broader permissions if repository defaults change or if the workflow is copied elsewhere. Concretely, in .github/workflows/test.yml, we will insert a permissions: block near the top of the file, after the on: section and before jobs:. No imports or additional methods are required.
| @@ -2,6 +2,9 @@ | ||
| on: | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest |
| needs: test | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - run: | | ||
| echo "this is test 2" | ||
| exit 1 | ||
| postError: |
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium test
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 months ago
In general, the fix is to explicitly declare a restrictive permissions: block for the workflow or for each job, granting only the minimal access required. Since none of the jobs in this workflow need to access the repository or modify GitHub resources, they can safely run with contents: read (or even contents: none if you are certain no repo access is ever needed). The simplest and clearest fix is to set permissions at the workflow level so it applies to all jobs.
Concretely, in .github/workflows/test.yml, add a root-level permissions: block near the top of the file, aligned with on: and jobs:. For example:
permissions:
contents: readThis ensures that the GITHUB_TOKEN for test, test2, and postError is limited to read-only repository contents, which is sufficient for this workflow and adheres to least privilege. No other code changes, methods, or imports are required.
| @@ -1,6 +1,8 @@ | ||
| --- | ||
| on: | ||
| pull_request: | ||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: |
Proposed changes
Checklist