TL;DR
- Start every service with contract-first design, short-lived tokens, object-level auth, schema validation, rate limits, replay guards, and evidence-by-default.
- Wire small, reliable CI gates and portable policies at edge and service.
- First 30 days, ship 5 changes: API-BOM, JWT
issandaudchecks, ownership checks on money and identity flows, write-route limits with request normalization, webhook signatures with a 5-minute window.
Who this is for, and how to use it
Developers, tech leads, and staff ICs building and running APIs. Use this to scaffold new services, harden legacy ones, and standardize controls across languages. Skim the Quick Wins, then drop the code snippets into your repo. Treat the PR Checklist as the acceptance criteria for every API change.
60-minute Quick Wins
- Add token checks: verify
iss,aud,exp, and signature in your middleware. - Add object ownership: check tenant and subject on every read and write of sensitive resources.
- Block overposting: strict request schemas with
additionalProperties: false. - Turn on limits: soft read limits, stricter write limits, plus request normalization.
- Verify webhooks: HMAC signature, timestamp, five-minute replay window, idempotency keys.
Threat model for builders
- IDOR/BOLA: object authorization missing or inconsistent.
- Mass assignment: extra fields accepted and honored.
- Token misuse: missing
aud/isscheck, long TTLs, reuse across services. - Parser exhaustion: deeply nested or oversized payloads.
- Webhook replay/spoof: no signature, long replay window.
- Version drift: old routes left live, undocumented endpoints.
Fix with contract-first design, strict types, portable policies, and negative tests in CI.
Day-0 service scaffold
Contract (OpenAPI 3.1, REST)
Token validation and ownership (Node, Express)
Strict schema with overposting block (Python, FastAPI)
Webhook signature verify and replay window (Node)
Edge guardrails (NGINX snippet)
Request normalization ideas
- Trim whitespace and normalize casing where safe.
- Sort object keys and de-duplicate array members that should be unique.
- Collapse repeated query parameters.
- Bound array lengths and string sizes.
- Canonicalize time and number formats.
GraphQL hardening
- Persisted queries only.
- Depth and cost limits.
- Field-level authorization for sensitive data.
- Disable introspection in production.
Apollo persisted queries
gRPC best practices
- mTLS for service-to-service.
- Method allowlists at the proxy.
- Deadlines on all calls, max message size.
- Method-level RBAC and context propagation.
Go unary interceptor sketch
CI gates that do not flake (GitHub Actions)
Example negative tests (bash)
Observability for security
Log decisions, not secrets. Add correlation for traceability.
Log shape (JSON)
Detections to wire
- 403 spikes by route and principal.
- Sequential object ID access.
- Tokens reused across services in short windows.
- Schema-violation bursts on a route or version.
- Repeated webhook IDs or timestamp skew.
Secrets management and tokens
- Do not commit secrets. Use your platform’s secret manager.
- Short TTL for access tokens.
- Verify iss, aud, exp on every call.
- Prefer JTI or similar to detect reuse.
- Rotate on incident and on a regular cadence.
Error handling that is safe
- Return minimal messages and standard codes.
- Put details in logs with masking.
- Never echo raw queries, tokens, or secrets.
- Use correlation IDs in responses for support.
API discovery and API-BOM
Track and publish a simple CSV or JSON, then render as a dashboard.
A 90 days plan for developers
30 days
- Contracts for top services, JWT
iss/audchecks, write-route limits + normalization, KPI baseline.
60 days
- Ownership checks on account, payment, password flows.
- Persisted GraphQL queries with depth and cost caps.
- CI neg tests for IDOR, overposting, wrong/expired tokens.
90 days
- Remove dead routes and versions, enforce deprecation calendar.
- Auth and schema error budgets with rollback rules.
- Update the golden service template.
PR checklist (drop into your repo)
- Contract updated and linted.
- Token checks present and tested.
- Ownership check on each sensitive read/write.
- Strict request schemas, no extra fields allowed.
- Limits and normalization configured on writes.
- Webhook signature, timestamp, idempotency where applicable.
- Logs mask PII and include correlation IDs.
- Negative tests added or updated.
- Evidence updated, contract and test results attached.
Introduction to Levo, how it helps developers
Levo adds privacy-preserving runtime visibility and contract validation without moving payloads outside your boundary. Findings become policies and CI tests you can adopt service by service. Pricing stays predictable as services and environments grow, so the secure path remains the easy path for developers.
See how this looks in practice, book a short working session on your two highest risk flows book a demo.
Conclusion
Contracts, portable policies, and small reliable tests make reliability a pipeline property, not a late-night firefight. Ship these guardrails with every service and you will move faster with fewer incidents and cleaner audits.
Related: Learn how Levo is solving the API security issue with it's fix first approach and a product which is scale agnostic, data privacy first and growth immune pricing Levo's API Solution.
FAQs
What is the fastest way to stop BOLA?
Add a shared ownership helper and call it on every sensitive read and write. Add two negative tests per route.
Edge or service for authorization?
Both. Edge for coarse checks and token validity. Service for object ownership and business rules. Keep both as code.
Will rate limits break real users?
Use soft limits for reads, stricter limits for writes. Scope by principal and route. Monitor 429s and tune weekly.
How do I make GraphQL safe without killing flexibility?
Persisted queries only, depth and cost caps, field-level authorization. Disable raw posts in production.
How do I secure gRPC quickly?
mTLS, method allowlists, deadlines, max message size. Add method-level RBAC at the proxy and propagate principal context.
What should I log for investigations?
Correlation ID, route, principal, decision, reason, latency. Mask or tokenize sensitive fields. Short retention for debug logs.
How do I detect IDOR early?
Alert on 403 spikes and sequential ID access. Pair with schema-violation alerts on the same route.
How do I secure webhooks?
HMAC signature with timestamp, five-minute replay window, idempotency keys, last-seen event store per sender.
How do I retrofit a legacy service?
Wrap at the gateway with token validation, schema checks, and limits. Add ownership checks in code next sprint. Put old versions on the deprecation calendar.
How do I avoid flaky CI gates?
Keep tests small with realistic fixtures. Assert on exact contract violations. Run new rules in monitor for a sprint before blocking.
Do I need to validate responses, not just requests?
Yes, response schemas prevent PII leaks and error over-sharing. Start on high-risk routes.
Where do I keep proofs for audits and customers?
Alongside policy code in the repo. Store contract files, test results, configs, and dashboards as artifacts. Automate a weekly export.
What is request normalization in practice?
Canonical field order, de-dup params, trim, lowercase where safe, bound lists and strings. Prevents near-duplicate floods and improves cache hit rates.
Should I rely on third-party SDKs for auth?
Treat them as helpers. Always validate tokens yourself and add ownership checks in your code.
Any tips for partner sandboxes?
Same policies as production with lower thresholds. Seed realistic data. Rotate credentials often. Publish a replay policy.







.png)
.png)
.png)