10000 [Security] AbstractVoter->supportsAttribute gives false positive if attribute is zero (0) by martynas-foodpanda · Pull Request #20734 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] AbstractVoter->supportsAttribute gives false positive if attribute is zero (0) #20734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Security] AbstractVoter method supportsAttribute gives false positiv…
…e if attribute is zero (0)
  • Loading branch information
Martynas Narbutas committed Dec 3, 2016
commit 8306530e6078132a661498e8ccf80813f782d857
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class AbstractVoter implements VoterInterface
*/
public function supportsAttribute($attribute)
{
return in_array($attribute, $this->getSupportedAttributes());
return in_array($attribute, $this->getSupportedAttributes(), true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@

class AbstractVoterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var TokenInterface
*/
protected $token;

protected function setUp()
{
$this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
}

/**
* @return array
*/
public function getTests()
{
return array(
Expand Down Expand Up @@ -53,6 +59,71 @@ public function testVote(array $attributes, $expectedVote, $object, $message)

$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
}

/**
* @return array
*/
public function getSupportsAttributeData()
{
return array(
'positive_string_edit' => array(
'expected' => true,
'attribute' => 'EDIT',
'message' => 'expected TRUE given as attribute EDIT is supported',
),
'positive_string_create' => array(
'expected' => true,
'attribute' => 'CREATE',
'message' => 'expected TRUE as given attribute CREATE is supported',
),

'negative_string_read' => array(
'expected' => false,
'attribute' => 'READ',
'message' => 'expected FALSE as given attribute READ is not supported',
),
'negative_string_random' => array(
'expected' => false,
'attribute' => 'random',
'message' => 'expected FALSE as given attribute "random" is not supported',
),
'negative_string_0' => array(
'expected' => false,
'attribute' => '0',
'message' => 'expected FALSE as given attribute "0" is not supported',
),
// this set of data gives false positive if in_array is not used with strict flag set to 'true'
'negative_int_0' => array(
'expected' => false,
'attribute' => 0,
'message' => 'expected FALSE as given attribute 0 is not string',
),
'negative_int_1' => array(
'expected' => false,
'attribute' => 1,
'message' => 'expected FALSE as given attribute 1 is not string',
),
'negative_int_7' => array(
'expected' => false,
'attribute' => 7,
'message' => 'expected FALSE as attribute 7 is not string',
),
);
}

/**
* @dataProvider getSupportsAttributeData
*
* @param bool $expected
* @param string $attribute
* @param string $message
*/
public function testSupportsAttribute($expected, $attribute, $message)
{
$voter = new AbstractVoterTest_Voter();

$this->assertEquals($expected, $voter->supportsAttribute($attribute), $message);
}
}

class AbstractVoterTest_Voter extends AbstractVoter
Expand Down
0