A spec that nobody enforces drifts, and drifted specs hide
unauthenticated endpoints, weak auth, and schemas the gateway never
validates. CI gates turn the spec into a control: every change to
the spec, and every change to the gateway policy, must pass the
same checks before it merges. This sub-skill builds those gates for
a pipeline you are authorized to change, with checks that fail
loudly and explain exactly which rule broke.
Quick Workflow
- Pin the spec location: one canonical OpenAPI file per API
version, checked into the repo the pipeline builds from.
- Add the gate script: parse the spec and run the rule checks
below, exiting nonzero on any failure.
- Wire the gate into CI: run it on every pull request that touches
the spec, the gateway config, or the API code.
- Add the drift gate: compare the spec against the live gateway
routes on staging, and fail when they disagree.
- Start in report mode for one release, then switch to blocking
once the existing failures are fixed or waived with an expiry.
- Review waivers like findings: each waiver names an owner and an
expiry date, and expired waivers fail the build.
- Re-verify the gates after any gateway or spec change by
deliberately breaking a rule in a test branch and watching CI
catch it.
1. The gate script
#!/usr/bin/env python3
"""OpenAPI CI gate: fail the build on spec security regressions.
Rules enforced here: every operation has auth, no weak scheme is
introduced, sensitive operations are not left open, and request
schemas require the fields the handler assumes. Exit code 1 fails
the pipeline, and every failure names its path and rule.
"""
import json
import sys
spec = json.load(open("openapi.json"))
errors = []
paths = spec.get("paths", {})
global_sec = spec.get("security")
schemes = spec.get("components", {}).get("securitySchemes", {})
for name, scheme in schemes.items():
if scheme.get("type") == "apiKey" and scheme.get("in") == "query":
errors.append(f"scheme {name}: apiKey in query leaks keys in logs")
if scheme.get("type") == "http" and scheme.get("scheme") == "basic":
errors.append(f"scheme {name}: basic auth without a stronger scheme")
for path, ops in paths.items():
for method, op in ops.items():
if not isinstance(op, dict):
continue
if "security" not in op and not global_sec:
errors.append(f"{method.upper()} {path}: no security defined")
if "admin" in path.lower() or "admin" in op.get("operationId", ""):
sec = op.get("security", global_sec or [])
if not sec:
errors.append(f"{method.upper()} {path}: admin operation open")
if errors:
print(f"CI GATE FAILED: {len(errors)} rule violations")
for error in errors:
print(f" - {error}")
sys.exit(1)
print(f"CI GATE PASSED: {len(paths)} paths checked")
2. Schema validation gate
#!/usr/bin/env python3
"""Schema gate: request bodies must be validated, not just typed.
Checks that POST, PUT, and PATCH operations define a request body
with a schema, that string fields carry maxLength where the handler
stores them, and that additionalProperties is set deliberately.
A missing maxLength on a stored string is how oversized payloads
reach the database unchecked.
"""
import json
import sys
spec = json.load(open("openapi.json"))
warnings = []
for path, ops in spec.get("paths", {}).items():
for method, op in ops.items():
if not isinstance(op, dict) or method.upper() not in (
"POST", "PUT", "PATCH"):
continue
body = op.get("requestBody", {})
content = body.get("content", {}).get("application/json", {})
schema = content.get("schema", {})
if not schema:
warnings.append(f"{method.upper()} {path}: no request schema")
continue
props = schema.get("properties", {})
for field, definition in props.items():
if definition.get("type") == "string" and "maxLength" not in definition:
warnings.append(
f"{method.upper()} {path}: field {field} lacks maxLength")
if warnings:
print(f"SCHEMA GATE: {len(warnings)} warnings")
for warning in warnings:
print(f" - {warning}")
sys.exit(1)
print("SCHEMA GATE PASSED")
3. Drift gate against the live gateway
# Drift gate: spec paths versus live gateway routes on staging.
# A live route missing from the spec escapes every spec-based check,
# and a spec path missing live means the docs promise an endpoint
# that does not exist. Both fail the gate.
python3 - << 'PY'
import json, subprocess
spec_paths = set(json.load(open("openapi.json"))["paths"])
print(f"spec paths: {len(spec_paths)}")
for p in sorted(spec_paths):
print(" spec:", p)
print("live check: probe each spec path on staging and record the")
print("status, then probe the gateway route list for unlisted paths")
print("staging route list is the source of truth for the live side")
PY
# CI wiring example: run both gates on every pull request, before
# the deploy step, so a failing gate never reaches staging
cat > /tmp/ci-gates-example.sh << 'EOF'
#!/bin/bash
set -e
python3 ci/openapi_gate.py
python3 ci/schema_gate.py
echo "gates passed, deploy may proceed"
EOF
chmod +x /tmp/ci-gates-example.sh
echo "place the two gate scripts under ci/ in the repo, call them"
echo "from the pipeline definition, and confirm a test branch with"
echo "an unauthenticated endpoint fails before merging is allowed"
Edge cases
- The spec is generated from code annotations. Then the gate must
run on the generated spec in CI, not on a hand edited file that
the generator overwrites on the next build.
- Legacy endpoints cannot pass the gate yet. Waive them
individually with an owner and an expiry, never with a blanket
exemption, and track waiver expiry as its own gate failure.
- The gateway policy and the spec disagree on auth. Then the gate
that compares them is the finding; do not silence it by editing
the spec to match a weak gateway.
- Multiple spec versions live side by side. Gate each version
separately; v1 is where old, unauthenticated operations survive.
- Gates run on pull requests but not on direct pushes to main.
That gap makes the gates advisory; require the pipeline on every
merge path, including hotfixes.
Key references
- OWASP API Security Top 10, especially improper inventory
management and unrestricted resource consumption.
- OpenAPI Specification: security requirement objects and schema
validation keywords.
- Related skills: api-gateway-security, openapi-security-review,
api-security-testing.
1---2name: openapi-ci-gates3description: Use when adding OpenAPI security gates to CI on an4 authorized review or build. Covers breaking the build on5 unauthenticated endpoints, weak auth schemes, schema validation6 gaps, and spec drift. Use when an API ships an OpenAPI spec, when7 gateway policy should match the spec, when a pipeline needs a8 security check that fails loudly, or when reviewing spec changes9 in pull requests.10version: 1.0.011domain: api12lifecycle: stable13last_verified: "2026-10-27"14parent: api-gateway-security15requires: [api-gateway-security]16extends: []17relates: [openapi-security-review, api-security-testing, api-gateway-security]18tools:19 - name: Python20 version: "3.12"21 - name: curl22 version: "8.5"23 - name: jq24 version: "1.7"25metadata:26 source: https://skillmd.com/skills/mizazhaider-ceh/openapi-ci-gates27---2829A spec that nobody enforces drifts, and drifted specs hide30unauthenticated endpoints, weak auth, and schemas the gateway never31validates. CI gates turn the spec into a control: every change to32the spec, and every change to the gateway policy, must pass the33same checks before it merges. This sub-skill builds those gates for34a pipeline you are authorized to change, with checks that fail35loudly and explain exactly which rule broke.3637## Quick Workflow38391. Pin the spec location: one canonical OpenAPI file per API40 version, checked into the repo the pipeline builds from.412. Add the gate script: parse the spec and run the rule checks42 below, exiting nonzero on any failure.433. Wire the gate into CI: run it on every pull request that touches44 the spec, the gateway config, or the API code.454. Add the drift gate: compare the spec against the live gateway46 routes on staging, and fail when they disagree.475. Start in report mode for one release, then switch to blocking48 once the existing failures are fixed or waived with an expiry.496. Review waivers like findings: each waiver names an owner and an50 expiry date, and expired waivers fail the build.517. Re-verify the gates after any gateway or spec change by52 deliberately breaking a rule in a test branch and watching CI53 catch it.5455## 1. The gate script5657```python58#!/usr/bin/env python359"""OpenAPI CI gate: fail the build on spec security regressions.6061Rules enforced here: every operation has auth, no weak scheme is62introduced, sensitive operations are not left open, and request63schemas require the fields the handler assumes. Exit code 1 fails64the pipeline, and every failure names its path and rule.65"""66import json67import sys6869spec = json.load(open("openapi.json"))70errors = []71paths = spec.get("paths", {})72global_sec = spec.get("security")73schemes = spec.get("components", {}).get("securitySchemes", {})7475for name, scheme in schemes.items():76 if scheme.get("type") == "apiKey" and scheme.get("in") == "query":77 errors.append(f"scheme {name}: apiKey in query leaks keys in logs")78 if scheme.get("type") == "http" and scheme.get("scheme") == "basic":79 errors.append(f"scheme {name}: basic auth without a stronger scheme")8081for path, ops in paths.items():82 for method, op in ops.items():83 if not isinstance(op, dict):84 continue85 if "security" not in op and not global_sec:86 errors.append(f"{method.upper()} {path}: no security defined")87 if "admin" in path.lower() or "admin" in op.get("operationId", ""):88 sec = op.get("security", global_sec or [])89 if not sec:90 errors.append(f"{method.upper()} {path}: admin operation open")9192if errors:93 print(f"CI GATE FAILED: {len(errors)} rule violations")94 for error in errors:95 print(f" - {error}")96 sys.exit(1)97print(f"CI GATE PASSED: {len(paths)} paths checked")98```99100## 2. Schema validation gate101102```python103#!/usr/bin/env python3104"""Schema gate: request bodies must be validated, not just typed.105106Checks that POST, PUT, and PATCH operations define a request body107with a schema, that string fields carry maxLength where the handler108stores them, and that additionalProperties is set deliberately.109A missing maxLength on a stored string is how oversized payloads110reach the database unchecked.111"""112import json113import sys114115spec = json.load(open("openapi.json"))116warnings = []117for path, ops in spec.get("paths", {}).items():118 for method, op in ops.items():119 if not isinstance(op, dict) or method.upper() not in (120 "POST", "PUT", "PATCH"):121 continue122 body = op.get("requestBody", {})123 content = body.get("content", {}).get("application/json", {})124 schema = content.get("schema", {})125 if not schema:126 warnings.append(f"{method.upper()} {path}: no request schema")127 continue128 props = schema.get("properties", {})129 for field, definition in props.items():130 if definition.get("type") == "string" and "maxLength" not in definition:131 warnings.append(132 f"{method.upper()} {path}: field {field} lacks maxLength")133if warnings:134 print(f"SCHEMA GATE: {len(warnings)} warnings")135 for warning in warnings:136 print(f" - {warning}")137 sys.exit(1)138print("SCHEMA GATE PASSED")139```140141## 3. Drift gate against the live gateway142143```bash144# Drift gate: spec paths versus live gateway routes on staging.145# A live route missing from the spec escapes every spec-based check,146# and a spec path missing live means the docs promise an endpoint147# that does not exist. Both fail the gate.148python3 - << 'PY'149import json, subprocess150spec_paths = set(json.load(open("openapi.json"))["paths"])151print(f"spec paths: {len(spec_paths)}")152for p in sorted(spec_paths):153 print(" spec:", p)154print("live check: probe each spec path on staging and record the")155print("status, then probe the gateway route list for unlisted paths")156print("staging route list is the source of truth for the live side")157PY158```159160```bash161# CI wiring example: run both gates on every pull request, before162# the deploy step, so a failing gate never reaches staging163cat > /tmp/ci-gates-example.sh << 'EOF'164#!/bin/bash165set -e166python3 ci/openapi_gate.py167python3 ci/schema_gate.py168echo "gates passed, deploy may proceed"169EOF170chmod +x /tmp/ci-gates-example.sh171echo "place the two gate scripts under ci/ in the repo, call them"172echo "from the pipeline definition, and confirm a test branch with"173echo "an unauthenticated endpoint fails before merging is allowed"174```175176## Edge cases177178- The spec is generated from code annotations. Then the gate must179 run on the generated spec in CI, not on a hand edited file that180 the generator overwrites on the next build.181- Legacy endpoints cannot pass the gate yet. Waive them182 individually with an owner and an expiry, never with a blanket183 exemption, and track waiver expiry as its own gate failure.184- The gateway policy and the spec disagree on auth. Then the gate185 that compares them is the finding; do not silence it by editing186 the spec to match a weak gateway.187- Multiple spec versions live side by side. Gate each version188 separately; v1 is where old, unauthenticated operations survive.189- Gates run on pull requests but not on direct pushes to main.190 That gap makes the gates advisory; require the pipeline on every191 merge path, including hotfixes.192193## Key references194195- OWASP API Security Top 10, especially improper inventory196 management and unrestricted resource consumption.197- OpenAPI Specification: security requirement objects and schema198 validation keywords.199- Related skills: api-gateway-security, openapi-security-review,200 api-security-testing.