CorsTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. /*
  3. * This file is part of asm89/stack-cors.
  4. *
  5. * (c) Alexander <iam.asm89@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Asm89\Stack\Tests;
  11. use Asm89\Stack\Cors;
  12. use Asm89\Stack\CorsService;
  13. use PHPUnit\Framework\TestCase;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. class CorsTest extends TestCase
  17. {
  18. /**
  19. * @test
  20. */
  21. public function it_does_modify_on_a_request_without_origin()
  22. {
  23. $app = $this->createStackedApp();
  24. $response = $app->handle(new Request());
  25. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  26. }
  27. /**
  28. * @test
  29. */
  30. public function it_does_modify_on_a_request_with_same_origin()
  31. {
  32. $app = $this->createStackedApp(array('allowedOrigins' => array('*')));
  33. $unmodifiedResponse = new Response();
  34. $request = new Request();
  35. $request->headers->set('Host', 'foo.com');
  36. $request->headers->set('Origin', 'http://foo.com');
  37. $response = $app->handle($request);
  38. $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
  39. }
  40. /**
  41. * @test
  42. */
  43. public function it_returns_allow_origin_header_on_valid_actual_request()
  44. {
  45. $app = $this->createStackedApp();
  46. $request = $this->createValidActualRequest();
  47. $response = $app->handle($request);
  48. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  49. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  50. }
  51. /**
  52. * @test
  53. */
  54. public function it_returns_allow_origin_header_on_allow_all_origin_request()
  55. {
  56. $app = $this->createStackedApp(array('allowedOrigins' => array('*')));
  57. $request = new Request();
  58. $request->headers->set('Origin', 'http://localhost');
  59. $response = $app->handle($request);
  60. $this->assertEquals(200, $response->getStatusCode());
  61. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  62. $this->assertEquals('*', $response->headers->get('Access-Control-Allow-Origin'));
  63. }
  64. /**
  65. * @test
  66. */
  67. public function it_returns_allow_headers_header_on_allow_all_headers_request()
  68. {
  69. $app = $this->createStackedApp(array('allowedHeaders' => array('*')));
  70. $request = $this->createValidPreflightRequest();
  71. $request->headers->set('Access-Control-Request-Headers', 'Foo, BAR');
  72. $response = $app->handle($request);
  73. $this->assertEquals(204, $response->getStatusCode());
  74. $this->assertEquals('Foo, BAR', $response->headers->get('Access-Control-Allow-Headers'));
  75. $this->assertEquals('Access-Control-Request-Headers, Access-Control-Request-Method', $response->headers->get('Vary'));
  76. }
  77. /**
  78. * @test
  79. */
  80. public function it_returns_allow_headers_header_on_allow_all_headers_request_credentials()
  81. {
  82. $app = $this->createStackedApp(array('allowedHeaders' => array('*'), 'supportsCredentials' => true));
  83. $request = $this->createValidPreflightRequest();
  84. $request->headers->set('Access-Control-Request-Headers', 'Foo, BAR');
  85. $response = $app->handle($request);
  86. $this->assertEquals(204, $response->getStatusCode());
  87. $this->assertEquals('Foo, BAR', $response->headers->get('Access-Control-Allow-Headers'));
  88. $this->assertEquals('Access-Control-Request-Headers, Access-Control-Request-Method', $response->headers->get('Vary'));
  89. }
  90. /**
  91. * @test
  92. */
  93. public function it_sets_allow_credentials_header_when_flag_is_set_on_valid_actual_request()
  94. {
  95. $app = $this->createStackedApp(array('supportsCredentials' => true));
  96. $request = $this->createValidActualRequest();
  97. $response = $app->handle($request);
  98. $this->assertTrue($response->headers->has('Access-Control-Allow-Credentials'));
  99. $this->assertEquals('true', $response->headers->get('Access-Control-Allow-Credentials'));
  100. }
  101. /**
  102. * @test
  103. */
  104. public function it_does_not_set_allow_credentials_header_when_flag_is_not_set_on_valid_actual_request()
  105. {
  106. $app = $this->createStackedApp();
  107. $request = $this->createValidActualRequest();
  108. $response = $app->handle($request);
  109. $this->assertFalse($response->headers->has('Access-Control-Allow-Credentials'));
  110. }
  111. /**
  112. * @test
  113. */
  114. public function it_sets_exposed_headers_when_configured_on_actual_request()
  115. {
  116. $app = $this->createStackedApp(array('exposedHeaders' => array('x-exposed-header', 'x-another-exposed-header')));
  117. $request = $this->createValidActualRequest();
  118. $response = $app->handle($request);
  119. $this->assertTrue($response->headers->has('Access-Control-Expose-Headers'));
  120. $this->assertEquals('x-exposed-header, x-another-exposed-header', $response->headers->get('Access-Control-Expose-Headers'));
  121. }
  122. /**
  123. * @test
  124. */
  125. public function it_adds_a_vary_header_when_wildcard_and_supports_credentials()
  126. {
  127. $app = $this->createStackedApp(array(
  128. 'allowedOrigins' => ['*'],
  129. 'supportsCredentials' => true,
  130. ));
  131. $request = $this->createValidActualRequest();
  132. $response = $app->handle($request);
  133. $this->assertTrue($response->headers->has('Vary'));
  134. $this->assertEquals('Origin', $response->headers->get('Vary'));
  135. }
  136. /**
  137. * @test
  138. */
  139. public function it_adds_multiple_vary_header_when_wildcard_and_supports_credentials()
  140. {
  141. $app = $this->createStackedApp(array(
  142. 'allowedOrigins' => ['*'],
  143. 'allowedMethods' => ['*'],
  144. 'supportsCredentials' => true,
  145. ));
  146. $request = $this->createValidPreflightRequest();
  147. $response = $app->handle($request);
  148. $this->assertTrue($response->headers->has('Vary'));
  149. $this->assertEquals('Origin, Access-Control-Request-Method', $response->headers->get('Vary'));
  150. }
  151. /**
  152. * @test
  153. */
  154. public function it_adds_a_vary_header_when_has_origin_patterns()
  155. {
  156. $app = $this->createStackedApp(array(
  157. 'allowedOriginsPatterns' => array('/l(o|0)calh(o|0)st/')
  158. ));
  159. $request = $this->createValidActualRequest();
  160. $response = $app->handle($request);
  161. $this->assertTrue($response->headers->has('Vary'));
  162. $this->assertEquals('Origin', $response->headers->get('Vary'));
  163. }
  164. /**
  165. * @test
  166. */
  167. public function it_doesnt_add_a_vary_header_when_wilcard_origins()
  168. {
  169. $app = $this->createStackedApp(array(
  170. 'allowedOrigins' => array('*', 'http://localhost')
  171. ));
  172. $request = $this->createValidActualRequest();
  173. $response = $app->handle($request);
  174. $this->assertFalse($response->headers->has('Vary'));
  175. }
  176. /**
  177. * @test
  178. */
  179. public function it_doesnt_add_a_vary_header_when_simple_origins()
  180. {
  181. $app = $this->createStackedApp(array(
  182. 'allowedOrigins' => array('http://localhost')
  183. ));
  184. $request = $this->createValidActualRequest();
  185. $response = $app->handle($request);
  186. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  187. $this->assertFalse($response->headers->has('Vary'));
  188. }
  189. /**
  190. * @test
  191. */
  192. public function it_adds_a_vary_header_when_multiple_origins()
  193. {
  194. $app = $this->createStackedApp(array(
  195. 'allowedOrigins' => array('http://localhost', 'http://example.com')
  196. ));
  197. $request = $this->createValidActualRequest();
  198. $response = $app->handle($request);
  199. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  200. $this->assertTrue($response->headers->has('Vary'));
  201. }
  202. /**
  203. * @test
  204. * @see http://www.w3.org/TR/cors/index.html#resource-implementation
  205. */
  206. public function it_appends_an_existing_vary_header()
  207. {
  208. $app = $this->createStackedApp(
  209. array(
  210. 'allowedOrigins' => ['*'],
  211. 'supportsCredentials' => true,
  212. ),
  213. array(
  214. 'Vary' => 'Content-Type'
  215. )
  216. );
  217. $request = $this->createValidActualRequest();
  218. $response = $app->handle($request);
  219. $this->assertTrue($response->headers->has('Vary'));
  220. $this->assertEquals('Content-Type, Origin', $response->headers->get('Vary'));
  221. }
  222. /**
  223. * @test
  224. */
  225. public function it_returns_access_control_headers_on_cors_request()
  226. {
  227. $app = $this->createStackedApp();
  228. $request = new Request();
  229. $request->headers->set('Origin', 'http://localhost');
  230. $response = $app->handle($request);
  231. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  232. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  233. }
  234. /**
  235. * @test
  236. */
  237. public function it_returns_access_control_headers_on_cors_request_with_pattern_origin()
  238. {
  239. $app = $this->createStackedApp(array(
  240. 'allowedOrigins' => array(),
  241. 'allowedOriginsPatterns' => array('/l(o|0)calh(o|0)st/')
  242. ));
  243. $request = $this->createValidActualRequest();
  244. $response = $app->handle($request);
  245. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  246. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  247. $this->assertTrue($response->headers->has('Vary'));
  248. $this->assertEquals('Origin', $response->headers->get('Vary'));
  249. }
  250. /**
  251. * @test
  252. */
  253. public function it_adds_vary_headers_on_preflight_non_preflight_options()
  254. {
  255. $app = $this->createStackedApp();
  256. $request = new Request();
  257. $request->setMethod('OPTIONS');
  258. $response = $app->handle($request);
  259. $this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
  260. }
  261. /**
  262. * @test
  263. */
  264. public function it_returns_access_control_headers_on_valid_preflight_request()
  265. {
  266. $app = $this->createStackedApp();
  267. $request = $this->createValidPreflightRequest();
  268. $response = $app->handle($request);
  269. $this->assertTrue($response->headers->has('Access-Control-Allow-Origin'));
  270. $this->assertEquals('http://localhost', $response->headers->get('Access-Control-Allow-Origin'));
  271. $this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
  272. }
  273. /**
  274. * @test
  275. */
  276. public function it_does_not_allow_request_with_origin_not_allowed()
  277. {
  278. $passedOptions = array(
  279. 'allowedOrigins' => array('http://notlocalhost'),
  280. );
  281. $service = new CorsService($passedOptions);
  282. $request = $this->createValidActualRequest();
  283. $response = new Response();
  284. $service->addActualRequestHeaders($response, $request);
  285. $this->assertNotEquals($request->headers->get('Origin'), $response->headers->get('Access-Control-Allow-Origin'));
  286. }
  287. /**
  288. * @test
  289. */
  290. public function it_does_not_modify_request_with_pattern_origin_not_allowed()
  291. {
  292. $passedOptions = array(
  293. 'allowedOrigins' => array(),
  294. 'allowedOriginsPatterns' => array('/l\dcalh\dst/')
  295. );
  296. $service = new CorsService($passedOptions);
  297. $request = $this->createValidActualRequest();
  298. $response = new Response();
  299. $service->addActualRequestHeaders($response, $request);
  300. $this->assertNotEquals($request->headers->get('Origin'), $response->headers->get('Access-Control-Allow-Origin'));
  301. }
  302. /**
  303. * @test
  304. */
  305. public function it_allow_methods_on_valid_preflight_request()
  306. {
  307. $app = $this->createStackedApp(array('allowedMethods' => array('get', 'put')));
  308. $request = $this->createValidPreflightRequest();
  309. $response = $app->handle($request);
  310. $this->assertTrue($response->headers->has('Access-Control-Allow-Methods'));
  311. // it will uppercase the methods
  312. $this->assertEquals('GET, PUT', $response->headers->get('Access-Control-Allow-Methods'));
  313. }
  314. /**
  315. * @test
  316. */
  317. public function it_returns_valid_preflight_request_with_allow_methods_all()
  318. {
  319. $app = $this->createStackedApp(array('allowedMethods' => array('*')));
  320. $request = $this->createValidPreflightRequest();
  321. $response = $app->handle($request);
  322. $this->assertTrue($response->headers->has('Access-Control-Allow-Methods'));
  323. // it will return the Access-Control-Request-Method pass in the request
  324. $this->assertEquals('GET', $response->headers->get('Access-Control-Allow-Methods'));
  325. $this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
  326. }
  327. /**
  328. * @test
  329. */
  330. public function it_returns_valid_preflight_request_with_allow_methods_all_credentials()
  331. {
  332. $app = $this->createStackedApp(array('allowedMethods' => array('*'), 'supportsCredentials' => true));
  333. $request = $this->createValidPreflightRequest();
  334. $response = $app->handle($request);
  335. $this->assertTrue($response->headers->has('Access-Control-Allow-Methods'));
  336. // it will return the Access-Control-Request-Method pass in the request
  337. $this->assertEquals('GET', $response->headers->get('Access-Control-Allow-Methods'));
  338. // it should vary this header
  339. $this->assertEquals('Access-Control-Request-Method', $response->headers->get('Vary'));
  340. }
  341. /**
  342. * @test
  343. */
  344. public function it_returns_ok_on_valid_preflight_request_with_requested_headers_allowed()
  345. {
  346. $app = $this->createStackedApp();
  347. $requestHeaders = 'X-Allowed-Header, x-other-allowed-header';
  348. $request = $this->createValidPreflightRequest();
  349. $request->headers->set('Access-Control-Request-Headers', $requestHeaders);
  350. $response = $app->handle($request);
  351. $this->assertEquals(204, $response->getStatusCode());
  352. $this->assertTrue($response->headers->has('Access-Control-Allow-Headers'));
  353. // the response will have the "allowedHeaders" value passed to Cors rather than the request one
  354. $this->assertEquals('x-allowed-header, x-other-allowed-header', $response->headers->get('Access-Control-Allow-Headers'));
  355. }
  356. /**
  357. * @test
  358. */
  359. public function it_sets_allow_credentials_header_when_flag_is_set_on_valid_preflight_request()
  360. {
  361. $app = $this->createStackedApp(array('supportsCredentials' => true));
  362. $request = $this->createValidPreflightRequest();
  363. $response = $app->handle($request);
  364. $this->assertTrue($response->headers->has('Access-Control-Allow-Credentials'));
  365. $this->assertEquals('true', $response->headers->get('Access-Control-Allow-Credentials'));
  366. }
  367. /**
  368. * @test
  369. */
  370. public function it_does_not_set_allow_credentials_header_when_flag_is_not_set_on_valid_preflight_request()
  371. {
  372. $app = $this->createStackedApp();
  373. $request = $this->createValidPreflightRequest();
  374. $response = $app->handle($request);
  375. $this->assertFalse($response->headers->has('Access-Control-Allow-Credentials'));
  376. }
  377. /**
  378. * @test
  379. */
  380. public function it_sets_max_age_when_set()
  381. {
  382. $app = $this->createStackedApp(array('maxAge' => 42));
  383. $request = $this->createValidPreflightRequest();
  384. $response = $app->handle($request);
  385. $this->assertTrue($response->headers->has('Access-Control-Max-Age'));
  386. $this->assertEquals(42, $response->headers->get('Access-Control-Max-Age'));
  387. }
  388. /**
  389. * @test
  390. */
  391. public function it_sets_max_age_when_zero()
  392. {
  393. $app = $this->createStackedApp(array('maxAge' => 0));
  394. $request = $this->createValidPreflightRequest();
  395. $response = $app->handle($request);
  396. $this->assertTrue($response->headers->has('Access-Control-Max-Age'));
  397. $this->assertEquals(0, $response->headers->get('Access-Control-Max-Age'));
  398. }
  399. /**
  400. * @test
  401. */
  402. public function it_doesnt_set_max_age_when_false()
  403. {
  404. $app = $this->createStackedApp(array('maxAge' => null));
  405. $request = $this->createValidPreflightRequest();
  406. $response = $app->handle($request);
  407. $this->assertFalse($response->headers->has('Access-Control-Max-Age'));
  408. }
  409. /**
  410. * @test
  411. */
  412. public function it_skips_empty_access_control_request_header()
  413. {
  414. $app = $this->createStackedApp();
  415. $request = $this->createValidPreflightRequest();
  416. $request->headers->set('Access-Control-Request-Headers', '');
  417. $response = $app->handle($request);
  418. $this->assertEquals(204, $response->getStatusCode());
  419. }
  420. /**
  421. * @test
  422. */
  423. public function it_doesnt_set_access_control_allow_origin_without_origin()
  424. {
  425. $app = $this->createStackedApp([
  426. 'allowedOrigins' => ['*'],
  427. 'supportsCredentials' => true,
  428. ]);
  429. $response = $app->handle(new Request);
  430. $this->assertFalse($response->headers->has('Access-Control-Allow-Origin'));
  431. }
  432. private function createValidActualRequest()
  433. {
  434. $request = new Request();
  435. $request->headers->set('Origin', 'http://localhost');
  436. return $request;
  437. }
  438. private function createValidPreflightRequest()
  439. {
  440. $request = new Request();
  441. $request->headers->set('Origin', 'http://localhost');
  442. $request->headers->set('Access-Control-Request-Method', 'get');
  443. $request->setMethod('OPTIONS');
  444. return $request;
  445. }
  446. private function createStackedApp(array $options = array(), array $responseHeaders = array())
  447. {
  448. $passedOptions = array_merge(array(
  449. 'allowedHeaders' => array('x-allowed-header', 'x-other-allowed-header'),
  450. 'allowedMethods' => array('delete', 'get', 'post', 'put'),
  451. 'allowedOrigins' => array('http://localhost'),
  452. 'exposedHeaders' => false,
  453. 'maxAge' => false,
  454. 'supportsCredentials' => false,
  455. ),
  456. $options
  457. );
  458. return new Cors(new MockApp($responseHeaders), $passedOptions);
  459. }
  460. }