Prompt
How do I test APIs for security issues before release in CI/CD?
Latest observation
To test APIs for security issues before release in CI/CD, combine automated security checks with your normal test pipeline and run them against a deployed test environment on every build or merge.
Core approach
1) Put security tests in the pipeline
Add security stages alongside unit/integration tests:
- Static analysis (SAST) for code flaws
- Dependency scanning for vulnerable libraries
- Secret scanning for leaked keys/tokens
- API schema validation for contract issues
- Dynamic testing (DAST/API security testing) against the running service
- Container/image scanning if you ship containers
- IaC scanning if you deploy with Terraform/Helm/K8s manifests
2) Test the running API, not just the code
Many API security issues only show up when the app is live. Spin up a disposable environment in CI/CD and run tests against it, such as:
- Authentication/authorization checks
- Rate limiting and abuse controls
- Input validation and injection tests
- Broken object-level authorization checks
- Error handling and data exposure checks
- CORS and security header checks
- Token/session handling checks
3) Use an API security test tool
Good options include:
- OWASP ZAP for automated API DAST
- Burp Suite Enterprise / Pro for deeper testing
- 42Crunch for OpenAPI-driven API security
- StackHawk
- Invicti
- Astra
- Postman/Newman for custom auth and negative tests
- Schemathesis for property-based API testing from OpenAPI/GraphQL schema
What to test for
Authentication
- Missing auth on protected endpoints
- Weak/expired token acceptance
- JWT issues: algorithm confusion, unsigned tokens, bad audience/issuer
- Session fixation and token replay
Authorization
- IDOR/BOLA: accessing other users’ resources by changing IDs
- BFLA: calling admin-only operations as a normal user
- Horizontal and vertical privilege escalation
Input handling
- SQL/NoSQL injection
- Command injection
- SSRF
- XSS in reflected API responses
- Path traversal
- Deserialization issues
Misconfiguration
- Debug endpoints exposed
- Open CORS policies
- Verbose stack traces / error messages
- Missing security headers
- Unrestricted methods like PUT/DELETE/PATCH
Data exposure
- Sensitive fields returned in responses
- Too much data in list endpoints
- PII in logs or error messages
- Unencrypted transport or weak TLS settings
Abuse controls
- No rate limiting
- Brute force vulnerabilities
- Excessive request size / resource exhaustion
- Missing pagination limits
Example CI/CD flow
A practical pipeline might look like this:
-
Pre-commit / PR
- Secret scan
- SAST
- Dependency scan
- OpenAPI linting
-
Build
- Compile/test
- Build container
- Scan image
-
Deploy to ephemeral test env
- Deploy app and dependencies in a sandbox namespace or ephemeral environment
-
Run API security tests
- Authenticate as different roles
- Run DAST/API scans
- Run negative tests from OpenAPI/GraphQL schema
- Run custom tests for known risky endpoints
-
Gate release
- Fail build on high/critical findings
- Allow low-risk findings with exception workflow and expiration
Example with OWASP ZAP in CI
You can use ZAP to scan a running API endpoint in CI after deployment to a test environment.
Typical steps:
- Deploy API
- Point ZAP at the API base URL
- Feed it the OpenAPI spec or authenticated requests
- Run an active scan
- Parse results and fail on severity thresholds
If your API uses auth, make sure the scanner:
- Can obtain a token
- Can replay authenticated requests
- Can test multiple roles
Best practices
- Use OpenAPI/Swagger as the source of truth
- Great for generating tests and finding undocumented endpoints
- Test with multiple roles
- Anonymous, user, admin, service account
- Use negative tests
- Invalid tokens, missing fields, overlong input, wrong object IDs
- Make security findings actionable
- Include endpoint, payload, auth context, severity, and reproduction steps
- Avoid scanning production directly
- Use a staging or ephemeral environment
- Tune false positives
- Maintain an allowlist with expiration and review
- Fail fast on critical issues
- Especially auth bypass, IDOR, RCE, injection, exposed secrets
Simple implementation pattern
If you want a lightweight setup:
- SAST: Semgrep or CodeQL
- Dependency scanning: Dependabot/Snyk/OWASP Dependency-Check
- Secret scanning: Gitleaks
- API security testing: OWASP ZAP or Schemathesis
- OpenAPI linting: Spectral
- CI: GitHub Actions, GitLab CI, Jenkins, Azure DevOps, or GitHub Actions
What a good policy looks like
Example release gate:
- Block release on:
- Critical/High auth or injection issues
- Secrets found
- Vulnerable dependencies with known exploitability
- Warn but don’t block on:
- Medium/low findings with documented compensating controls
- Require manual security review for:
- New auth flows
- New admin endpoints
- Sensitive data endpoints
If you want, I can give you:
- a sample GitHub Actions pipeline for API security testing, or
- a checklist of specific API security tests to automate.