Middle · Network services

Authorization and hostile input

What must a Go API validate beyond successful JSON decoding?

Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.

Reveal a strong answer
  1. Bound body bytes and decoding work, validate required values and reject unsupported fields if the API contract requires it. Validate identifiers and ranges; decoding into a struct does not establish business validity.

  2. Authenticate identity and authorize every target resource, including tenant scope. Do not trust a tenant ID supplied by the client without binding it to the caller's rights. Parameterize database operations and avoid building shell commands from request strings.

  3. Protect secrets in logs, errors and traces. Use TLS and deployment-level controls, and handle replay/idempotency on sensitive operations. Escaping output is specific to its context; HTML, SQL and a shell have different rules.

Common trap

A valid JWT says who a request claims to be; it does not authorize every object ID in the URL.

Try it

A user requests /orders/another-tenant-order. Where should the tenant predicate be enforced?

Worked solution

Include the authorized tenant scope in the authoritative lookup/update predicate and test cross-tenant access. A UI filter or a precheck followed by an unscoped write leaves a security gap.

Change the constraint

How would you test oversized JSON and an expensive but syntactically valid nested payload?

How did you do?

Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.

Not marked. Progress stays on this browser, separate from course completion.

Primary references

Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.