skip to content

In Cloud Storage for Firebase, what does a URL from getDownloadUrl() grant, how do Storage Security Rules apply to it, and how is it revoked?

level: middleimportance: should knowfreq 45%

answer

  1. the URL carries its own credential
  2. token query parameter
  3. rules run when the URL is requested
  4. long-lived until revoked
  5. revoke in the Firebase console

basics

~20 s

It is a long-lived HTTPS link with a token that lets anyone holding it read the file without signing in. Storage rules check the app's getDownloadUrl() call, not each fetch, and revoking the token in the Firebase console kills the URL.

solid answer

~50 s

`getDownloadUrl()` reads the object's metadata, which includes its download tokens, and builds the object's HTTPS address with `alt=media` and a `token` query parameter. That request is made as the signed-in user, so Storage Security Rules decide whether the app may obtain the URL at all. Fetching the URL afterwards needs no Firebase identity: the token is the credential, so anyone who has the link can read the file, and the URL has no built-in expiry. The SDK documents it as a long-lived URL with a revocable token that a developer can revoke in the Firebase console; revoking it breaks every copy of URLs built from that token. So for a public recipe photo, storing the URL in the database is convenient; for a private one, store the object path and read it through the SDK, where each read is checked against the rules.

go deeper

for a junior

Recall that getDownloadUrl() returns a shareable HTTPS link with a token, and that anyone holding the link can open the file.

for a middle

Explain the split: the rules check the app's request for the URL, the token authorises every later fetch, and revocation in the console is what ends it.

for a senior

Choose per file type between storing URLs and storing paths, and know the incident move when a private file's link leaks: revoke the token or delete the object, not just edit the rules.

for a principal

Set the team rule for which content may ever be shared by download URL, and make revocation part of the moderation and account-deletion runbooks rather than an afterthought.

## What getDownloadUrl() returns In **Cloud Storage for Firebase**, every object can carry one or more **download tokens** in its metadata. When an Android app calls `getDownloadUrl()` on a `StorageReference` (the `downloadUrl` property in Kotlin), the SDK: 1. Sends a metadata request for the object, authenticated as the current Firebase user. 2. Reads the `downloadTokens` field from the response, which can hold a comma-separated list, and takes the first token. 3. Builds the object's Firebase Storage HTTPS address and appends `alt=media` and `token=<that token>`. The result is a plain `https://` URL. The Android SDK's own documentation calls it "a long lived download URL with a revokable token" that "can be used to share the file with others". You can hand it to an image loader, put it in an `<img>` tag on the web companion, or send it to another user. If the object does not exist, the metadata request fails and the task fails with `StorageException.ERROR_OBJECT_NOT_FOUND`. The call is also not allowed on the bucket root. ## Where Storage Security Rules apply This is the part candidates most often get backwards. - **Getting the URL is a read.** The metadata request in step 1 carries the user's Firebase Authentication credentials, so the Storage rules for that path must allow the read. A user who may not read the file gets `ERROR_NOT_AUTHORIZED` instead of a URL. - **Using the URL is not checked against the user.** A later `GET` on the URL carries no Firebase identity. The token in the query string is what authorises it, which is exactly why the URL can be shared with clients that never signed in. - **SDK reads are checked every time.** `getBytes()`, `getStream()` and `getFile()` each make an authenticated request, so each one is evaluated against the rules for the current user at that moment. | Access path | Who is checked | Survives a rules change? | |---|---|---| | SDK read (`getBytes`, `getFile`, `getStream`) | the signed-in user, on every request | no, the next read sees the new rules | | Requesting the URL (`getDownloadUrl`) | the signed-in user, once | not applicable | | Fetching an existing download URL | nobody; the token is enough | yes, until the token is revoked | The practical consequence: tightening the rules after a URL has been handed out does not stop that URL from working. ## Lifetime and revocation A download URL has no expiry of its own. Unlike an ID token it does not lapse after an hour, and unlike a signed URL on the underlying Google Cloud bucket (a separate subject) it carries no time limit you chose. It stops working when: - the token is **revoked**, which a developer can do for the file in the Firebase console; - the object is deleted. Revoking acts on the token, so every copy of every URL built from it fails at once, including copies stored in a database, cached by an image loader or pasted into a chat. Anything that stored the old URL has to request a fresh one, and that new request passes through the rules again. ## Choosing between a URL and a path For the recipe-sharing app, the choice follows from who should see the photo. - **Public recipe photos.** Storing the download URL next to the recipe document is reasonable. Every client, including the web companion, can render it without an extra round trip, and a leaked link exposes nothing that was not already public. - **Private drafts or profile documents.** Store the object **path** (`drafts/u42/p7.jpg`) and read it through the SDK, so each read is evaluated for the current user. A URL stored in a database is only as private as everyone who ever saw that database row. - **Moderation.** When a photo is reported, deleting the object or revoking its token is what removes access; editing the rules is not enough for links already in circulation. ## Answers that lose points - "The URL only works for the user who requested it." The token is not bound to a user. - "It expires like an ID token." It has no expiry; only revocation or deletion ends it. - "Rules are evaluated when someone opens the link." They were evaluated when the app asked for the link. - "Strict database rules keep stored URLs private." They protect the row, not the link once read.

  • A reported photo must disappear at once, but its download URL is cached in hundreds of clients. What do you do?
    Revoke the file's download token in the Firebase console, or delete the object. Either way, every copy of URLs built from that token fails, wherever it was cached. Changing the Storage Security Rules would not help, because fetching an existing download URL is authorised by its token, not by the rules. Clients that still need the file must request a fresh URL, which goes through the rules again.
  • When would you store the object path instead of the download URL in your database?
    When the file is private or access can change: a draft, an ID scan, a paid recipe. With the path, each client calls the Storage SDK, so every read is evaluated against the current rules for the current user. A stored download URL works for anyone who ever reads that row, and keeps working after the user's access is withdrawn until the token is revoked.

A download URL is like a printed guest pass with a code on it: the front desk checks who you are before printing it, but the door only checks the code, so anyone holding the pass gets in until the desk cancels that code.

  • Storage Security Rules are checked every time someone opens the download URL
  • Download URLs expire after an hour, like Firebase ID tokens
  • A download URL only works for the user who requested it
  • Tightening the Storage rules immediately disables URLs already handed out
  • Strict database rules keep stored download URLs private