Skip to content

Load remote images only from public addresses - #2146

Open
YeungKC wants to merge 5 commits into
mainfrom
fix/remote-image-private-addresses
Open

YeungKC wants to merge 5 commits into
mainfrom
fix/remote-image-private-addresses

Conversation

@YeungKC

@YeungKC YeungKC commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Image URLs come from message content. The client that loads remote images for display now requests them only from public addresses.

sendImageRequest(url)
  for each hop, at most 10 redirects (followed by the app)
    require http or https
    require a public IP address, or a name that is not local
    if the client has the resolver: require a name that is not one of its proxies
    send
resolver (no proxy, or a SOCKS5 proxy)
  keep only the public addresses of a name; let the proxy names through
Connection URL is checked Resolved addresses are checked
Direct yes yes
SOCKS5 (app setting or socks5:// from the environment) yes yes
HTTP, or socks5h:// from the environment yes no, the proxy resolves names
  • Local means loopback, private, link-local, multicast and reserved ranges, documentation ranges, localhost and .local names.
  • In 2001::/23 (IETF protocol assignments), only the parts that the IANA registry marks as globally reachable are allowed. Teredo is marked N/A, so it is rejected.
  • 198.18.0.0/15 stays allowed, because fake-IP proxies answer DNS queries with it. NAT64 addresses are checked by their IPv4 address.
  • With SOCKS5, an empty resolver result makes the HTTP library send the host name to the proxy. The resolver therefore returns 255.255.255.255, so the connection fails.
  • An app proxy replaces the environment proxies, as in the native client. An HTTP app proxy installs no resolver, so no proxy name is rejected: the proxy gets all names.

As the native client does when it follows redirects itself:

  • Credentials from the image URL stay on redirects to the same origin, and are dropped for another origin.
  • The previous URL is the Referer, without user info and fragment, and not from https to http.
  • A same-scheme Location without //, such as https:next.png, is relative.

Known limits

Dart has no IDNA, so these differ from the native client:

  • Host names may use Latin-1, basic Cyrillic, kana, CJK ideographs and Hangul. Other non-ASCII characters are rejected, and so are decomposed spellings of the accepted ones.
  • The Referer of such a host has the percent-encoded spelling, not the xn-- one.
  • Two names in other scripts (also xn-- names) count as possibly the same proxy. Where proxy names are rejected, such a proxy name stops images on such hosts.

Unusual Location forms, such as a : in the first relative segment, still fail to load.

Evidence

  • test/utils/remote_image_request_test.dart: the checks were reverted one at a time, and a test failed each time.
  • flutter analyze: no issues. flutter test: 543 passed, 13 skipped.
  • CI cannot load the native HTTP client, so the final commit was also run against it on macOS. A public image loads. Redirects, direct connections and SOCKS5 proxies do not reach a loopback address. Credentials on redirects match the native client in four cases. A name in another script reaches an HTTP proxy in xn-- form.
  • Not tested: Windows and Linux at run time, or a real SOCKS5 proxy product.

Merge Danger

Door: two-way

Blast Radius: all remote images. An image on a LAN or .local host no longer loads. Without a proxy, an image host that a local DNS server resolves only to a private address no longer loads either. A host name with other non-ASCII characters, for example Greek or Arabic letters, no longer loads.

@gaotic

gaotic Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

AI review done for 49b6c50 in 18m 12s · 3 comments

Quality: The updated tests cover the proxy exemption and IPv6 range changes. Redirect compatibility still needs work, as verified with mocked requests and inspection of the native client’s request-building path.

Role Model Status
Reviewer GPT codex/gpt-6.2-sol · effort high ok
Summary codex/gpt-6.2-sol · effort medium ok

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · GPT (Grok failed) · 667aa13

Targeted Flutter analysis and all 20 image-request and cache tests passed. Four additional focused checks reproduced the reported defects, and git diff --check passed.

Comment thread lib/utils/remote_image_request.dart Resolved
Comment thread lib/utils/remote_image_request.dart Resolved
Comment thread lib/utils/remote_image_request.dart Outdated

// Rejects escaped and non-ASCII hosts, which the native client maps to
// other names, such as full-width digits to an IP address.
if (!_hostName.hasMatch(host)) return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hostname regex rejects valid Unicode image URLs before rhttp can apply IDNA conversion. A focused check rejected bücher.example and accepted the equivalent xn--bcher-kva.example. Canonicalize HTTP(S) hostnames with the native client's URL rules before applying the address checks, and send the same canonical URL that passed validation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 70d6019 for Latin-1, basic Cyrillic, kana, CJK ideographs and Hangul. Dart has no IDNA, so the URL is not rewritten: IDNA keeps each of these letters in a non-ASCII (xn--) label, so the ASCII checks still apply, and the native client converts the host. Other non-ASCII characters, such as full-width letters or Greek, are still rejected. In 41bc815, if a configured proxy has a name in another script or an xn-- name, image hosts of that kind are rejected, because the two spellings cannot be compared without IDNA.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recorded as a known limit (see "Known limits" in the description of #2146). Latin-1, basic Cyrillic, kana, CJK ideographs and Hangul pass. Other characters are still rejected. Dart has no IDNA, and a longer list of accepted characters is not reliable, because IDNA maps or drops some characters. A full fix needs an IDNA library, or these checks inside the native client, which is a larger change than this PR.

Comment thread lib/utils/remote_image_request.dart Resolved

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · GPT · 41bc815

Needs fixes before merge. Targeted Flutter analysis and both diff whitespace checks passed, as did all 48 existing tests across the image request, cache, and media suites. Four focused regression checks reproduced the findings below.

Comment thread lib/utils/remote_image_request.dart Resolved
// As the native client does when it follows redirects itself.
referer = target.isScheme('https') && next.isScheme('http')
? null
: target.replace(userInfo: '').removeFragment().toString();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redirects from Unicode hosts send a Referer with a different hostname spelling from the request. A same-origin redirect sent https://b%C3%BCcher.example/a.png, while reqwest serializes the host as xn--bcher-kva.example. This can break servers that check the header hostname. Build the header from the previous URL after applying the same IDNA canonicalization as the request.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed with the native client: main sends Referer: http://xn--bcher-kva.example/a.png, and this branch sends the percent-encoded host. Not changed in this PR. Writing the same spelling needs IDNA, which Dart does not have. This is now listed as a limit in the description, and stays open together with the decision on adding an IDNA library.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recorded as a known limit (see "Known limits" in the description of #2146). The Referer keeps the percent-encoded spelling of such a host. Writing the xn-- spelling needs IDNA, which Dart does not have. A full fix needs an IDNA library, or redirects handled inside the native client, which is a larger change than this PR.

Comment thread lib/utils/remote_image_request.dart Resolved
Comment thread lib/utils/remote_image_request.dart Resolved

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · GPT · 5033936

Needs fixes before merge. Targeted Flutter analysis, all 65 tests across six image-related suites, and both diff whitespace checks passed. Five focused checks using mocked HTTP and DNS reproduced four defects.

Comment thread lib/utils/remote_image_request.dart Resolved
Comment thread lib/utils/remote_image_request.dart Resolved
// As the native client does when it follows redirects itself.
referer = target.isScheme('https') && next.isScheme('http')
? null
: target.replace(userInfo: '').removeFragment().toString();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous Referer issue persists. A redirect from bücher.example sends https://b%C3%BCcher.example/a.png, while reqwest sends requests to xn--bcher-kva.example. Servers that check the header hostname can reject the image. Build the header using the same IDNA canonicalization as the request URL.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. This is the same gap as the earlier Referer thread: the same spelling needs IDNA. Not changed. Both stay open.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recorded as a known limit (see "Known limits" in the description of #2146). Same gap and reason as in the earlier Referer thread: the xn-- spelling needs IDNA.

Comment thread lib/utils/remote_image_request.dart Resolved

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · GPT · 49b6c50

Needs fixes before merge. All 36 remote-image tests, targeted Flutter analysis, and both diff whitespace checks passed. Three additional mocked redirect checks reproduced regressions.

if (next.scheme == target.scheme &&
next.host == target.host &&
next.port == target.port) {
next = next.replace(userInfo: target.userInfo);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strip credentials supplied by a cross-origin Location. The mocked second request retained test:placeholder from the redirect URL, and rhttp builds a fresh reqwest request that turns that user info into Basic auth. The previous automatic redirect sent no such header. Clear userInfo on cross-origin targets. https://raw.githubusercontent.com/seanmonstar/reqwest/v0.13.4/src/async_impl/request.rs

);
// As for the native client, the credentials of the URL stay on its origin.
if (next.scheme == target.scheme &&
next.host == target.host &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare canonical hosts before deciding whether to keep credentials. A mocked redirect from [2606:4700:0:0:0:0:0:1] to [2606:4700::1] dropped user:pw, although both literals identify the same origin. Compare parsed IPv6 address bytes and retain the scheme and port checks.

// As the native client does when it follows redirects itself.
referer = target.isScheme('https') && next.isScheme('http')
? null
: target.replace(userInfo: '').removeFragment().toString();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previously reported Referer mismatch persists. A mocked redirect from bücher.example sent https://b%C3%BCcher.example/a.png, while the native client uses https://xn--bcher-kva.example/a.png. Servers that check the referrer hostname can reject the image. Serialize the header with the same IDNA canonicalization as the native request URL.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant