Integrate OWASP ZAP security testing into Jenkins CI/CD workflow using OpenAPI spec for scanning and reporting vulnerabilities.
Integrate OWASP ZAP security testing into your Jenkins CI/CD workflow by leveraging the OpenAPI spec exposed at /v3/api-docs in your Spring Boot application. This guide walks you through updating your Jenkinsfile, creating a ZAP scan script, publishing HTML reports, and fixing security headers.
This script retrieves your service’s NodePort, invokes the ZAP API scan against the OpenAPI spec, and organizes the report for Jenkins.
Copy
Ask AI
#!/bin/bash# Fetch the NodePort that exposes our servicePORT=$(kubectl -n default get svc ${serviceName} -o json | jq .spec.ports[0].nodePort)# Ensure write permissions for the report directorychmod 777 $(pwd)# Run the OWASP ZAP API scan against the OpenAPI specdocker run \ -v $(pwd):/zap/wrk/:rw \ -t owasp/zap2docker-weekly \ zap-api-scan.py \ -t $applicationURL:$PORT/v3/api-docs \ -f openapi \ -r zap_report.htmlexit_code=$?# Move the HTML report into its own foldermkdir -p owasp-zap-reportmv zap_report.html owasp-zap-reportecho "Exit Code: $exit_code"if [[ $exit_code -ne 0 ]]; then echo "OWASP ZAP found vulnerabilities. Please check the HTML report." exit 1else echo "No vulnerabilities detected by OWASP ZAP."fi
Save this as zap.sh, make it executable (chmod +x zap.sh), and commit it alongside your Jenkinsfile.
If OWASP ZAP exits with a non-zero code, the pipeline will fail. Review the HTML report to triage any findings.
Open the OWASP ZAP HTML Report link on your Jenkins build page to explore vulnerabilities:
Select any alert for detailed information:
In this demo, text/plain responses are expected and can be treated as false positives, but the missing X-Content-Type-Options: nosniff header is a genuine low-risk issue.
Open your application endpoint in a browser and inspect the response headers under Developer Tools → Network. You should see that X-Content-Type-Options is absent:
Next, enhance your Spring Boot security configuration to include the X-Content-Type-Options: nosniff header and eliminate the warning.