1 | <?php namespace Neomerx\Cors\Strategies; |
||
30 | class Settings implements SettingsStrategyInterface |
||
31 | { |
||
32 | use LoggerAwareTrait; |
||
33 | |||
34 | /** |
||
35 | * 'All' value for allowed origins. |
||
36 | */ |
||
37 | const VALUE_ALLOW_ORIGIN_ALL = CorsResponseHeaders::VALUE_ALLOW_ORIGIN_ALL; |
||
38 | |||
39 | /** |
||
40 | * 'All' values for allowed headers. |
||
41 | */ |
||
42 | const VALUE_ALLOW_ALL_HEADERS = '*'; |
||
43 | |||
44 | /** |
||
45 | * @var string|array If specified as array (recommended for better performance) it should |
||
46 | * be in parse_url() result format. |
||
47 | * |
||
48 | * @see http://php.net/manual/function.parse-url.php |
||
49 | */ |
||
50 | private $serverOrigin = [ |
||
51 | 'scheme' => '', |
||
52 | 'host' => ParsedUrlInterface::DEFAULT_PORT, |
||
53 | 'port' => '', |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * A list of allowed request origins (lower-cased, no trail slashes). |
||
58 | * Value `true` enables and value `null` disables origin. |
||
59 | * If all origins '*' are enabled all settings for other origins are ignored. |
||
60 | * |
||
61 | * For example, |
||
62 | * |
||
63 | * $allowedOrigins = [ |
||
64 | * 'http://example.com:123' => true, |
||
65 | * 'http://evil.com' => null, |
||
66 | * self::VALUE_ALLOW_ORIGIN_ALL => null, |
||
67 | * ]; |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private $allowedOrigins = []; |
||
72 | |||
73 | /** |
||
74 | * A list of allowed request methods (case sensitive). Value `true` enables and value `null` disables method. |
||
75 | * |
||
76 | * For example, |
||
77 | * |
||
78 | * $allowedMethods = [ |
||
79 | * 'GET' => true, |
||
80 | * 'PATCH' => true, |
||
81 | * 'POST' => true, |
||
82 | * 'PUT' => null, |
||
83 | * 'DELETE' => true, |
||
84 | * ]; |
||
85 | * |
||
86 | * Security Note: you have to remember CORS is not access control system and you should not expect all cross-origin |
||
87 | * requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' headers request |
||
88 | * will be made without pre-flight. Thus you can not restrict such requests with CORS and should use other means. |
||
89 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight request so |
||
90 | * disabling it will not restrict access to resource(s). |
||
91 | * |
||
92 | * You can read more on 'simple' methods at http://www.w3.org/TR/cors/#simple-method |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | private $allowedMethods = []; |
||
97 | |||
98 | /** |
||
99 | * A list of allowed request headers (lower-cased). Value `true` enables and value `null` disables header. |
||
100 | * |
||
101 | * For example, |
||
102 | * |
||
103 | * $allowedHeaders = [ |
||
104 | * 'content-type' => true, |
||
105 | * 'x-custom-request-header' => null, |
||
106 | * self::VALUE_ALLOW_ALL_HEADERS => null, |
||
107 | * ]; |
||
108 | * |
||
109 | * Security Note: you have to remember CORS is not access control system and you should not expect all cross-origin |
||
110 | * requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' headers request |
||
111 | * will be made without pre-flight. Thus you can not restrict such requests with CORS and should use other means. |
||
112 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight request so |
||
113 | * disabling it will not restrict access to resource(s). |
||
114 | * |
||
115 | * You can read more on 'simple' headers at http://www.w3.org/TR/cors/#simple-header |
||
116 | * |
||
117 | * @var array |
||
118 | */ |
||
119 | private $allowedHeaders = []; |
||
120 | |||
121 | /** |
||
122 | * A list of headers (case insensitive) which will be made accessible to user agent (browser) in response. |
||
123 | * Value `true` enables and value `null` disables header. |
||
124 | * |
||
125 | * For example, |
||
126 | * |
||
127 | * $exposedHeaders = [ |
||
128 | * 'Content-Type' => true, |
||
129 | * 'X-Custom-Response-Header' => true, |
||
130 | * 'X-Disabled-Header' => null, |
||
131 | * ]; |
||
132 | * |
||
133 | * @var string[] |
||
134 | */ |
||
135 | private $exposedHeaders = []; |
||
136 | |||
137 | /** |
||
138 | * If access with credentials is supported by the resource. |
||
139 | * |
||
140 | * @var bool |
||
141 | */ |
||
142 | private $isUsingCredentials = false; |
||
143 | |||
144 | /** |
||
145 | * Pre-flight response cache max period in seconds. |
||
146 | * |
||
147 | * @var int |
||
148 | */ |
||
149 | private $preFlightCacheMaxAge = 0; |
||
150 | |||
151 | /** |
||
152 | * If allowed methods should be added to pre-flight response when 'simple' method is requested (see #6.2.9 CORS). |
||
153 | * |
||
154 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
155 | * |
||
156 | * @var bool |
||
157 | */ |
||
158 | private $isForceAddMethods = false; |
||
159 | |||
160 | /** |
||
161 | * If allowed headers should be added when request headers are 'simple' and |
||
162 | * non of them is 'Content-Type' (see #6.2.10 CORS). |
||
163 | * |
||
164 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
165 | * |
||
166 | * @var bool |
||
167 | */ |
||
168 | private $isForceAddHeaders = false; |
||
169 | |||
170 | /** |
||
171 | * If request 'Host' header should be checked against server's origin. |
||
172 | * |
||
173 | * @var bool |
||
174 | */ |
||
175 | private $isCheckHost = false; |
||
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 10 | public function getServerOrigin() |
|
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | 17 | public function setServerOrigin($origin) |
|
194 | |||
195 | /** |
||
196 | * @inheritdoc |
||
197 | */ |
||
198 | 3 | public function isPreFlightCanBeCached(RequestInterface $request) |
|
202 | |||
203 | /** |
||
204 | * @inheritdoc |
||
205 | */ |
||
206 | 3 | public function getPreFlightCacheMaxAge(RequestInterface $request) |
|
210 | |||
211 | /** |
||
212 | * @inheritdoc |
||
213 | */ |
||
214 | 9 | public function setPreFlightCacheMaxAge($cacheMaxAge) |
|
220 | |||
221 | /** |
||
222 | * @inheritdoc |
||
223 | */ |
||
224 | 2 | public function isForceAddAllowedMethodsToPreFlightResponse() |
|
228 | |||
229 | /** |
||
230 | * @inheritdoc |
||
231 | */ |
||
232 | 9 | public function setForceAddAllowedMethodsToPreFlightResponse($forceFlag) |
|
238 | |||
239 | /** |
||
240 | * @inheritdoc |
||
241 | */ |
||
242 | 1 | public function isForceAddAllowedHeadersToPreFlightResponse() |
|
246 | |||
247 | /** |
||
248 | * @inheritdoc |
||
249 | */ |
||
250 | 9 | public function setForceAddAllowedHeadersToPreFlightResponse($forceFlag) |
|
256 | |||
257 | /** |
||
258 | * @inheritdoc |
||
259 | */ |
||
260 | 4 | public function isRequestCredentialsSupported(RequestInterface $request) |
|
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | 17 | public function setRequestCredentialsSupported($isSupported) |
|
274 | |||
275 | /** |
||
276 | * @inheritdoc |
||
277 | */ |
||
278 | 8 | public function isRequestOriginAllowed(ParsedUrlInterface $requestOrigin) |
|
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | 17 | public function setRequestAllowedOrigins(array $origins) |
|
300 | |||
301 | /** |
||
302 | * @inheritdoc |
||
303 | */ |
||
304 | 5 | public function isRequestMethodSupported($method) |
|
310 | |||
311 | /** |
||
312 | * @inheritdoc |
||
313 | */ |
||
314 | 5 | public function isRequestAllHeadersSupported($headers) |
|
336 | |||
337 | /** |
||
338 | * @inheritdoc |
||
339 | */ |
||
340 | 3 | public function getRequestAllowedMethods(RequestInterface $request, $requestMethod) |
|
344 | |||
345 | /** |
||
346 | * @inheritdoc |
||
347 | */ |
||
348 | 17 | public function setRequestAllowedMethods(array $methods) |
|
354 | |||
355 | /** |
||
356 | * @inheritdoc |
||
357 | */ |
||
358 | 3 | public function getRequestAllowedHeaders(RequestInterface $request, array $requestHeaders) |
|
362 | |||
363 | /** |
||
364 | * @inheritdoc |
||
365 | */ |
||
366 | 17 | public function setRequestAllowedHeaders(array $headers) |
|
372 | |||
373 | /** |
||
374 | * @inheritdoc |
||
375 | */ |
||
376 | 2 | public function getResponseExposedHeaders(RequestInterface $request) |
|
380 | |||
381 | /** |
||
382 | * @inheritdoc |
||
383 | */ |
||
384 | 17 | public function setResponseExposedHeaders(array $headers) |
|
390 | |||
391 | /** |
||
392 | * @inheritdoc |
||
393 | */ |
||
394 | 10 | public function isCheckHost() |
|
398 | |||
399 | /** |
||
400 | * @inheritdoc |
||
401 | */ |
||
402 | 17 | public function setCheckHost($checkFlag) |
|
408 | |||
409 | /** |
||
410 | * Select only enabled items from $list. |
||
411 | * |
||
412 | * @param array $list |
||
413 | * |
||
414 | * @return array |
||
415 | */ |
||
416 | 6 | protected function getEnabledItems(array $list) |
|
428 | } |
||
429 |