Rocket.Chat.ReactNative icon indicating copy to clipboard operation
Rocket.Chat.ReactNative copied to clipboard

bug: Attachments not downloadable on Android — file URL becomes "content://net.demola.chat.android.provider/files/<filename>"

Open felipe-demola opened this issue 1 year ago • 10 comments

Describe the Bug

On Android, tapping any file attachment in a room/thread fails to open or download. I tested image, audio, DOCX, and PDF files with the same result. When I tried a .txt file and chose “Open in browser,” I saw the attachment resolves to a content URI:

content://net.demola.chat.android.provider/files/<filename>

This is an Android content URI (not an http/https web URL). The app/intent flow appears to pass a content:// URI that external apps can’t open—likely due to missing temporary read permission or a FileProvider configuration/authority issue—so the attachment never opens.

Steps to Reproduce

  1. Open the app on Android and enter any chat.
  2. Tap an existing file attachment (e.g., PDF, image, doc), or upload a new one and then tap it.
  3. Choose the app to open the file

Expected Behavior

  • Tapping an attachment should open it in a suitable viewer or download it successfully on Android.

Actual Behavior

  • The file open/download fails.
  • The file URL displayed/resolved is: content://net.demola.chat.android.provider/files/<filename>
  • Android cannot open it (launched fails to open / images stay loading forever)

Rocket.Chat Server Version

7.9.3

Rocket.Chat App Version

4.65.0.107519

Device Name

Samsung Galaxy M35

OS Version

Android 15

Additional Context

No response

felipe-demola avatar Oct 14 '25 13:10 felipe-demola

@felipe-demola, could you please share a short video attachment of the same issue? Thank you!

JASIM0021 avatar Oct 15 '25 07:10 JASIM0021

@felipe-demola, could you please share a short video attachment of the same issue? Thank you!

Sure. Here's the video attachment. All those files open as expected at the browser version of Demola Chat.

https://github.com/user-attachments/assets/db4a0bad-8a62-42f0-87ee-aabb0e6f4d5c

felipe-demola avatar Oct 15 '25 15:10 felipe-demola

unable to re-produce @felipe-demola https://github.com/user-attachments/assets/72499a1a-c791-4afb-b2bf-2badfdeff069

JASIM0021 avatar Oct 15 '25 18:10 JASIM0021

can i solved this problem please assigned me

ayushsingh1js avatar Nov 27 '25 18:11 ayushsingh1js

I was able to reproduce and solve this issue with the help of AI:


Issue: File attachments and images fail to load on Android (403 Forbidden)

Problem Description

File attachments and images in chat rooms fail to load on Android, showing a perpetual loading spinner for images and failing to download/open files. The browser version of Rocket.Chat works correctly with the same server, indicating this is a mobile app-specific issue.

Root Cause

The issue is in formatAttachmentUrl.ts. When constructing URLs for file downloads, the function checks the FileUpload_ProtectFiles setting to determine whether to append authentication tokens (rc_token and rc_uid) to the URL:

const protectFiles = store.getState().settings.FileUpload_ProtectFiles;

The problem: This setting returns undefined when the server settings haven't been fully loaded into the Redux store yet (which commonly happens on app startup or immediately after login). When protectFiles is undefined (falsy), the authentication tokens are NOT appended to the download URL.

Since most Rocket.Chat servers have FileUpload_ProtectFiles enabled by default, the server returns 403 Forbidden when the app tries to download files without authentication tokens.

Debug Evidence

With logging added to the download function:

[formatAttachmentUrl] Input: { attachmentUrl: '/file-upload/xxx/image.png', protectFiles: undefined }
[formatAttachmentUrl] Built full URL: 'https://server.com/file-upload/xxx/image.png'  // No auth tokens!
[MediaDownload] Download result: { status: 403 }  // Server rejects unauthenticated request

After the fix:

[formatAttachmentUrl] Input: { attachmentUrl: '/file-upload/xxx/image.png', protectFiles: true }
[formatAttachmentUrl] Built full URL with auth: 'https://server.com/file-upload/xxx/image.png?rc_token=xxx&rc_uid=xxx'
[MediaDownload] Download result: { status: 200 }  // Success!

Proposed Fix

In formatAttachmentUrl.ts, change:

const protectFiles = store.getState().settings.FileUpload_ProtectFiles;

To:

// Default to true when setting is undefined - safer to include auth tokens
// This handles the case when settings haven't been loaded from the server yet
const protectFiles = store.getState().settings.FileUpload_ProtectFiles ?? true;

Why this fix is safe

  1. If FileUpload_ProtectFiles is true (most common): Auth tokens are correctly added ✓
  2. If FileUpload_ProtectFiles is false: Auth tokens are not added (original behavior) ✓
  3. If FileUpload_ProtectFiles is undefined (settings not loaded yet): Auth tokens are now added as a safe default ✓

Adding authentication tokens to a request that doesn't require them is harmless - the server will simply ignore them. However, NOT adding tokens when they're required causes the 403 error.

Affected Versions

This issue affects the React Native mobile app. The timing of when it manifests depends on network conditions and how quickly the settings are loaded from the server after login.

Steps to Reproduce

  1. Install a fresh version of the Rocket.Chat mobile app (or clear app data)
  2. Log in to a server with FileUpload_ProtectFiles enabled (default)
  3. Immediately navigate to a room with image/file attachments
  4. Observe that images show loading spinners indefinitely and files fail to download

machadolucas avatar Dec 01 '25 16:12 machadolucas

The problem: This setting returns undefined when the server settings haven't been fully loaded into the Redux store yet (which commonly happens on app startup or immediately after login). When protectFiles is undefined (falsy), the authentication tokens are NOT appended to the download URL.

@machadolucas how do you start downloading an attachment right after app startup or login?

diegolmello avatar Dec 01 '25 16:12 diegolmello

The problem: This setting returns undefined when the server settings haven't been fully loaded into the Redux store yet (which commonly happens on app startup or immediately after login). When protectFiles is undefined (falsy), the authentication tokens are NOT appended to the download URL.

@machadolucas how do you start downloading an attachment right after app startup or login?

Maybe it was bad choice or words 😆, sorry - but "right after" does not mean immediately after here. But for example opening a chat room or private message which contain images right after login in the app.

I didn't investigate why FileUpload_ProtectFiles setting was not being loaded, but setting it to true made all my attachments and images work.

machadolucas avatar Dec 01 '25 16:12 machadolucas

I didn't investigate why FileUpload_ProtectFiles setting was not being loaded

This is my main concern with setting the value true as a fallback. We might have an issue here and we would be only masking it. For example, I followed the steps you've mentioned on our workspace (open.rocket.chat) and it worked, so it could be an issue on your own server 😬 Check if requests to settings are working properly.

diegolmello avatar Dec 01 '25 20:12 diegolmello

We might have an issue here and we would be only masking it

Indeed that is the case:


Root Cause Found: AWS WAF Blocking Long Query Strings

After extensive debugging, we identified the root cause of why FileUpload_ProtectFiles was undefined in the app, causing file attachments and images to fail loading (403 errors).

The Issue:

The /api/v1/settings.public endpoint is called with all ~80 setting IDs in the query string parameter _id. This creates a URL approximately 2,374 characters long:

https://server.com/api/v1/settings.public?_id=Jitsi_Enable_Teams,Jitsi_Enable_Channels,...(80+ settings)...&offset=0

When this request passes through AWS CloudFront/WAF, it gets blocked with a 403 error:

<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
Request blocked.
Generated by cloudfront (CloudFront)

Reproduction:

# Short URL works fine (200 OK):
curl "https://your-server.com/api/v1/settings.public?_id=FileUpload_ProtectFiles&offset=0"

# Long URL with all settings gets blocked (403):
curl "https://your-server.com/api/v1/settings.public?_id=Jitsi_Enable_Teams,Jitsi_Enable_Channels,...&offset=0"

Impact:

Because getSettings() fails silently (returns early on non-success), the FileUpload_ProtectFiles setting never gets populated in Redux. When formatAttachmentUrl() checks this setting to decide whether to add authentication tokens (rc_token, rc_uid) to file URLs, it finds undefined and doesn't add them. The server then returns 403 for file downloads because they lack authentication.


In my opinion, supporting POST requests with settings IDs in the body instead of query string would be a cleaner approach. Now I know customizing my firewall settings could solve this, but since the default behavior of at least AWS WAF is to block such requests as potentially malicious, other people may find the same issue with firewalls.

machadolucas avatar Dec 01 '25 21:12 machadolucas

Agreed. I'll take this to discussion internally. Thanks!

diegolmello avatar Dec 02 '25 12:12 diegolmello