DEV Community

Private Avatar Storage: Signed URL vs Public CDN for Auth App Profile Images

Short answer: keep auth app user profile images private and issue short-lived signed URLs for authenticated reads; use a public CDN URL only when the image is intentionally public and must be fetched without a session.

For a marketplace with per-tenant data, that boundary matters more than a small difference in delivery convenience. Store an object key, not a URL, on the user record. That reference can outlive an individual delivery URL.

A signed URL is a temporary capability that the application can mint again, and a public CDN URL is a distribution decision that should not quietly become an authorization decision.

This is the design I would ship first for a user avatar or file-upload flow. It keeps authorization in the app, keeps storage credentials out of the browser, and leaves room to change the delivery layer later. The example below deliberately uses a provider-neutral interface: the important contract is what the application is allowed to ask for, not a vendor-specific SDK.

Start with the avatar object's lifecycle

An avatar is created, replaced, displayed, and eventually deleted. Design those transitions before choosing a delivery URL.

For an account, tenant, or admin area, private storage plus signed reads gives each transition an explicit authorization point: the application checks the session and tenant, then grants time-limited access to one object. A presigned URL is a temporary capability, not the user's durable identity.

Choose a public CDN URL when the product has made a different promise: anyone may fetch the image, crawlers may fetch it later, and revoking one viewer's access is not a requirement. Public delivery is a good fit for an intentionally open seller directory or a shareable public profile. It is a poor default for an account avatar merely because a CDN is easy to put in an img element.

The distinction is easy to lose. A URL in a database looks permanent even when it expires, while a public URL looks harmless even when it gets copied into logs, email, browser history, or another page. Decide the exposure model before tuning cache headers.

Keep the key, not the URL

The implementation should make authorization unavoidable.

The request path is straightforward: authenticate the session, resolve the tenant and user, load the stored object key, authorize the read, then ask the storage adapter for a short-lived URL. Uploads follow the same ownership check and write a new key before updating the user record.

type AvatarRef = { tenantId : string ; userId : string ; objectKey : string ; contentType : string ; } ;
type Storage = { put ( input : { key : string ; body : Uint8Array ; contentType : string ; } ) : Promise < void > ; signRead ( input : { key : string ; expiresInSeconds : number } ) : Promise < string > ; remove ( input : { key : string } ) : Promise < void > ; } ;
function avatarKey ( tenantId : string , userId : string , version : string ) : string { return `tenants/${tenantId}/users/${userId}/avatars/${version}` ; }
async function avatarUrl ( storage : Storage , session : { tenantId : string ; userId : string } , avatar : AvatarRef | null , ) : Promise < string | null > { if ( ! avatar || avatar . tenantId !== session . tenantId ) return null ; if ( avatar . userId !== session . userId ) return null ; return storage . signRead ( { key : avatar . objectKey , expiresInSeconds : 300 , } ) ; }

The five-minute value is an application policy example, not a universal answer. A profile page that stays open for an hour may need a refresh path; a highly sensitive admin image may need a shorter window. I’m not sure which duration is right without knowing the session lifetime, threat model, and cache behavior, so I would test that decision instead of hiding it inside a storage helper.

The application should validate the uploaded content, constrain size, set the intended content type, and generate an unguessable versioned key. It should not trust a filename as an ownership identifier.

A database transaction or an explicit cleanup job should handle the old object after a successful replacement, because changing the row and deleting the previous bytes are separate operations.

How should signed URLs and public CDNs differ for auth app profile images?

Signed URLs are capabilities, so leakage has a bounded lifetime if the expiry is short. They are also awkward for broad public caching: two signatures for the same object may be different URLs, and a cache policy that ignores the query string can accidentally blur the access boundary. Treat the signature and its query parameters as part of the security contract.

Public CDN URLs have the opposite operational shape. They are simple to embed and cache, but anyone who obtains the URL can request the object for as long as the object is public. Removing access may require invalidation, renaming, or deleting the object, and none of those actions should be confused with checking the current user's session.

Requirement Private object plus signed URL Public CDN URL
Viewer must have an authenticated session Strong fit Wrong default
Anyone may fetch the image Requires a public delivery layer Strong fit
Per-tenant authorization Check before signing Must be enforced before publication
Immediate revocation Shorten expiry and remove access Requires a public-cache and object removal plan
Stable URL in a user record Store the object key instead Public URL can be stable, but is durable exposure

Consider a seller changing an avatar while a buyer has the seller page open. The database should point to the new version only after the new bytes have been accepted, and a subsequent authorized read should sign that new object. The old object then needs a deliberate cleanup path; deleting it before the record update creates a broken image, while leaving it forever creates storage and retention debt. If the page uses an application image route, its cache must also distinguish a new version from the old one.

This is why a versioned object key and a short-lived read are a better starting contract than a single mutable public URL: the replacement, authorization check, and eventual deletion remain independently observable.

There is a useful middle option for some products: keep the origin private and expose an application-controlled image route or image service. That route can enforce tenant authorization and then stream or redirect to a signed read. It costs an extra hop and needs careful caching, but it preserves a stable application URL without making the origin object public.

Three words describe the choice: exposure, expiry, revocation.

Test the policy, not just the upload form

The most common mistake is checking that a user is logged in but failing to check that the object belongs to the same tenant. Test a valid tenant, a valid user in another tenant, a deleted avatar, and an old signed URL after replacement. A 404 for a missing object should not turn into a new signed URL for a key the session does not own.

This is the part I would put in CI before spending time on image resizing or CDN tuning, because a polished upload form cannot repair an authorization mistake.

Test browser caching too. A private image can remain visible in a device cache after the server would deny a new request; the product's threat model should say whether that is acceptable. Test expiry during a long-lived page session and define whether the client refreshes the URL or shows a placeholder.

Upload tests should cover an incorrect MIME type, an oversized body, a duplicated filename, and a failed storage write before the database update. Restore and deletion tests matter for marketplace tenants as well: tenant removal must have a defined policy for avatar objects, old versions, and backups rather than relying on an orphan scan that nobody monitors.

The catch is that private signed delivery is not suitable when the image must be permanently reachable by unauthenticated clients. Use a public delivery design for that requirement. Conversely, stick with private storage when the image is an account artifact, even if a public URL would make the first demo faster.

When is public delivery the right contract?

Start with the narrowest audience. If the answer is β€œthe signed-in user and authorized members of that tenant,” persist an object key and sign each read after authorization. If the answer is β€œanyone with a browser,” use a deliberately public delivery path and document that copying the URL is allowed.

  • Do not put expiring URLs in durable user records.
  • Do not use a CDN cache rule to compensate for a missing authorization check.
  • Do not make a public avatar the default and hope that obscurity will act like access control.

For a Python AI builder shipping marketplace features, this keeps image handling boring enough to maintain while the rest of the product changes quickly.

The larger architectural win is the separation: the database owns identity, the application owns policy, and the storage layer owns bytes. That separation makes the eventual CDN, origin, or storage-provider choice a bounded migration instead of a rewrite of user authorization.

References

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.