2013-08-17 09:16:48 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-08-17 09:16:48 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-08-17 09:16:48 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2017-04-12 20:14:11 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
namespace OC\AppFramework\Middleware\Security;
|
|
|
|
|
2016-04-22 13:28:48 +00:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\AppNotEnabledException;
|
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException;
|
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
|
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
|
2016-07-20 15:37:30 +00:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\StrictCookieMissingException;
|
2014-05-06 14:29:19 +00:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2016-01-28 13:33:02 +00:00
|
|
|
use OC\Security\CSP\ContentSecurityPolicyManager;
|
2016-10-25 19:36:17 +00:00
|
|
|
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
|
2016-10-24 09:00:00 +00:00
|
|
|
use OC\Security\CSRF\CsrfTokenManager;
|
2017-10-23 21:40:17 +00:00
|
|
|
use OCP\App\IAppManager;
|
2016-01-28 13:33:02 +00:00
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
2016-09-15 09:26:24 +00:00
|
|
|
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
|
2014-04-20 14:12:46 +00:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2015-11-28 10:06:46 +00:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2013-10-05 14:59:06 +00:00
|
|
|
use OCP\AppFramework\Middleware;
|
2013-08-20 23:00:26 +00:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2016-07-29 11:41:30 +00:00
|
|
|
use OCP\AppFramework\OCSController;
|
2018-02-26 14:32:17 +00:00
|
|
|
use OCP\IL10N;
|
2014-05-28 00:12:01 +00:00
|
|
|
use OCP\INavigationManager;
|
|
|
|
use OCP\IURLGenerator;
|
2013-10-06 22:33:54 +00:00
|
|
|
use OCP\IRequest;
|
2014-05-28 00:12:01 +00:00
|
|
|
use OCP\ILogger;
|
2014-11-14 16:20:51 +00:00
|
|
|
use OCP\AppFramework\Controller;
|
2014-11-17 14:10:47 +00:00
|
|
|
use OCP\Util;
|
2015-11-28 10:06:46 +00:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to do all the authentication and checking stuff for a controller method
|
|
|
|
* It reads out the annotations of a controller method and checks which if
|
|
|
|
* security things should be checked and also handles errors in case a security
|
|
|
|
* check fails
|
|
|
|
*/
|
|
|
|
class SecurityMiddleware extends Middleware {
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var INavigationManager */
|
2014-05-28 00:12:01 +00:00
|
|
|
private $navigationManager;
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var IRequest */
|
2013-08-17 09:16:48 +00:00
|
|
|
private $request;
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var ControllerMethodReflector */
|
2014-05-06 17:13:59 +00:00
|
|
|
private $reflector;
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var string */
|
2014-05-28 00:12:01 +00:00
|
|
|
private $appName;
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var IURLGenerator */
|
2014-05-28 00:12:01 +00:00
|
|
|
private $urlGenerator;
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var ILogger */
|
2014-05-28 00:12:01 +00:00
|
|
|
private $logger;
|
2017-04-12 20:14:11 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $isLoggedIn;
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var bool */
|
2014-05-28 00:12:01 +00:00
|
|
|
private $isAdminUser;
|
2016-01-28 13:33:02 +00:00
|
|
|
/** @var ContentSecurityPolicyManager */
|
|
|
|
private $contentSecurityPolicyManager;
|
2016-10-24 09:00:00 +00:00
|
|
|
/** @var CsrfTokenManager */
|
|
|
|
private $csrfTokenManager;
|
2016-10-25 19:36:17 +00:00
|
|
|
/** @var ContentSecurityPolicyNonceManager */
|
|
|
|
private $cspNonceManager;
|
2017-10-23 21:40:17 +00:00
|
|
|
/** @var IAppManager */
|
|
|
|
private $appManager;
|
2018-02-26 14:32:17 +00:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
2014-05-06 17:13:59 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
/**
|
2013-10-06 22:33:54 +00:00
|
|
|
* @param IRequest $request
|
2014-05-06 14:29:19 +00:00
|
|
|
* @param ControllerMethodReflector $reflector
|
2014-05-28 00:12:01 +00:00
|
|
|
* @param INavigationManager $navigationManager
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param string $appName
|
2017-04-12 20:14:11 +00:00
|
|
|
* @param bool $isLoggedIn
|
2014-05-28 00:12:01 +00:00
|
|
|
* @param bool $isAdminUser
|
2016-01-28 13:33:02 +00:00
|
|
|
* @param ContentSecurityPolicyManager $contentSecurityPolicyManager
|
2016-10-24 09:00:00 +00:00
|
|
|
* @param CSRFTokenManager $csrfTokenManager
|
2016-10-25 19:36:17 +00:00
|
|
|
* @param ContentSecurityPolicyNonceManager $cspNonceManager
|
2017-10-23 21:40:17 +00:00
|
|
|
* @param IAppManager $appManager
|
2018-02-26 14:32:17 +00:00
|
|
|
* @param IL10N $l10n
|
2013-08-17 09:16:48 +00:00
|
|
|
*/
|
2014-05-28 00:12:01 +00:00
|
|
|
public function __construct(IRequest $request,
|
2015-07-20 10:54:22 +00:00
|
|
|
ControllerMethodReflector $reflector,
|
|
|
|
INavigationManager $navigationManager,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
ILogger $logger,
|
|
|
|
$appName,
|
2017-04-12 20:14:11 +00:00
|
|
|
$isLoggedIn,
|
2016-01-28 13:33:02 +00:00
|
|
|
$isAdminUser,
|
2016-10-24 09:00:00 +00:00
|
|
|
ContentSecurityPolicyManager $contentSecurityPolicyManager,
|
2016-10-25 19:36:17 +00:00
|
|
|
CsrfTokenManager $csrfTokenManager,
|
2017-10-23 21:40:17 +00:00
|
|
|
ContentSecurityPolicyNonceManager $cspNonceManager,
|
2018-02-26 14:32:17 +00:00
|
|
|
IAppManager $appManager,
|
|
|
|
IL10N $l10n
|
2017-12-13 13:41:56 +00:00
|
|
|
) {
|
2014-05-28 00:12:01 +00:00
|
|
|
$this->navigationManager = $navigationManager;
|
2013-08-17 09:16:48 +00:00
|
|
|
$this->request = $request;
|
2014-05-06 14:29:19 +00:00
|
|
|
$this->reflector = $reflector;
|
2014-05-28 00:12:01 +00:00
|
|
|
$this->appName = $appName;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->logger = $logger;
|
2017-04-12 20:14:11 +00:00
|
|
|
$this->isLoggedIn = $isLoggedIn;
|
2014-05-28 00:12:01 +00:00
|
|
|
$this->isAdminUser = $isAdminUser;
|
2016-01-28 13:33:02 +00:00
|
|
|
$this->contentSecurityPolicyManager = $contentSecurityPolicyManager;
|
2016-10-24 09:00:00 +00:00
|
|
|
$this->csrfTokenManager = $csrfTokenManager;
|
2016-10-25 19:36:17 +00:00
|
|
|
$this->cspNonceManager = $cspNonceManager;
|
2017-10-23 21:40:17 +00:00
|
|
|
$this->appManager = $appManager;
|
2018-02-26 14:32:17 +00:00
|
|
|
$this->l10n = $l10n;
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This runs all the security checks before a method call. The
|
|
|
|
* security checks are determined by inspecting the controller method
|
|
|
|
* annotations
|
2016-07-29 11:41:30 +00:00
|
|
|
* @param Controller $controller the controller
|
2013-08-17 09:16:48 +00:00
|
|
|
* @param string $methodName the name of the method
|
|
|
|
* @throws SecurityException when a security check fails
|
|
|
|
*/
|
2017-08-01 15:32:03 +00:00
|
|
|
public function beforeController($controller, $methodName) {
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
// this will set the current navigation entry of the app, use this only
|
|
|
|
// for normal HTML requests and not for AJAX requests
|
2014-05-28 00:12:01 +00:00
|
|
|
$this->navigationManager->setActiveEntry($this->appName);
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
// security checks
|
2014-05-06 14:29:19 +00:00
|
|
|
$isPublicPage = $this->reflector->hasAnnotation('PublicPage');
|
2013-08-20 19:21:21 +00:00
|
|
|
if(!$isPublicPage) {
|
2017-04-12 20:14:11 +00:00
|
|
|
if(!$this->isLoggedIn) {
|
2015-11-28 10:06:46 +00:00
|
|
|
throw new NotLoggedInException();
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 14:29:19 +00:00
|
|
|
if(!$this->reflector->hasAnnotation('NoAdminRequired')) {
|
2014-05-28 00:12:01 +00:00
|
|
|
if(!$this->isAdminUser) {
|
2018-02-26 14:32:17 +00:00
|
|
|
throw new NotAdminException($this->l10n->t('Logged in user must be an admin'));
|
2013-08-20 19:21:21 +00:00
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 15:37:30 +00:00
|
|
|
// Check for strict cookie requirement
|
|
|
|
if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) {
|
|
|
|
if(!$this->request->passesStrictCookieCheck()) {
|
|
|
|
throw new StrictCookieMissingException();
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 14:10:47 +00:00
|
|
|
// CSRF check - also registers the CSRF token since the session may be closed later
|
|
|
|
Util::callRegister();
|
2014-05-06 14:29:19 +00:00
|
|
|
if(!$this->reflector->hasAnnotation('NoCSRFRequired')) {
|
2016-07-29 11:41:30 +00:00
|
|
|
/*
|
|
|
|
* Only allow the CSRF check to fail on OCS Requests. This kind of
|
|
|
|
* hacks around that we have no full token auth in place yet and we
|
|
|
|
* do want to offer CSRF checks for web requests.
|
2018-01-15 21:05:06 +00:00
|
|
|
*
|
|
|
|
* Additionally we allow Bearer authenticated requests to pass on OCS routes.
|
|
|
|
* This allows oauth apps (e.g. moodle) to use the OCS endpoints
|
2016-07-29 11:41:30 +00:00
|
|
|
*/
|
|
|
|
if(!$this->request->passesCSRFCheck() && !(
|
2018-01-15 21:05:06 +00:00
|
|
|
$controller instanceof OCSController && (
|
|
|
|
$this->request->getHeader('OCS-APIREQUEST') === 'true' ||
|
|
|
|
strpos($this->request->getHeader('Authorization'), 'Bearer ') === 0
|
|
|
|
)
|
|
|
|
)) {
|
2015-11-28 10:06:46 +00:00
|
|
|
throw new CrossSiteRequestForgeryException();
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 16:20:51 +00:00
|
|
|
/**
|
|
|
|
* FIXME: Use DI once available
|
2015-11-28 10:06:46 +00:00
|
|
|
* Checks if app is enabled (also includes a check whether user is allowed to access the resource)
|
2014-11-14 16:20:51 +00:00
|
|
|
* The getAppPath() check is here since components such as settings also use the AppFramework and
|
|
|
|
* therefore won't pass this check.
|
2018-02-28 19:26:03 +00:00
|
|
|
* If page is public, app does not need to be enabled for current user/visitor
|
2014-11-14 16:20:51 +00:00
|
|
|
*/
|
2018-02-28 19:26:03 +00:00
|
|
|
if(\OC_App::getAppPath($this->appName) !== false && !$isPublicPage && !$this->appManager->isEnabledForUser($this->appName)) {
|
2015-11-28 10:06:46 +00:00
|
|
|
throw new AppNotEnabledException();
|
2014-11-14 16:20:51 +00:00
|
|
|
}
|
2017-04-12 20:14:11 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 13:33:02 +00:00
|
|
|
/**
|
|
|
|
* Performs the default CSP modifications that may be injected by other
|
|
|
|
* applications
|
|
|
|
*
|
|
|
|
* @param Controller $controller
|
|
|
|
* @param string $methodName
|
|
|
|
* @param Response $response
|
|
|
|
* @return Response
|
|
|
|
*/
|
2017-08-01 15:32:03 +00:00
|
|
|
public function afterController($controller, $methodName, Response $response) {
|
2016-01-28 13:33:02 +00:00
|
|
|
$policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy();
|
|
|
|
|
2016-09-15 09:26:24 +00:00
|
|
|
if (get_class($policy) === EmptyContentSecurityPolicy::class) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2016-01-28 13:33:02 +00:00
|
|
|
$defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy();
|
|
|
|
$defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy);
|
|
|
|
|
2016-10-25 19:36:17 +00:00
|
|
|
if($this->cspNonceManager->browserSupportsCspV3()) {
|
2016-10-24 09:00:00 +00:00
|
|
|
$defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue());
|
|
|
|
}
|
|
|
|
|
2016-01-28 13:33:02 +00:00
|
|
|
$response->setContentSecurityPolicy($defaultPolicy);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If an SecurityException is being caught, ajax requests return a JSON error
|
|
|
|
* response and non ajax requests redirect to the index
|
|
|
|
* @param Controller $controller the controller that is being called
|
|
|
|
* @param string $methodName the name of the method that will be called on
|
|
|
|
* the controller
|
|
|
|
* @param \Exception $exception the thrown exception
|
2016-04-07 17:51:27 +00:00
|
|
|
* @throws \Exception the passed in exception if it can't handle it
|
2013-08-17 09:16:48 +00:00
|
|
|
* @return Response a Response object or null in case that the exception could not be handled
|
|
|
|
*/
|
2017-08-01 15:32:03 +00:00
|
|
|
public function afterException($controller, $methodName, \Exception $exception) {
|
2015-11-28 10:06:46 +00:00
|
|
|
if($exception instanceof SecurityException) {
|
2016-07-20 15:37:30 +00:00
|
|
|
if($exception instanceof StrictCookieMissingException) {
|
|
|
|
return new RedirectResponse(\OC::$WEBROOT);
|
|
|
|
}
|
2015-11-28 10:06:46 +00:00
|
|
|
if (stripos($this->request->getHeader('Accept'),'html') === false) {
|
2013-08-17 09:16:48 +00:00
|
|
|
$response = new JSONResponse(
|
|
|
|
array('message' => $exception->getMessage()),
|
|
|
|
$exception->getCode()
|
|
|
|
);
|
|
|
|
} else {
|
2015-11-28 10:06:46 +00:00
|
|
|
if($exception instanceof NotLoggedInException) {
|
2017-05-15 12:33:27 +00:00
|
|
|
$params = [];
|
|
|
|
if (isset($this->request->server['REQUEST_URI'])) {
|
|
|
|
$params['redirect_url'] = $this->request->server['REQUEST_URI'];
|
|
|
|
}
|
|
|
|
$url = $this->urlGenerator->linkToRoute('core.login.showLoginForm', $params);
|
2015-11-28 10:06:46 +00:00
|
|
|
$response = new RedirectResponse($url);
|
|
|
|
} else {
|
|
|
|
$response = new TemplateResponse('core', '403', ['file' => $exception->getMessage()], 'guest');
|
|
|
|
$response->setStatus($exception->getCode());
|
|
|
|
}
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 14:21:56 +00:00
|
|
|
$this->logger->logException($exception, [
|
|
|
|
'level' => \OCP\Util::DEBUG,
|
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2013-08-17 09:16:48 +00:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|