Repository navigation
fix: confine repository root directories and build artifact reads - #14808
Conversation
Root directories are joined onto checkout paths and fed to mkdir, mv and rsync in the builds worker, so a value or a checked-out link could resolve outside the checkout. Validate them as repository-relative paths at the API, and in the worker resolve each segment inside its checkout without following links before merging, with rsync keeping only links inside the template and its exit code checked. Build jobs could write the whole project directory on the local builds volume, and artifact reads followed links. Mount only the deployment's output directory and the project cache, and read a deployment file only when it is a regular file inside its device.
Function and site create, update and template deployment refuse a root directory with a parent segment, and the deprecated template root directory on function create is validated the same way.
|
Review complete. 🟡 3 medium 💬 Inline comments (2)
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Legacy // src/Appwrite/Platform/Modules/Functions/Workers/Builds.php
322 $rootDirectory = $resource->getAttribute('providerRootDirectory', '');
323 $rootDirectory = \rtrim($rootDirectory, '/');
324 $rootDirectory = \ltrim($rootDirectory, '.');
325 $rootDirectory = \ltrim($rootDirectory, '/');
326 if (!(new Directory(\PHP_INT_MAX))->isValid($rootDirectory)) {
327 throw new BuildException('Root directory must stay inside the repository.');
328 }The build worker validates the ltrim/rtrim-normalized 🧹 Nitpicks (1) — 🟢 1 low
The change introduces filesystem path confinement for Functions/Sites deployments: a new VCS
Reviewed commit: 4b012cc |
🟢 Tier S · Ready to mergeAdds a repository-relative directory validator to function, site, and VCS inputs, and confines repository-root resolution and template merges to checked-out trees. Local build jobs mount only deployment output and project cache, while readiness and download reads reject artifact paths that escape the device root. The newest change also validates legacy roots at the shared VCS deployment boundary and adds regression coverage for traversal values. Latest changes: The newest commit rejects escaping roots in Deployments::createFromVcs before archive, clone, or worker submission, and adds a unit regression test for legacy traversal values.
📂 Walkthrough · 10
✅ Fixed since the last review · 1
Merged |
There was a problem hiding this comment.
This PR hardens the deployment pipeline against path traversal by adding a Directory validator and symlink-aware confine()/isArtifact() guards, narrowing build volume mounts to per-deployment subPaths, and adding e2e/unit coverage.
Key findings
- 🟡 New Sites test stole
#[Retry]fromtestSiteTemplate— SitesCustomServerTest.php:3073 - 🟡 Legacy
providerRootDirectorybypasses the worker validation — Builds.php:322 - 🟡 Unchecked pre-creation of volume subPath directories — Deployments.php:809
| if ($device instanceof Local) { | ||
| $device->createDirectory($device->getRoot()); | ||
| $device->createDirectory($outputDirectory); | ||
| $device->createDirectory($cacheDirectory); | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Unchecked pre-creation of volume subPath directories
storage() pre-creates the deployment output and cache directories because Docker's named-volume subPath must exist before the container starts, but the createDirectory return values are ignored (src/Appwrite/Deployment/Deployments.php:810-811). Local::createDirectory uses error-suppressed mkdir and can silently fail on EACCES, ENOSPC, or a missing builds volume root. The job is then submitted successfully and fails later at container creation with an opaque orchestrator error instead of failing fast at submission.
📋 Prompt for AI Agents
In src/Appwrite/Deployment/Deployments.php lines 809-812, storage() calls $device->createDirectory($outputDirectory) and $device->createDirectory($cacheDirectory) on the Local device but ignores their boolean results. Capture both return values and throw an Exception (server-side code, not user-facing) like 'Unable to prepare builds volume directory ...' when either returns false, because Docker volume-subPath mounts require the directories to exist before the build container starts; an unchecked failure surfaces only as a cryptic orchestrator container-creation error later.
There was a problem hiding this comment.
Leaving this as is, deliberately. Local::createDirectory() returns true when the directory already exists, and the previous single project-root pre-creation also ignored the result. If the directory is missing, the orchestrator refuses the container, and that failure comes back through the job exit callback and fails the deployment. Throwing here would make building the job payload depend on the host filesystem, and it breaks the payload unit tests outside the container. The legacy providerRootDirectory finding outside the diff is fixed in 1430fbd (see the reply on Builds.php).
There was a problem hiding this comment.
The explanation doesn't fully address the concern. The argument that 'the orchestrator refuses the container and that failure comes back through the job exit callback' is valid for a missing file, but Docker named-volume subPath mounts require the subdirectory to exist on the host volume before the container starts — if it does not, the orchestrator error is typically opaque and not cleanly routed through the job exit callback in the same way a runtime failure is. Additionally, the comparison to the old single-root pre-creation ignoring its result is not a strong justification: that was also a latent bug, and the new code now mounts two subPaths instead of one, increasing the surface area.
The unit test argument is worth acknowledging — throwing directly inside 'storage()' could complicate payload unit tests. A reasonable middle ground would be to log a warning (e.g. via 'Console::warning') when 'createDirectory' returns false, which preserves the current behavior while making silent failures observable without breaking tests. That said, the core issue — that a false return is completely invisible — remains unaddressed in this diff.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Trimming leading dots turned a parent segment into a plain name before the check ran, and redeploys reach createFromVcs without the builds worker, so validate the stored value as is in both places and fail the deployment when it leaves the repository. Keep the retry on the site template test.
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
…ement # Conflicts: # src/Appwrite/Deployment/Deployments.php
Ported the root directory validation and the builds worker confinement. The build artifact checks target the orchestrator build path, which 1.9.x does not have. (cherry picked from commit e2c3e34)
Ported the root directory validation and the builds worker confinement. The build artifact checks target the orchestrator build path, which 1.9.x does not have. (cherry picked from commit e2c3e34)
Ported the root directory validation and the builds worker confinement. The build artifact checks target the orchestrator build path, which 1.9.x does not have. (cherry picked from commit e2c3e34)
Ported the root directory validation and the builds worker confinement. The build artifact checks target the orchestrator build path, which 1.9.x does not have. (cherry picked from commit e2c3e34)
…ement fix: confine repository root directories and build artifact reads (cherry picked from commit e2c3e34)
What does this PR do?
Confines repository root directories and build artifact reads to the directories they belong to.
Root directories
Appwrite\Vcs\Validator\Directory: a repository-relative directory with no..segment and no control characters. A leading/or./keeps meaning the repository root, as the builds worker already reads it, so existing clients that send/apps/webor./keep working.providerRootDirectoryon function and site create/update,rootDirectoryon function and site template deployments, the deprecatedtemplateRootDirectoryon function create, andproviderRootDirectoryon the VCS contents and detections endpoints. The OpenAPI spec maps it likeText, so the generated specs do not change.realpathto itself. The template merge usesrsync --safe-linksand fails the build on a non-zero exit. The "root directory with spaces" rename and the directory handed tocreateFromDirectorygo through the same resolution.Build artifacts
app-{projectId}/{deploymentId}) and the project's cache directory (app-{projectId}/cache) rather than the whole project directory. Both are created before submission because a Docker volume subpath must exist. The orchestrator accepts nested subpaths and multiple mounts of one volume.Deployments::isArtifact(): on the local device, a deployment file is readable only when it is a regular file inside the device root and itsrealpathequals its path. The Jobs worker's readiness check and both deployment download endpoints (output and source) use it. A file that fails the check is treated as missing.packages/vcsGit::normalizeRepositoryPath()still accepts..segments. This PR handles that on the app side and does not change the package.Test Plan
tests/unit/Vcs/Validator/DirectoryTest.phpcovers the accepted and refused paths.tests/unit/Deployment/DeploymentsTest.phpcovers the narrowed mounts, isolation from sibling deployments and projects, andisArtifact()against a regular file, a link, a linked parent directory, a parent segment, a directory, a missing file and paths outside the root.testRootDirectoryMustStayInsideRepositoryinFunctionsCustomServerTestandSitesCustomServerTest. A repository-anchored root is accepted. Template deployment, create and update return 400 for..,../other,x/../../yand/../x, and the stored value does not change.tests/unit/SDKandtests/unit/GraphQL. I did not run the e2e lanes locally because the docker stack is shared on this machine. CI runs them, and the Functions and Sites build lanes also exercise the narrowed mounts end to end.Related PRs and Issues
None.
Checklist
Related: appwrite-labs/cloud#6673