DEV Community

SiegfriedFletcher5869
SiegfriedFletcher5869

Posted on

Marketplace Generated Images: 4 Tenant-Isolation Rules for Object Storage and Signed URLs

The least complex design for a marketplace serving AI-generated reports is a private object store, application-owned keys, and short-lived signed download URLs. Put tenant identity in the database record and in the key prefix, then make every read check both before signing. This keeps a leaked URL narrow and makes cleanup measurable.

The decision table: which storage shape fits your report pipeline?

Shape Pick this when Main trade-off
Private bucket plus app-signed reads Reports are generated asynchronously and customers download them later You operate signing, metadata, and deletion jobs
Database blobs Files are tiny and transactional with a row update Database backups and query paths grow with media bytes
Public CDN objects Content is intentionally public, such as a marketing preview Revocation and tenant isolation are weak
Managed media service Transformations, moderation, and delivery are the product Provider-specific policy and export boundaries need review

For authenticated marketplace reports, the first row is usually the cleanest boundary. Originals and thumbnails stay private. The API decides whether a customer can receive a URL; the storage service only receives a constrained operation.

Start with the policy, not the bucket name. Define an owner tenant, report ID, variant, and expiration class in one record. A key such as tenants/t_42/reports/r_901/thumbs/320.webp is useful for listing and incident response, but it is not an authorization check by itself. The database check remains authoritative.

How should a Node.js app isolate AI images, thumbnails, signed URLs, and lifecycle cleanup?

Think of the request as a four-step line: customer session -> tenant-scoped report row -> object metadata -> signed response. If any link is missing, a customer can guess a key, replay an old URL, or keep a deleted report alive through a cache.

Here is a provider-neutral TypeScript adapter. It assumes the storage implementation can sign a GET with an expiry; the application still performs the tenant and status checks first.

type ReportVariant = "original" | "thumb-320" | "thumb-800";

type ReportAsset = {
  tenantId: string;
  reportId: string;
  variant: ReportVariant;
  objectKey: string;
  state: "ready" | "expired" | "erased";
  expiresAt: Date;
};

type AssetStore = {
  signGet(input: { key: string; expiresInSeconds: number }): Promise<string>;
};

function assertSafeSegment(value: string): void {
  if (!/^[a-zA-Z0-9_-]+$/.test(value)) throw new Error("invalid identifier");
}

async function createDownloadUrl(
  store: AssetStore,
  asset: ReportAsset,
  sessionTenantId: string,
): Promise<string> {
  assertSafeSegment(asset.tenantId);
  assertSafeSegment(asset.reportId);
  if (asset.tenantId !== sessionTenantId) throw new Error("forbidden");
  if (asset.state !== "ready" || asset.expiresAt <= new Date()) {
    throw new Error("asset unavailable");
  }

  // The signed URL is a delivery detail, not a replacement for authorization.
  return store.signGet({ key: asset.objectKey, expiresInSeconds: 300 });
}
Enter fullscreen mode Exit fullscreen mode

The handler should load ReportAsset through a tenant-filtered query, not accept objectKey from the browser. Return Content-Disposition: attachment for a download when the filename must be controlled; MDN documents how that response header separates inline display from attachment behavior. Keep signed query strings out of logs and tracing attributes.

A thumbnail worker writes a new variant only after the original has a verified checksum. Store width, height, content type, source version, and transform version alongside the key. If resizing code changes, the version tells you whether a thumbnail can be regenerated instead of blindly trusting an old object.

Keep one invariant.

I once treated a five-minute URL as the retention policy. It wasn't. The URL expired, but the object and its database row stayed forever because no job owned deletion. The fix was a state transition with an observable deadline: ready -> expired -> erased, plus a sweeper that reports age of the oldest expired asset.

What does lifecycle cleanup need to prove for each tenant?

Cleanup is a control loop, not a cron line. On each pass, select rows whose retention deadline has passed, re-check legal holds and active disputes, mark them expired, delete the original and derivatives, then mark erased with an audit event. A retry must be idempotent: deleting an already-absent object should converge on the same database state. In a marketplace, imagine tenant t_42 asks to erase report r_901 while a resize worker is reading the original. The deletion job should first record an erasure intent and prevent new signed URLs, then let the worker either observe the tombstone before writing or publish a derivative that is immediately swept by the same intent. A periodic reconciliation compares database rows with object listings, flags keys with no live row, and records the last successful scan cursor. That extra pass catches a worker killed after upload but before its database commit; without it, the object has no owner and no expiration clock. The useful alert is not “cleanup ran.” It is “oldest eligible asset is 37 minutes past its deadline,” grouped by tenant and retention class so one noisy customer cannot hide a broader failure.

Measure four things separately: assets eligible for deletion, deletion attempts, confirmed erasures, and lag from eligibility to erasure. Alert on lag and on a growing count of rows without an object checksum. A storage dashboard that shows only total bytes cannot tell you whether tenant isolation or cleanup is failing.

The privacy rule is straightforward: an erasure request must cover derived thumbnails as well as originals. GDPR Article 17 describes the right to erasure, but contracts may require a legal hold. Record the hold reason and reviewer, and make the deletion worker skip held assets until the hold is released. Do not hide that exception in a vendor console.

Operational limits and the choice you should revisit

Private object storage is a poor fit when customers need permanent, anonymously cacheable URLs or when the team cannot run a deletion monitor. Use a public delivery path only for assets with no tenant or account boundary. Database blobs can be reasonable for a few kilobytes, yet they become painful when report images increase backup time and row-level contention.

The catch is operational ownership. You need key-prefix tests, signed URL expiry tests, a cross-tenant access test, and a drill that deletes an original while a thumbnail job is in flight. Managed media tooling may reduce that work, but verify that it can export object metadata, enforce per-tenant policy, and honor erasure holds. Your mileage may vary because report size, retention law, and CDN behavior dominate the byte and request profile.

Keep the rule visible in code review: the browser supplies a report ID, the server supplies the tenant, and storage receives one short-lived operation. That division is more durable than any particular bucket setting.

References

Further reading

Top comments (0)