Repository navigation
fix(vcs): build a webhook push only at the branch head the provider reports (appwrite:appwrite:alert#4648) - #15279
Conversation
…eports GitLab, Gitea and Bitbucket authenticate every webhook delivery with the one instance-wide secret Appwrite registers on each connected repository's webhook, so any tenant on the instance can sign a push for another tenant's repository id. Pinning the source to the resolved repository stopped a forged push from naming a repository the sender controls, but the branch and the commit still came from the delivery body: a push naming the victim's production branch and any commit that resolves in the victim's repository was built and activated. That let a sender roll a victim's production back to an old revision, promote an unmerged branch, or promote a revision they contributed through a pull request, all built and run with the victim project's token, build configuration and variables. A delivery authenticated with that secret speaks for no particular repository, so trust its branch and commit only once the provider itself reports the commit as that branch's head, which a forged delivery cannot choose. GitHub is unaffected: its webhook secret belongs to the Appwrite-owned App, which is why it registers no repository webhook of its own. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Review complete. 🟠 1 high · 🟡 2 medium 💬 Inline comments (2)
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — Head-check gate exempts pull-request deliveries · Deployment.php:176–193 · // src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php
176 if (empty($providerPullRequestId) && !\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $vcs->getSupportedWebhookScopes(), true)) {
177 $head = $vcs->getLatestCommit($owner, $repositoryName, $providerBranch)['commitHash'] ?? '';
178
179 if (empty($providerCommitHash) || $providerCommitHash !== $head) {
180 Span::add("{$logBase}.build.skipped.reason", 'commit is not the branch head');
181 Span::add("{$logBase}.build.skipped", 'true');
182 Console::warning("Skipping repository '{$repositoryId}': pushed commit '{$providerCommitHash}' is not the head of branch '{$providerBranch}'");
183 continue;
184 }
185 }
186
187 $isAuthorized = !$external;The new branch-head verification only applies when This PR hardens the push-deployment webhook path against forged deliveries when the provider only supports an instance-wide webhook secret: before enqueueing a build, the code asks the provider for the latest commit on the target branch and skips repositories whose reported commit hash does not match the delivery's commit. Coverage is added at the unit level (adapter webhook-scope behavior and head-check skipping) and in the Gitea console e2e suite.
Reviewed commit: 69eafe6 |
🟢 Tier S · Ready to merge
Push deployments from GitLab, Gitea, and Bitbucket now use a commit only when the provider confirms it is the named branch’s head; mismatches and missing branches are skipped, while other lookup failures remain retryable. Gogs also verifies branch existence, and the GitHub installation-scoped flow and pull-request path remain unchanged. Unit and Gitea end-to-end tests cover genuine and forged pushes, repository-source pinning, and missing branches. Latest changes: The newest commit checks that Gitea’s fallback branch-lookup headers are an array before reading the status code, avoiding an invalid offset when the response shape is unexpected.
📂 Walkthrough · 7
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
There was a problem hiding this comment.
Adds a provider-API branch-head verification gate to GitHub webhook deployments for VCS providers without installation-scoped webhooks, plus unit and e2e coverage.
Key findings
- 🟠 Unhandled
getLatestCommitfailures fail the whole webhook — Deployment.php:177 - 🟡 Head-check gate exempts pull-request deliveries — Deployment.php:176
- 🟡 N+1
getLatestCommitcalls per push delivery — Deployment.php:176
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
A provider error during the branch head lookup now skips that repository and records the reason on the span, so the delivery is still acknowledged. The head is looked up once per owner, repository and branch within a delivery.
… errors A branch the provider does not know is skipped and recorded on the span. Any other head lookup error reaches the existing error path, so the delivery is reported as failed and can be redelivered. The Gitea, Gogs, GitLab and Bitbucket adapters now report a missing branch as RepositoryNotFound, as the GitHub adapter already does.
The existing Gitea e2e test now also sends a delivery for a missing branch and checks it is acknowledged without creating a deployment.
Gitea 1.21 answers the latest commit lookup for a missing branch with a server error, so the adapter checks the branch itself before reporting the failure.
Keeps the package static analysis baseline unchanged.
fix(vcs): build a webhook push only at the branch head the provider reports (appwrite:appwrite:alert#4648) (cherry picked from commit 8d99ba4)
What does this PR do?
Fixes code-scanning alert appwrite:appwrite:alert#4648.
GitLab, Gitea and Bitbucket authenticate every webhook delivery with the one instance-wide secret (
_APP_VCS_<PROVIDER>_WEBHOOK_SECRET) that Appwrite itself registers on each connected repository's webhook (RepositoryWebhooks::ensure()), so any tenant that has connected a repository on the instance holds the secret every other tenant's deliveries are signed with.c092f4d040pinned a webhook deployment's owner and repository name to the resolved repository, so a forged delivery can no longer name a repository the sender controls — but the branch and the commit hash were still taken from the delivery body:$activatewas set when the body's branch equalled the resource's production branch, andcreateFromVcs()as the build ref.So a signed delivery naming a victim's provider repository id, the victim's production branch and any commit that resolves in the victim's repository produced an activated production deployment of a revision the victim never pushed to that branch: a rollback to a known-vulnerable commit, an unmerged branch head, or a revision contributed through a pull request — built and run with the victim project's installation token, build configuration and variables.
A delivery authenticated with that secret speaks for no particular repository, so
createGitDeployments()now trusts a push's branch and commit only once the provider itself reports the commit as that branch's head (getLatestCommit()through the stored installation), which a forged delivery cannot choose. A mismatch is skipped with a span reason; a provider lookup failure still surfaces as a failed delivery the provider can retry. GitHub is unaffected — its webhook secret belongs to the Appwrite-owned App, which is why it registers no repository webhook of its own — and the pull request path is untouched (it creates no deployment).Longer term, the alert's other recommendation still stands: issue a per-repository webhook secret at
RepositoryWebhooks::ensure()time, store it on therepositoriesdocument and verify a delivery against the secret of the repository it claims, which would also remove the need for the unscopedproviderRepositoryId-only lookup.Test Plan
tests/unit/Vcs/GitDeploymentsTest.php(new, written first and failing before the fix — the forged push created a deployment withactivate => true):testAPushNamingACommitThatIsNotTheBranchHeadIsRefused— a push on the production branch naming a commit the provider does not report as that branch's head makes no deployment.testAPushOfTheBranchHeadIsBuiltOnAProviderWithAnInstanceWideSecret— a genuine push of the branch head still builds and still activates.tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php:testGiteaPushCannotActivateARevisionTheBranchDoesNotPointAt(new) — a signed delivery naming the production branch with a commit only a side branch points at yields no deployment of that revision and leaves the live deployment where it was.testGiteaPushCannotRedirectBuildSource— the forged delivery now carries the victim's real head commit, so it still reaches deployment creation and still proves owner/name pinning.Run locally:
vendor/bin/phpunit tests/unit/Vcs tests/unit/Deployment tests/unit/Functions(159 tests, OK),composer lintand PHPStan on the changed files. The Gitea API shapes the new e2e test relies on (branches/main,contents/<path>withnew_branch) were verified against a realgitea/gitea:1.21.5; the VCSGitea e2e lane itself runs in CI.Related PRs and Issues
c092f4d040("fix(vcs): pin a webhook deployment's source to the resolved repository").Checklist
🤖 Generated with Claude Code