Dev.to WebDev šŸ›  Dev šŸ‘ 1 šŸ“– 6 min read

Why Private Avatar URLs Expire: Public URLs, Signed Downloads, and Tenant Access

Short answer: a private object storage bucket should not hand out a permanent anonymous avatar URL. Keep the object key in your database, authorize the viewer in your application, and mint a short-lived signed download U

Short answer: a private object storage bucket should not hand out a permanent anonymous avatar URL. Keep the object key in your database, authorize the viewer in your application, and mint a short-lived signed download URL when the avatar is requested; use a separate public delivery path only for images that are deliberately public.

This is an access-control decision before it is a URL-format decision. A logistics app might store tenants/acme/users/42/avatar.webp, then let an authenticated dispatcher view it. The same app might expose a public tracking page. Those two viewers should not receive the same kind of link, even if the pixels are identical.

Why does a private avatar image public URL stop working?

A public URL identifies a resource that an anonymous client can fetch. A signed download URL identifies a resource plus a temporary authorization to fetch it. The signature and its expiry are the security boundary, so expiration is expected behavior, not evidence that the upload disappeared.

The common mistake is saving the signed URL in the user record. Save the stable object key instead. A profile response can contain a freshly generated avatarUrl, while the database continues to contain only the key and its tenant ownership. That keeps a disposable permission out of durable application state.

There is another browser rule in the mix. Cross-Origin Resource Sharing (CORS) tells a browser which origins may read a response; it does not make a private object publicly authorized. If a request is rejected before authorization, changing Access-Control-Allow-Origin will not fix it. If authorization succeeds but browser JavaScript cannot inspect the response, then CORS is worth investigating. The two failures look similar from a frontend console, but they belong to different layers.

That distinction saves time.

Keep it private.

How should private object storage signed download URLs serve tenant avatars?

The request flow should be boring: the client asks the application for an avatar, the application authenticates the viewer, checks the viewer's tenant and relationship to the user, asks the storage layer for a signed GET, and returns the URL. The browser then uses that URL as the image source. Storage credentials stay on the server.

Here is a TypeScript boundary that leaves storage-specific signing behind an interface. It does not persist the returned URL, and it refuses to sign a key that is not owned by the requesting tenant.

type AvatarRecord = {
  tenantId: string;
  userId: string;
  objectKey: string;
};

type SignedDownload = {
  url: string;
  expiresAt: string;
};

interface PrivateObjectStore {
  signGet(input: { key: string; expiresInSeconds: number }): Promise<SignedDownload>;
}

async function getAvatarUrl(
  viewer: { tenantId: string; canViewUsers: boolean },
  avatar: AvatarRecord | undefined,
  store: PrivateObjectStore,
): Promise<SignedDownload | null> {
  if (!avatar || !viewer.canViewUsers || avatar.tenantId !== viewer.tenantId) {
    return null;
  }

  return store.signGet({
    key: avatar.objectKey,
    expiresInSeconds: 300,
  });
}

The application endpoint can return the result of getAvatarUrl without changing the stored record. When the URL expires, the client asks for the avatar again. A page that can remain open for hours should renew before expiry or handle an image-load failure by requesting a new URL once. Do not put a storage secret in frontend code, and do not make the signing lifetime enormous just to avoid a refresh; a copied URL is still a bearer credential until it expires.

The exact lifetime is a product choice. Five minutes may be reasonable for a dispatcher console, while a background export may need a different handoff. I’m not sure there is a universal number: the right value depends on how sensitive the image is, how often the page refreshes, and how quickly access must be revoked.

What should public URLs and signed downloads mean in an avatar system?

Use a stable public URL when anonymous delivery is intentional and operationally supported. Use a signed URL when the application must decide who can read the object. The name of the field should make that distinction visible: publicAvatarUrl and signedAvatarUrl are different contracts, not interchangeable aliases.

Delivery contract Authorization check Store in durable profile data? Typical surface
Public URL Publication policy The stable URL can be stored Public tracking or profile page
Signed download URL Viewer identity and tenant membership No; store the object key Authenticated dashboard
Application image endpoint Viewer or public application policy Store the object key A product that needs one controlled URL

For a public tracking page, one option is to publish a selected derivative through a delivery layer and retain the original under a private key. For an authenticated dashboard, the application-mediated signed download is usually the clearer boundary. A CDN can improve delivery, but it does not replace tenant authorization; its cache key, invalidation behavior, and origin policy still need review.

The catch is that signed links are inconvenient for places that expect one durable string, such as an email that may be opened much later or a public profile indexed by a crawler. In those cases, private signed delivery is not suitable. Choose a deliberately public publication path, or serve the image through an application endpoint that enforces its own public policy. Do not quietly weaken a private bucket because a frontend component wants a permanent URL.

What fails in production, and how can it be tested?

Start with tenant isolation tests. A user from tenant A must not obtain a signed key for tenant B, even when the object key is guessed. Test missing records, revoked membership, and a user who can view the profile but not its private documents. The response should not reveal whether another tenant's key exists.

Then test time. Freeze the application clock around expiry, verify that a new request returns a new signed URL, and make sure an old URL is never copied back into the database. Test browser behavior separately: an unauthorized response, a successful response blocked from JavaScript by CORS, and a successful <img> load are three different observations.

One production bug is often a cache bug wearing a storage costume. A service worker, browser cache, API cache, or serialized client state may keep the old URL after the server has correctly issued a new one. For example, a dispatcher opens a tenant profile at 09:00 and the client stores the five-minute URL in a global user cache; at 09:06, the API can be perfectly healthy while the image element still points at the expired value. If the refresh path then writes that stale value back into profile state, every later screen inherits the same failure. Keep the signed value out of long-lived profile records, choose cache headers that match the permission lifetime, and log the object key plus tenant identifier without logging the full signed query string. When investigating, compare the URL's expiry with the client cache timestamp and the authorization decision separately, because a fresh signature cannot repair a wrong tenant check and a correct tenant check cannot repair a stale browser value.

For a concrete rollout, first inventory every place that reads or serializes avatars. Add an authorization test before changing the response shape. Ship the server field and client refresh behavior together. Watch expiry-related responses and cross-tenant denial tests in deployment checks. Finally, document which surfaces are public and which require a viewer identity; that sentence is more valuable than a bucket name hidden in infrastructure code.

References

šŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.