Skip to content

Download images to send only from public addresses - #2147

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

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

Conversation

@YeungKC

@YeungKC YeungKC commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #2146, which adds sendImageRequest.

downloadImageFile and downloadImageBytes download an image by URL, for example to send it. They now use the same request function as images that are shown. Its rules for host names and the Referer come from #2146.

 downloadImageFile / downloadImageBytes
-  createRHttpClient().send(GET url)
+  sendImageRequest(url)

Evidence

  • test/utils/image_test.dart. With the old request function, both "does not request a local address" cases fail. With the change, they pass. The size limit tests still run against a local server.
  • flutter analyze: no issues. flutter test: 545 passed, 13 skipped.
  • Not tested: sending an image by URL from a signed-in app.

Merge Danger

Door: two-way

Blast Radius: images downloaded by URL. An image on a LAN or .local host can no longer be sent by URL, nor can one on a host name that #2146 rejects.

@YeungKC
YeungKC requested a review from crossle November 26, 2026 06:29
@YeungKC
YeungKC added this pull request to stack #2148 November 26, 2026 06:29
@gaotic

gaotic Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

AI review of bd82f84 failed after 18m 34s; details are in the review host's log.

@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 · 055da3c

Targeted Flutter analysis and git diff --check passed. All 31 focused tests passed, and two temporary tests reproduced the reported regressions. Verification was limited to automated checks.

Comment thread lib/utils/image.dart
String url, {
ProxyConfig? proxyConfig,
int maxBytes = maxDownloadedImageBytes,
@visibleForTesting ImageSender send = sendImageRequest,

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.

Public image URLs with Unicode hostnames now fail before DNS. Both download functions reject https://bücher.de/image.png because Dart exposes the host as b%C3%BCcher.de, while the equivalent xn--bcher-kva.de passes validation. The previous rhttp path applied IDNA normalization. Normalize the host to ASCII before applying the public-address checks, and use that same normalized URL for 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.

Fixed in the base branch (#2146, 70d6019 and 41bc815) and merged here in 6ad87a6. Hosts such as bücher.de pass now. The set of accepted scripts is limited; see the description of #2146.

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). bücher.de and other names in the accepted scripts pass now. Names in other scripts are still rejected, because Dart has no IDNA to check the name that the native client will use.

Comment thread lib/utils/image.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 · 6ad87a6

Needs fixes before merge. Targeted Flutter analysis and both git diff --check comparisons passed. All 60 existing focused tests passed, and two temporary regression tests reproduced the reported failures.

Comment thread lib/utils/image.dart
}) async {
final resolved = Uri.base.resolve(url);
final response = await _sendImageRequest(resolved, proxyConfig);
final response = await send(resolved, proxyConfig: proxyConfig);

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 shared helper changes the request target when a redirect contains percent-encoded unreserved characters. In a mock download, Location: /image%7E.png?key=token%41 produced /image~.png?key=tokenA, which can invalidate signatures that cover the raw path or query. Resolve and send each redirect with its escaped path and query preserved, while still validating every destination.

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.

Not changed. Dart's Uri rewrites percent-encoded unreserved characters, which RFC 3986 (sections 2.3 and 6.2.2.2) defines as equivalent. On main, the first request already goes through this rewrite: with the native client, /image%7E.png?key=token%41&sig=a%2Fb%3D is requested as /image~.png?key=tokenA&sig=a%2Fb%3D. Only redirect targets kept the original spelling there. Reserved escapes such as %2F and %3D stay as they are, before and after this change. To keep the raw text, the string that is sent would have to differ from the URL that is checked.

Comment thread lib/utils/image.dart
}) async {
final resolved = Uri.base.resolve(url);
final response = await _sendImageRequest(resolved, proxyConfig);
final response = await send(resolved, proxyConfig: proxyConfig);

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 Unicode hostname regression is still present for decomposed spellings. The validator accepts bücher.de but rejects bu%CC%88cher.de before DNS, even though both map to xn--bcher-kva.de under IDNA. Normalize hostnames with IDNA before applying the public-address checks, and send the request using the same normalized hostname.

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: bu%CC%88cher.de loaded on main (requested as xn--bcher-kva.de) and is now rejected. Not changed in this PR. It is part of the stated limit that other non-ASCII characters are rejected. Accepting decomposed spellings needs the IDNA mapping itself, because IDNA drops some combining characters, so a longer list of accepted characters is not reliable. Leaving this open for a 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). Decomposed spellings are still rejected. Accepting them needs the IDNA mapping itself, because IDNA drops some combining characters.

@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 · fc135bc

Needs fixes. flutter analyze --no-pub passed for the four affected files and both git diff --check comparisons passed. The two focused Flutter test invocations did not complete within the 120-second timeout, so their results could not be independently verified here.

await request.response.close();
});
addTearDown(subscription.cancel);
final client = IOClient();

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.

This test constructs a plain IOClient, whose underlying HttpClient can still honor the process http_proxy/https_proxy settings. Clearing imageEnvironment only affects sendImageRequest's resolver, so on a machine with a proxy and no loopback bypass the request can fail with Connection refused before the local server sends the oversized response. Configure the test client to bypass the proxy for loopback, or use a client/transport whose proxy behavior is controlled by the test.

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.

Not changed. With HTTP_PROXY set and no loopback bypass, flutter test cannot load any test file, because its own local connection goes to the proxy. This also happens with test/utils/number_test.dart, which makes no request, so the test body is not reached. With a loopback bypass, IOClient connects directly, and the image tests pass with a proxy set.

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