Skip to content

Commit 4238543

Browse files
williamdesDeepSeek
andcommitted
security: Ask for the write permission of a collaborator
canCommitTo() called GET /repos/{repo}/collaborators/{user} since 4060bde, which answers 204 for every collaborator of the repository, read-only and triage included: all of them were given the write access that ec8dd51 introduced, and could forward reports, close them or see the statistics. Ask GET /repos/{repo}/collaborators/{user}/permission instead, and accept only admin, maintain and write, from the permission or the role_name key. Any other answer is logged and refused. Signed-off-by: William Desportes <williamdes@wdes.fr> Co-Authored-By: DeepSeek <noreply@deepseek.com>
1 parent 10d1a24 commit 4238543

2 files changed

Lines changed: 234 additions & 9 deletions

File tree

‎src/Controller/Component/GithubApiComponent.php‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
use function array_merge;
3030
use function assert;
3131
use function http_build_query;
32+
use function in_array;
33+
use function is_array;
34+
use function is_string;
3235
use function json_encode;
3336
use function strtoupper;
3437

@@ -170,13 +173,15 @@ public function sendRequest(
170173

171174
/**
172175
* generate the url to redirect the user to for authorization given the
173-
* requested scope.
176+
* requested scope and the OAuth state.
174177
*
175-
* @param string $scope the api scope for the user to authorize
178+
* @param string|null $scope the api scope for the user to authorize
179+
* @param string|null $state the OAuth state used to protect the callback
180+
* against login CSRF
176181
*
177182
* @return string the generated url to redirect the user to
178183
*/
179-
public function getRedirectUrl(string|null $scope = null): string
184+
public function getRedirectUrl(string|null $scope = null, string|null $state = null): string
180185
{
181186
$url = 'https://github.com/login/oauth/authorize';
182187
$data = [
@@ -190,33 +195,53 @@ public function getRedirectUrl(string|null $scope = null): string
190195
),
191196
'scope' => $scope,
192197
];
198+
if ($state !== null) {
199+
$data['state'] = $state;
200+
}
193201

194202
return $url . '?' . http_build_query($data);
195203
}
196204

197205
/**
198206
* Check if a user can commit to a rep.
199207
*
208+
* Only the permissions that allow writing to the repository are accepted.
209+
* The collaborators endpoint cannot be used for this: it answers 204 for
210+
* every collaborator, read only and triage ones included.
211+
*
200212
* @param string $username the username to check
201213
* @param string $repoPath the repo path of the repo to check for
202214
* @param string $access_token the github access token
203215
*
204-
* @return bool true if the user is a collaborator and false if they are not
216+
* @return bool true if the user can write to the repository and false if they cannot
205217
*/
206218
public function canCommitTo(string $username, string $repoPath, string $access_token): bool
207219
{
208-
[, $status] = $this->apiRequest(
209-
'repos/' . $repoPath . '/collaborators/' . $username,
220+
[$permission, $status] = $this->apiRequest(
221+
'repos/' . $repoPath . '/collaborators/' . $username . '/permission',
210222
[],
211223
ClientRequest::METHOD_GET,
212224
true,
213225
$access_token,
214226
);
215-
if ($status !== 204 && $status !== 404) {
216-
Log::error('Collaborators call ended in a status code: ' . $status);
227+
if ($status !== 200) {
228+
Log::error(
229+
'Collaborator permission call for ' . $username . ' on ' . $repoPath
230+
. ' ended in a status code: ' . $status,
231+
);
232+
233+
return false;
217234
}
218235

219-
return $status === 204;
236+
if (! is_array($permission)) {
237+
Log::error('Collaborator permission call for ' . $username . ' on ' . $repoPath . ' returned no data');
238+
239+
return false;
240+
}
241+
242+
$role = $permission['permission'] ?? $permission['role_name'] ?? null;
243+
244+
return is_string($role) && in_array($role, ['admin', 'maintain', 'write'], true);
220245
}
221246

222247
/**
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
/**
4+
* phpMyAdmin Error reporting server
5+
* Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
6+
*
7+
* Licensed under The MIT License
8+
* For full copyright and license information, please see the LICENSE.txt
9+
* Redistributions of files must retain the above copyright notice.
10+
*
11+
* @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
12+
* @license https://opensource.org/licenses/mit-license.php MIT License
13+
*
14+
* @see https://www.phpmyadmin.net/
15+
*/
16+
17+
namespace App\Test\TestCase\Controller\Component;
18+
19+
use App\Controller\Component\GithubApiComponent;
20+
use Cake\Controller\ComponentRegistry;
21+
use Cake\Http\TestSuite\HttpClientTrait;
22+
use Cake\Log\Engine\ArrayLog;
23+
use Cake\Log\Log;
24+
use Cake\TestSuite\TestCase;
25+
use PHPUnit\Framework\Attributes\DataProvider;
26+
27+
use function is_array;
28+
use function json_encode;
29+
30+
/**
31+
* Tests for the write-access check of the GitHub API component.
32+
*/
33+
class GithubApiComponentTest extends TestCase
34+
{
35+
use HttpClientTrait;
36+
37+
private const string PERMISSION_URL
38+
= 'https://api.github.com/repos/phpmyadmin/phpmyadmin/collaborators/pma-bot/permission';
39+
40+
protected GithubApiComponent $GithubApi;
41+
42+
private ArrayLog $logCapture;
43+
44+
private mixed $originalErrorLogConfig;
45+
46+
public function setUp(): void
47+
{
48+
parent::setUp();
49+
50+
$this->loadRoutes();
51+
$this->GithubApi = new GithubApiComponent(new ComponentRegistry());
52+
53+
$this->originalErrorLogConfig = Log::getConfig('error');
54+
Log::drop('error');
55+
Log::setConfig('error', [
56+
'className' => ArrayLog::class,
57+
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
58+
]);
59+
$errorLog = Log::engine('error');
60+
$this->assertInstanceOf(ArrayLog::class, $errorLog);
61+
$this->logCapture = $errorLog;
62+
$this->logCapture->clear();
63+
}
64+
65+
public function tearDown(): void
66+
{
67+
$this->cleanupMockResponses();
68+
69+
Log::drop('error');
70+
if (is_array($this->originalErrorLogConfig)) {
71+
Log::setConfig('error', $this->originalErrorLogConfig);
72+
}
73+
74+
parent::tearDown();
75+
}
76+
77+
/** @return array<string, array{string}> */
78+
public static function writePermissionProvider(): array
79+
{
80+
return [
81+
'admin' => ['admin'],
82+
'maintain' => ['maintain'],
83+
'write' => ['write'],
84+
];
85+
}
86+
87+
/** @return array<string, array{string}> */
88+
public static function readOnlyPermissionProvider(): array
89+
{
90+
return [
91+
'read' => ['read'],
92+
'triage' => ['triage'],
93+
'none' => ['none'],
94+
'unknown' => ['some-new-role'],
95+
];
96+
}
97+
98+
/**
99+
* A collaborator with a writing permission is allowed to commit.
100+
*/
101+
#[DataProvider('writePermissionProvider')]
102+
public function testCanCommitToAllowsWritingPermissions(string $permission): void
103+
{
104+
$this->mockClientGet(
105+
self::PERMISSION_URL,
106+
$this->newClientResponse(200, [], json_encode(['permission' => $permission, 'role_name' => $permission])),
107+
);
108+
109+
$this->assertTrue($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
110+
$this->assertSame([], $this->logCapture->read());
111+
}
112+
113+
/**
114+
* A collaborator with a read only permission may not commit.
115+
*
116+
* The permission endpoint is the only one called: the collaborators endpoint
117+
* answers 204 for every collaborator, read-only ones included.
118+
*/
119+
#[DataProvider('readOnlyPermissionProvider')]
120+
public function testCanCommitToRejectsReadOnlyPermissions(string $permission): void
121+
{
122+
$this->mockClientGet(
123+
self::PERMISSION_URL,
124+
$this->newClientResponse(200, [], json_encode(['permission' => $permission, 'role_name' => $permission])),
125+
);
126+
127+
$this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
128+
}
129+
130+
/**
131+
* Role names are accepted when the permission field is not available.
132+
*/
133+
public function testCanCommitToUsesRoleNameWhenPermissionIsMissing(): void
134+
{
135+
$this->mockClientGet(
136+
self::PERMISSION_URL,
137+
$this->newClientResponse(200, [], json_encode(['role_name' => 'write'])),
138+
);
139+
140+
$this->assertTrue($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
141+
}
142+
143+
/**
144+
* Any answer that is not a 200 means no access and is logged.
145+
*/
146+
public function testCanCommitToRejectsAndLogsNonOkAnswers(): void
147+
{
148+
$this->mockClientGet(
149+
self::PERMISSION_URL,
150+
$this->newClientResponse(404, [], json_encode(['message' => 'Not Found'])),
151+
);
152+
153+
$this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
154+
155+
$logs = $this->logCapture->read();
156+
$this->assertCount(1, $logs);
157+
$this->assertStringContainsString('404', $logs[0]);
158+
}
159+
160+
/**
161+
* A server error is not mistaken for a granted permission either.
162+
*/
163+
public function testCanCommitToRejectsServerError(): void
164+
{
165+
$this->mockClientGet(
166+
self::PERMISSION_URL,
167+
$this->newClientResponse(500, [], json_encode(['message' => 'Server Error'])),
168+
);
169+
170+
$this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
171+
$this->assertCount(1, $this->logCapture->read());
172+
}
173+
174+
/**
175+
* The collaborator endpoint must not be used any more.
176+
*/
177+
public function testCanCommitToDoesNotCallTheCollaboratorEndpoint(): void
178+
{
179+
$this->mockClientGet(
180+
'https://api.github.com/repos/phpmyadmin/phpmyadmin/collaborators/pma-bot',
181+
$this->newClientResponse(204, [], ''),
182+
);
183+
$this->mockClientGet(
184+
self::PERMISSION_URL,
185+
$this->newClientResponse(200, [], json_encode(['permission' => 'read'])),
186+
);
187+
188+
$this->assertFalse($this->GithubApi->canCommitTo('pma-bot', 'phpmyadmin/phpmyadmin', 'token'));
189+
}
190+
191+
/**
192+
* The authorize url can carry the OAuth state.
193+
*/
194+
public function testGetRedirectUrlContainsTheState(): void
195+
{
196+
$url = $this->GithubApi->getRedirectUrl('public_repo', 'the-state');
197+
198+
$this->assertStringContainsString('state=the-state', $url);
199+
}
200+
}

0 commit comments

Comments
 (0)