OpenLocalEditorController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\Controller;
  8. use OCA\Files\Db\OpenLocalEditor;
  9. use OCA\Files\Db\OpenLocalEditorMapper;
  10. use OCP\AppFramework\Db\DoesNotExistException;
  11. use OCP\AppFramework\Http;
  12. use OCP\AppFramework\Http\Attribute\BruteForceProtection;
  13. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  14. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  15. use OCP\AppFramework\Http\DataResponse;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\AppFramework\Utility\ITimeFactory;
  18. use OCP\DB\Exception;
  19. use OCP\IRequest;
  20. use OCP\Security\ISecureRandom;
  21. use Psr\Log\LoggerInterface;
  22. class OpenLocalEditorController extends OCSController {
  23. public const TOKEN_LENGTH = 128;
  24. public const TOKEN_DURATION = 600; // 10 Minutes
  25. public const TOKEN_RETRIES = 50;
  26. protected ITimeFactory $timeFactory;
  27. protected OpenLocalEditorMapper $mapper;
  28. protected ISecureRandom $secureRandom;
  29. protected LoggerInterface $logger;
  30. protected ?string $userId;
  31. public function __construct(
  32. string $appName,
  33. IRequest $request,
  34. ITimeFactory $timeFactory,
  35. OpenLocalEditorMapper $mapper,
  36. ISecureRandom $secureRandom,
  37. LoggerInterface $logger,
  38. ?string $userId
  39. ) {
  40. parent::__construct($appName, $request);
  41. $this->timeFactory = $timeFactory;
  42. $this->mapper = $mapper;
  43. $this->secureRandom = $secureRandom;
  44. $this->logger = $logger;
  45. $this->userId = $userId;
  46. }
  47. /**
  48. * Create a local editor
  49. *
  50. * @param string $path Path of the file
  51. *
  52. * @return DataResponse<Http::STATUS_OK, array{userId: ?string, pathHash: string, expirationTime: int, token: string}, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
  53. *
  54. * 200: Local editor returned
  55. */
  56. #[NoAdminRequired]
  57. #[UserRateLimit(limit: 10, period: 120)]
  58. public function create(string $path): DataResponse {
  59. $pathHash = sha1($path);
  60. $entity = new OpenLocalEditor();
  61. $entity->setUserId($this->userId);
  62. $entity->setPathHash($pathHash);
  63. $entity->setExpirationTime($this->timeFactory->getTime() + self::TOKEN_DURATION); // Expire in 10 minutes
  64. for ($i = 1; $i <= self::TOKEN_RETRIES; $i++) {
  65. $token = $this->secureRandom->generate(self::TOKEN_LENGTH, ISecureRandom::CHAR_ALPHANUMERIC);
  66. $entity->setToken($token);
  67. try {
  68. $this->mapper->insert($entity);
  69. return new DataResponse([
  70. 'userId' => $this->userId,
  71. 'pathHash' => $pathHash,
  72. 'expirationTime' => $entity->getExpirationTime(),
  73. 'token' => $entity->getToken(),
  74. ]);
  75. } catch (Exception $e) {
  76. if ($e->getCode() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
  77. // Only retry on unique constraint violation
  78. throw $e;
  79. }
  80. }
  81. }
  82. $this->logger->error('Giving up after ' . self::TOKEN_RETRIES . ' retries to generate a unique local editor token for path hash: ' . $pathHash);
  83. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  84. }
  85. /**
  86. * Validate a local editor
  87. *
  88. * @param string $path Path of the file
  89. * @param string $token Token of the local editor
  90. *
  91. * @return DataResponse<Http::STATUS_OK, array{userId: string, pathHash: string, expirationTime: int, token: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
  92. *
  93. * 200: Local editor validated successfully
  94. * 404: Local editor not found
  95. */
  96. #[NoAdminRequired]
  97. #[BruteForceProtection(action: 'openLocalEditor')]
  98. public function validate(string $path, string $token): DataResponse {
  99. $pathHash = sha1($path);
  100. try {
  101. $entity = $this->mapper->verifyToken($this->userId, $pathHash, $token);
  102. } catch (DoesNotExistException $e) {
  103. $response = new DataResponse([], Http::STATUS_NOT_FOUND);
  104. $response->throttle(['userId' => $this->userId, 'pathHash' => $pathHash]);
  105. return $response;
  106. }
  107. $this->mapper->delete($entity);
  108. if ($entity->getExpirationTime() <= $this->timeFactory->getTime()) {
  109. $response = new DataResponse([], Http::STATUS_NOT_FOUND);
  110. $response->throttle(['userId' => $this->userId, 'pathHash' => $pathHash]);
  111. return $response;
  112. }
  113. return new DataResponse([
  114. 'userId' => $this->userId,
  115. 'pathHash' => $pathHash,
  116. 'expirationTime' => $entity->getExpirationTime(),
  117. 'token' => $entity->getToken(),
  118. ]);
  119. }
  120. }