Loading
Code Deb

Designing Reliable TypeScript SDKs: Validation, Safe Serialization and Tenant Boundaries

Design a reliable TypeScript SDK with bounded runtime validation, JSON serialization, scoped HTTP clients and tenant authorization enforced on the server.

Layered software modules route validated serialized data into separate tenant compartments.

TypeScript can reject an invalid object at compile time while an HTTP API still accepts it at runtime. Even a valid tenant header and a valid record ID do not prove that the authenticated principal is authorized to use that record.

A cross-tenant authorization fixture

We modeled 30 tenants with 10 records each. Each record receives two synthetic read requests: one from its owning tenant and one from a neighboring tenant. The unscoped policy looks up the object without checking its ownership, while the scoped policy permits only matching tenant IDs.

PolicyUnauthorized records returned
ID lookup without tenant check300
Owner-scoped lookup0

The 600-request model contains 300 deliberately unauthorized requests, and all would pass the unscoped rule. This is a policy simulation, not an assessment of any production API. OWASP API1:2023 identifies missing object-level authorization as a major API risk.

Types, validation and authorization are three layers

// Derive tenant scope from a verified session.
const principal = await authenticate(request);
const record = await db.orders.findFirst({
  where: { id: validatedOrderId, tenantId: principal.tenantId }
});
if (!record || !canRead(principal, record)) return notFound();

Type validation rejects malformed data. Authentication establishes the principal. Authorization permits the specific operation on the specific resource. None can be substituted with an organization ID supplied by a client. SDK Core includes validation and scoped client primitives; infrastructure and server authorization remain application-owned.

The serialization boundary can collapse business identities

Our test compares numeric conversions of strings 9007199254740992 and 9007199254740993. They become the same JavaScript numeric value. RFC 8259's JSON interoperability guidance identifies the exact binary64 integer range as plus or minus (2^53 minus 1). For identifiers and amounts outside a chosen exact representation, use validated strings or another explicit interchange contract. Data Bridge research demonstrates collisions across a larger set.

Escaping is contextual, not a universal sanitizer

An encoded JSON string, HTML text node, URL component and XML attribute each have different syntax. Do not escape a value once and reuse it everywhere. Reject unexpected object keys and avoid invoking untrusted getters while traversing objects. In addition, validate the maximum input size, depth and record count before performing expensive normalization. Security logs should follow the projection and redaction experiment.

What an SDK test suite cannot replace

Test the public package from an isolated consumer and cover ESM imports, declarations, invalid inputs and boundary cases. Then separately test the application with independent tenants, read and write operations, unauthorized roles and retries. A successful utility test does not demonstrate correct authentication infrastructure. Webhook signature verification is one example: a mathematically valid signature does not establish authorized business state. Keep package guarantees precise enough that a consumer can tell which obligations remain theirs.

Method and reproducibility: Controlled synthetic model. See Code Deb's recorded experiment results and reproduction script in the repository. Figures are reproducible from declared inputs, not undisclosed client outcomes. The cited external material is independent of Code Deb's experiment.

More from Code Deb

Read about our agency, explore available services, or contact the team for a project discussion.