Skip to content

Commit 271b4f4

Browse files
williamdesDeepSeek
andcommitted
security: Tie the OAuth callback to the session that started it
The login did not send a state parameter since the first version of the flow, and df83038 made it destroy the session without renewing it: a callback could be replayed from another browser and sign the user in as the account of somebody else (login CSRF). The scope asked for went far beyond what the application does when 51c6b68 added read:org and repo for the org membership call, which no longer exists: no read:org and no user:email consumer is left, the issue and comment routes only need public_repo. Send a random state, compare it with hash_equals() and forget it whatever the answer, renew the session before the developer is written in it, and only redirect to a page of this application after the login. Signed-off-by: William Desportes <williamdes@wdes.fr> Co-Authored-By: DeepSeek <noreply@deepseek.com>
1 parent 4238543 commit 271b4f4

4 files changed

Lines changed: 265 additions & 60 deletions

File tree

‎src/Controller/DevelopersController.php‎

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
use Cake\Datasource\EntityInterface;
2424
use Cake\Http\Response;
2525

26+
use function bin2hex;
27+
use function hash_equals;
28+
use function is_array;
29+
use function is_string;
30+
use function parse_url;
31+
use function random_bytes;
32+
use function str_starts_with;
33+
use function strcasecmp;
34+
35+
use const PHP_URL_HOST;
36+
2637
/**
2738
* Developer controller handling developer login/logout/register.
2839
*
@@ -50,14 +61,41 @@ public function initialize(): void
5061

5162
public function login(): Response
5263
{
53-
$this->request->getSession()->destroy();
54-
$url = $this->GithubApi->getRedirectUrl('user:email,public_repo,read:org,repo');
64+
$session = $this->request->getSession();
65+
$session->destroy();
66+
67+
$state = bin2hex(random_bytes(32));
68+
$session->write('oauth_state', $state);
69+
70+
$url = $this->GithubApi->getRedirectUrl('public_repo', $state);
5571

5672
return $this->redirect($url);
5773
}
5874

5975
public function callback(): Response|null
6076
{
77+
$session = $this->request->getSession();
78+
$state = $this->request->getQuery('state');
79+
$expectedState = $session->read('oauth_state');
80+
// The state is single use, whether the check succeeds or not.
81+
$session->delete('oauth_state');
82+
83+
if (
84+
! is_string($state)
85+
|| ! is_string($expectedState)
86+
|| $expectedState === ''
87+
|| ! hash_equals($expectedState, $state)
88+
) {
89+
$flash_class = 'alert alert-error';
90+
$this->Flash->set(
91+
'We were not able to verify your login attempt.'
92+
. ' Please try again later',
93+
['params' => ['class' => $flash_class]],
94+
);
95+
96+
return $this->redirect('/');
97+
}
98+
6199
$code = $this->request->getQuery('code');
62100
$accessToken = $this->GithubApi->getAccessToken($code);
63101
if (empty($code) || empty($accessToken)) {
@@ -97,14 +135,42 @@ public function callback(): Response|null
97135
);
98136

99137
$last_page = $this->request->getSession()->read('last_page');
100-
if (empty($last_page)) {
101-
$last_page = [
102-
'controller' => 'reports',
103-
'action' => 'index',
104-
];
138+
// Only redirect inside this application after the login.
139+
if (is_array($last_page) || (is_string($last_page) && $this->isLocalUrl($last_page))) {
140+
return $this->redirect($last_page);
105141
}
106142

107-
return $this->redirect($last_page);
143+
return $this->redirect([
144+
'controller' => 'reports',
145+
'action' => 'index',
146+
]);
147+
}
148+
149+
/**
150+
* Whether a URL points inside this application.
151+
*
152+
* A relative path is accepted, as is an absolute URL whose host is the
153+
* configured application host. Anything else, including protocol relative
154+
* URLs, is rejected.
155+
*
156+
* @param string $url the URL to check
157+
*
158+
* @return bool true when the URL belongs to this application
159+
*/
160+
protected function isLocalUrl(string $url): bool
161+
{
162+
if (str_starts_with($url, '/')) {
163+
return ! str_starts_with($url, '//');
164+
}
165+
166+
$host = parse_url($url, PHP_URL_HOST);
167+
if (! is_string($host)) {
168+
return false;
169+
}
170+
171+
$baseHost = parse_url((string) Configure::read('App.fullBaseUrl'), PHP_URL_HOST);
172+
173+
return is_string($baseHost) && strcasecmp($host, $baseHost) === 0;
108174
}
109175

110176
public function logout(): Response
@@ -127,9 +193,11 @@ protected function authenticateDeveloper(array $userInfo, string $accessToken):
127193
$developer = $this->Developers->newEmptyEntity();
128194
}
129195

130-
$developerId = $this->Developers->saveFromGithub($userInfo, $accessToken, $developer);
131-
$this->request->getSession()->write('Developer.id', $developerId);
132-
$this->request->getSession()->write('access_token', $accessToken);
133-
$this->request->getSession()->write('read_only', ! $userInfo['has_commit_access']);
196+
$developerId = $this->Developers->saveFromGithub($userInfo, $developer);
197+
$session = $this->request->getSession();
198+
$session->renew();
199+
$session->write('Developer.id', $developerId);
200+
$session->write('access_token', $accessToken);
201+
$session->write('read_only', ! $userInfo['has_commit_access']);
134202
}
135203
}

‎src/Model/Table/DevelopersTable.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function saveFromGithub(array $githubInfo, string $accessToken, EntityInt
4242
$developer->patch([
4343
'full_name' => $githubInfo['name'],
4444
'gravatar_id' => $githubInfo['gravatar_id'],
45-
'email' => $githubInfo['email'],
45+
// The login no longer asks for user:email, so /user only returns
46+
// the address when the developer made it public.
47+
'email' => $githubInfo['email'] ?? null,
4648
'github_id' => $githubInfo['id'],
4749
'access_token' => $accessToken,
4850
'has_commit_access' => $githubInfo['has_commit_access'] ? 1 : 0,

0 commit comments

Comments
 (0)