Security for Engineers/API & Service Security

Injection, SSRF & Input Validation

SQL/NoSQL/command injection prevention, SSRF in URL fetchers, prototype pollution awareness, and validation at the boundary.

3/5Overview: 30m

SQL injection

Untrusted input concatenated into SQL:

-- NEVER "SELECT * FROM users WHERE id = " + userInput

Fix: parameterized queries / prepared statements always.

ORM doesn't save you if you use raw queries or sort from user input unsanitized.

NoSQL injection

MongoDB: {"$gt": ""} in JSON body bypasses password check.

Validate types; use schema validation; don't pass user objects directly to query builders.

Command injection

os.system("convert " + filename) — filename ; rm -rf /

Use library APIs; never shell with user input.

SSRF — server-side request forgery

Your server fetches user-supplied URL → attacker hits internal metadata (169.254.169.254), internal admin panels.

Mitigations:

  • Allowlist domains (weak alone)
  • Block private IP ranges (RFC1918, link-local)
  • Egress firewall / network policies
  • No redirects to internal hosts

Common in: webhooks, PDF renderers, image proxies, CI "clone repo" features.

Input validation at boundary

  • Schema validation — JSON Schema, protobuf, Zod at API edge
  • Size limits — max body, max array length, max recursion
  • Type coercion — reject don't silently cast

Validate early; business rules deeper.

Prototype pollution (JS)

merge(userInput, defaults) — attacker sets __proto__.

Use safe merge libraries; Object.create(null) for maps; validate keys.

Mass assignment

User.update(request.json) — attacker sets is_admin: true.

Allowlist updatable fields explicitly.

Cross-reference: LLD → API & Component Design for validation placement in class design.

Further Reading

Hands-On Tasks (Optional)

Security design drills — threat modeling, auth flows, and incident playbooks. Assumes Networking (TLS) fundamentals.

  • Secure a URL preview feature

    User submits URL; server fetches title. Block SSRF to 169.254.169.254, internal DNS, file://. Describe allowlist vs blocklist approach.

    15m