API Input Boundaries: Validation and Sensitive Data
Follow an API integration endpoint through size and type checks, business rules, object authorization, secret storage, and log redaction.
Keep request limits, schema validation, business rules, and object authorization as separate checks. This guide follows an integration endpoint from bounded input through secret storage and a safe response, including what to redact from logs. It links to focused guides on API credentials and object-level permissions.
API Input Boundaries: Validation and Sensitive Data
Requests cross several boundaries before they can change application state. A profile update or integration setup can fail in different ways: the body may be too large, the fields may not match the contract, the caller may lack access to the target account, or a credential may leak through an error log.
Keep those checks separate. The walkthrough below follows one endpoint that registers an external integration for an account.
Boundary rules
- Cap request size and require the expected media type before parsing untrusted bytes.
- Validate the accepted field names and types, then check business rules for the operation.
- Authenticate the caller and authorize access to the specific account or object before a side effect.
- Use parameterized operations for storage and context-aware encoding for output; validation alone does not make data safe for every sink.
- Keep credentials out of URLs, responses, routine logs, and error messages.
These rules overlap in the request path, but they answer different questions. A well-formed value may still be invalid for the account, unauthorized, or unsafe when sent to a database or rendered in a browser.
The OWASP Input Validation Cheat Sheet recommends server-side validation and request-size limits before parsing. For logs, the OWASP Logging Cheat Sheet advises excluding or masking tokens, passwords, keys, and payment data.
Walkthrough: register an integration
Suppose an account administrator calls POST /v1/accounts/{accountId}/integrations. The endpoint accepts a provider name and a credential. The request details below are deliberately omitted; the service should never need to print the submitted credential to explain a failure.
POST /v1/accounts/{accountId}/integrations
Authorization: Bearer [redacted]
Content-Type: application/json
[request body omitted]
| Stage | Check for this endpoint | If the check fails |
|---|---|---|
| Request boundary | Require JSON and enforce byte and read-time limits before parsing | Reject the request without reading an unbounded body or logging its contents |
| Authentication | Verify the caller and derive identity from trusted credentials | Reject the request without looking up account-specific state |
| Shape and type | Accept only documented fields such as provider and credential; validate types, allowed provider values, and field bounds; reject unexpected fields |
Return a stable validation error without echoing the submitted value |
| Object authorization | Resolve the account within the caller’s trusted tenant context and require integration-management permission on that account | Deny access before exposing account-specific state or touching the secret store |
| Business rules | After authorization, confirm that the provider is enabled for the account and that the account can add this integration | Return a documented business error; do not create a partial integration |
| Secret handling | Send the credential to a secret manager using a service identity; persist only its reference in the application record | Do not return success until the secret and reference are accounted for; reconcile an orphaned secret if the metadata write fails |
| Response and logging | Return the integration ID and status, not the credential. Log a request ID, route template, provider, outcome, and stable error code | Redact authorization headers, credentials, request bodies, and exception details that could contain them |
Enforce transport limits before parsing, then authenticate the caller before looking up account-specific state. Check object permissions before exposing business details; only after access is established should you validate account rules and store the credential.
This TypeScript sketch shows the order; helper implementations depend on the framework:
async function registerIntegration(
request: RequestContext,
): Promise<HttpResponse> {
requireJsonContentType(request);
const body = await readBoundedBody(request, MAX_REQUEST_BYTES);
const actor = await authenticate(request);
const input = parseIntegrationInput(body); // Allowlist and validate fields.
const account = await accounts.findForTenant(
actor.tenantId,
request.params.accountId,
);
authorize(actor, "integration:write", account);
validateProviderForAccount(account, input.provider);
const secretRef = await secretStore.write({
accountId: account.id,
provider: input.provider,
value: input.credential,
});
const integration = await integrations.create({
accountId: account.id,
provider: input.provider,
secretRef,
});
auditLog.write({
requestId: request.id,
route: "POST /v1/accounts/{accountId}/integrations",
provider: input.provider,
outcome: "created",
});
return json({ id: integration.id, status: "configured" }, 201);
}
The sketch omits the framework’s error mapping and secret-store recovery details. In a real implementation, account for the boundary between secret storage and the application database: a successful secret write followed by a failed metadata write may leave an orphan that needs cleanup or reconciliation. Never put the secret value in an exception message while handling that failure. If a credential is exposed in logs or an error response, treat it as compromised and rotate it.
Further reading
- API Keys, Sessions, and Service Credentials covers credential types, rotation, and caller identity.
- API Scopes, Roles, and Object-Level Permissions shows how to authorize a caller against a specific resource.
- Secrets management covers secret storage and rotation through the credential lifecycle.
- OWASP Secrets Management Cheat Sheet — Secret storage, access, and rotation.
Conclusion
Walk the request in order: bound and validate it, authorize the target resource, store credentials through a protected path, and log the result without copying sensitive input.
Category
Related Posts
GraphQL Subscriptions: Transports, Security, and Scaling
Choose GraphQL subscription transports, plan authorization and reconnects, and scale event delivery across servers with security and deployment checks.
CSRF, CORS, and SSRF: Defending Web Request Boundaries
Learn how CSRF, CORS, and SSRF differ, then apply cookie, origin, allowlist, and egress controls to protect browser and server request boundaries.
API Authentication vs. Authorization: Identity and Access
Understand API authentication and authorization, how they differ in request handling, and how to avoid common identity and access-control mistakes.