Skip to content

Commit 7bdd366

Browse files
Merge pull request #17575 from nextcloud/bugfix/noid/expect-nonce
fix(hostedhpb): Expect nonce on request
2 parents ba1e3aa + e9cf891 commit 7bdd366

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

lib/Controller/HostedSignalingServerController.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use OCA\Talk\Service\HostedSignalingServerService;
1616
use OCP\AppFramework\Http;
1717
use OCP\AppFramework\Http\Attribute\ApiRoute;
18+
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
1819
use OCP\AppFramework\Http\Attribute\OpenAPI;
1920
use OCP\AppFramework\Http\Attribute\PublicPage;
21+
use OCP\AppFramework\Http\Attribute\RequestHeader;
2022
use OCP\AppFramework\Http\DataResponse;
2123
use OCP\AppFramework\OCSController;
2224
use OCP\Http\Client\IClientService;
@@ -42,28 +44,44 @@ public function __construct(
4244
/**
4345
* Get the authentication credentials
4446
*
45-
* @return DataResponse<Http::STATUS_OK, array{nonce: string}, array{}>|DataResponse<Http::STATUS_PRECONDITION_FAILED, null, array{}>
47+
* @return DataResponse<Http::STATUS_OK, array{nonce: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_PRECONDITION_FAILED, null, array{}>
4648
*
4749
* 200: Authentication credentials returned
50+
* 403: Provided nonce is wrong
4851
* 412: Getting authentication credentials is not possible
4952
*/
5053
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
5154
#[PublicPage]
55+
#[BruteForceProtection(action: 'hosted-hpb-nonce')]
56+
#[RequestHeader(name: 'x-account-service-nonce', description: 'Random string provided to the hostedsignalingserver entity, so it can verify that it was requested')]
5257
#[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/hostedsignalingserver/auth', requirements: [
5358
'apiVersion' => '(v1)',
5459
])]
5560
public function auth(): DataResponse {
61+
$sentNonce = $this->request->getHeader('x-account-service-nonce');
62+
if ($sentNonce === '') {
63+
$response = new DataResponse(null, Http::STATUS_FORBIDDEN);
64+
$response->throttle();
65+
return $response;
66+
}
67+
5668
$storedNonce = $this->config->getAppValue('spreed', 'hosted-signaling-server-nonce', '');
57-
// reset nonce after one request
58-
$this->config->deleteAppValue('spreed', 'hosted-signaling-server-nonce');
69+
if ($storedNonce === '') {
70+
return new DataResponse(null, Http::STATUS_PRECONDITION_FAILED);
71+
}
5972

60-
if ($storedNonce !== '') {
61-
return new DataResponse([
62-
'nonce' => $storedNonce,
63-
]);
73+
if (!hash_equals($storedNonce, $sentNonce)) {
74+
$response = new DataResponse(null, Http::STATUS_FORBIDDEN);
75+
$response->throttle();
76+
return $response;
6477
}
6578

66-
return new DataResponse(null, Http::STATUS_PRECONDITION_FAILED);
79+
// reset nonce after one request
80+
$this->config->deleteAppValue('spreed', 'hosted-signaling-server-nonce');
81+
82+
return new DataResponse([
83+
'nonce' => $storedNonce,
84+
]);
6785
}
6886

6987
/**

0 commit comments

Comments
 (0)