Security Configuration
This guide covers the security settings introduced in the Team Coca P0 Security Fixes (ADR-018).
All security features are enabled by default with secure settings. You only need to configure them if you're customizing for your environment. One exception: STOA_MCP_AUDIENCE has no default and must be set explicitly.
Quick Referenceβ
| Feature | Setting | Default | Rollback |
|---|---|---|---|
| JWT Audience | STOA_MCP_AUDIENCE | none (required) | No disable switch; correct the value |
| CORS | CORS_ORIGINS | Whitelist | Set to "*" |
| SSE Limits | SSE_LIMITER_ENABLED | true | Set to false |
| Trusted Proxies | SSE_TRUSTED_PROXIES | "" (none) | Configure CIDRs |
JWT Audience Validation (CAB-938)β
What It Doesβ
Validates that MCP access tokens carry the resource audience (aud claim) configured for this gateway, preventing token reuse from other identity-provider clients.
Configurationβ
# Environment variable (required, no default)
STOA_MCP_AUDIENCE=<MCP_AUDIENCE>
Settingsβ
| Setting | Type | Default | Description |
|---|---|---|---|
STOA_MCP_AUDIENCE | string | none (required) | Single resource audience that MCP access tokens must carry. Unset or empty: MCP entrypoints reject every authenticated request, and a gateway with STOA_ENVIRONMENT=prod refuses to start. |
Keycloak Configurationβ
Ensure your Keycloak client includes the audience in access tokens. The mapped value must match STOA_MCP_AUDIENCE exactly:
{
"clientId": "<MCP_CLIENT_ID>",
"protocolMappers": [
{
"name": "audience-mapper",
"protocol": "openid-connect",
"protocolMapper": "oidc-audience-mapper",
"config": {
"included.client.audience": "<MCP_AUDIENCE>",
"access.token.claim": "true"
}
}
]
}
Testingβ
# Decode a token and check the audience
echo $TOKEN | cut -d. -f2 | base64 -d | jq '.aud'
# Should include the value of STOA_MCP_AUDIENCE
Rollbackβ
There is no switch to disable audience validation. If authentication breaks, correct STOA_MCP_AUDIENCE so that it matches the aud claim your identity provider issues, then restart the gateway.
An empty STOA_MCP_AUDIENCE is not a bypass. The gateway treats it as missing configuration: MCP entrypoints reject all authenticated requests, and startup fails when STOA_ENVIRONMENT=prod.
CORS Configuration (CAB-950)β
What It Doesβ
Restricts which origins can make cross-origin requests to the API, preventing CSRF and data exfiltration attacks.
Configurationβ
# Environment variables
CORS_ORIGINS=https://console.stoa.dev,https://portal.stoa.dev
CORS_ALLOW_METHODS=GET,POST,PUT,DELETE,OPTIONS
CORS_ALLOW_HEADERS=Authorization,Content-Type,X-Request-ID,X-Tenant-ID
CORS_EXPOSE_HEADERS=X-Request-ID,X-Trace-ID
CORS_MAX_AGE=600
Settingsβ
| Setting | Type | Default | Description |
|---|---|---|---|
CORS_ORIGINS | string | See below | Comma-separated allowed origins |
CORS_ALLOW_METHODS | string | GET,POST,PUT,DELETE,OPTIONS | Allowed HTTP methods |
CORS_ALLOW_HEADERS | string | Authorization,Content-Type,... | Allowed request headers |
CORS_EXPOSE_HEADERS | string | X-Request-ID,X-Trace-ID | Headers exposed to browser |
CORS_MAX_AGE | int | 600 | Preflight cache duration (seconds) |
Default Originsβ
https://console.stoa.dev
https://portal.stoa.dev
https://console.gostoa.dev
https://portal.gostoa.dev
Local Developmentβ
For local development, add localhost origins:
CORS_ORIGINS=https://console.stoa.dev,http://localhost:3000,http://localhost:5173
Testingβ
# Test CORS preflight
curl -X OPTIONS ${STOA_API_URL}/v1/tools \
-H "Origin: https://evil.com" \
-H "Access-Control-Request-Method: GET" \
-v
# Should return 403 or missing Access-Control-Allow-Origin
Rollbackβ
# Allow all origins (NOT RECOMMENDED!)
export CORS_ORIGINS="*"
Never use CORS_ORIGINS="*" in production. This allows any website to make authenticated requests to your API.
SSE Connection Limits (CAB-939)β
What It Doesβ
Prevents connection exhaustion attacks (Slowloris) by limiting SSE connections per IP, per tenant, and globally.
Configurationβ
# Environment variables
SSE_LIMITER_ENABLED=true
SSE_TRUSTED_PROXIES=10.100.0.0/16 # Your cluster CIDR
Settingsβ
| Setting | Type | Default | Description |
|---|---|---|---|
SSE_LIMITER_ENABLED | bool | true | Enable/disable rate limiting |
SSE_TRUSTED_PROXIES | string | "" | Trusted proxy CIDRs for X-Forwarded-For |
Built-in Limitsβ
These limits are hardcoded but can be adjusted via code:
| Limit | Value | Description |
|---|---|---|
MAX_PER_IP | 10 | Max connections from single IP |
MAX_PER_TENANT | 100 | Max connections per tenant |
MAX_TOTAL | 5000 | Global connection limit |
IDLE_TIMEOUT | 30s | Close idle connections after |
MAX_DURATION | 1h | Maximum connection lifetime |
RATE_LIMIT_PER_MIN | 5 | New connections per IP per minute |
Trusted Proxiesβ
If you're behind a load balancer or ingress controller, you must configure trusted proxies to get accurate client IPs.
# EKS example
SSE_TRUSTED_PROXIES=10.100.0.0/16
# GKE example
SSE_TRUSTED_PROXIES=10.0.0.0/8
# Multiple CIDRs
SSE_TRUSTED_PROXIES=10.100.0.0/16,172.16.0.0/12
If not configured, the limiter will use the direct connection IP (which may be your load balancer).
Testingβ
# Test rate limit (should get 429 on 11th connection)
for i in {1..15}; do
curl -N "${STOA_API_URL}/mcp/sse" \
-H "Authorization: Bearer $TOKEN" &
done
# Check active connections (if you have access)
curl ${STOA_API_URL}/metrics | grep stoa_sse
Response Codesβ
| Code | Meaning | Header |
|---|---|---|
| 429 | Too Many Requests | Retry-After: 60 |
Rollbackβ
# Disable SSE rate limiting
export SSE_LIMITER_ENABLED=false
Container Security (CAB-945)β
Pod Security Standardsβ
STOA enforces the restricted Pod Security Standard on the stoa-system namespace:
apiVersion: v1
kind: Namespace
metadata:
name: stoa-system
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restricted
Deployment Security Contextβ
All STOA deployments use this security context:
spec:
automountServiceAccountToken: false # Unless needed
securityContext:
runAsNonRoot: true
fsGroup: 1000
containers:
- securityContext:
runAsUser: 1000
runAsGroup: 1000
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: [ALL]
seccompProfile:
type: RuntimeDefault
Network Policiesβ
The control plane is isolated with NetworkPolicy:
# Ingress: Only from MCP Gateway and Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: stoa-mcp-gateway
- from:
- namespaceSelector:
matchLabels:
name: ingress-nginx
# Egress: Only to known services
egress:
- to:
- podSelector:
matchLabels:
app: postgresql
- to:
- podSelector:
matchLabels:
app: keycloak
# ... and DNS / OpenBao (Vault API)
Verificationβ
# Check PSS enforcement
kubectl get ns stoa-system -o jsonpath='{.metadata.labels}' | jq
# Check security context
kubectl get pod -n stoa-system -o jsonpath='{.items[0].spec.securityContext}'
# Test NetworkPolicy (should fail from random pod)
kubectl run test --rm -it --image=alpine -- wget -O- http://stoa-control-plane:8080
Monitoringβ
Prometheus Metrics (P1)β
These metrics will be added in a follow-up release:
# SSE active connections
stoa_sse_connections_total{tenant="acme"}
# SSE rejected connections
stoa_sse_rejected_total{reason="ip_limit"}
# JWT validation failures
stoa_jwt_validation_failures_total{reason="invalid_audience"}
Alertsβ
Recommended alerts:
- alert: STOAHighSSERejectionRate
expr: rate(stoa_sse_rejected_total[5m]) > 10
labels:
severity: warning
annotations:
summary: High SSE rejection rate (possible attack)
- alert: STOAJWTValidationFailures
expr: rate(stoa_jwt_validation_failures_total[5m]) > 5
labels:
severity: warning
annotations:
summary: JWT validation failures detected
Troubleshootingβ
JWT Validation Errorsβ
Symptom: 401 Unauthorized on MCP endpoints. The message depends on the path: MCP REST routes (for example /mcp/tools/list, /mcp/tools/call) return "Invalid audience" and the gateway logs Invalid audience: expected <value>; the shared MCP transport handlers (streamable HTTP, SSE, WebSocket) return a generic "Invalid token" and do not log that detail
Solution:
- Check token audience:
echo $TOKEN | cut -d. -f2 | base64 -d | jq '.aud' - Verify Keycloak client mapper configuration
- Confirm
STOA_MCP_AUDIENCEis set and matches theaudclaim exactly (single value, case-sensitive)
CORS Errorsβ
Symptom: Browser console shows "CORS policy" errors
Solution:
- Check origin is in whitelist:
echo $CORS_ORIGINS - Add origin:
CORS_ORIGINS="...,https://your-domain.com" - Check for typos (http vs https)
SSE Rate Limitingβ
Symptom: 429 Too Many Requests with Retry-After: 60
Solution:
- Check if legitimate traffic or attack
- Verify client IP detection:
curl -v ... | grep X-Forwarded-For - Configure trusted proxies:
SSE_TRUSTED_PROXIES=... - Temporarily disable:
SSE_LIMITER_ENABLED=false
Container Securityβ
Symptom: Pod fails to start with "container has runAsNonRoot and image will run as root"
Solution:
- Use non-root base image
- Add
USER 1000to Dockerfile - Set
runAsUserin deployment
Referencesβ
- ADR-018: Security Hardening P0
- OWASP API Security Top 10
- Kubernetes Pod Security Standards
- JWT Best Practices (RFC 8725)
Last updated: 2026-01-25 β Team Coca Security Audit π«