From 5cf3a20703795116d3445fe78f0be9e509b59216 Mon Sep 17 00:00:00 2001 From: Marc Veldman Date: Tue, 15 Oct 2019 18:56:49 +0200 Subject: [PATCH] Test argument really is a string before doing a filter_var. --- src/Assert.php | 12 ++++++++++++ tests/AssertTest.php | 4 ++++ tests/static-analysis/assert-email.php | 15 +++++++++++++++ tests/static-analysis/assert-ip.php | 15 +++++++++++++++ tests/static-analysis/assert-ipv4.php | 15 +++++++++++++++ tests/static-analysis/assert-ipv6.php | 15 +++++++++++++++ 6 files changed, 76 insertions(+) create mode 100644 tests/static-analysis/assert-email.php create mode 100644 tests/static-analysis/assert-ip.php create mode 100644 tests/static-analysis/assert-ipv4.php create mode 100644 tests/static-analysis/assert-ipv6.php diff --git a/src/Assert.php b/src/Assert.php index 79b1036e..9f039d80 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -633,11 +633,14 @@ public static function false($value, $message = '') } /** + * @psalm-assert string $value + * * @param mixed $value * @param string $message */ public static function ip($value, $message = '') { + static::string($value, $message); if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be an IP. Got: %s', @@ -647,11 +650,14 @@ public static function ip($value, $message = '') } /** + * @psalm-assert string $value + * * @param mixed $value * @param string $message */ public static function ipv4($value, $message = '') { + static::string($value, $message); if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be an IPv4. Got: %s', @@ -661,11 +667,14 @@ public static function ipv4($value, $message = '') } /** + * @psalm-assert string $value + * * @param mixed $value * @param string $message */ public static function ipv6($value, $message = '') { + static::string($value, $message); if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be an IPv6. Got %s', @@ -675,11 +684,14 @@ public static function ipv6($value, $message = '') } /** + * @psalm-assert string $value + * * @param mixed $value * @param string $message */ public static function email($value, $message = '') { + static::string($value, $message); if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { static::reportInvalidArgument(\sprintf( $message ?: 'Expected a value to be a valid e-mail address. Got %s', diff --git a/tests/AssertTest.php b/tests/AssertTest.php index d5884feb..35e236ba 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -441,6 +441,7 @@ public function getTests() array('throws', array(function() { trigger_error('test'); }, 'Unthrowable'), false, false, 70000), array('throws', array(function() { throw new Error(); }, 'Throwable'), true, true, 70000), array('ip', array('192.168.0.1'), true), + array('ip', array(new ToStringClass('192.168.0.1')), false), array('ip', array('255.255.255.255'), true), array('ip', array('0.0.0.0'), true), array('ip', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), true), @@ -453,6 +454,7 @@ public function getTests() array('ip', array(null), false), array('ip', array(false), false), array('ipv4', array('192.168.0.1'), true), + array('ipv4', array(new ToStringClass('192.168.0.1')), false), array('ipv4', array('255.255.255.255'), true), array('ipv4', array('0.0.0.0'), true), array('ipv4', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), false), @@ -468,6 +470,7 @@ public function getTests() array('ipv6', array('255.255.255.255'), false), array('ipv6', array('0.0.0.0'), false), array('ipv6', array('2001:0db8:0000:0042:0000:8a2e:0370:7334'), true), + array('ipv6', array(new ToStringClass('2001:0db8:0000:0042:0000:8a2e:0370:7334')), false), array('ipv6', array('::ffff:192.0.2.1'), true), array('ipv6', array('::1'), true), array('ipv6', array('::'), true), @@ -480,6 +483,7 @@ public function getTests() array('email', array(123), false), array('email', array('foo.com'), false), array('email', array('foo@bar.com'), true), + array('email', array(new ToStringClass('foo@bar.com')), false), array('uniqueValues', array(array('qwerty', 'qwerty')), false), array('uniqueValues', array(array('asdfg', 'qwerty')), true), array('uniqueValues', array(array(123, '123')), false), diff --git a/tests/static-analysis/assert-email.php b/tests/static-analysis/assert-email.php new file mode 100644 index 00000000..ed4a297f --- /dev/null +++ b/tests/static-analysis/assert-email.php @@ -0,0 +1,15 @@ +