Repository navigation
fix(deployments): validate x-appwrite-id to prevent path traversal - #13996
Conversation
The chunked-upload branch of the function and site deployment endpoints read the x-appwrite-id header straight into the on-disk deployment name without validating it. A `..` value escapes the per-project storage root (Device::getAbsolutePath collapses `..` lexically with no containment check), letting any key with functions.write/sites.write write into other tenants' storage and overwrite their deployment archives. The escaped path is also persisted as the deployment's sourcePath, which the deletes worker later trusts, turning a normal delete into a cross-project file delete (CWE-22). Storage file uploads already UID-validate the same header; the two deployment endpoints did not. Apply the identical check right after the content-range parse, rejecting invalid ids with STORAGE_INVALID_APPWRITE_ID.
🟢 Tier S · Ready to merge
Adds a
📂 Walkthrough · 4
Reviewed |
|
| ]); | ||
|
|
||
| $this->assertEquals(400, $deployment['headers']['status-code']); | ||
| $this->assertEquals(Exception::STORAGE_INVALID_APPWRITE_ID, $deployment['body']['type']); |
There was a problem hiding this comment.
Error assertion mirrors production
This test compares the response type with Exception::STORAGE_INVALID_APPWRITE_ID, the production constant that supplies that value. The site test does the same. If the constant changes, both tests will accept the new value instead of catching a change to the public API. The repository requires tests to assert observable behavior rather than mirror source code; assert the literal storage_invalid_appwrite_id response type in both tests before merging.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Functions/FunctionsCustomServerTest.php
Line: 718
Comment:
**Error assertion mirrors production**
This test compares the response type with `Exception::STORAGE_INVALID_APPWRITE_ID`, the production constant that supplies that value. The site test does the same. If the constant changes, both tests will accept the new value instead of catching a change to the public API. The repository requires tests to assert observable behavior rather than mirror source code; assert the literal `storage_invalid_appwrite_id` response type in both tests before merging.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
What
The chunked-upload branch of the function and site deployment endpoints read the
x-appwrite-idheader straight into the on-disk deployment filename without validating it. A..value escapes the per-project storage root, so any key withfunctions.write/sites.writecan write attacker-controlled bytes outside its project's storage — including over another tenant's deployment archive. The same escaped path is persisted as the deploymentsourcePathand later trusted by the deletes worker, turning a normal function/site delete into a cross-project arbitrary file delete (CWE-22).Storage file uploads already UID-validate this exact header (
Buckets/Files/Create.php); the two deployment endpoints did not. This applies the identical check right after the content-range parse.Root cause
Functions/Http/Deployments/Create.phpandSites/Http/Deployments/Create.phpreadx-appwrite-idinto$deploymentIdwith no validation, then pass it togetPath($deploymentId . '.' . ext).Device::getAbsolutePath()collapses..segments lexically (array_pop) with no root-containment assertion, soroot/../../../tmp/x.gzresolves to/tmp/x.gz; the Local device writes there, and the S3 device folds the same..into the object key.$id(no format check in the database layer) and stored assourcePath;Deletes.php(deleteDeploymentFiles) later calls$device->delete($sourcePath, true).Fix
in both deployment actions, matching the Storage reference implementation. Both files already imported
UID, andSTORAGE_INVALID_APPWRITE_IDalready exists.Tests
Added a regression test to each of
FunctionsCustomServerTestandSitesCustomServerTest(testCreateDeploymentRejectsPathTraversalId) that posts a chunked deployment withx-appwrite-id: ../../../tmp/appwrite-pocand asserts:400with typestorage_invalid_appwrite_id, andThe traversal primitive was reproduced directly against the real
Utopia\Storage\Device\Localin a running build:../../../tmp/appwrite-poc→/tmp/appwrite-poc.gzand../../functions/app-victim/…→ the victim project's directory. Pint andphp -lpass on all four files.No E2E was executed locally for this path — the shared local stack was being cycled during development, so the new e2e regressions need to run in CI (Kind + Tilt). Please confirm they go red without the two
Create.phphunks and green with them.Residual hardening (follow-ups, not in this PR)
Device::getAbsolutePath()so a resolved path can never leave the device root (defence in depth for any other caller).deployments.sourcePathvalues outside the expectedapp-{projectId}prefix, since rows written by vulnerable versions would still be trusted by the delete cascade after this patch.Reported to security@ by Nhat Anh Vu and Sakil Ahamed.