2014-05-08 13:47:18 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2015-02-23 13:28:53 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-05-08 13:47:18 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-05-08 13:47:18 +04:00
|
|
|
namespace OC\AppFramework\Middleware\Security;
|
|
|
|
|
2015-11-28 13:06:46 +03:00
|
|
|
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
|
2014-05-11 19:55:59 +04:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2016-06-17 12:01:35 +03:00
|
|
|
use OC\Authentication\Exceptions\PasswordLoginForbiddenException;
|
2016-07-20 19:36:15 +03:00
|
|
|
use OC\Security\Bruteforce\Throttler;
|
2016-05-25 10:58:01 +03:00
|
|
|
use OC\User\Session;
|
2015-07-20 13:54:22 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2014-05-08 13:47:18 +04:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\AppFramework\Middleware;
|
2016-05-25 10:58:01 +03:00
|
|
|
use OCP\IRequest;
|
2014-05-08 13:47:18 +04:00
|
|
|
|
|
|
|
/**
|
2015-05-22 14:17:27 +03:00
|
|
|
* This middleware sets the correct CORS headers on a response if the
|
2014-05-08 13:47:18 +04:00
|
|
|
* controller has the @CORS annotation. This is needed for webapps that want
|
2016-04-07 20:51:27 +03:00
|
|
|
* to access an API and don't run on the same domain, see
|
2014-05-08 13:47:18 +04:00
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
|
|
|
|
*/
|
|
|
|
class CORSMiddleware extends Middleware {
|
2016-07-20 19:36:15 +03:00
|
|
|
/** @var IRequest */
|
2014-05-08 13:47:18 +04:00
|
|
|
private $request;
|
2016-07-20 19:36:15 +03:00
|
|
|
/** @var ControllerMethodReflector */
|
2014-05-11 19:55:59 +04:00
|
|
|
private $reflector;
|
2016-07-20 19:36:15 +03:00
|
|
|
/** @var Session */
|
2015-05-22 14:17:27 +03:00
|
|
|
private $session;
|
2016-07-20 19:36:15 +03:00
|
|
|
/** @var Throttler */
|
|
|
|
private $throttler;
|
2015-05-22 14:17:27 +03:00
|
|
|
|
2014-05-08 13:47:18 +04:00
|
|
|
/**
|
2014-05-11 16:03:58 +04:00
|
|
|
* @param IRequest $request
|
2014-05-11 19:55:59 +04:00
|
|
|
* @param ControllerMethodReflector $reflector
|
2016-05-25 10:58:01 +03:00
|
|
|
* @param Session $session
|
2016-07-20 19:36:15 +03:00
|
|
|
* @param Throttler $throttler
|
2014-05-08 13:47:18 +04:00
|
|
|
*/
|
2015-05-22 14:17:27 +03:00
|
|
|
public function __construct(IRequest $request,
|
2015-07-20 13:54:22 +03:00
|
|
|
ControllerMethodReflector $reflector,
|
2016-07-20 19:36:15 +03:00
|
|
|
Session $session,
|
|
|
|
Throttler $throttler) {
|
2014-05-08 13:47:18 +04:00
|
|
|
$this->request = $request;
|
2014-05-11 19:55:59 +04:00
|
|
|
$this->reflector = $reflector;
|
2015-05-22 14:17:27 +03:00
|
|
|
$this->session = $session;
|
2016-07-20 19:36:15 +03:00
|
|
|
$this->throttler = $throttler;
|
2014-05-08 13:47:18 +04:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:17:27 +03:00
|
|
|
/**
|
|
|
|
* This is being run in normal order before the controller is being
|
|
|
|
* called which allows several modifications and checks
|
|
|
|
*
|
|
|
|
* @param Controller $controller the controller that is being called
|
|
|
|
* @param string $methodName the name of the method that will be called on
|
|
|
|
* the controller
|
2015-07-20 13:54:22 +03:00
|
|
|
* @throws SecurityException
|
2015-05-22 14:17:27 +03:00
|
|
|
* @since 6.0.0
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function beforeController($controller, $methodName) {
|
2015-05-22 14:17:27 +03:00
|
|
|
// ensure that @CORS annotated API routes are not used in conjunction
|
|
|
|
// with session authentication since this enables CSRF attack vectors
|
|
|
|
if ($this->reflector->hasAnnotation('CORS') &&
|
2020-04-10 15:19:56 +03:00
|
|
|
!$this->reflector->hasAnnotation('PublicPage')) {
|
2015-05-22 14:17:27 +03:00
|
|
|
$user = $this->request->server['PHP_AUTH_USER'];
|
|
|
|
$pass = $this->request->server['PHP_AUTH_PW'];
|
|
|
|
|
|
|
|
$this->session->logout();
|
2016-06-17 12:01:35 +03:00
|
|
|
try {
|
2016-07-20 19:36:15 +03:00
|
|
|
if (!$this->session->logClientIn($user, $pass, $this->request, $this->throttler)) {
|
2016-06-17 12:01:35 +03:00
|
|
|
throw new SecurityException('CORS requires basic auth', Http::STATUS_UNAUTHORIZED);
|
|
|
|
}
|
|
|
|
} catch (PasswordLoginForbiddenException $ex) {
|
|
|
|
throw new SecurityException('Password login forbidden, use token instead', Http::STATUS_UNAUTHORIZED);
|
2015-05-22 14:17:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 13:47:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is being run after a successful controllermethod call and allows
|
|
|
|
* the manipulation of a Response object. The middleware is run in reverse order
|
|
|
|
*
|
|
|
|
* @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 Response $response the generated response from the controller
|
|
|
|
* @return Response a Response object
|
2015-07-20 13:54:22 +03:00
|
|
|
* @throws SecurityException
|
2014-05-08 13:47:18 +04:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function afterController($controller, $methodName, Response $response) {
|
2014-05-08 13:47:18 +04:00
|
|
|
// only react if its a CORS request and if the request sends origin and
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (isset($this->request->server['HTTP_ORIGIN']) &&
|
2014-05-11 19:55:59 +04:00
|
|
|
$this->reflector->hasAnnotation('CORS')) {
|
2014-05-08 13:47:18 +04:00
|
|
|
|
2015-05-22 14:17:27 +03:00
|
|
|
// allow credentials headers must not be true or CSRF is possible
|
2014-05-08 13:47:18 +04:00
|
|
|
// otherwise
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($response->getHeaders() as $header => $value) {
|
|
|
|
if (strtolower($header) === 'access-control-allow-credentials' &&
|
2014-05-08 13:47:18 +04:00
|
|
|
strtolower(trim($value)) === 'true') {
|
|
|
|
$msg = 'Access-Control-Allow-Credentials must not be '.
|
2014-05-11 19:55:59 +04:00
|
|
|
'set to true in order to prevent CSRF';
|
2014-05-08 13:47:18 +04:00
|
|
|
throw new SecurityException($msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$origin = $this->request->server['HTTP_ORIGIN'];
|
|
|
|
$response->addHeader('Access-Control-Allow-Origin', $origin);
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:54:22 +03:00
|
|
|
/**
|
|
|
|
* If an SecurityException is being caught return a JSON error response
|
|
|
|
*
|
|
|
|
* @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 20:51:27 +03:00
|
|
|
* @throws \Exception the passed in exception if it can't handle it
|
2015-07-20 13:54:22 +03:00
|
|
|
* @return Response a Response object or null in case that the exception could not be handled
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function afterException($controller, $methodName, \Exception $exception) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($exception instanceof SecurityException) {
|
2020-10-05 16:12:57 +03:00
|
|
|
$response = new JSONResponse(['message' => $exception->getMessage()]);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($exception->getCode() !== 0) {
|
2015-07-20 13:54:22 +03:00
|
|
|
$response->setStatus($exception->getCode());
|
|
|
|
} else {
|
|
|
|
$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
2014-05-08 13:47:18 +04:00
|
|
|
}
|