协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
security-api-security【开发与工程】Use this skill when asked about API security, OWASP API Top 10, rate limiting, API authentication, JWT security, OAuth2, API key management, API gateway, WAF, API abuse, request signing, or API threat modeling. This skill enforces: OWASP API Top 10 threat modeling, authentication/authorization patterns (JWT, OAuth2, API keys), rate limiting with tier…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Design comprehensive API security controls covering OWASP API Top 10 threat modeling, authentication (JWT, OAuth2, API keys), rate limiting (token bucket, sliding window, per-user/per-endpoint), input validation (JSON schema, allowlist), WAF rules (ModSecurity CRS), request signing, and audit logging.
Exact user phrases: "API security", "OWASP API Top 10", "rate limiting", "API auth", "JWT security", "OAuth2", "API gateway", "WAF", "API abuse", "API threat modeling", "API protection", "API authentication", "API authorization", "API keys", "OAuth2 API", "API gateway config", "request signing", "API audit", "GraphQL security".
Before activating, verify:
API security checklist with threat model, protection configuration, monitoring setup.
# Threat model table
# Rate limit policy
# WAF rules
# Auth middleware config
# Audit log schema
No preamble. No postamble. No explanations. No filler/hedging/transitions. Compress output — why use many token when few do trick.
300 lines of configuration and policy.
Map each resource against OWASP API Top 10:
API1: Broken object-level authorization. Check user owns resource via JWT sub claim. Use UUIDs, not sequential IDs.
API2: Broken authentication. Weak JWT, no MFA, no login rate limit. Enforce strong passwords, short-lived tokens.
API3: Excessive data exposure. Return only needed fields per endpoint. Never return password hashes or internal IDs.
API4: Rate limiting. No throttling leads to DoS. Paginate lists, limit body size, rate limit.
API5: Broken function-level auth. Deny by default, test all roles against all endpoints.
API6: Mass assignment. Use DTOs, whitelist updatable fields only.
API7: Security misconfiguration. CORS misconfigured, debug endpoints exposed. Audit configs, disable debug in production.
API8: Injection. SQL, NoSQL, command injection. Parameterized queries, input validation.
API9: Improper asset management. Old API versions running unpatched. Maintain OpenAPI spec inventory.
API10: Unsafe consumption. API consuming compromised external service. Validate external responses, timeout calls.
JWT: RS256 or ES256 asymmetric signing. Access token: 15 min expiry. Refresh token: 7 days with rotation. Claims: sub, iss, aud, exp, iat, jti, scope. Validate signature, expiry, issuer, audience on every request.
OAuth2 flows: Authorization Code + PKCE for SPAs. Client Credentials for M2M. Device grant for CLI tools. Rotate refresh tokens on each use.
API keys: Cryptographically random, prefixed identifier. Hashed at rest with bcrypt. Rate-limited per key with tiered quotas. Revocable with immediate invalidation. Scoped to specific resources and actions.
Ban: basic auth, session cookies, JWT with alg: none.
RBAC: roles assigned to users, permissions assigned to roles. Deny by default, whitelist allowed actions.
ABAC: attribute-based fine-grained access. Check resource owner, region, tier, time.
API gateway: validate JWT, extract scopes. Pass claims upstream via request headers.
Service layer: check resource ownership.
userId == resource.ownerId.
Validate actions against role matrix.
Policy-as-code: OPA for cross-cutting authorization. Organization-level and multi-resource policies.
Test: every role against every endpoint in CI. Automated authorization fuzzing. Regression test for each access control change.
Algorithms: Sliding window log: most accurate, most memory. Sliding window counter: accurate, efficient (default). Token bucket: allows bursts, easy to understand. Fixed window: simple but edge case bursts.
Per client: by API key, IP, or JWT sub claim. Tiers: free (10/min), basic (100/min), premium (1000/min). Burst: 2x rate with 503 on sustained overage.
Per endpoint: login (5/min), search (30/min), export (2/min). Global: 10000 req/min per gateway instance.
Response: 429 Too Many Requests. Headers: Retry-After, X-RateLimit-Limit, X-RateLimit-Remaining.
Distributed: Redis-based counter with consistent hashing.
Validation rules:
Reject: SQL injection patterns, NoSQL operators ($where, $gt). XML external entities, path traversal (../). Oversized payloads, parameter pollution.
WAF: OWASP ModSecurity CRS. Paranoia level 1 for production (low false positives). Paranoia level 4 for testing (maximum coverage).
API-specific WAF rules: Block requests without Accept header. Enforce Content-Type for POST/PUT. Reject oversized payloads. Block parameter pollution. Rate limit by endpoint pattern. Geo-block for admin endpoints.
For critical operations: payment, data deletion, config changes.
HMAC-based signing: Client signs method + URI + body hash + timestamp. Uses HMAC-SHA256 with shared secret. Server recomputes and compares. Reject if timestamp drift exceeds 5 minutes.
Prevents replay attacks, tampering, unauthorized source. Middleware at gateway or service mesh sidecar. Reference pattern: AWS Signature V4.
Audit events: Auth decisions (success, failure, reason). Privilege escalation. Sensitive data access. Configuration changes. API key creation and revocation. Rate limit breaches. WAF rule triggers.
Schema: timestamp, eventType, userId, clientIp, resource, action, statusCode, userAgent, requestId.
Storage: append-only immutable log. 90 days hot retention, 1 year cold.
Alerts:
Dashboard: auth failure heatmap, rate limit hits, top abused endpoints, WAF trigger trends.
const jwt = require('jsonwebtoken');
function authenticateJWT(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Missing token' });
}
const token = authHeader.split(' ')[1];
try {
const decoded = jwt.verify(token, process.env.JWT_PUBLIC_KEY, {
algorithms: ['RS256'],
issuer: 'https://auth.example.com',
audience: 'api.example.com',
});
req.user = decoded;
next();
} catch (err) {
return res.status(403).json({ error: 'Invalid token' });
}
}
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
limiter = Limiter(key_func=get_remote_address)
@app.get("/api/resource")
@limiter.limit("100/minute")
async def get_resource(request: Request):
return {"data": "resource"}
@app.post("/api/login")
@limiter.limit("5/minute")
async def login(request: Request):
return {"token": "..."}
func jwtMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenStr := extractBearerToken(r)
if tokenStr == "" {
http.Error(w, "Missing token", http.StatusUnauthorized)
return
}
claims := &Claims{}
token, err := jwt.ParseWithClaims(tokenStr, claims, keyFunc)
if err != nil || !token.Valid {
http.Error(w, "Invalid token", http.StatusForbidden)
return
}
ctx := context.WithValue(r.Context(), "user", claims)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
from pydantic import BaseModel, Field, EmailStr
from typing import Optional
class CreateUserRequest(BaseModel):
email: EmailStr
name: str = Field(..., min_length=1, max_length=100)
age: int = Field(..., ge=0, le=150)
role: str = Field(..., pattern="^(admin|user|viewer)quot;)
@app.post("/api/users")
async def create_user(user: CreateUserRequest):
return {"id": uuid4(), **user.model_dump()}
JWT library configured to accept alg: none allows attackers to forge arbitrary tokens. Always enforce a specific algorithm (RS256 or ES256). Never accept alg: none. Validate algorithm on every token parse.
Using auto-increment IDs (1, 2, 3, ...) in URLs enables enumeration attacks: GET /api/users/1, /api/users/2. Use UUID v4 or ULID for all resource identifiers. Enforce object-level authorization checks.
Setting Access-Control-Allow-Origin: * or reflecting the Origin header without validation allows any website to make cross-origin requests. Restrict to specific origins. Do not allow credentials with wildcard origins.
Rate limiting implemented in application code can be bypassed by attacking the application server directly. Enforce rate limits at the API gateway or reverse proxy level (Nginx, Kong, Envoy). Application-level limits are defense-in-depth.
Detailed error messages and stack traces in API responses reveal implementation details, library versions, and code paths. Always return generic error messages to clients. Log detailed errors server-side.
Changing API behavior without versioning breaks existing clients. Use URL prefix (/api/v1/, /api/v2/) or header-based versioning (Accept: application/vnd.api+json;version=2). Maintain deprecated versions with sunset headers.
Health check, metrics, and admin endpoints exposed without authentication. /actuator, /health, /metrics, /swagger-ui.html leak information. Secure all endpoints including operational ones.
security-sast-dast for API-specific DAST scanning
backend-api-design for endpoint design and versioning
` interface EventObserver { onEvent(event: T): Promise; }
class EventBus { private observers: Set<EventObserver> = new Set(); subscribe(observer: EventObserver): void { this.observers.add(observer); } unsubscribe(observer: EventObserver): void { this.observers.delete(observer); } async emit(event: T): Promise { const results = Array.from(this.observers).map(o => o.onEvent(event)); await Promise.allSettled(results); } } `
config: defaults: timeout: 30s retryCount: 3 overrides: production: timeout: 60s retryCount: 5 development: timeout: 300s retryCount: 1
| Metric | Threshold | Severity | Action |
|---|---|---|---|
| Error rate | > 1% over 5min | Critical | Page on-call |
| p99 latency | > 2s over 5min | Warning | Investigate |
| Throughput drop | > 50% over 1min | Critical | Check upstream |
| Queue depth | > 1000 over 1min | Warning | Scale consumers |
| Disk usage | > 85% | Warning | Clean or expand |
| Memory usage | > 90% heap | Critical | Restart or scale |
| Anti-Pattern | Symptom | Root Cause | Solution |
|---|---|---|---|
| Premature optimization | Complex code for no measured benefit | Guessing instead of profiling | Measure first, optimize based on data |
| Copy-paste reuse | Duplicate code across codebase | Lack of abstraction | Extract shared logic into libraries |
| Gold-plating | Features with no current requirement | Over-engineering | YAGNI — build what's needed now |
| Magical thinking | Assumptions without validation | Skipping error handling | Handle all failure modes explicitly |
Cache hierarchy: L1 (in-memory local) → L2 (distributed Redis/Memcached) → L3 (CDN/Edge). Cache invalidation: TTL-based (simple, stale), event-based (complex, fresh), write-through (consistent, higher write latency), write-behind (fast writes, eventual consistency).