Skip to content

Commit 987d40d

Browse files
Address review feedback on fetchZAPResults.sh
- Add mkdir -p results/ before curl writes to it (fixes failure on fresh clone) - Pass API key via X-ZAP-API-Key header instead of URL query param (keeps key out of process list and shell history) - Redirect all error/usage output to stderr - Validate getBenchmarkVersion.sh output is non-empty before building filename
1 parent f7af459 commit 987d40d

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

scripts/fetchZAPResults.sh

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,72 @@
33
# Downloads a ZAP XML report from a URL and saves it to the results/ directory.
44
# After downloading, run createScorecards.sh to generate the scorecard.
55
#
6-
# Usage: scripts/fetchZAPResults.sh <ZAP_REPORT_URL> [OUTPUT_FILENAME]
6+
# Usage: scripts/fetchZAPResults.sh <ZAP_REPORT_URL> [OUTPUT_FILENAME] [API_KEY]
77
#
88
# Examples:
99
# scripts/fetchZAPResults.sh http://172.17.0.3:8090/OTHER/core/other/xmlreport/
10-
# scripts/fetchZAPResults.sh "http://zap:8090/OTHER/core/other/xmlreport/?apikey=abc123"
1110
# scripts/fetchZAPResults.sh http://zap:8090/OTHER/core/other/xmlreport/ my-zap-results.xml
11+
# scripts/fetchZAPResults.sh http://zap:8090/OTHER/core/other/xmlreport/ "" my-secret-api-key
1212

1313
source scripts/requireCommand.sh
1414

1515
requireCommand curl
1616

1717
if [ $# -eq 0 ]; then
18-
echo "Usage: $0 <ZAP_REPORT_URL> [OUTPUT_FILENAME]"
19-
echo ""
20-
echo "Downloads a ZAP XML report from the given URL and saves it to results/."
21-
echo "After downloading, run createScorecards.sh to generate the scorecard."
22-
echo ""
23-
echo "Examples:"
24-
echo " $0 http://172.17.0.3:8090/OTHER/core/other/xmlreport/"
25-
echo " $0 \"http://zap:8090/OTHER/core/other/xmlreport/?apikey=abc123\""
26-
echo " $0 http://zap:8090/OTHER/core/other/xmlreport/ my-zap-results.xml"
18+
echo "Usage: $0 <ZAP_REPORT_URL> [OUTPUT_FILENAME] [API_KEY]" >&2
19+
echo "" >&2
20+
echo "Downloads a ZAP XML report from the given URL and saves it to results/." >&2
21+
echo "After downloading, run createScorecards.sh to generate the scorecard." >&2
22+
echo "" >&2
23+
echo "Arguments:" >&2
24+
echo " ZAP_REPORT_URL URL to the ZAP XML report endpoint" >&2
25+
echo " OUTPUT_FILENAME Optional custom filename (saved under results/)" >&2
26+
echo " API_KEY Optional ZAP API key (passed via header, not in URL)" >&2
27+
echo "" >&2
28+
echo "Examples:" >&2
29+
echo " $0 http://172.17.0.3:8090/OTHER/core/other/xmlreport/" >&2
30+
echo " $0 http://zap:8090/OTHER/core/other/xmlreport/ my-zap-results.xml" >&2
31+
echo " $0 http://zap:8090/OTHER/core/other/xmlreport/ \"\" my-secret-api-key" >&2
2732
exit 1
2833
fi
2934

3035
zap_url="$1"
3136

32-
if [ $# -ge 2 ]; then
37+
if [ -n "${2:-}" ]; then
3338
filename="$2"
3439
else
3540
benchmark_version=$(scripts/getBenchmarkVersion.sh)
41+
if [ -z "${benchmark_version}" ]; then
42+
echo "ERROR: Could not determine Benchmark version from pom.xml." >&2
43+
exit 1
44+
fi
3645
date_stamp=$(date +%Y%m%d)
3746
filename="Benchmark_${benchmark_version}-ZAP-${date_stamp}.xml"
3847
fi
3948

49+
api_key="${3:-}"
50+
51+
mkdir -p results/
4052
output="results/${filename}"
4153

54+
curl_args=(-sS -o "${output}" -w '%{http_code}' --connect-timeout 10 --max-time 120)
55+
if [ -n "${api_key}" ]; then
56+
curl_args+=(-H "X-ZAP-API-Key: ${api_key}")
57+
fi
58+
4259
echo "Downloading ZAP report from: ${zap_url}"
43-
http_code=$(curl -sS -o "${output}" -w '%{http_code}' --connect-timeout 10 --max-time 120 "${zap_url}")
60+
http_code=$(curl "${curl_args[@]}" "${zap_url}")
4461

4562
if [ "${http_code}" -ne 200 ]; then
46-
echo "ERROR: Download failed with HTTP status ${http_code}"
63+
echo "ERROR: Download failed with HTTP status ${http_code}" >&2
4764
rm -f "${output}"
4865
exit 1
4966
fi
5067

5168
if ! head -2 "${output}" | grep -q "OWASPZAPReport"; then
52-
echo "ERROR: Downloaded file does not appear to be a ZAP XML report."
53-
echo "First 3 lines of downloaded content:"
54-
head -3 "${output}"
69+
echo "ERROR: Downloaded file does not appear to be a ZAP XML report." >&2
70+
echo "First 3 lines of downloaded content:" >&2
71+
head -3 "${output}" >&2
5572
rm -f "${output}"
5673
exit 1
5774
fi

0 commit comments

Comments
 (0)