Repository navigation
fix(storage): require ownership on chunked upload resume - #14014
Conversation
🔵 Tier A · Mergeable after minor fixes
The PR restricts chunked Storage upload resumes to callers with update permission or the original uploader, while preserving permissions set on the first chunk. Client e2e tests cover owner, delegated-user, unauthorized-user, guest, and completed-upload retry behavior. The latest refactor records uploader metadata inline without changing the authorization behavior. Latest changes: The newest commit replaces the withUploader helper with inline array_merge calls at both file-creation sites and removes the helper.
Fix with agent prompt📂 Walkthrough · 2
⏳ Still open from earlier reviews · 1
Reviewed the commits since |
|
| $metadata = $file->getAttribute('metadata', []); | ||
| $uploaderId = \is_array($metadata) ? ($metadata[self::UPLOADER_ID] ?? '') : ''; | ||
| if (\is_string($uploaderId) && $uploaderId !== '' && $uploaderId === $user->getId()) { | ||
| return; | ||
| } | ||
|
|
||
| throw new Exception(Exception::USER_UNAUTHORIZED, $authorization->getDescription()); |
There was a problem hiding this comment.
Existing uploads cannot resume
Partial uploads started before this change have no uploaderId in their metadata. If their creator has bucket create access but neither bucket nor file update access, the new checks cannot recognize them as the uploader. Their next chunk is rejected, leaving an upload they previously could finish stuck after deployment.
Knowledge Base Used: Storage and file handling
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php
Line: 648-654
Comment:
**Existing uploads cannot resume**
Partial uploads started before this change have no `uploaderId` in their metadata. If their creator has bucket create access but neither bucket nor file update access, the new checks cannot recognize them as the uploader. Their next chunk is rejected, leaving an upload they previously could finish stuck after deployment.
**Knowledge Base Used:** [Storage and file handling](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/storage-files-api.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Resuming a file with Content-Range and x-appwrite-id loaded it with authorization skipped, so any caller who could create in the bucket could continue the upload and replace its permissions. Resume now requires bucket or file update permission, or the user who started the upload, and later chunks leave the original permissions in place. Co-authored-by: Harsh Mahajan <[email protected]>
The pending file is created in the same request that sends the first chunk, so the resume check was rejecting guests after the transfer. Only an upload that already existed is checked. An empty guest id is still not ownership, so another guest cannot continue it. Co-authored-by: Harsh Mahajan <[email protected]>
7f66e5f to
2649a24
Compare
A caller who cannot read the finished file gets not-found on get, and a chunked resume does not return the file model. Co-authored-by: Harsh Mahajan <[email protected]>
| $firstBody = 'resume-chunk'; | ||
| $nextBody = 'x'; | ||
| $firstRange = 'bytes 0-' . (\strlen($firstBody) - 1) . '/' . $totalSize; | ||
| $nextRange = 'bytes ' . $chunkSize . '-' . $chunkSize . '/' . $totalSize; |
There was a problem hiding this comment.
Use a full-sized first part in the multipart resume test
With S3-backed storage, the successful resume cases fail during multipart completion because resume-chunk is below S3's 5 MiB minimum for non-final parts. Please make the first body $chunkSize bytes and its range cover that whole part, so this regression test also works with the supported S3 adapter.
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Storage/StorageCustomClientTest.php
Line: 1640-1643
Comment:
**Use a full-sized first part in the multipart resume test**
With S3-backed storage, the successful resume cases fail during multipart completion because `resume-chunk` is below S3's 5 MiB minimum for non-final parts. Please make the first body `$chunkSize` bytes and its range cover that whole part, so this regression test also works with the supported S3 adapter.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · testing · Reply if this doesn't apply.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Files live in a collection per bucket, so the bucketId comparison never fails, and upload metadata is built by the server, so a chunk cannot overwrite the uploader.
Brings in main's OAuth2 state and nonce validation (#14012), which cloud needs, along with the realtime origin alignment (#14020), subscriber target ownership (#14019), GitLab primary email (#14010), impersonation MFA (#14017), write-only webhook passwords (#14016), chunked-upload resume ownership (#14014), preview authorize validation (#14013), nightly forcing (#14015) and scheduled-function enqueue checks (#14011). The merge had no textual conflicts, and main left composer.json and composer.lock unchanged. So the lock keeps the branch's database and async pins. Main's assertResumeAllowed() in Storage Buckets/Files/Create checks Input(Database::PERMISSION_UPDATE, ...). utopia-php/database 8 has no PERMISSION_* constants, so every chunked resume would fail with an undefined-constant error. It now passes PermissionType::Update, as the rest of that action already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What does this PR do?
POST /v1/storage/buckets/:bucketId/fileswithContent-Rangeandx-appwrite-idresumes a chunked upload by loading the existing file with authorization skipped. Bucket create permission was the only check, so another user who can create in the bucket could continue someone else's incomplete upload. The final chunk also wrote the caller's$permissionsonto that file.Resume is now allowed only when the caller may update the file, using the same rules as
Files/Update.php:A later chunk no longer replaces permissions stored on the first chunk. The same check runs before a completed upload is returned for an idempotent resume, so that response is not given to a caller who cannot update the file.
The pending document is created in the same request as the first chunk. That request is not a resume:
finalizeUploadonly runs the ownership check when the file already existed. Guests have an empty user id, which is stored but never treated as ownership, so a guest can create withContent-Rangeand a different guest cannot continue that upload.Same class of bug as #13118 (chunked deployment resume), applied to Storage files (Help Scout #1468091).
Test Plan
testCreateFileResumeRequiresOwnership:401 user_unauthorized) and the file is unchangedtestCreateFileChunkedAsGuest:Content-Rangecreate succeedsRelated PRs and Issues
Checklist