Skip to content

Commit 66348cd

Browse files
Ibraheem OlasupoIbraheem Olasupo
authored andcommitted
Implement milestone 6 dynamic API security validation
1 parent 4fc94e2 commit 66348cd

96 files changed

Lines changed: 3013 additions & 110 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/api-security.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: API Dynamic Security
2+
3+
on:
4+
pull_request:
5+
branches: ["main"]
6+
push:
7+
branches: ["main"]
8+
9+
permissions:
10+
contents: read
11+
12+
concurrency:
13+
group: api-dynamic-security-${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
dynamic-api-security:
18+
runs-on: ubuntu-24.04
19+
timeout-minutes: 25
20+
steps:
21+
# actions/checkout v4.2.2
22+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
23+
24+
# actions/setup-python v5.6.0
25+
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065
26+
with:
27+
python-version: "3.11"
28+
cache: "pip"
29+
30+
- name: Install dynamic test dependencies
31+
run: |
32+
python -m pip install --upgrade pip==25.1.1
33+
python -m pip install -e ".[dev]"
34+
35+
- name: Dynamic tool inventory
36+
run: make dynamic-tools
37+
38+
- name: Dynamic pytest validation
39+
run: make dynamic-fast
40+
41+
- name: Schemathesis local API validation
42+
run: make schemathesis-test
43+
44+
- name: OWASP ZAP local baseline
45+
run: make zap-baseline
46+
47+
- name: Dynamic evidence and reports
48+
run: make dynamic-evidence verify-dynamic-evidence dynamic-report

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Run:
99
```bash
1010
make quality
1111
make appsec-fast
12+
make dynamic-fast
1213
```
1314

1415
Use deterministic synthetic data only. Do not add real patient, NHS, genomic, credential, or cloud account data.
@@ -20,4 +21,5 @@ Use deterministic synthetic data only. Do not add real patient, NHS, genomic, cr
2021
- Do not introduce wildcard CORS defaults.
2122
- Do not commit generated caches, local databases, logs, or environment files.
2223
- Run scanner targets after changing dependencies, Dockerfile, Terraform, authentication or request-handling code.
24+
- Keep dynamic-security scans pointed only at localhost, loopback or approved local Docker targets.
2325
- Do not add broad scanner suppressions. Use `security/config/suppressions.yaml` with an owner, expiry and exact path or rule scope.

Makefile

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: setup install format format-check lint type-check test test-coverage auth-test api-security-test terraform-fmt terraform-fmt-check terraform-init terraform-validate terraform-test infrastructure-test infrastructure-evidence verify-infrastructure-evidence infrastructure-report security-tools secrets-scan sast sast-semgrep sast-bandit semgrep-test sca dependency-audit sbom verify-sbom iac-scan checkov-scan container-build-security container-scan appsec-fast appsec-full appsec-evidence verify-appsec-evidence appsec-report pre-commit-install pre-commit-run quality run docker-build docker-run threat-model-validate threat-model-evidence verify-threat-model-evidence threat-model-report api-security-evidence verify-api-security-evidence api-security-report dev-token-researcher dev-token-approver dev-token-auditor clean
1+
.PHONY: setup install format format-check lint type-check test test-coverage auth-test api-security-test terraform-fmt terraform-fmt-check terraform-init terraform-validate terraform-test infrastructure-test infrastructure-evidence verify-infrastructure-evidence infrastructure-report security-tools secrets-scan sast sast-semgrep sast-bandit semgrep-test sca dependency-audit sbom verify-sbom iac-scan checkov-scan container-build-security container-scan appsec-fast appsec-full appsec-evidence verify-appsec-evidence appsec-report dynamic-tools dynamic-server-start dynamic-server-wait dynamic-server-stop schemathesis-test api-schema-security-test zap-baseline zap-api-scan auth-boundary-test authorisation-boundary-test object-access-test input-mutation-test security-header-test cors-test resource-consumption-test audit-dynamic-test dast dynamic-evidence verify-dynamic-evidence dynamic-report dynamic-fast dynamic-full pre-commit-install pre-commit-run quality run docker-build docker-run threat-model-validate threat-model-evidence verify-threat-model-evidence threat-model-report api-security-evidence verify-api-security-evidence api-security-report dev-token-researcher dev-token-approver dev-token-auditor clean
22

33
PYTHON ?= python3
44
VENV ?= .venv
@@ -120,6 +120,69 @@ appsec-fast: secrets-scan sast dependency-audit
120120

121121
appsec-full: appsec-fast sbom verify-sbom checkov-scan container-build-security container-scan appsec-evidence verify-appsec-evidence appsec-report
122122

123+
dynamic-tools:
124+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py tools
125+
126+
dynamic-server-start:
127+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py server-start
128+
129+
dynamic-server-wait:
130+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py server-wait
131+
132+
dynamic-server-stop:
133+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py server-stop
134+
135+
schemathesis-test:
136+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py schemathesis
137+
138+
api-schema-security-test: schemathesis-test
139+
140+
zap-baseline:
141+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py zap
142+
143+
zap-api-scan: zap-baseline
144+
145+
auth-boundary-test:
146+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k authentication --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
147+
148+
authorisation-boundary-test:
149+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k authorisation --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
150+
151+
object-access-test:
152+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k object_level --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
153+
154+
input-mutation-test:
155+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k input_mutation --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
156+
157+
security-header-test:
158+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k security_headers --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
159+
160+
cors-test:
161+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k cors --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
162+
163+
resource-consumption-test:
164+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k rate_limit --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
165+
166+
audit-dynamic-test:
167+
PYTHONPATH=src $(PYTEST) tests/dynamic -q -k audit --json-report --json-report-file=outputs/security/dynamic/raw/pytest-dynamic.json
168+
169+
dast: zap-baseline
170+
171+
dynamic-evidence:
172+
PYTHONPATH=src $(PYTHON) -m genomic_research_access_api.security.dynamic.evidence --timestamp 2026-01-01T00:00:00Z
173+
174+
verify-dynamic-evidence:
175+
PYTHONPATH=src $(PYTHON) -m genomic_research_access_api.security.dynamic.evidence --verify
176+
177+
dynamic-report:
178+
PYTHONPATH=src $(PYTHON) -m genomic_research_access_api.security.dynamic.report
179+
180+
dynamic-fast:
181+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py pytest
182+
183+
dynamic-full:
184+
PYTHONPATH=src $(PYTHON) scripts/dynamic_security_tools.py full
185+
123186
pre-commit-install:
124187
$(PYTHON) -m pip install pre-commit==3.8.0
125188
$(VENV)/bin/pre-commit install

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Milestone 4 delivered a secure AWS ECS Fargate Terraform reference architecture,
2626

2727
Milestone 5 delivered the core AppSec pipeline: pinned scanner configuration, Semgrep and Bandit SAST, project-scoped pip-audit SCA, deterministic CycloneDX SBOM evidence, Checkov IaC reporting, Gitleaks and Trivy wrapper targets, AppSec reports and SHA-pinned security workflows.
2828

29+
Milestone 6 adds local-only dynamic API security validation: dynamic pytest boundary tests, pinned Schemathesis OpenAPI testing, pinned OWASP ZAP baseline scanning, bounded resource-consumption checks, deterministic dynamic evidence and dynamic-security reports. Dynamic scans are restricted to localhost, loopback and approved local Docker targets.
30+
2931
## Milestone 1 Scope
3032

3133
Implemented:
@@ -183,6 +185,8 @@ Open `http://127.0.0.1:8000/docs` for FastAPI's local OpenAPI UI.
183185
- `make checkov-scan`: run Checkov and record Terraform findings.
184186
- `make container-build-security`: build the local image for scanning.
185187
- `make container-scan`: run Trivy against the local image.
188+
- `make dynamic-fast`: run local dynamic pytest security tests.
189+
- `make dynamic-full`: run dynamic pytest, Schemathesis, ZAP, evidence verification and reports.
186190
- `make appsec-evidence`: generate deterministic AppSec evidence.
187191
- `make verify-appsec-evidence`: verify AppSec evidence checksums and SBOM shape.
188192
- `make quality`: run format, lint, type, coverage, auth, API-security, infrastructure and evidence checks.

SECURITY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ If you identify a vulnerability or unsafe pattern in this demonstration code, op
66

77
Do not submit real patient data, NHS data, genomic data, credentials, cloud account identifiers, or sensitive operational details in reports.
88

9-
Current implemented security controls include local JWT authentication, role-based and object-level authorisation, threat-model validation, non-deployed Terraform reference controls, and a Milestone 5 AppSec pipeline.
9+
Current implemented security controls include local JWT authentication, role-based and object-level authorisation, threat-model validation, non-deployed Terraform reference controls, a Milestone 5 AppSec pipeline, and Milestone 6 local dynamic API security validation.
1010

1111
Run local security checks with:
1212

1313
```bash
1414
make quality
1515
make appsec-fast
16+
make dynamic-fast
1617
```
1718

18-
Docker-backed Gitleaks and Trivy scans require a running Docker daemon when native binaries are not installed. Do not treat local scanner evidence as a production vulnerability-management programme or release approval gate.
19+
Docker-backed Gitleaks, Trivy, Schemathesis and ZAP scans require a running Docker daemon when native binaries or scanner containers are used. Dynamic scan targets must remain local-only. Do not treat local scanner evidence as a production vulnerability-management programme, penetration test or release approval gate.

docs/architecture/application-architecture.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ flowchart LR
108108
```
109109

110110
The scanner pipeline is local and CI-oriented. It does not publish images, create cloud resources or implement vulnerability-management workflow states.
111+
112+
## Milestone 6 Dynamic Security Architecture
113+
114+
```mermaid
115+
flowchart LR
116+
LocalAPI["FastAPI on 127.0.0.1"] --> Pytest["Dynamic pytest boundary tests"]
117+
LocalAPI --> Schema["Schemathesis OpenAPI tests"]
118+
LocalAPI --> Zap["OWASP ZAP baseline"]
119+
Pytest --> Raw["outputs/security/dynamic/raw"]
120+
Schema --> Raw
121+
Zap --> Raw
122+
Raw --> Evidence["Deterministic dynamic evidence"]
123+
Evidence --> Reports["Dynamic security reports"]
124+
```
125+
126+
Dynamic validation starts and stops a local API instance, rejects non-local scan targets, runs bounded generated traffic and records deterministic evidence. It does not scan external hosts, deploy cloud resources or add release-gate logic.

docs/limitations.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
- Runtime state is stored in memory and resets on restart.
77
- Local JWT keys under `tests/fixtures/keys/` are synthetic development material and are not production secrets.
88
- The audit event API is for local demonstration only and is restricted by role.
9-
- External identity-provider integration, AWS deployment, production rate limiting, immutable audit-log operations, release gates and vulnerability-management operations are intentionally deferred.
9+
- External identity-provider integration, AWS deployment, distributed production rate limiting, immutable audit-log operations, release gates and vulnerability-management operations are intentionally deferred.
1010
- AWS Terraform is present as a reference architecture only; no resources are deployed.
1111
- Terraform validation is skipped locally when Terraform is unavailable and is configured for CI through a pinned setup action.
1212
- Gitleaks and Trivy local execution require native binaries or a running Docker daemon for pinned container fallbacks.
13+
- Schemathesis and ZAP dynamic scans require Docker for pinned scanner containers and are restricted to local targets.
1314
- Checkov findings are captured as reporting evidence in Milestone 5; full Terraform remediation is not claimed.
15+
- Dynamic API security evidence is local synthetic validation and is not penetration-test coverage.

docs/milestones/milestone-6.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Milestone 6: API and Dynamic Security Validation
2+
3+
Milestone 6 adds local-only dynamic validation for the Genomic Research Access API.
4+
5+
Implemented controls:
6+
7+
- Dynamic pytest boundary tests for authentication, authorisation, object-level access, input mutation, security headers, CORS, resource consumption and audit events.
8+
- Pinned Schemathesis container execution against the local OpenAPI schema.
9+
- Pinned OWASP ZAP baseline scan against the local API only.
10+
- Local target safeguards for `127.0.0.1`, `localhost`, `::1`, `host.docker.internal` and the approved Docker service name `api`.
11+
- Bounded in-memory application rate limiter for deterministic dynamic tests.
12+
- Deterministic evidence under `outputs/security/dynamic/` and reports under `reports/security/`.
13+
14+
Milestone 6 does not add release gates, canonical finding normalisation, risk scoring, exception workflow, cloud deployment, Redis, API Gateway or external scanning.

docs/operations/local-development.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ make appsec-fast
3232

3333
This runs Semgrep rule tests, SAST, project-scoped dependency audit, SBOM validation, Checkov reporting and AppSec evidence verification. Docker-backed secret and container scans are available through `make secrets-scan` and `make container-scan` when Docker is running.
3434

35+
## Dynamic API Security Checks
36+
37+
```bash
38+
make dynamic-fast
39+
make dynamic-full
40+
```
41+
42+
`dynamic-fast` runs local in-process API security tests. `dynamic-full` adds local API startup, pinned Schemathesis OpenAPI testing, pinned OWASP ZAP baseline scanning, deterministic evidence verification and report generation. Do not override scan targets to external hosts.
43+
3544
## Local API Tokens
3645

3746
Protected `/api/v1/*` routes require a bearer token. Generate synthetic local tokens with:

docs/roadmap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ The broader portfolio roadmap may include:
77
3. Authentication and object-level authorisation. Delivered.
88
4. Secure AWS infrastructure with Terraform. Delivered as non-deployed configuration.
99
5. Automated security scanning. Delivered as local AppSec pipeline configuration and evidence.
10-
6. AWS and Terraform security.
10+
6. API and dynamic security validation. Delivered as local-only Schemathesis, ZAP and dynamic pytest evidence.
1111
7. Vulnerability management and release gates.
1212
8. Developer enablement and Security Champions.
1313

14-
Milestones 1, 2, 3, 4 and 5 are implemented in this repository state. Future milestones should update this document when they are actually delivered.
14+
Milestones 1, 2, 3, 4, 5 and 6 are implemented in this repository state. Future milestones should update this document when they are actually delivered.

0 commit comments

Comments
 (0)