Skip to content

fix(s3): bound multipart parts, reclaim expired staging, honour bucket antivirus - #14796

Merged
ChiragAgg5k merged 6 commits into
mainfrom
fix/s3-gateway-limits
Oct 23, 2026
Merged

ChiragAgg5k merged 6 commits into
mainfrom
fix/s3-gateway-limits

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Hardens the S3-compatible gateway around multipart limits, staging cleanup, duplicate names and the bucket antivirus setting.

Multipart limits

  • UploadPart now checks the part number against the S3 range (1 to 10000) and checks the part size and the running total of the upload's other parts against the bucket's maximumFileSize before writing to the device. Before this, only CompleteMultipartUpload checked the total, after every part was already staged. Re-uploading a part number replaces its earlier size instead of adding to it.

Expired staging

  • Expired uploads used to have only their index entry dropped, which left staged chunks on the device. getMultipartUploads() now aborts the device staging of an expired upload and then purges it. The cached state outlives MULTIPART_TTL until it is purged, so it is read back with a long retention to find the path and the device upload ID. Staging that already backs a committed object is left in place. Abort failures are logged and don't fail the request.
  • CreateMultipartUpload runs the same sweep for its bucket, so expired staging is reclaimed even when nobody lists uploads.

Antivirus

  • The gateway doesn't scan objects. PutObject, CopyObject, CreateMultipartUpload, UploadPart and CompleteMultipartUpload now refuse buckets that the REST upload would scan: _APP_STORAGE_ANTIVIRUS=enabled, bucket antivirus on, and a Local device. Before, the gateway wrote to those buckets without scanning. The condition matches the REST path, so non-Local devices and deployments with antivirus off behave as before. Reads are unchanged. Buckets created through S3 keep antivirus: false, since enabling it would make them unwritable through the gateway.

Duplicate names

  • REST allows two files with the same name in a folder. ListObjects / ListObjectsV2 used to fail for the whole bucket when that happened. They now list the oldest file per key, using the existing $sequence ordering. Single-object operations on an ambiguous key still return 400 for that key only.

Also replaced an inline \Utopia\System\System with an import.

Test Plan

New E2E tests in tests/e2e/Services/Storage/S3CustomServerTest.php:

  • testUploadPartLimits (also in the s3 group so it runs against MinIO): with a 1 KiB bucket, a 1025-byte part, a second part that pushes the total past the limit, and part numbers 0 and 10001 return 400. Re-uploading part 1 succeeds, and the upload still completes and reads back.
  • testListObjectsWithDuplicateNames: two REST files with the same name plus one other. Both V1 and V2 listings return 200 with each key once.
  • testWriteAntivirusBucket: on an antivirus-enabled bucket, PutObject and CreateMultipartUpload return 400 when antivirus is enabled on a Local device (the antivirus CI lane), and 200 otherwise.
  • setupBucket() now creates buckets with antivirus: false, so the existing S3 tests also run in the antivirus lane.

The expired-staging sweep has no E2E test: forcing expiry means waiting 24 hours or rewriting the cache envelope's timestamp in Redis, which ties the test to the cache encoding.

Pint, PHPStan (host, phpstan.neon) and Rector pass on touched files. E2E is left to the CI lanes; I didn't bring up a local stack.

Related PRs and Issues

None.

Related: appwrite-labs/cloud#6628

…t antivirus

Multipart parts were written to the device before any size check, so only completion compared the total against the bucket's maximum file size. Each part now checks itself and the running total, and part numbers are limited to the S3 range of 1 to 10000.

Expired uploads only had their index entry dropped, leaving staged chunks on the device. Listing and creating uploads now abort the staging of expired uploads unless it already backs a committed object.

The gateway does not scan objects, so writes to buckets the REST upload would scan are refused instead of silently skipping the bucket's antivirus setting.

Listings keep the oldest file per key instead of failing the whole bucket when two files share a name.
@luxor-code-review

luxor-code-review Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 2 medium

💬 Inline comments (2)

  • 🟡 findObject rejects keys listings now tolerate — Get.php:84
  • 🟡 Expiry abort skips multipart/object locks — Base.php:1030
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Purge state even when device abort fails (Base.php:1036) — In expireMultipartUpload any exception from findObject or abort is caught and only logged, then purgeMultipartUpload runs unconditionally (src/Appwrite/Platform/Modules/S3/Http/S3/Base.php:1036-1041).

This PR hardens the S3 storage-proxy gateway: it enforces the 1–10000 part-number range and bucket maximumFileSize on upload parts, tolerates duplicate object keys in listings via uniqueObjects(), rejects writes to antivirus-scanned local buckets through validateAntivirus(), and adds lazy expiry of abandoned multipart uploads so expired staging is aborted and purged when the upload is listed or re-created. E2E tests cover the part limits, duplicate-name listing, and the antivirus gate.

Files Change
src/Appwrite/Platform/Modules/S3/Http/S3/Base.php Adds expiry purge logic, antivirus validation, and shared S3 constraint checks in the proxy base
src/Appwrite/Platform/Modules/S3/Http/S3/Create.php Enforces part-number range, cumulative part size, and antivirus gating on write paths
src/Appwrite/Platform/Modules/S3/Http/S3/Get.php Deduplicates listing results so duplicate keys no longer fail listings
tests/e2e/Services/Storage/S3CustomServerTest.php Adds e2e tests for part limits, duplicate names, and antivirus gating

Reviewed commit: a315a1b

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 22, 2026
@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

This PR adds multipart part-number and aggregate-size limits, reclaims expired multipart staging, applies antivirus checks to S3 writes, and deduplicates duplicate object keys in listings. It also expands the S3 E2E coverage for these behaviors and configures test buckets with antivirus disabled by default.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
src/Appwrite/Platform/Modules/S3/Http/S3/Base.php Adds multipart bounds and expired-upload cleanup, antivirus validation, and duplicate-key handling.
src/Appwrite/Platform/Modules/S3/Http/S3/Create.php Passes locking and bucket state through multipart upload creation and part handling.
src/Appwrite/Platform/Modules/S3/Http/S3/Get.php Uses the expired-upload sweep for multipart listings and deduplicates object listings.
tests/e2e/Services/Storage/S3CustomServerTest.php Adds multipart-limit, duplicate-listing, and antivirus coverage; disables antivirus on default test buckets.
🔇 Filtered out · 2

Findings Hansi considered but did not post.

Finding Why
Reject multipart staging before writing to Local storage Verifier: The antivirus policy is size-dependent, not a prohibition on staging. validateAntivirus rejects only Local uploads at or below APP_LIMIT_ANTIVIRUS, matching the REST upload's condition in Storage/Http/Buckets/Files/Create.php:421. Multipart size is known only at completion, and this check runs before finalize or creation/replacement of a readable object document. Guarding initiation or early parts would incorrectly prevent permitted large multipart uploads; testWriteAntivirusBucket explicitly covers that case. Pending staging remains abortable and expired staging is reclaimed by expireMultipartUpload. No unscanned small object becomes accessible through this path.
Keep ambiguous single-object keys returning 400 Verifier: The current implementation deliberately defines a canonical S3 object as the oldest file for a key: findObject orders by $sequence, and uniqueObjects retains that same oldest file for listings. Reads and locked mutations consistently use this lookup; they do not select an arbitrary duplicate or delete all matching native files. The subsequent commit explicitly changed duplicate resolution, and testListObjectsWithDuplicateNames now asserts successful GET and HEAD as well as collapsed listings. Returning 400 was the prior behavior, but no supplied repository rule requires retaining it, and the finding establishes no bug beyond disagreeing with the intentional mapping policy.

Reviewed 0ff3fa7 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

ghost
ghost previously requested changes Oct 22, 2026

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Tier B · 1 blocking finding to address. Summary

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adds multipart part/size limits, duplicate-key listing tolerance, antivirus gating, and lazy expiry of abandoned multipart uploads in the S3 storage proxy, with e2e coverage.

Key findings

  • 🟡 findObject rejects keys listings now tolerate — Get.php:84
  • 🟡 Expiry abort skips multipart/object locks — Base.php:1030

…xpiry

Single-object reads now agree with listings instead of failing on names REST allows to repeat.

Expiry takes the object lock that completion commits under, re-checks the state there, and matches committed objects by staging path, so neither a racing completion nor a duplicate name can make it drop or keep the wrong staging.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Tier B · See the inline comments. Summary

Devices keep the bytes first staged for a part number, so accepting new content would record its size and ETag against the old bytes. Retries with identical content still succeed.
… expired mid-write

The ETag is MD5, so it cannot prove a retry matches the staged bytes. A part that finishes after expiry purged its upload now fails instead of resurrecting state for aborted staging.
@ghost
ghost dismissed their stale review October 22, 2026 14:56

The findings that requested changes are resolved. See the summary comment for what is still open.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · See the inline comments. Summary

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/s3-gateway-limits (after).

Metric Before After Change
🚀 Requests/sec 181.78 198.94 🟢 +9.4%
⏱️ Latency P50 92.65 ms 88 ms ⚪ -5%
⏱️ Latency P95 224.45 ms 198.24 ms 🟢 -11.7%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 88 198.24 12,540 198.94 -26.21
Account 156.38 289.1 660 11.22 -54.7
TablesDB 86.3 162.24 6,820 110.93 -13.49
Storage 79.37 182.46 3,300 54.96 -12.69
Functions 122.7 228.59 1,760 30.01 -25.56

Top API waits (after)

API request Max wait (ms)
account.prefs.update 457.19
account.name.update 397.33
functions.create 386.64
storage.buckets.create 340.4
functions.variables.update 326.91

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@ChiragAgg5k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Tier A · See the inline comments. Summary

…xpiry off live parts

The antivirus gate refused every write to a scanned local bucket, while the REST upload skips scanning above APP_LIMIT_ANTIVIRUS. The gateway now refuses only writes at or under that limit, checked where the final size is known: a single PUT or copy, and multipart completion.

Expiry took only the object lock, so it could abort staging while a part was still being written under the multipart lock. It now holds the multipart lock around the object lock. It also keeps the upload state when the device abort fails, so the next listing retries instead of losing the path; a NotFound abort means nothing was staged and still purges.
@ChiragAgg5k

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k
ChiragAgg5k merged commit 00837ff into main Oct 23, 2026
46 checks passed
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
fix(s3): bound multipart parts, reclaim expired staging, honour bucket antivirus

(cherry picked from commit 00837ff)
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