Skip to content

Commit 495b2ce

Browse files
committed
Ensure placeholders strictly matches roles
1 parent 0240d67 commit 495b2ce

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/Symfony/Component/Security/Core/Role/RoleHierarchy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@ private function getPlaceholderPattern(string $role): string|false
134134
/** @var int $count */
135135
$placeholderPattern = preg_replace(pattern: '/(?<=_)\\\\\*(?=_|$)/', replacement: '[^\*]*', subject: preg_quote($role), count: $count);
136136

137-
return ($count > 0) ? sprintf('/%s/', $placeholderPattern) : false;
137+
return ($count > 0) ? sprintf('/^%s$/', $placeholderPattern) : false;
138138
}
139139
}

src/Symfony/Component/Security/Core/Tests/Role/RoleHierarchyTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testGetReachableRoleNamesWithPlaceholders()
3737
'ROLE_BAZ_*' => ['ROLE_USER'],
3838
'ROLE_FOO_*' => ['ROLE_BAZ_FOO'],
3939
'ROLE_BAR_*' => ['ROLE_BAZ_BAR'],
40+
'ROLE_QUX_*_BAR' => ['ROLE_FOOBAR'],
4041
]);
4142

4243
$this->assertEquals(['ROLE_BAZ_A', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_BAZ_A']));
@@ -47,6 +48,10 @@ public function testGetReachableRoleNamesWithPlaceholders()
4748

4849
// Multiple roles matching multiple placeholders
4950
$this->assertEquals(['ROLE_FOO_A', 'ROLE_BAR_A', 'ROLE_BAZ_FOO', 'ROLE_BAZ_BAR', 'ROLE_USER'], $role->getReachableRoleNames(['ROLE_FOO_A', 'ROLE_BAR_A']));
51+
52+
// Test placeholders don't match more than the pattern
53+
$this->assertEquals(['FOO_ROLE_FOO_A'], $role->getReachableRoleNames(['FOO_ROLE_FOO_A'])); // Doesn't start with ROLE_FOO_
54+
$this->assertEquals(['ROLE_QUX_A_BARA'], $role->getReachableRoleNames(['ROLE_QUX_A_BARA'])); // Doesn't end with _BAR
5055
}
5156

5257
public function testGetReachableRoleNamesWithRecursivePlaceholders()

0 commit comments

Comments
 (0)