Skip to content

Commit 66e8b76

Browse files
Merge pull request #1147 from boluwacodes/fix/issues-848-850-860-861
feat: Security hardening, API validation, and ZK documentation (#848, #850, #860, #861)
2 parents 2ce7cf4 + ac2c38c commit 66e8b76

9 files changed

Lines changed: 2270 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: OpenAPI Spec Validation & Codegen
2+
on:
3+
pull_request:
4+
branches: [main]
5+
paths:
6+
- 'backend/openapi.yaml'
7+
- 'backend/src/**/*.js'
8+
- 'sdk/client/**'
9+
- '.github/workflows/openapi-validation.yml'
10+
push:
11+
branches: [main]
12+
paths:
13+
- 'backend/openapi.yaml'
14+
- 'backend/src/index.js'
15+
# Push trigger ensures spec stays in sync with route changes on main
16+
jobs:
17+
validate-spec:
18+
name: Validate OpenAPI spec completeness
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
- uses: actions/setup-node@v4
23+
with:
24+
node-version: 20
25+
cache: npm
26+
27+
- name: Install backend dependencies
28+
run: npm install
29+
working-directory: backend
30+
31+
- name: Validate OpenAPI spec covers all routes
32+
run: node backend/scripts/validate-openapi-coverage.js
33+
34+
- name: Install Spectral CLI for spec linting
35+
run: npm install -g @stoplight/spectral-cli
36+
37+
- name: Lint OpenAPI spec with Spectral
38+
run: |
39+
if [ -f backend/.spectral.yaml ]; then
40+
spectral lint backend/openapi.yaml --ruleset backend/.spectral.yaml
41+
else
42+
spectral lint backend/openapi.yaml
43+
fi
44+
continue-on-error: true
45+
46+
codegen-drift:
47+
name: Check generated client is up to date
48+
runs-on: ubuntu-latest
49+
needs: validate-spec
50+
steps:
51+
- uses: actions/checkout@v4
52+
- uses: actions/setup-node@v4
53+
with:
54+
node-version: 20
55+
cache: npm
56+
57+
- name: Install sdk/client dependencies
58+
run: npm install
59+
working-directory: sdk/client
60+
61+
- name: Regenerate TypeScript client from openapi.yaml
62+
run: npm run generate
63+
working-directory: sdk/client
64+
65+
- name: Check for drift in generated code
66+
run: |
67+
if ! git diff --exit-code sdk/client/src/; then
68+
echo ""
69+
echo "❌ ERROR: The generated TypeScript client in sdk/client/src/ is out of date."
70+
echo ""
71+
echo "To fix this:"
72+
echo " 1. Run: cd sdk/client && npm run generate"
73+
echo " 2. Commit the updated files"
74+
echo ""
75+
exit 1
76+
fi
77+
78+
- name: Verify generated client compiles
79+
run: npm run build
80+
working-directory: sdk/client
81+
82+
- name: Run generated client tests
83+
run: npm test
84+
working-directory: sdk/client
85+
continue-on-error: true
86+
87+
validate-examples:
88+
name: Validate OpenAPI examples
89+
runs-on: ubuntu-latest
90+
needs: validate-spec
91+
steps:
92+
- uses: actions/checkout@v4
93+
- uses: actions/setup-node@v4
94+
with:
95+
node-version: 20
96+
97+
- name: Install openapi-examples-validator
98+
run: npm install -g openapi-examples-validator
99+
100+
- name: Validate examples in spec
101+
run: openapi-examples-validator backend/openapi.yaml
102+
continue-on-error: true

backend/.spectral.yaml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Spectral OpenAPI Linting Rules
2+
# https://stoplight.io/open-source/spectral
3+
4+
extends: [[spectral:oas, all]]
5+
6+
rules:
7+
# Require operation IDs for all operations (needed for code generation)
8+
operation-operationId: error
9+
10+
# Require descriptions for all operations
11+
operation-description: warn
12+
13+
# Require tags for all operations (helps with SDK organization)
14+
operation-tags: warn
15+
16+
# Require examples in responses
17+
oas3-valid-media-example: warn
18+
19+
# Require parameter descriptions
20+
oas3-parameter-description: warn
21+
22+
# Enforce consistent parameter naming (camelCase)
23+
oas3-parameter-name-invalid: warn
24+
25+
# No $ref siblings (OpenAPI 3.0 limitation)
26+
no-$ref-siblings: error
27+
28+
# Require security definitions
29+
operation-security-defined: error
30+
31+
# Path parameters must be defined
32+
path-params: error
33+
34+
# Success responses must be defined
35+
operation-success-response: error
36+
37+
# Custom rules specific to Trivela API
38+
39+
# All POST/PUT/PATCH operations should have request body
40+
trivela-mutating-operations-request-body:
41+
description: POST/PUT/PATCH operations should have a request body
42+
given: $.paths[*][?(@property === 'post' || @property === 'put' || @property === 'patch')]
43+
severity: warn
44+
then:
45+
field: requestBody
46+
function: truthy
47+
48+
# All error responses should reference Error schema
49+
trivela-error-response-schema:
50+
description: Error responses (4xx, 5xx) should use Error schema
51+
given: $.paths[*][*].responses[?(@property >= 400)]
52+
severity: warn
53+
then:
54+
field: content.application/json.schema.$ref
55+
function: pattern
56+
functionOptions:
57+
match: "#/components/schemas/Error"
58+
59+
# Operations returning lists should have pagination
60+
trivela-list-operations-pagination:
61+
description: GET operations returning arrays should document pagination
62+
given: $.paths[*].get.responses.200.content.application/json.schema
63+
severity: hint
64+
then:
65+
- field: properties.data
66+
function: truthy
67+
- field: properties.total
68+
function: truthy
69+
- field: properties.limit
70+
function: truthy
71+
- field: properties.offset
72+
function: truthy

0 commit comments

Comments
 (0)