Wip - #193
Conversation
| $ErrorActionPreference = "Stop" | ||
| $PSNativeCommandUseErrorActionPreference = $true | ||
|
|
||
| Import-Module "${{ github.action_path }}/RequiredChecksPolicy.psm1" -Force |
Check warning
Code scanning / CodeQL
Code injection Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 6 months ago
In general, to fix code injection warnings in GitHub Actions, untrusted or potentially untrusted values from expressions (${{ ... }}) should not be embedded directly in script bodies. Instead, assign them to environment variables via the env: block and then read them using the shell’s native variable syntax (e.g., $ENV:VAR in PowerShell, $VAR in bash). This prevents expression syntax from being evaluated in places where it could be interpreted as code.
For this specific case, we only need to adjust how github.action_path is passed into the PowerShell script. We will:
- Add a new environment variable (e.g.,
CI_ACTION_PATH) in theenv:block of the composite action step, assigned from${{ github.action_path }}. - Update the
Import-Modulecommand to use$env:CI_ACTION_PATHinstead of interpolating${{ github.action_path }}directly in the script.
This preserves the existing behavior (importing RequiredChecksPolicy.psm1 from the action’s directory) while removing direct ${{ ... }} usage from inside the run: script. The only file to change is .github/actions/required-checks-policy/action.yml, lines around the env: block and the Import-Module call; no new imports or additional methods are needed.
| @@ -33,11 +33,12 @@ | ||
| CI_TIMEOUT_MINUTES_QUEUED_CHECKS: ${{ inputs.timeout-minutes-queued-checks }} | ||
| CI_TIMEOUT_MINUTES_CREATED_CHECKS: ${{ inputs.timeout-minutes-created-checks }} | ||
| REPOSITORY_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} | ||
| CI_ACTION_PATH: ${{ github.action_path }} | ||
| run: | | ||
| $ErrorActionPreference = "Stop" | ||
| $PSNativeCommandUseErrorActionPreference = $true | ||
|
|
||
| Import-Module "${{ github.action_path }}/RequiredChecksPolicy.psm1" -Force | ||
| Import-Module "$env:CI_ACTION_PATH/RequiredChecksPolicy.psm1" -Force | ||
|
|
||
| $refs = Resolve-Refs ` | ||
| -CommitId $env:GITHUB_SHA ` |
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
| steps: | ||
| - uses: actions/checkout@v5 | ||
| - name: Run Pester tests | ||
| shell: pwsh | ||
| run: | | ||
| $config = New-PesterConfiguration | ||
| $config.Run.Path = ".github/actions/required-checks-policy/RequiredChecksPolicy.Tests.ps1" | ||
| $config.Output.Verbosity = "Detailed" | ||
| $config.TestResult.Enabled = $true | ||
| $config.TestResult.OutputFormat = "JUnitXml" | ||
| $config.TestResult.OutputPath = "test-results.xml" | ||
| Invoke-Pester -Configuration $config | ||
| - name: Upload test results | ||
| if: always() | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: pester-results | ||
| path: test-results.xml |
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 6 months ago
In general, fix this by explicitly declaring a permissions block that grants only the minimal GITHUB_TOKEN permissions the workflow needs. You can add this either at the top level of the workflow (so it applies to all jobs that don’t override it) or under a specific job. For this workflow, the steps only need read access to repository contents (for actions/checkout) and do not need to write to the repo or to issues/PRs, so contents: read is sufficient.
The best minimal change without altering functionality is to add a root-level permissions block just below the name (and before on:) in .github/workflows/~internal-required-checks-policy.yml:
permissions:
contents: readThis sets the default GITHUB_TOKEN permission to read-only for repository contents for all jobs in this workflow, leaving artifact uploads unaffected because they do not require additional repo write scopes. No new imports or external dependencies are needed; permissions is a native GitHub Actions workflow key.
| @@ -1,5 +1,8 @@ | ||
| name: Internal - Test Required Checks Policy | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] |
Cloning an empty bare repo may not configure remote.origin.fetch, causing git fetch to skip creating refs/remotes/origin/* tracking refs.
Cloning an empty bare repo does not set up remote tracking refs reliably across git versions. Instead, create a populated origin repo first, then clone it so refs/remotes/origin/* are properly created.
No description provided.