api-security-spectral
API Spec Linting with Spectral
You are a security engineer linting OpenAPI/AsyncAPI specifications using Spectral to enforce security best practices in API design.
When to use
Use this skill when asked to review an OpenAPI (Swagger) or AsyncAPI spec for security issues before deployment.
Prerequisites
- Spectral installed (
npm install -g @stoplight/spectral-cli) - Verify:
spectral --version
Instructions
- Identify the target — Determine the API spec file (YAML/JSON).
- Run the scan:
spectral lint <spec-file> --format json > spectral-results.json- With custom ruleset:
spectral lint <spec> --ruleset .spectral.yml --format json - Specific format:
spectral lint openapi.yaml --format pretty
- With custom ruleset:
- Parse the results — Read JSON output and present findings:
| # | Severity | Rule | Path | Message | Line |
|---|----------|------|------|---------|------|
- Summarize — Provide total issues by severity and specific spec fixes.
Key Security Rules to Check
| Rule | Description |
|---|---|
oas3-api-servers |
API servers must use HTTPS |
operation-operationId |
All operations need unique IDs |
operation-description |
Operations should be documented |
| Security scheme defined | OAuth2/API key/Bearer token present |
| Input validation | Request body schema with constraints |
| Error responses | 401, 403, 429 responses defined |
| Rate limiting | Headers for rate limit documented |
| No eval/dynamic paths | Path parameters properly constrained |
Custom Security Ruleset
Create .spectral.yml with security-focused rules:
extends: ["spectral:oas"]
rules:
oas3-server-https:
description: Server URLs must use HTTPS
given: "$.servers[*].url"
then:
function: pattern
functionOptions:
match: "^https://"
More from vchirrav/owasp-secure-coding-md
mobile-security-mobsf
Run MobSF (Mobile Security Framework) for automated static and dynamic analysis of Android and iOS apps. Detects insecure storage, weak crypto, hardcoded secrets, and permission issues.
14sast-eslint-security
Run ESLint with security plugins on JavaScript/TypeScript code. Detects eval usage, non-literal RegExp, prototype pollution, and other JS/TS security anti-patterns.
5api-security-schemathesis
Run Schemathesis for property-based API security testing. Generates test cases from OpenAPI/GraphQL schemas to find crashes, 500 errors, and spec violations.
4sbom-syft
Run Syft to generate Software Bill of Materials (SBOM) from container images and filesystems. Outputs CycloneDX or SPDX formats for supply chain compliance.
3secret-scan-gitleaks
Run Gitleaks to detect hardcoded secrets in git repositories. Finds API keys, tokens, passwords, and credentials in code and git history.
3network-scan-nmap
Run Nmap for network discovery and security auditing. Performs port scanning, service detection, OS fingerprinting, and vulnerability script scanning.
3