@@ -14,96 +14,98 @@ jobs:
1414 runs-on : ubuntu-latest
1515 if : github.event.workflow_run.conclusion == 'success'
1616 timeout-minutes : 15
17+ permissions :
18+ contents : read
19+ actions : read
20+ pull-requests : read
1721 steps :
1822 - name : echo event
1923 run : cat $GITHUB_EVENT_PATH
2024 - name : Download PR number artifact
21- if : github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
22- uses : dawidd6/action-download-artifact@v14
25+ if : github.event.workflow_run.event == 'pull_request'
26+ uses : dawidd6/action-download-artifact@5c98f0b039f36ef966fdb7dfa9779262785ecb05 # v14
2327 with :
2428 workflow : SonarCloud Build
2529 run_id : ${{ github.event.workflow_run.id }}
2630 name : PR_NUMBER
2731 - name : Read PR_NUMBER.txt
28- if : github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
32+ if : github.event.workflow_run.event == 'pull_request'
2933 id : pr_number
30- uses : juliangruber/read-file-action@v1
34+ uses : juliangruber/read-file-action@b549046febe0fe86f8cb4f93c24e284433f9ab58 # v1
3135 with :
3236 path : ./PR_NUMBER.txt
3337 - name : Request GitHub API for PR data
34- if : github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
35- uses : octokit/request-action@v2.x
38+ if : github.event.workflow_run.event == 'pull_request'
39+ uses : octokit/request-action@05a2312de9f8207044c4c9e41fe19703986acc13 # v2.x
3640 id : get_pr_data
3741 with :
3842 route : GET /repos/{full_name}/pulls/{number}
3943 number : ${{ steps.pr_number.outputs.content }}
4044 full_name : ${{ github.event.repository.full_name }}
4145 env :
4246 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
43- - uses : actions/checkout@v6
47+ - uses : actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
4448 with :
45- repository : ${{ github.event.workflow_run.head_repository.full_name }}
46- ref : ${{ github.event.workflow_run.head_branch }}
4749 fetch-depth : 0
48- - name : Checkout base branch
49- if : github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
50- run : |
51- git remote add upstream ${{ github.event.repository.clone_url }}
52- git fetch upstream
53- git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }}
54- git checkout ${{ github.event.workflow_run.head_branch }}
55- git clean -ffdx && git reset --hard HEAD
50+ - name : Download build artifacts
51+ uses : dawidd6/action-download-artifact@5c98f0b039f36ef966fdb7dfa9779262785ecb05 # v14
52+ with :
53+ workflow : SonarCloud Build
54+ run_id : ${{ github.event.workflow_run.id }}
55+ name : build-artifacts
5656 - name : Cache SonarCloud packages
57- uses : actions/cache@v5
57+ uses : actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
5858 with :
5959 path : ~/.sonar/cache
6060 key : ${{ runner.os }}-sonar
6161 restore-keys : ${{ runner.os }}-sonar
62- - name : Cache Maven packages
63- uses : actions/cache@v5
64- with :
65- path : ~/.m2
66- key : ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
67- restore-keys : ${{ runner.os }}-m2
6862
6963 - name : Set up JDK 17
70- uses : actions/setup-java@v5
64+ uses : actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5
7165 with :
7266 distribution : ' zulu'
7367 java-version : ' 17'
68+ cache : ' maven'
7469
7570 - name : Set Common Sonar Variables
7671 id : sonar_env
72+ # Use $GITHUB_OUTPUT; the legacy ##[set-output] syntax is deprecated and disabled.
7773 run : |
78- echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \
79- -Dsonar.projectKey=com-pas_compas-scl-auto-alignment \
80- -Dsonar.organization=com-pas )"
74+ echo "sonar_opts=-Dsonar.host.url=https://sonarcloud.io -Dsonar.projectKey=com-pas_compas-scl-auto-alignment -Dsonar.organization=com-pas" >> "$GITHUB_OUTPUT"
8175 - name : Create custom Maven Settings.xml
82- uses : whelk-io/maven-settings-xml-action@v22
76+ uses : whelk-io/maven-settings-xml-action@9dc09b23833fa9aa7f27b63db287951856f3433d # v22
8377 with :
8478 output_file : custom_maven_settings.xml
8579 servers : ' [{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
8680 - name : Build and analyze (Pull Request)
87- if : ${{ github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target') }}
81+ if : ${{ github.event.workflow_run.event == 'pull_request' }}
82+ # Pass user-controlled PR metadata through env vars to prevent shell/argument injection.
8883 env :
8984 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
9085 SONAR_TOKEN : ${{ secrets.SONAR_TOKEN }}
86+ SONAR_PR_BRANCH : ${{ fromJson(steps.get_pr_data.outputs.data).head.ref }}
87+ SONAR_PR_KEY : ${{ fromJson(steps.get_pr_data.outputs.data).number }}
88+ SONAR_PR_BASE : ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }}
89+ SONAR_SCM_REVISION : ${{ github.event.workflow_run.head_sha }}
9190 run : |
9291 ./mvnw -B -s custom_maven_settings.xml -Psonar \
9392 ${{ steps.sonar_env.outputs.sonar_opts }} \
94- -Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} \
95- -Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} \
96- -Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} \
97- -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
98- clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
93+ " -Dsonar.pullrequest.branch=$SONAR_PR_BRANCH" \
94+ " -Dsonar.pullrequest.key=$SONAR_PR_KEY" \
95+ " -Dsonar.pullrequest.base=$SONAR_PR_BASE" \
96+ " -Dsonar.scm.revision=$SONAR_SCM_REVISION" \
97+ org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
9998 - name : Build and analyze (Push)
10099 if : ${{ github.event.workflow_run.event == 'push' }}
100+ # Pass user-controlled branch/revision values through env vars to prevent injection.
101101 env :
102102 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
103103 SONAR_TOKEN : ${{ secrets.SONAR_TOKEN }}
104+ SONAR_SCM_REVISION : ${{ github.event.workflow_run.head_sha }}
105+ SONAR_BRANCH_NAME : ${{ github.event.workflow_run.head_branch }}
104106 run : |
105107 ./mvnw -B -s custom_maven_settings.xml -Psonar \
106108 ${{ steps.sonar_env.outputs.sonar_opts }} \
107- -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
108- -Dsonar.branch.name=${{ github.event.workflow_run.head_branch }} \
109- clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
109+ " -Dsonar.scm.revision=$SONAR_SCM_REVISION" \
110+ " -Dsonar.branch.name=$SONAR_BRANCH_NAME" \
111+ org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
0 commit comments