Skip to content

fix(deployments): validate x-appwrite-id to prevent path traversal - #13996

Merged
ChiragAgg5k merged 1 commit into
appwrite:mainfrom
ChiragAgg5k:fix/deployment-id-path-traversal
Sep 29, 2026
Merged

ChiragAgg5k merged 1 commit into
appwrite:mainfrom
ChiragAgg5k:fix/deployment-id-path-traversal

Conversation

@ChiragAgg5k

Copy link
Copy Markdown
Member

What

The chunked-upload branch of the function and site deployment endpoints read the x-appwrite-id header straight into the on-disk deployment filename without validating it. A .. value escapes the per-project storage root, so any key with functions.write / sites.write can write attacker-controlled bytes outside its project's storage — including over another tenant's deployment archive. The same escaped path is persisted as the deployment sourcePath and 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.php and Sites/Http/Deployments/Create.php read x-appwrite-id into $deploymentId with no validation, then pass it to getPath($deploymentId . '.' . ext).
  • Device::getAbsolutePath() collapses .. segments lexically (array_pop) with no root-containment assertion, so root/../../../tmp/x.gz resolves to /tmp/x.gz; the Local device writes there, and the S3 device folds the same .. into the object key.
  • The traversal string is accepted as the deployment document $id (no format check in the database layer) and stored as sourcePath; Deletes.php (deleteDeploymentFiles) later calls $device->delete($sourcePath, true).

Fix

$idValidator = new UID();
if (!$idValidator->isValid($deploymentId)) {
    throw new Exception(Exception::STORAGE_INVALID_APPWRITE_ID);
}

in both deployment actions, matching the Storage reference implementation. Both files already imported UID, and STORAGE_INVALID_APPWRITE_ID already exists.

Tests

Added a regression test to each of FunctionsCustomServerTest and SitesCustomServerTest (testCreateDeploymentRejectsPathTraversalId) that posts a chunked deployment with x-appwrite-id: ../../../tmp/appwrite-poc and asserts:

  • the request is rejected 400 with type storage_invalid_appwrite_id, and
  • no poisoned deployment row is persisted (the deployments list stays empty) — i.e. the guard rejects before any write, not just with a message.

The traversal primitive was reproduced directly against the real Utopia\Storage\Device\Local in a running build: ../../../tmp/appwrite-poc → /tmp/appwrite-poc.gz and ../../functions/app-victim/… → the victim project's directory. Pint and php -l pass 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.php hunks and green with them.

Residual hardening (follow-ups, not in this PR)

  • Clamp Device::getAbsolutePath() so a resolved path can never leave the device root (defence in depth for any other caller).
  • Migration/sanitizer to reset pre-existing deployments.sourcePath values outside the expected app-{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.

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.
@ghost

ghost commented Sep 29, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The fix mirrors the existing Storage validation, the validation runs before any write, and both endpoints have regression tests.

Adds a UID validation of the x-appwrite-id header in the chunked-upload branch of the function and site deployment create endpoints. Invalid IDs, including path traversal strings, now fail with STORAGE_INVALID_APPWRITE_ID before any path or deployment document is created. Each service gets an e2e regression test.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
src/Appwrite/Platform/Modules/Functions/Http/Deployments/Create.php UID-validate x-appwrite-id after the content-range parse, throwing STORAGE_INVALID_APPWRITE_ID.
src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php Same UID validation of x-appwrite-id for site deployments.
tests/e2e/Services/Functions/FunctionsCustomServerTest.php Regression test: a traversal x-appwrite-id is rejected with 400 and no deployment is persisted.
tests/e2e/Services/Sites/SitesCustomServerTest.php Same regression test for site deployments.

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

@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

@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Critical risk] Adds path traversal validation to deployment upload handlers.

The validation appears sound, but the explicit repository requirement for behavior-based assertions should be satisfied before merging.

Fix All in Claude CodeFindings

  1. P2 Error assertion mirrors production ▶
Fix with agent prompt
### Issue 1
tests/e2e/Services/Functions/FunctionsCustomServerTest.php:718
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.

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!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR validates caller-supplied deployment IDs on ranged function and site uploads before using them as storage filenames. It also adds API regressions for traversal IDs and checks that no deployment record is created.

  • The new response-type assertions mirror a production constant instead of pinning the public error type.

Reviews (1) · Last reviewed commit: "fix(deployments): validate x-appwrite-id..."

]);

$this->assertEquals(400, $deployment['headers']['status-code']);
$this->assertEquals(Exception::STORAGE_INVALID_APPWRITE_ID, $deployment['body']['type']);

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.

P2 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!

Fix in Claude Code Fix in Codex

@ChiragAgg5k
ChiragAgg5k merged commit 6bfa0d9 into appwrite:main Sep 29, 2026
54 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/deployment-id-path-traversal branch September 29, 2026 17:36
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.

2 participants