Completed
Push — master ( 7f40fb...921667 )
by Thomas
10:29
created

CORSMiddleware::afterController()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 6
nop 3
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Bernhard Posselt <[email protected]>
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Stefan Weil <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2018, ownCloud GmbH
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\AppFramework\Middleware\Security;
27
28
use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
29
use OC\AppFramework\Utility\ControllerMethodReflector;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\AppFramework\Http\Response;
34
use OCP\AppFramework\Middleware;
35
use OCP\IRequest;
36
use OCP\IUserSession;
37
use OCP\IConfig;
38
39
/**
40
 * This middleware sets the correct CORS headers on a response if the
41
 * controller has the @CORS annotation. This is needed for webapps that want
42
 * to access an API and don't run on the same domain, see
43
 * https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
44
 */
45
class CORSMiddleware extends Middleware {
46
47
	/**
48
	 * @var IRequest
49
	 */
50
	private $request;
51
52
	/**
53
	 * @var ControllerMethodReflector
54
	 */
55
	private $reflector;
56
57
	/**
58
	 * @var IUserSession
59
	 */
60
	private $session;
61
62
	/**
63
	 * @var IConfig
64
	 */
65
	private $config;
66
67
	/**
68
	 * @param IRequest $request
69
	 * @param ControllerMethodReflector $reflector
70
	 * @param IUserSession $session
71
	 * @param IConfig $config
72
	 */
73 View Code Duplication
	public function __construct(IRequest $request,
74
								ControllerMethodReflector $reflector,
75
								IUserSession $session,
76
								IConfig $config) {
77
		$this->request = $request;
78
		$this->reflector = $reflector;
79
		$this->session = $session;
80
		$this->config = $config;
81
	}
82
83
	/**
84
	 * This is being run after a successful controllermethod call and allows
85
	 * the manipulation of a Response object. The middleware is run in reverse order
86
	 *
87
	 * @param Controller $controller the controller that is being called
88
	 * @param string $methodName the name of the method that will be called on
89
	 *                           the controller
90
	 * @param Response $response the generated response from the controller
91
	 * @return Response a Response object
92
	 * @throws SecurityException
93
	 */
94
	public function afterController($controller, $methodName, Response $response){
95
		// only react if its a CORS request and if the request sends origin and
96
		$userId = null;
97
		if (!is_null($this->session->getUser())) {
98
			$userId = $this->session->getUser()->getUID();
99
		}
100
101
		if($this->request->getHeader("Origin") !== null &&
102
			$this->reflector->hasAnnotation('CORS') && !is_null($userId)) {
103
104
			$requesterDomain = $this->request->getHeader("Origin");
105
106
			$headers = \OC_Response::setCorsHeaders($userId, $requesterDomain, $this->config);
107
			foreach ($headers as $key => $value) {
108
				$response->addHeader($key, implode(',', $value));
109
			}
110
111
			// allow credentials headers must not be true or CSRF is possible
112
			// otherwise
113
			foreach($response->getHeaders() as $header => $value) {
114
				if(strtolower($header) === 'access-control-allow-credentials' &&
115
				   strtolower(trim($value)) === 'true') {
116
					$msg = 'Access-Control-Allow-Credentials must not be '.
117
						   'set to true in order to prevent CSRF';
118
					throw new SecurityException($msg);
119
				}
120
			}
121
		}
122
		return $response;
123
	}
124
125
	/**
126
	 * If an SecurityException is being caught return a JSON error response
127
	 *
128
	 * @param Controller $controller the controller that is being called
129
	 * @param string $methodName the name of the method that will be called on
130
	 *                           the controller
131
	 * @param \Exception $exception the thrown exception
132
	 * @throws \Exception the passed in exception if it can't handle it
133
	 * @return Response a Response object or null in case that the exception could not be handled
134
	 */
135
	public function afterException($controller, $methodName, \Exception $exception){
136
		if($exception instanceof SecurityException){
137
			$response =  new JSONResponse(['message' => $exception->getMessage()]);
138
			if($exception->getCode() !== 0) {
139
				$response->setStatus($exception->getCode());
140
			} else {
141
				$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
142
			}
143
			return $response;
144
		}
145
146
		throw $exception;
147
	}
148
149
}
150