IpUtilsTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\HttpFoundation\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  13. use Symfony\Component\HttpFoundation\IpUtils;
  14. class IpUtilsTest extends TestCase
  15. {
  16. use ExpectDeprecationTrait;
  17. public function testSeparateCachesPerProtocol()
  18. {
  19. $ip = '192.168.52.1';
  20. $subnet = '192.168.0.0/16';
  21. $this->assertFalse(IpUtils::checkIp6($ip, $subnet));
  22. $this->assertTrue(IpUtils::checkIp4($ip, $subnet));
  23. $ip = '2a01:198:603:0:396e:4789:8e99:890f';
  24. $subnet = '2a01:198:603:0::/65';
  25. $this->assertFalse(IpUtils::checkIp4($ip, $subnet));
  26. $this->assertTrue(IpUtils::checkIp6($ip, $subnet));
  27. }
  28. /**
  29. * @dataProvider getIpv4Data
  30. */
  31. public function testIpv4($matches, $remoteAddr, $cidr)
  32. {
  33. $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
  34. }
  35. public static function getIpv4Data()
  36. {
  37. return [
  38. [true, '192.168.1.1', '192.168.1.1'],
  39. [true, '192.168.1.1', '192.168.1.1/1'],
  40. [true, '192.168.1.1', '192.168.1.0/24'],
  41. [false, '192.168.1.1', '1.2.3.4/1'],
  42. [false, '192.168.1.1', '192.168.1.1/33'], // invalid subnet
  43. [true, '192.168.1.1', ['1.2.3.4/1', '192.168.1.0/24']],
  44. [true, '192.168.1.1', ['192.168.1.0/24', '1.2.3.4/1']],
  45. [false, '192.168.1.1', ['1.2.3.4/1', '4.3.2.1/1']],
  46. [true, '1.2.3.4', '0.0.0.0/0'],
  47. [true, '1.2.3.4', '192.168.1.0/0'],
  48. [false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation
  49. [false, 'an_invalid_ip', '192.168.1.0/24'],
  50. [false, '', '1.2.3.4/1'],
  51. ];
  52. }
  53. /**
  54. * @dataProvider getIpv6Data
  55. */
  56. public function testIpv6($matches, $remoteAddr, $cidr)
  57. {
  58. if (!\defined('AF_INET6')) {
  59. $this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
  60. }
  61. $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
  62. }
  63. public static function getIpv6Data()
  64. {
  65. return [
  66. [true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'],
  67. [false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'],
  68. [false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'],
  69. [true, '0:0:0:0:0:0:0:1', '::1'],
  70. [false, '0:0:603:0:396e:4789:8e99:0001', '::1'],
  71. [true, '0:0:603:0:396e:4789:8e99:0001', '::/0'],
  72. [true, '0:0:603:0:396e:4789:8e99:0001', '2a01:198:603:0::/0'],
  73. [true, '2a01:198:603:0:396e:4789:8e99:890f', ['::1', '2a01:198:603:0::/65']],
  74. [true, '2a01:198:603:0:396e:4789:8e99:890f', ['2a01:198:603:0::/65', '::1']],
  75. [false, '2a01:198:603:0:396e:4789:8e99:890f', ['::1', '1a01:198:603:0::/65']],
  76. [false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'],
  77. [false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'],
  78. [false, '', '::1'],
  79. [false, '127.0.0.1', '::1'],
  80. [false, '0.0.0.0/8', '::1'],
  81. [false, '::1', '127.0.0.1'],
  82. [false, '::1', '0.0.0.0/8'],
  83. [true, '::ffff:10.126.42.2', '::ffff:10.0.0.0/0'],
  84. ];
  85. }
  86. /**
  87. * @group legacy
  88. */
  89. public function testIpTriggersDeprecationOnNull()
  90. {
  91. $this->expectDeprecation('Since symfony/http-foundation 5.4: Passing null as $requestIp to "Symfony\Component\HttpFoundation\IpUtils::checkIp()" is deprecated, pass an empty string instead.');
  92. $this->assertFalse(IpUtils::checkIp(null, '192.168.1.1'));
  93. }
  94. /**
  95. * @group legacy
  96. */
  97. public function testIp4TriggersDeprecationOnNull()
  98. {
  99. $this->expectDeprecation('Since symfony/http-foundation 5.4: Passing null as $requestIp to "Symfony\Component\HttpFoundation\IpUtils::checkIp4()" is deprecated, pass an empty string instead.');
  100. $this->assertFalse(IpUtils::checkIp4(null, '192.168.1.1'));
  101. }
  102. /**
  103. * @group legacy
  104. */
  105. public function testIp6TriggersDeprecationOnNull()
  106. {
  107. $this->expectDeprecation('Since symfony/http-foundation 5.4: Passing null as $requestIp to "Symfony\Component\HttpFoundation\IpUtils::checkIp6()" is deprecated, pass an empty string instead.');
  108. $this->assertFalse(IpUtils::checkIp6(null, '2a01:198:603:0::/65'));
  109. }
  110. /**
  111. * @requires extension sockets
  112. */
  113. public function testAnIpv6WithOptionDisabledIpv6()
  114. {
  115. $this->expectException(\RuntimeException::class);
  116. if (\defined('AF_INET6')) {
  117. $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
  118. }
  119. IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
  120. }
  121. /**
  122. * @dataProvider invalidIpAddressData
  123. */
  124. public function testInvalidIpAddressesDoNotMatch($requestIp, $proxyIp)
  125. {
  126. $this->assertFalse(IpUtils::checkIp4($requestIp, $proxyIp));
  127. }
  128. public static function invalidIpAddressData()
  129. {
  130. return [
  131. 'invalid proxy wildcard' => ['192.168.20.13', '*'],
  132. 'invalid proxy missing netmask' => ['192.168.20.13', '0.0.0.0'],
  133. 'invalid request IP with invalid proxy wildcard' => ['0.0.0.0', '*'],
  134. ];
  135. }
  136. /**
  137. * @dataProvider anonymizedIpData
  138. */
  139. public function testAnonymize($ip, $expected)
  140. {
  141. $this->assertSame($expected, IpUtils::anonymize($ip));
  142. }
  143. public static function anonymizedIpData()
  144. {
  145. return [
  146. ['192.168.1.1', '192.168.1.0'],
  147. ['1.2.3.4', '1.2.3.0'],
  148. ['2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603::'],
  149. ['2a01:198:603:10:396e:4789:8e99:890f', '2a01:198:603:10::'],
  150. ['::1', '::'],
  151. ['0:0:0:0:0:0:0:1', '::'],
  152. ['1:0:0:0:0:0:0:1', '1::'],
  153. ['0:0:603:50:396e:4789:8e99:0001', '0:0:603:50::'],
  154. ['[0:0:603:50:396e:4789:8e99:0001]', '[0:0:603:50::]'],
  155. ['[2a01:198::3]', '[2a01:198::]'],
  156. ['::ffff:123.234.235.236', '::ffff:123.234.235.0'], // IPv4-mapped IPv6 addresses
  157. ['::123.234.235.236', '::123.234.235.0'], // deprecated IPv4-compatible IPv6 address
  158. ];
  159. }
  160. /**
  161. * @dataProvider getIp4SubnetMaskZeroData
  162. */
  163. public function testIp4SubnetMaskZero($matches, $remoteAddr, $cidr)
  164. {
  165. $this->assertSame($matches, IpUtils::checkIp4($remoteAddr, $cidr));
  166. }
  167. public static function getIp4SubnetMaskZeroData()
  168. {
  169. return [
  170. [true, '1.2.3.4', '0.0.0.0/0'],
  171. [true, '1.2.3.4', '192.168.1.0/0'],
  172. [false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation
  173. ];
  174. }
  175. }