Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 6 min read

Tenant Isolation for Browser-Based Thumbnail Upload CORS Errors: Storage Boundaries

Short answer: use a short-lived, tenant-scoped signed upload and a narrow CORS policy for the exact browser origin, method, and headers; keep object storage private and let a worker resize the accepted object. Start wit

Short answer: use a short-lived, tenant-scoped signed upload and a narrow CORS policy for the exact browser origin, method, and headers; keep object storage private and let a worker resize the accepted object.

Start with the origin.

The browser is not your authorization layer.

A browser thumbnail upload should take the shortest safe path: the application authorizes a small, tenant-scoped upload, the browser sends bytes directly to object storage, and a worker creates the resized image. When CORS fails, the fix is usually a boundary mismatch, not a missing magic header.

The least complex design is a short-lived signed upload URL plus an explicit CORS rule for the exact application origins and methods. Keep the bucket private. Treat the upload token as a capability, and make the object key carry the tenant boundary. This avoids proxying media through the API while keeping authorization in the application where tenant context exists.

Choice CORS surface Isolation property Operational cost
API proxies bytes Same-origin from the browser Strong central enforcement API bandwidth and timeout pressure
Signed direct upload Storage origin must answer CORS preflight Scope can be tenant, object, method, and expiry Requires careful policy and diagnostics
Public upload endpoint Broad and easy to test Weak default boundary Cleanup and abuse controls become central

My default is the middle row. It has one more moving part, but it keeps large media away from the application process. The important qualification is that a signed URL is authorization to one operation, not permission to browse a tenant's objects.

Why do browser-based thumbnail uploads trigger CORS errors?

CORS is enforced by the browser. A server can receive the request and return a successful status while the browser refuses to expose the response to JavaScript. That distinction wastes time. Check the network panel, not only the application error.

A thumbnail upload often triggers an OPTIONS preflight because the request uses a method or header outside the browser's simple-request rules. The storage response needs an Access-Control-Allow-Origin value matching the requesting origin, an allowed method matching the actual upload method, and allowed headers covering the headers the browser asked to send. The preflight response also needs to be successful enough for the browser to continue.

The useful debugging split is mechanical. First capture the browser's Origin, the requested method, and the complete Access-Control-Request-Headers value. Then inspect the preflight response before looking at the signed URL. If the response omits one requested header, the browser stops before the upload reaches the object. If the upload reaches storage but the later thumbnail read is blocked, the GET policy is incomplete even though PUT was correct. This sequence separates policy mismatch, signature scope, and media validation; collapsing them into one β€œCORS error” makes tenant bugs harder to see.

Do not use a wildcard origin for a credentialed request. Do not assume that allowing PUT also permits a later GET used to display the thumbnail. These are separate browser operations. Origin is the useful clue; the URL in the address bar is not.

The failure I look for first is policy drift between environments. A preview deployment has a different origin from production. The signed URL is generated correctly, but the storage CORS rule only names the production origin. The upload works in one tab and fails in another. That is a configuration difference, not evidence that the signature is broken.

Which boundary should own tenant isolation?

The API should decide who may upload. Storage should enforce what the issued capability can do. Put an opaque tenant identifier in the object key, keep the key server-generated, and bind the intended content type and object size to the authorization record. A client-supplied filename is metadata, not an access-control decision.

For example, the application can return a signed operation shaped like this. The endpoint is intentionally generic; the policy details belong to the storage implementation and its signing library.

type UploadGrant = {
  url: string;
  objectKey: string;
  method: "PUT";
  expiresAt: string;
  requiredHeaders: Record<string, string>;
};

async function createThumbnailGrant(
  tenantId: string,
  contentType: string,
  byteLength: number,
): Promise<UploadGrant> {
  if (!/^image\/(jpeg|png|webp)$/.test(contentType)) {
    throw new Error("Unsupported thumbnail type");
  }
  if (byteLength < 1 || byteLength > 5_000_000) {
    throw new Error("Thumbnail size is outside the allowed range");
  }

  const objectKey = `tenants/${tenantId}/incoming/${crypto.randomUUID()}`;
  return signSingleObjectUpload({
    objectKey,
    method: "PUT",
    contentType,
    contentLength: byteLength,
    expiresInSeconds: 300,
  });
}

The 5 MB ceiling here is an application decision for thumbnails, not a storage limit. It makes accidental video uploads fail early and gives the worker a bounded input. The five-minute expiry is also a policy choice. Measure the upload path and choose a window that covers normal clients without leaving a capability valid for an afternoon.

A worker should validate the object again after upload. Browser-provided content type is a hint. Inspect the file, reject unsupported formats, and write the derivative into a separate tenant-scoped prefix. Do not let a successful CORS exchange become a successful media ingestion decision.

How do I debug the preflight without guessing?

Reproduce the browser's preflight with a small request that includes the same origin, method, and requested headers. The point is to inspect the response, not to prove that a command-line client can upload. A command-line client does not enforce browser CORS.

curl -i -X OPTIONS 'https://objects.example.test/tenant-object' \
  -H 'Origin: https://app.example.test' \
  -H 'Access-Control-Request-Method: PUT' \
  -H 'Access-Control-Request-Headers: content-type,x-upload-token'

Record four things: the Origin you sent, the requested method, every requested header, and the response status plus CORS headers. Then compare them with the actual upload request. A policy that allows content-type but not x-upload-token is still a failure. So is a rule that answers OPTIONS but omits Access-Control-Allow-Methods: PUT.

Test the complete sequence in a clean browser context: grant creation, preflight, upload, thumbnail read, and worker completion. Test at least the production origin and one preview origin. Include an expired grant and an object key from another tenant. The expected result is not merely β€œthe upload works”; it is β€œthe browser can perform the intended operation and no other tenant operation is granted.”

Log request correlation IDs, tenant IDs, object keys after redaction, origin, method, and policy version. Never log the signed URL query string. It can contain the capability you are trying to protect.

When is proxying the better runner-up?

A direct upload is a poor fit when the application must transform every byte before storage, when the storage endpoint cannot be given an acceptable CORS policy, or when compliance controls require the API to inspect the stream synchronously. Proxying costs application bandwidth and can make retries expensive, but it gives one origin and one authorization boundary.

That is the trade-off. Direct upload reduces byte handling in the API, while proxying reduces the number of browser-visible boundaries. Pick based on tenant isolation and inspection requirements, not on which configuration file looks shorter.

CORS should be narrow: known origins, required methods, required headers, and the smallest useful exposed-response set. Keep private objects private. Store the policy as versioned deployment configuration, and exercise it in an integration test that uses a real browser. A green unit test for the signer cannot detect a missing preflight header.

The decision rule is simple: authorize in the API, constrain in the signed capability, and verify at the storage boundary. That gives thumbnail uploads a direct path without turning CORS into a tenant boundary by accident.

Further reading

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.