11name : OWASP PR Scanner
22
33on :
4- pull_request_target :
4+ pull_request :
55 types : [opened, synchronize, reopened]
66
77permissions :
@@ -35,34 +35,22 @@ jobs:
3535
3636 - name : Determine changed files for this PR
3737 id : diff
38- shell : bash
3938 run : |
4039 BASE_SHA="${{ github.event.pull_request.base.sha }}"
4140 HEAD_SHA="${{ github.event.pull_request.head.sha }}"
4241 RAW="$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" || true)"
4342 APP_CHANGED="$(echo "$RAW" \
44- | grep -E '\.(js|jsx|ts|tsx|py|java|go|rb|php|html|css|md)$' \
45- | grep -E '^(src/|backend/|app/|services/)' || true)"
46- SCANNER_ONLY="$(echo "$RAW" | grep -E '^scanner/' || true)"
47- if [ -z "$APP_CHANGED" ] && [ -n "$SCANNER_ONLY" ]; then
48- echo "only_scanner_changes=true" >> $GITHUB_OUTPUT
49- else
50- if [ -z "$APP_CHANGED" ]; then
51- APP_CHANGED="$(git ls-files src backend app services 2>/dev/null || true)"
52- fi
53- echo "changed_files<<EOF" >> $GITHUB_OUTPUT
54- echo "$APP_CHANGED" >> $GITHUB_OUTPUT
55- echo "EOF" >> $GITHUB_OUTPUT
43+ | grep -E '\.(js|jsx|ts|tsx|py|java|go|rb|php|html|css|md|conf|yml|yaml|json)$' \
44+ || true)"
45+ if [ -z "$APP_CHANGED" ]; then
46+ APP_CHANGED="$(git ls-files)"
5647 fi
48+ echo "changed_files<<EOF" >> $GITHUB_OUTPUT
49+ echo "$APP_CHANGED" >> $GITHUB_OUTPUT
50+ echo "EOF" >> $GITHUB_OUTPUT
5751
58- - name : Skip when only scanner/** changed
59- if : steps.diff.outputs.only_scanner_changes == 'true'
60- run : echo "Only scanner/** changed; skipping scan."
61-
62- - name : Run OWASP scanner on changed files
63- if : steps.diff.outputs.only_scanner_changes != 'true'
52+ - name : Run OWASP scanner
6453 id : owasp
65- shell : bash
6654 run : |
6755 CHANGED_FILES="${{ steps.diff.outputs.changed_files }}"
6856 if [ -z "$CHANGED_FILES" ]; then
7159 exit 0
7260 fi
7361
62+ if [ ! -d "scanner" ]; then
63+ echo "::error::Scanner module not found (scanner/)."
64+ exit 1
65+ fi
66+
7467 : > owasp-results.txt
7568 EXIT=0
7669 while IFS= read -r file; do
@@ -87,38 +80,27 @@ jobs:
8780 else
8881 echo "vulnerabilities_found=false" >> $GITHUB_OUTPUT
8982 fi
90-
91- exit $EXIT || true
83+ exit 0
9284
9385 - name : Create PR comment body
94- id : comment
95- if : always() && steps.diff.outputs.only_scanner_changes != 'true'
96- shell : bash
86+ if : always()
9787 run : |
98- if [ -f owasp-results.txt ]; then
99- RESULTS="$(cat owasp-results.txt)"
100- else
101- RESULTS="No scanner output available."
102- fi
103-
88+ RESULTS=$(cat owasp-results.txt || echo "No results.")
10489 if [ "${{ steps.owasp.outputs.vulnerabilities_found }}" == "true" ]; then
10590 echo 'comment_body<<EOF' >> $GITHUB_ENV
10691 echo '## 🔒 OWASP Scanner Results' >> $GITHUB_ENV
10792 echo '' >> $GITHUB_ENV
108- echo 'Vulnerabilities were detected in the changed files:' >> $GITHUB_ENV
109- echo '' >> $GITHUB_ENV
93+ echo 'Vulnerabilities were detected:' >> $GITHUB_ENV
11094 echo '```' >> $GITHUB_ENV
11195 echo "$RESULTS" >> $GITHUB_ENV
11296 echo '```' >> $GITHUB_ENV
113- echo '' >> $GITHUB_ENV
114- echo '⛔ Please address these findings before merging.' >> $GITHUB_ENV
97+ echo '⛔ Please address these before merging.' >> $GITHUB_ENV
11598 echo 'EOF' >> $GITHUB_ENV
11699 else
117100 echo 'comment_body<<EOF' >> $GITHUB_ENV
118101 echo '## 🔒 OWASP Scanner Results' >> $GITHUB_ENV
119102 echo '' >> $GITHUB_ENV
120- echo 'No vulnerabilities detected in the changed files.' >> $GITHUB_ENV
121- echo '' >> $GITHUB_ENV
103+ echo 'No vulnerabilities detected.' >> $GITHUB_ENV
122104 echo '```' >> $GITHUB_ENV
123105 echo "$RESULTS" >> $GITHUB_ENV
124106 echo '```' >> $GITHUB_ENV
@@ -128,22 +110,19 @@ jobs:
128110
129111 - name : Comment PR
130112 uses : peter-evans/create-or-update-comment@v4
131- if : always() && steps.diff.outputs.only_scanner_changes != 'true'
132113 with :
133114 issue-number : ${{ github.event.pull_request.number }}
134115 body : ${{ env.comment_body }}
135116
136117 - name : Upload scan artifact
137- if : always()
138118 uses : actions/upload-artifact@v4
139119 with :
140120 name : owasp-scan-results
141- path : |
142- owasp-results.txt
121+ path : owasp-results.txt
143122 retention-days : 5
144123
145124 - name : Fail if vulnerabilities found
146125 if : steps.owasp.outputs.vulnerabilities_found == 'true'
147126 run : |
148- echo "::error::OWASP scanner reported vulnerabilities. Failing the job. "
127+ echo "::error::OWASP scanner reported vulnerabilities."
149128 exit 1
0 commit comments