Skip to content

Commit

Permalink
fix: Add brute force protection to form endpoints
Browse files Browse the repository at this point in the history
Endpoints that query for forms are now protected against brute force
attacks to find valid forms, invalid hashes or IDs.

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Aug 7, 2024
1 parent 75f51b1 commit 5b50cec
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\IMapperException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
Expand Down Expand Up @@ -132,23 +134,23 @@ public function getSharedForms(): DataResponse {
* @CORS
* @NoAdminRequired
*
* Get a partial form by its hash. Implicitely checks, if the user has access.
* Get a partial form by its hash. Implicitly checks, if the user has access.
*
* @param string $hash The form hash
* @return DataResponse
* @throws OCSBadRequestException if forbidden or not found
*/
#[BruteForceProtection(action: 'form')]

Check warning on line 142 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L142

Added line #L142 was not covered by tests
public function getPartialForm(string $hash): DataResponse {
try {
$form = $this->formMapper->findByHash($hash);
} catch (IMapperException $e) {
$this->logger->debug('Could not find form');
throw new OCSBadRequestException();
return $this->throttledResponse(Http::STATUS_NOT_FOUND);

Check warning on line 148 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L148

Added line #L148 was not covered by tests
}

if (!$this->formsService->hasUserAccess($form)) {
$this->logger->debug('User has no permissions to get this form');
throw new OCSForbiddenException();
return $this->throttledResponse(Http::STATUS_NOT_FOUND);

Check warning on line 153 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L153

Added line #L153 was not covered by tests
}

return new DataResponse($this->formsService->getPartialFormArray($form));
Expand All @@ -162,21 +164,20 @@ public function getPartialForm(string $hash): DataResponse {
*
* @param int $id FormId
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
#[BruteForceProtection(action: 'form')]

Check warning on line 168 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L168

Added line #L168 was not covered by tests
public function getForm(int $id): DataResponse {
try {
$form = $this->formMapper->findById($id);
$formData = $this->formsService->getForm($form);
} catch (IMapperException $e) {
$this->logger->debug('Could not find form');
throw new OCSBadRequestException();
return $this->throttledResponse(Http::STATUS_NOT_FOUND);

Check warning on line 175 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L175

Added line #L175 was not covered by tests
}

if (!$this->formsService->hasUserAccess($form)) {
$this->logger->debug('User has no permissions to get this form');
throw new OCSForbiddenException();
return $this->throttledResponse(Http::STATUS_NOT_FOUND);

Check warning on line 180 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L180

Added line #L180 was not covered by tests
}

return new DataResponse($formData);
Expand Down Expand Up @@ -1484,4 +1485,10 @@ private function getFormIfAllowed(int $id): Form {
}
return $form;
}

private function throttledResponse(int $status): DataResponse {
$response = new DataResponse([], $status);
$response->throttle();
return $response;

Check warning on line 1492 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L1489-L1492

Added lines #L1489 - L1492 were not covered by tests
}
}

0 comments on commit 5b50cec

Please sign in to comment.