Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,25 @@

class OCSShareAPIMiddleware extends Middleware {
public function __construct(
private IManager $shareManager,
private IL10N $l,
private readonly IManager $shareManager,
private readonly IL10N $l,
) {
}

/**
* @param Controller $controller
* @param string $methodName
*
* @throws OCSNotFoundException
*/
#[\Override]
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, string $methodName): void {
if ($controller instanceof ShareAPIController) {
if (!$this->shareManager->shareApiEnabled()) {
throw new OCSNotFoundException($this->l->t('Share API is disabled'));
}
}
}

/**
* @param Controller $controller
* @param string $methodName
* @param Response $response
* @return Response
*/
#[\Override]
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, string $methodName, Response $response): Response {
if ($controller instanceof ShareAPIController) {
/** @var ShareAPIController $controller */
$controller->cleanup();
Expand Down
24 changes: 3 additions & 21 deletions apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ public function __construct(
) {
}

/**
* @param Controller $controller
* @param string $methodName
* @throws S2SException
*/
#[\Override]
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, string $methodName): void {
if (!($controller instanceof ShareInfoController)) {
return;
}
Expand All @@ -40,15 +35,8 @@ public function beforeController($controller, $methodName) {
}
}

/**
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @throws \Exception
* @return Response
*/
#[\Override]
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, string $methodName, \Exception $exception): JSONResponse {
if (!($controller instanceof ShareInfoController)) {
throw $exception;
}
Expand All @@ -60,14 +48,8 @@ public function afterException($controller, $methodName, \Exception $exception)
throw $exception;
}

/**
* @param Controller $controller
* @param string $methodName
* @param Response $response
* @return Response
*/
#[\Override]
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, string $methodName, Response $response) {
if (!($controller instanceof ShareInfoController)) {
return $response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
* @throws S2SException
*/
#[\Override]
public function beforeController($controller, $methodName): void {
public function beforeController(Controller $controller, string $methodName): void {
if (!$this->isSharingEnabled()) {
throw new NotFoundException('Sharing is disabled.');
}
Expand All @@ -70,7 +70,7 @@ public function beforeController($controller, $methodName): void {
* @throws \Exception
*/
#[\Override]
public function afterException($controller, $methodName, \Exception $exception): Response {
public function afterException(Controller $controller, string $methodName, \Exception $exception): Response {
if (is_a($exception, NotFoundException::class)) {
return new NotFoundResponse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
* @throws NotSubAdminException
*/
#[\Override]
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, string $methodName): void {
// If AuthorizedAdminSetting, the check will be done in the SecurityMiddleware
if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin && !$this->reflector->hasAnnotationOrAttribute('AuthorizedAdminSetting', AuthorizedAdminSetting::class)) {
throw new NotSubAdminException();
Expand All @@ -56,7 +56,7 @@ public function beforeController($controller, $methodName) {
* @return Response
*/
#[\Override]
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, string $methodName, \Exception $exception) {
if ($exception instanceof NotSubAdminException) {
throw new OCSException($exception->getMessage(), Http::STATUS_FORBIDDEN);
}
Expand Down
5 changes: 3 additions & 2 deletions core/Middleware/TwoFactorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public function __construct(
* @param string $methodName
*/
#[\Override]
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, string $methodName): void {
if ($this->reflector->hasAnnotationOrAttribute('NoTwoFactorRequired', NoTwoFactorRequired::class)) {
// Route handler explicitly marked to work without finished 2FA are
// not blocked
return;
}

/** @psalm-suppress TypeDoesNotContainType The class is defined in a different app */
if ($controller instanceof APIController && $methodName === 'poll') {
// Allow polling the twofactor nextcloud notifications state
return;
Expand Down Expand Up @@ -121,7 +122,7 @@ private function checkTwoFactor(Controller $controller, IUser $user) {
}

#[\Override]
public function afterException($controller, $methodName, Exception $exception) {
public function afterException(Controller $controller, string $methodName, Exception $exception) {
if ($exception instanceof TwoFactorAuthRequiredException) {
$params = [
'redirect_url' => $this->request->getParam('redirect_url'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OC\AppFramework\Middleware;

use OC\Core\Controller\LoginController;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Events\BeforeLoginTemplateRenderedEvent;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\Response;
Expand All @@ -27,7 +28,7 @@ public function __construct(
}

#[\Override]
public function afterController($controller, $methodName, Response $response): Response {
public function afterController(Controller $controller, string $methodName, Response $response): Response {
if ($response instanceof TemplateResponse) {
if ($controller instanceof LoginController) {
$this->dispatcher->dispatchTyped(new BeforeLoginTemplateRenderedEvent($response));
Expand Down
11 changes: 5 additions & 6 deletions lib/private/AppFramework/Middleware/CompressionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OC\AppFramework\Middleware;

use OC\AppFramework\OCS\BaseResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
Expand All @@ -18,18 +19,16 @@
use OCP\IRequest;

class CompressionMiddleware extends Middleware {
/** @var bool */
private $useGZip;
private bool $useGZip = false;

public function __construct(
private IRequest $request,
) {
$this->useGZip = false;
}

#[\Override]
public function afterController($controller, $methodName, Response $response) {
// By default we do not gzip
public function afterController(Controller $controller, string $methodName, Response $response): Response {
// By default, we do not gzip
$allowGzip = false;

// Only return gzipped content for 200 responses
Expand Down Expand Up @@ -63,7 +62,7 @@ public function afterController($controller, $methodName, Response $response) {
}

#[\Override]
public function beforeOutput($controller, $methodName, $output) {
public function beforeOutput(Controller $controller, string $methodName, string $output): string {
if (!$this->useGZip) {
return $output;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OC\AppFramework\Middleware;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
Expand All @@ -22,7 +23,7 @@ public function __construct(
}

#[\Override]
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, string $methodName, Response $response) {
$etagHeader = $this->request->getHeader('IF_NONE_MATCH');
if ($etagHeader !== '' && $response->getETag() !== null && trim($etagHeader) === '"' . $response->getETag() . '"') {
$response->setStatus(Http::STATUS_NOT_MODIFIED);
Expand Down
39 changes: 6 additions & 33 deletions lib/private/AppFramework/Middleware/OCSMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,15 @@
use OCP\IRequest;

class OCSMiddleware extends Middleware {
/** @var int */
private $ocsVersion;
private int $ocsVersion = 2;

/**
* @param IRequest $request
*/
public function __construct(
private IRequest $request,
private readonly IRequest $request,
) {
}

/**
* @param Controller $controller
* @param string $methodName
*/
#[\Override]
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, string $methodName): void {
if ($controller instanceof OCSController) {
if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) {
$this->ocsVersion = 2;
Expand All @@ -48,15 +40,8 @@ public function beforeController($controller, $methodName) {
}
}

/**
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @throws \Exception
* @return BaseResponse
*/
#[\Override]
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, string $methodName, \Exception $exception): Response {
if ($controller instanceof OCSController && $exception instanceof OCSException) {
$code = $exception->getCode();
if ($code === 0) {
Expand All @@ -69,14 +54,8 @@ public function afterException($controller, $methodName, \Exception $exception)
throw $exception;
}

/**
* @param Controller $controller
* @param string $methodName
* @param Response $response
* @return Response
*/
#[\Override]
public function afterController($controller, $methodName, Response $response) {
public function afterController(Controller $controller, string $methodName, Response $response): Response {
/*
* If a different middleware has detected that a request unauthorized or forbidden
* we need to catch the response and convert it to a proper OCS response.
Expand Down Expand Up @@ -105,13 +84,7 @@ public function afterController($controller, $methodName, Response $response) {
return $response;
}

/**
* @param Controller $controller
* @param int $code
* @param string $message
* @return V1Response|V2Response
*/
private function buildNewResponse(Controller $controller, $code, $message) {
private function buildNewResponse(Controller $controller, int $code, string $message): V1Response|V2Response {
$format = $this->request->getFormat();
if ($format === null || !$controller->isResponderRegistered($format)) {
$format = 'xml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -10,28 +12,29 @@
use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
use OCA\Files_Sharing\AppInfo\Application;
use OCP\AppFramework\AuthPublicShareController;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\PublicShareController;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\Security\Bruteforce\IThrottler;

class PublicShareMiddleware extends Middleware {

public function __construct(
private IRequest $request,
private ISession $session,
private IConfig $config,
private IThrottler $throttler,
private readonly IRequest $request,
private readonly ISession $session,
private readonly IAppConfig $appConfig,
private readonly IThrottler $throttler,
) {
}

#[\Override]
public function beforeController($controller, $methodName) {
public function beforeController(Controller $controller, string $methodName): void {
if (!($controller instanceof PublicShareController)) {
return;
}
Expand Down Expand Up @@ -82,7 +85,7 @@ public function beforeController($controller, $methodName) {
}

#[\Override]
public function afterException($controller, $methodName, \Exception $exception) {
public function afterException(Controller $controller, string $methodName, \Exception $exception) {
if (!($controller instanceof PublicShareController)) {
throw $exception;
}
Expand All @@ -109,20 +112,12 @@ private function getFunctionForRoute(string $route): string {
* Check if link sharing is allowed
*/
private function isLinkSharingEnabled(): bool {
// Check if the shareAPI is enabled
if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') {
return false;
}

// Check whether public sharing is enabled
if ($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
return false;
}

return true;
// Check if the shareAPI and public sharing is enabled
return $this->appConfig->getValueBool('core', 'shareapi_enabled', true)
&& $this->appConfig->getValueBool('core', 'shareapi_allow_links', true);
}

private function throttle($bruteforceProtectionAction, $token): void {
private function throttle(string $bruteforceProtectionAction, string $token): void {
$ip = $this->request->getRemoteAddress();
$this->throttler->sleepDelayOrThrowOnMax($ip, $bruteforceProtectionAction);
$this->throttler->registerAttempt($bruteforceProtectionAction, $ip, ['token' => $token]);
Expand Down
Loading
Loading