Repository navigation
fix(avatars): cap remote fetch size and rate limit image and favicon - #14788
Conversation
Remote images, favicons and photo-provider downloads were buffered in full, after automatic content decoding, with no size limit. The avatars client now sets CURLOPT_MAXFILESIZE_LARGE to APP_STORAGE_READ_BUFFER, the same 20MB ceiling storage uses for in-memory processing. cURL counts decoded bytes against it and aborts the transfer as soon as it is passed, so a compressed response cannot expand past the cap either. Connect and total timeouts move onto the shared client, and the image and favicon routes get a per-IP abuse limit.
|
Review complete. 🟠 1 high 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
The change hardens the
Reviewed commit: 58ce3ef |
🟢 Tier S · Ready to mergeThe shared avatar HTTP client now caps remote response bodies at the storage read-buffer limit and applies connection and total timeouts. The image and favicon routes use that client and have per-URL/IP abuse limits. E2E coverage checks small and oversized remote files, verifies both route limits, and closes the large-file fixture handle even when upload assertions fail. Latest changes: The newest commits add E2E checks for image and favicon abuse limits and ensure the large-file fixture handle closes if its upload loop fails.
📂 Walkthrough · 5
✅ Fixed since the last review · 1
Reviewed the commits since |
There was a problem hiding this comment.
This PR adds a 20MB download cap, shared client timeouts, and abuse rate-limit labels to the Avatars remote-fetch endpoints, plus an e2e regression test, but the size cap relies on a libcurl option that does not apply to HTTP transfers.
Key findings
- 🟠
CURLOPT_MAXFILESIZE_LARGEcap does not apply to HTTP transfers — resources.php:112
| $container->set('clientForAvatars', fn () => (new Client(new CurlAdapter(options: [ | ||
| \CURLOPT_MAXFILESIZE_LARGE => APP_STORAGE_READ_BUFFER, | ||
| ]), new PublicInternet( | ||
| ...\array_map( | ||
| fn (string $range) => new IPRange($range), | ||
| \array_filter(\array_map('trim', \explode(',', System::getEnv('_APP_ALLOWED_INTERNAL_ADDRESSES', '')))), | ||
| ) | ||
| )), []); | ||
| )))->withConnectTimeout(5)->withTimeout(15), []); |
There was a problem hiding this comment.
🟠 security · high
CURLOPT_MAXFILESIZE_LARGE cap does not apply to HTTP transfers
The size cap added to clientForAvatars (app/init/resources.php:112-113) relies on CURLOPT_MAXFILESIZE_LARGE, but libcurl enforces this option only on FTP/FILE/SFTP downloads — it does not affect HTTP transfers, which is all these clients ever do. The CurlAdapter write callback (packages/client/src/Adapter/Curl/Client.php:358) accumulates response body bytes with no size limit, so the intended 20 MB cap is a no-op. An attacker-controlled target of /avatars/image or /avatars/favicon can still stream arbitrarily large (including gzip-decoded) bodies into memory within the 15s timeout, reproducing the resource exhaustion this change sets out to prevent.
📋 Prompt for AI Agents
In app/init/resources.php around lines 112-113, the CURLOPT_MAXFILESIZE_LARGE => APP_STORAGE_READ_BUFFER option on the CurlAdapter does not apply to HTTP transfers per libcurl semantics, so the intended cap on avatar fetches is a no-op. Add a byte-counting guard in packages/client/src/Adapter/Curl/Client.php's CURLOPT_WRITEFUNCTION callback (around line 358) — or a client-level decorator used only by clientForAvatars — that aborts the transfer (throws a PSR-18 client exception) once accumulated body bytes exceed APP_STORAGE_READ_BUFFER, so the exception surfaces in the Avatars actions as AVATAR_REMOTE_URL_FAILED before unbounded memory is consumed.
There was a problem hiding this comment.
This option does apply to HTTP. libcurl documents CURLOPT_MAXFILESIZE_LARGE for FTP, HTTP and MQTT, and since 8.4.0 it also stops a transfer whose size is unknown up front once it passes the limit. The Appwrite image ships curl 8.22.0 (curl_version() in appwrite/appwrite:2.6.1). I checked it against a local server, using the adapter configured the same way as clientForAvatars. A chunked 192MB plain body was aborted at exactly 20,000,000 bytes with Exceeded the maximum allowed file size in 0.06s. A 200KB gzip body that decodes to 200MB was aborted at 19,986,934 decoded bytes with Would have exceeded max file size. Peak memory was 6MB in both cases, so the limit is checked against decoded bytes as they are written. The e2e test also covers the chunked case: the storage /view endpoint streams files over 20MB without a Content-Length, and the test asserts avatar_remote_url_failed. Without the cap, image returns avatar_image_not_found instead. I did not use a byte-counting write callback: when a PHP callback throws inside the cURL write function, ext/curl still reports the full chunk as handled, so the transfer keeps running until the timeout. The handle nitpick is fixed in 943795b: the upload loop now closes it in finally.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
CI enables router protection, which refuses requests addressed to a bare IP, so the fixture is served through an API rule on a nip.io name that resolves to loopback.
Guests past 60 fetches a minute from one address get 429 on both routes. The large-file upload in the avatars test now closes its handle when a chunk assertion fails.
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
…ckport #14788) The image and favicon avatars read a remote body of any size into memory. They now keep at most APP_STORAGE_READ_BUFFER bytes and fail with avatar_remote_url_failed past it, and both routes are limited to 60 requests per minute per IP. 1.9 fetches through utopia-php/fetch, whose cURL adapter takes no raw options, so the cap is enforced in a chunk callback rather than with CURLOPT_MAXFILESIZE_LARGE. The remote-file-too-large E2E test is not ported: it relies on the internal address allowlist 1.9 does not have. (cherry picked from commit d2c83c3)
…ckport #14788) The image and favicon avatars read a remote body of any size into memory. They now keep at most APP_STORAGE_READ_BUFFER bytes and fail with avatar_remote_url_failed past it, and both routes are limited to 60 requests per minute per IP. 1.9 fetches through utopia-php/fetch, whose cURL adapter takes no raw options, so the cap is enforced in a chunk callback rather than with CURLOPT_MAXFILESIZE_LARGE. The remote-file-too-large E2E test is not ported: it relies on the internal address allowlist 1.9 does not have. (cherry picked from commit d2c83c3)
…ckport #14788) The image and favicon avatars read a remote body of any size into memory. They now keep at most APP_STORAGE_READ_BUFFER bytes and fail with avatar_remote_url_failed past it, and both routes are limited to 60 requests per minute per IP. 1.9 fetches through utopia-php/fetch, whose cURL adapter takes no raw options, so the cap is enforced in a chunk callback rather than with CURLOPT_MAXFILESIZE_LARGE. The remote-file-too-large E2E test is not ported: it relies on the internal address allowlist 1.9 does not have. (cherry picked from commit d2c83c3)
…ckport #14788) The image and favicon avatars read a remote body of any size into memory. They now keep at most APP_STORAGE_READ_BUFFER bytes and fail with avatar_remote_url_failed past it, and both routes are limited to 60 requests per minute per IP. 1.9 fetches through utopia-php/fetch, whose cURL adapter takes no raw options, so the cap is enforced in a chunk callback rather than with CURLOPT_MAXFILESIZE_LARGE. The remote-file-too-large E2E test is not ported: it relies on the internal address allowlist 1.9 does not have. (cherry picked from commit d2c83c3)
…ckport #14788) The image and favicon avatars read a remote body of any size into memory. They now fetch through a cURL adapter that aborts the transfer once the body passes APP_STORAGE_READ_BUFFER (CURLOPT_MAXFILESIZE_LARGE plus a write callback that stops on the cap), failing with avatar_remote_url_failed, and both routes are limited to 60 requests per minute per IP. 1.9 fetches through utopia-php/fetch, whose cURL adapter takes no raw options, hence the small Appwrite\Utopia\Fetch\Adapter\Curl. The remote-file-too-large E2E test is not ported: it relies on the internal address allowlist 1.9 does not have. (cherry picked from commit d2c83c3)
…tch-cap fix(avatars): cap remote fetch size and rate limit image and favicon (cherry picked from commit d2c83c3)
What does this PR do?
Bounds how much the avatars endpoints read from a remote URL.
clientForAvatars(used by/v1/avatars/image,/v1/avatars/faviconand the Gravatar, Libravatar and OAuth2 photo providers) now setsCURLOPT_MAXFILESIZE_LARGEtoAPP_STORAGE_READ_BUFFER(20MB), the ceiling storage already uses for anything it holds in memory (previews, compression, encryption). The image and favicon handlers already map any transfer error toAVATAR_REMOTE_URL_FAILED; photo providers treat it as a miss and fall through to the next provider.Content-Length, so a compressed response stops at the cap after decoding. I checked this against a local server: a 192MB plain stream and a 200KB gzip body that decodes to 200MB were both cut off at the cap within milliseconds, and peak memory stayed at 6MB.withTimeout(15)in the image and favicon handlers. Photo providers keep their own shorter budget./v1/avatars/imageand/v1/avatars/favicongetabuse-keyurl:{url},ip:{ip}with 60 requests per minute.I didn't stream through
Client::stream()with a byte-counting sink. When a sink throws inside the cURL write callback, PHP's ext/curl does not abort the transfer: the callback still reports the full chunk length, so cURL keeps downloading until the server finishes or the timeout fires. Memory stays bounded, but the connection does not. The adapter option stops the transfer itself.Test Plan
AvatarsCustomServerTest::testGetRemoteFileTooLarge. It uploadstests/resources/disk-a/large-file.mp4(23.6MB) andlogo.pngto a public bucket, then points the avatars endpoints at this instance's own/viewURL. That URL goes through an API rule on a*.127.0.0.1.nip.ioname, because CI's router protection refuses bare IP hosts. The name resolves to loopback, which the test.envallows through_APP_ALLOWED_INTERNAL_ADDRESSES. The view endpoint streams files above 20MB in chunks with noContent-Length, so the test exercises the mid-transfer cutoff./avatars/imageon the small file returns a PNG./avatars/imageand/avatars/faviconon the large file return 404avatar_remote_url_failed. Without the cap, image returnsavatar_image_not_foundand favicon goes on to fetch/favicon.ico.composer lint, PHPStan (host,phpstan.neon) andcomposer refactor:checkpass on the touched files.Related PRs and Issues
None.
Checklist
Related: appwrite-labs/cloud#6674