Skip to content

Commit a3c0304

Browse files
williamdesDeepSeek
andcommitted
security: Retry the permission with the server token
4238543 reads the permission with the token of the login, which only carries the scopes the user agreed to share, and the GitHub documentation reserves the collaborators endpoint for tokens carrying read:org and repo. A maintainer whose login token is refused would silently drop to read-only, which is what ec8dd51 and 5fbb8c5 built the read-only access for. Ask with the token of the server as a second attempt, and keep the refusal when both are refused. Signed-off-by: William Desportes <williamdes@wdes.fr> Co-Authored-By: DeepSeek <noreply@deepseek.com>
1 parent 52bd08d commit a3c0304

2 files changed

Lines changed: 94 additions & 9 deletions

File tree

‎src/Controller/Component/GithubApiComponent.php‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,19 @@ public function getRedirectUrl(string|null $scope = null, string|null $state = n
217217
*/
218218
public function canCommitTo(string $username, string $repoPath, string $access_token): bool
219219
{
220-
[$permission, $status] = $this->apiRequest(
221-
'repos/' . $repoPath . '/collaborators/' . $username . '/permission',
222-
[],
223-
ClientRequest::METHOD_GET,
224-
true,
225-
$access_token,
226-
);
220+
[$role, $status] = $this->getCollaboratorPermission($username, $repoPath, $access_token);
221+
222+
if ($status !== 200) {
223+
// The token of a login only carries the scopes the user agreed to
224+
// share, and the endpoint refuses a read-only account as well:
225+
// ask a second time with the token of the server, which is not
226+
// tied to what a user agreed to give.
227+
$serverToken = (string) Configure::read('GithubAccessToken');
228+
if ($serverToken !== '' && $serverToken !== $access_token) {
229+
[$role, $status] = $this->getCollaboratorPermission($username, $repoPath, $serverToken);
230+
}
231+
}
232+
227233
if ($status !== 200) {
228234
Log::error(
229235
'Collaborator permission call for ' . $username . ' on ' . $repoPath
@@ -233,15 +239,41 @@ public function canCommitTo(string $username, string $repoPath, string $access_t
233239
return false;
234240
}
235241

236-
if (! is_array($permission)) {
242+
if ($role === null) {
237243
Log::error('Collaborator permission call for ' . $username . ' on ' . $repoPath . ' returned no data');
238244

239245
return false;
240246
}
241247

248+
return in_array($role, ['admin', 'maintain', 'write'], true);
249+
}
250+
251+
/**
252+
* Read the permission of a user on a repository.
253+
*
254+
* @param string $username GitHub login of the user
255+
* @param string $repoPath The repo slug
256+
* @param string $access_token Token used to ask the API
257+
*
258+
* @return array{string|null, int} The role of the user, and the status code
259+
*/
260+
private function getCollaboratorPermission(string $username, string $repoPath, string $access_token): array
261+
{
262+
[$permission, $status] = $this->apiRequest(
263+
'repos/' . $repoPath . '/collaborators/' . $username . '/permission',
264+
[],
265+
ClientRequest::METHOD_GET,
266+
true,
267+
$access_token,
268+
);
269+
270+
if (! is_array($permission)) {
271+
return [null, $status];
272+
}
273+
242274
$role = $permission['permission'] ?? $permission['role_name'] ?? null;
243275

244-
return is_string($role) && in_array($role, ['admin', 'maintain', 'write'], true);
276+
return [is_string($role) ? $role : null, $status];
245277
}
246278

247279
/**

‎tests/TestCase/Controller/Component/GithubApiComponentTest.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
use App\Controller\Component\GithubApiComponent;
2020
use Cake\Controller\ComponentRegistry;
21+
use Cake\Core\Configure;
2122
use Cake\Http\TestSuite\HttpClientTrait;
2223
use Cake\Log\Engine\ArrayLog;
2324
use Cake\Log\Log;
2425
use Cake\TestSuite\TestCase;
2526
use PHPUnit\Framework\Attributes\DataProvider;
2627

28+
use function count;
2729
use function is_array;
2830
use function json_encode;
2931

@@ -188,6 +190,57 @@ public function testCanCommitToDoesNotCallTheCollaboratorEndpoint(): void
188190
$this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
189191
}
190192

193+
/**
194+
* The token of a login only carries the scopes the user agreed to share:
195+
* when the API refuses it, the permission is read with the server token so
196+
* that a maintainer does not lose the write access.
197+
*/
198+
public function testCanCommitToFallsBackToTheServerToken(): void
199+
{
200+
Configure::write('GithubAccessToken', 'server-token');
201+
202+
$component = new class (new ComponentRegistry()) extends GithubApiComponent {
203+
/** @var array<int, string> */
204+
public array $usedTokens = [];
205+
206+
public function apiRequest(
207+
string $path,
208+
array|string $data,
209+
string $method,
210+
bool $returnStatus = false,
211+
string $access_token = '',
212+
): array {
213+
$this->usedTokens[] = $access_token;
214+
215+
if (count($this->usedTokens) === 1) {
216+
return [['message' => 'Resource not accessible'], 403];
217+
}
218+
219+
return [['permission' => 'write'], 200];
220+
}
221+
};
222+
223+
$this->assertTrue($component->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'user-token'));
224+
$this->assertSame(['user-token', 'server-token'], $component->usedTokens);
225+
226+
Configure::delete('GithubAccessToken');
227+
}
228+
229+
/**
230+
* Without a server token, a refused login token is the answer: no access.
231+
*/
232+
public function testCanCommitToRefusesWithoutAnyUsableToken(): void
233+
{
234+
Configure::delete('GithubAccessToken');
235+
$this->mockClientGet(
236+
self::PERMISSION_URL,
237+
$this->newClientResponse(403, [], json_encode(['message' => 'Resource not accessible'])),
238+
);
239+
240+
$this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
241+
$this->assertCount(1, $this->logCapture->read());
242+
}
243+
191244
/**
192245
* The authorize url can carry the OAuth state.
193246
*/

0 commit comments

Comments
 (0)