SubRequestHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpKernel\HttpCache;
  11. use Symfony\Component\HttpFoundation\IpUtils;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. /**
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. *
  18. * @internal
  19. */
  20. class SubRequestHandler
  21. {
  22. /**
  23. * @return Response
  24. */
  25. public static function handle(HttpKernelInterface $kernel, Request $request, $type, $catch)
  26. {
  27. // save global state related to trusted headers and proxies
  28. $trustedProxies = Request::getTrustedProxies();
  29. $trustedHeaders = array(
  30. Request::HEADER_FORWARDED => Request::getTrustedHeaderName(Request::HEADER_FORWARDED),
  31. Request::HEADER_CLIENT_IP => Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP),
  32. Request::HEADER_CLIENT_HOST => Request::getTrustedHeaderName(Request::HEADER_CLIENT_HOST),
  33. Request::HEADER_CLIENT_PROTO => Request::getTrustedHeaderName(Request::HEADER_CLIENT_PROTO),
  34. Request::HEADER_CLIENT_PORT => Request::getTrustedHeaderName(Request::HEADER_CLIENT_PORT),
  35. );
  36. // remove untrusted values
  37. $remoteAddr = $request->server->get('REMOTE_ADDR');
  38. if (!IpUtils::checkIp($remoteAddr, $trustedProxies)) {
  39. foreach (array_filter($trustedHeaders) as $name) {
  40. $request->headers->remove($name);
  41. $request->server->remove('HTTP_'.strtoupper(str_replace('-', '_', $name)));
  42. }
  43. }
  44. // compute trusted values, taking any trusted proxies into account
  45. $trustedIps = array();
  46. $trustedValues = array();
  47. foreach (array_reverse($request->getClientIps()) as $ip) {
  48. $trustedIps[] = $ip;
  49. $trustedValues[] = sprintf('for="%s"', $ip);
  50. }
  51. if ($ip !== $remoteAddr) {
  52. $trustedIps[] = $remoteAddr;
  53. $trustedValues[] = sprintf('for="%s"', $remoteAddr);
  54. }
  55. // set trusted values, reusing as much as possible the global trusted settings
  56. if ($name = $trustedHeaders[Request::HEADER_FORWARDED]) {
  57. $trustedValues[0] .= sprintf(';host="%s";proto=%s', $request->getHttpHost(), $request->getScheme());
  58. $request->headers->set($name, $v = implode(', ', $trustedValues));
  59. $request->server->set('HTTP_'.strtoupper(str_replace('-', '_', $name)), $v);
  60. }
  61. if ($name = $trustedHeaders[Request::HEADER_CLIENT_IP]) {
  62. $request->headers->set($name, $v = implode(', ', $trustedIps));
  63. $request->server->set('HTTP_'.strtoupper(str_replace('-', '_', $name)), $v);
  64. }
  65. if (!$name && !$trustedHeaders[Request::HEADER_FORWARDED]) {
  66. $request->headers->set('X-Forwarded-For', $v = implode(', ', $trustedIps));
  67. $request->server->set('HTTP_X_FORWARDED_FOR', $v);
  68. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, 'X_FORWARDED_FOR');
  69. }
  70. // fix the client IP address by setting it to 127.0.0.1,
  71. // which is the core responsibility of this method
  72. $request->server->set('REMOTE_ADDR', '127.0.0.1');
  73. // ensure 127.0.0.1 is set as trusted proxy
  74. if (!IpUtils::checkIp('127.0.0.1', $trustedProxies)) {
  75. Request::setTrustedProxies(array_merge($trustedProxies, array('127.0.0.1')));
  76. }
  77. try {
  78. $e = null;
  79. $response = $kernel->handle($request, $type, $catch);
  80. } catch (\Throwable $e) {
  81. } catch (\Exception $e) {
  82. }
  83. // restore global state
  84. Request::setTrustedHeaderName(Request::HEADER_CLIENT_IP, $trustedHeaders[Request::HEADER_CLIENT_IP]);
  85. Request::setTrustedProxies($trustedProxies);
  86. if (null !== $e) {
  87. throw $e;
  88. }
  89. return $response;
  90. }
  91. }