PublicShareMiddleware.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\AppFramework\Middleware\PublicShare;
  25. use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
  26. use OCP\AppFramework\AuthPublicShareController;
  27. use OCP\AppFramework\Http\NotFoundResponse;
  28. use OCP\AppFramework\Middleware;
  29. use OCP\AppFramework\PublicShareController;
  30. use OCP\Files\NotFoundException;
  31. use OCP\IConfig;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\Security\Bruteforce\IThrottler;
  35. class PublicShareMiddleware extends Middleware {
  36. /** @var IRequest */
  37. private $request;
  38. /** @var ISession */
  39. private $session;
  40. /** @var IConfig */
  41. private $config;
  42. /** @var IThrottler */
  43. private $throttler;
  44. public function __construct(IRequest $request, ISession $session, IConfig $config, IThrottler $throttler) {
  45. $this->request = $request;
  46. $this->session = $session;
  47. $this->config = $config;
  48. $this->throttler = $throttler;
  49. }
  50. public function beforeController($controller, $methodName) {
  51. if (!($controller instanceof PublicShareController)) {
  52. return;
  53. }
  54. $controllerClassPath = explode('\\', get_class($controller));
  55. $controllerShortClass = end($controllerClassPath);
  56. $bruteforceProtectionAction = $controllerShortClass . '::' . $methodName;
  57. $this->throttler->sleepDelayOrThrowOnMax($this->request->getRemoteAddress(), $bruteforceProtectionAction);
  58. if (!$this->isLinkSharingEnabled()) {
  59. throw new NotFoundException('Link sharing is disabled');
  60. }
  61. // We require the token parameter to be set
  62. $token = $this->request->getParam('token');
  63. if ($token === null) {
  64. throw new NotFoundException();
  65. }
  66. // Set the token
  67. $controller->setToken($token);
  68. if (!$controller->isValidToken()) {
  69. $this->throttle($bruteforceProtectionAction, $token);
  70. $controller->shareNotFound();
  71. throw new NotFoundException();
  72. }
  73. // No need to check for authentication when we try to authenticate
  74. if ($methodName === 'authenticate' || $methodName === 'showAuthenticate') {
  75. return;
  76. }
  77. // If authentication succeeds just continue
  78. if ($controller->isAuthenticated()) {
  79. return;
  80. }
  81. // If we can authenticate to this controller do it else we throw a 404 to not leak any info
  82. if ($controller instanceof AuthPublicShareController) {
  83. $this->session->set('public_link_authenticate_redirect', json_encode($this->request->getParams()));
  84. throw new NeedAuthenticationException();
  85. }
  86. $this->throttle($bruteforceProtectionAction, $token);
  87. throw new NotFoundException();
  88. }
  89. public function afterException($controller, $methodName, \Exception $exception) {
  90. if (!($controller instanceof PublicShareController)) {
  91. throw $exception;
  92. }
  93. if ($exception instanceof NotFoundException) {
  94. return new NotFoundResponse();
  95. }
  96. if ($controller instanceof AuthPublicShareController && $exception instanceof NeedAuthenticationException) {
  97. return $controller->getAuthenticationRedirect($this->getFunctionForRoute($this->request->getParam('_route')));
  98. }
  99. throw $exception;
  100. }
  101. private function getFunctionForRoute(string $route): string {
  102. $tmp = explode('.', $route);
  103. return array_pop($tmp);
  104. }
  105. /**
  106. * Check if link sharing is allowed
  107. */
  108. private function isLinkSharingEnabled(): bool {
  109. // Check if the shareAPI is enabled
  110. if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') {
  111. return false;
  112. }
  113. // Check whether public sharing is enabled
  114. if ($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
  115. return false;
  116. }
  117. return true;
  118. }
  119. private function throttle($bruteforceProtectionAction, $token): void {
  120. $ip = $this->request->getRemoteAddress();
  121. $this->throttler->sleepDelay($ip, $bruteforceProtectionAction);
  122. $this->throttler->registerAttempt($bruteforceProtectionAction, $ip, ['token' => $token]);
  123. }
  124. }