Repository navigation
fix(vcs): bind installation state to the console user and an expiry - #14793
Conversation
The state handed to GitHub, GitLab, Gitea and Bitbucket was an HMAC over the project and redirect URLs only, so a state minted by one console user stayed valid forever and for anyone. Sign the initiating user id, an expiry and a nonce into it, and have the callbacks require the current console session to belong to that user before connecting an installation.
|
Review complete. 🟠 1 high 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
This PR introduces
Reviewed commit: 8894555 |
🟢 Tier S · Ready to merge
VCS installation callbacks now use signed, expiring state bound to the initiating Console user, and reject mismatched or replayed state. GitHub also exchanges the returned authorization code and verifies access to the selected installation before persisting it. Unit and end-to-end tests cover state validation and callback failures. Latest changes: The latest commits pass the injected OAuth2 client into the GitHub callback action and add an end-to-end check that a callback without an authorization code does not create an installation.
📂 Walkthrough · 8
Reviewed the commits since |
There was a problem hiding this comment.
Adds a signed, expiring VCS OAuth state helper and wires GitHub/Gitea authorize and callback endpoints to it, plus unit and e2e coverage.
Key findings
- 🟠 Signed
stateis replayable;nonceis never consumed — State.php:48
| public static function verify(string $state, string $key): ?self | ||
| { | ||
| $decoded = \json_decode($state, true); | ||
|
|
||
| if (!\is_array($decoded)) { | ||
| return null; | ||
| } | ||
|
|
||
| foreach (['projectId', 'userId', 'success', 'failure', 'nonce', 'signature'] as $field) { | ||
| if (!\is_string($decoded[$field] ?? null)) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| if (!\is_int($decoded['expire'] ?? null)) { | ||
| return null; | ||
| } | ||
|
|
||
| $verified = new self( | ||
| $decoded['projectId'], | ||
| $decoded['userId'], | ||
| $decoded['success'], | ||
| $decoded['failure'], | ||
| $decoded['expire'], | ||
| $decoded['nonce'], | ||
| ); | ||
|
|
||
| if (!\hash_equals($verified->signature($key), $decoded['signature'])) { | ||
| return null; | ||
| } | ||
|
|
||
| return $verified; | ||
| } | ||
|
|
||
| public function isExpired(): bool | ||
| { | ||
| return $this->expire < \time(); | ||
| } |
There was a problem hiding this comment.
🟠 security · high
Signed state is replayable; nonce is never consumed
The nonce in Appwrite\Vcs\State is random but never persisted, so any validly signed state is replayable for its full 1-hour TTL — State::verify checks only the HMAC and expiry (src/Appwrite/Vcs/State.php:48). The user-binding check stops cross-account use but does not bind the OAuth transaction: driving the victim's logged-in browser to the callback with the victim's own signed state plus an attacker's installation_id/code passes every check and attaches the attacker's VCS installation — and its access — to the victim's project. Signed states travel in redirect URLs (browser history, access logs, Referer), so obtaining one within the TTL is realistic.
📋 Prompt for AI Agents
Enforce one-time use of the VCS state. In the Authorize actions (src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php and src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php), after State::create(...), persist the state's nonce in the platform cache/DB keyed by nonce with a 3600-second TTL before redirecting to the provider. In each callback (src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php:94 and src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php:81), after State::verify() succeeds, atomically claim the nonce (add-if-absent) and route the flow through the existing failure() redirect when the nonce is missing or already consumed. Without this, a leaked signed state can be replayed within the TTL with a different installation_id/code to attach an attacker-controlled VCS installation to the victim's project.
There was a problem hiding this comment.
Fixed in b359313: after the signature, expiry and user checks pass, each callback claims the state's nonce in vcsCommentLocks (the same pattern the Entire and Origin handlers use), so a state connects at most one installation. A replay redirects to the failure URL. There are e2e tests for this on both the Gitea and GitHub callbacks.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
The state was bound to its user and expiry but could be completed more than once within that hour. Claim its nonce in the callback so each state connects at most one installation, and cover the GitHub callback's state checks end to end.
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
…inding # Conflicts: # src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php # tests/e2e/Services/VCSGitHub/VCSGitHubConsoleClientTest.php
…inding fix(vcs): bind installation state to the console user and an expiry (cherry picked from commit bbbc01d)
What does this PR do?
Hardens the S3-compatible gateway around multipart limits, staging cleanup, duplicate names and the bucket antivirus setting.
Multipart limits
UploadPartnow checks the part number against the S3 range (1 to 10000) and checks the part size and the running total of the upload's other parts against the bucket'smaximumFileSizebefore writing to the device. Before this, onlyCompleteMultipartUploadchecked the total, after every part was already staged. Re-uploading a part number replaces its earlier size instead of adding to it.Expired staging
getMultipartUploads()now aborts the device staging of an expired upload and then purges it. The cached state outlivesMULTIPART_TTLuntil it is purged, so it is read back with a long retention to find the path and the device upload ID. Staging that already backs a committed object is left in place. Abort failures are logged and don't fail the request.CreateMultipartUploadruns the same sweep for its bucket, so expired staging is reclaimed even when nobody lists uploads.Antivirus
_APP_STORAGE_ANTIVIRUS=enabled, bucketantiviruson, and a Local device. Before, the gateway wrote to those buckets without scanning. The condition matches the REST path, so non-Local devices and deployments with antivirus off behave as before. Reads are unchanged. Buckets created through S3 keepantivirus: false, since enabling it would make them unwritable through the gateway.Duplicate names
$sequenceordering. Single-object operations on an ambiguous key still return 400 for that key only.Also replaced an inline
\Utopia\System\Systemwith an import.Test Plan
New E2E tests in
tests/e2e/Services/Storage/S3CustomServerTest.php:testUploadPartLimits(also in thes3group so it runs against MinIO): with a 1 KiB bucket, a 1025-byte part, a second part that pushes the total past the limit, and part numbers 0 and 10001 return 400. Re-uploading part 1 succeeds, and the upload still completes and reads back.testListObjectsWithDuplicateNames: two REST files with the same name plus one other. Both V1 and V2 listings return 200 with each key once.testWriteAntivirusBucket: on an antivirus-enabled bucket, PutObject and CreateMultipartUpload return 400 when antivirus is enabled on a Local device (the antivirus CI lane), and 200 otherwise.setupBucket()now creates buckets withantivirus: false, so the existing S3 tests also run in the antivirus lane.The expired-staging sweep has no E2E test: forcing expiry means waiting 24 hours or rewriting the cache envelope's timestamp in Redis, which ties the test to the cache encoding.
Pint, PHPStan (host,
phpstan.neon) and Rector pass on touched files. E2E is left to the CI lanes; I didn't bring up a local stack.Related PRs and Issues
None.
Related: appwrite-labs/cloud#6666