Chat attachments: fetch only from trusted hosts on arrival, and check every redirect

Open
#866 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
dart, flutter, rust

Research direction

Start with encrypted_image_message.dart and attachment_providers.dart to trace automatic image fetching, then read rust/src/nostr/blossom.rs for download and redirect handling. Check the attachment payload parser and existing tests before changing the fetch rules. Done means untrusted image hosts wait for a user action, unsafe hosts and redirects are refused, and the listed host, redirect, and widget cases pass.

Written by the indexing model from the issue text.

Description

area: rust-core area: ui bug

Summary

The URL of a chat attachment is chosen by whoever sent it, and the app fetches it with only two checks: it must be https and end in a 64-hex hash. Two things follow:

  1. An image is fetched as soon as its bubble is shown, from any host. A counterparty can send an image_encrypted message whose blossom_url points at a server it runs, and learn the other party's IP address the moment the chat is opened. No tap is needed. Mostro keeps the two parties apart; this hands one of them the other's address.
  2. Redirects are followed without checking them again. An https URL can redirect to http, to 127.0.0.1 or to a private address, and the app makes that request. The hash check protects what gets decrypted, but it runs after the request: a counterparty can make the app send GET requests into the user's own network.

Both came up in the review of MostroP2P/protocol#88, which adds these rules to the attachments spec (Receiving, rules 2 and 3, and the automatic-fetch paragraph). Line references are at main, ab0fa199.

1. Images are fetched on arrival, from any host

Where: EncryptedImageMessage watches attachmentDataProvider, which downloads the blob as soon as the bubble builds ("downloaded and decrypted on arrival, as v1 does"). payload::parse accepts any https host.

What happens: the fetch reaches the host the sender named, on every target. On the web, the browser sends the request whatever the CORS answer is. File bubbles are not affected: they download only when the user taps Open, Share or Save.

Fix: fetch automatically only from the servers the app uploads to (BLOSSOM_SERVERS). For any other host, show the bubble with that host and a Download action, and fetch only once the user taps it.

2. Redirects are followed without checking them again

Where: download_blob checks https:// on the URL it was given, then fetch_verified uses http_client, a default reqwest::Client.

What happens: reqwest 0.12's default policy follows up to 10 redirects, including from https to http (it only drops the Referer), and https_only is off. Nothing checks the host either, before or after a redirect, so https://127.0.0.1/<hash>, https://192.168.1.1/<hash> or a redirect to either is fetched. This applies to the native targets. On the web, the browser's mixed-content and private-network rules already refuse most of these.

Fix, for downloads:

  • Refuse a host that is localhost or an IP address in a loopback, private, link-local or unspecified range, IPv4 and IPv6 (IPv4-mapped included).
  • On native, check the resolved addresses too, through a reqwest DNS resolver that filters them. The connection then uses the same resolution, so a rebinding cannot slip a private address in between the check and the connect.
  • Re-apply https and the host rule on every redirect with a custom redirect::Policy, or refuse redirects. Some Blossom servers redirect a GET to their storage, so a checked redirect is friendlier than none. Set https_only(true) on the download client.

Tests

  • Unit tests for the host rule: loopback, private, link-local and unspecified addresses in IPv4 and IPv6, IPv4-mapped IPv6, and localhost, all refused; a public host accepted.
  • A local server that redirects to http://… and to 127.0.0.1: both refused, and nothing is decrypted.
  • A widget test: an image from a host not in BLOSSOM_SERVERS shows the host and fetches nothing until Download is tapped.

Related

  • MostroP2P/protocol#87 and #88 also change the attachment file key so that a solver can open the files. That needs its own work here: a fallback reader first, then the new sender. It is not part of this issue.
  • v1 fetches images on arrival as well ("as v1 does"), so MostroP2P/mobile has the same exposure.
Dominant language
Dart
Stars
11
Forks
9
Avg merge
2d 1h
Merged PRs (30d)
16

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MostroP2P/app

All issues in MostroP2P/app

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.