Skip to content

Commit 1cabcf9

Browse files
committed
Sanitize/improve placeholder patterns
1 parent 9a48bd9 commit 1cabcf9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ protected function buildRoleMap()
7575

7676
$this->map[$main] = array_unique($this->map[$main]);
7777

78-
if (str_contains($main, '*')) {
79-
$this->rolePlaceholdersPatterns[$main] = sprintf('/%s/', strtr($main, ['*' => '[^\*]+']));
78+
if (str_contains($main, '*') && false !== ($pattern = $this->getPlaceholderPattern($main))) {
79+
$this->rolePlaceholdersPatterns[$main] = $pattern;
8080
}
8181
}
8282
}
@@ -119,4 +119,20 @@ private function getMatchingPlaceholders(array $roles): array
119119

120120
return $resolved;
121121
}
122+
123+
/**
124+
* Build the regex pattern for the given role:
125+
* - Replace valid wildcards with a non-wildcard matching pattern and
126+
* - Escape reserved regex characters
127+
*
128+
* A valid wildcard is a * prefixed with _ and immediately followed by _ or EOL.
129+
*
130+
* @return string|false The regex pattern, or false if there is no valid wildcard in the role
131+
*/
132+
private function getPlaceholderPattern(string $role): string|false
133+
{
134+
/** @var int $count */
135+
$placeholderPattern = \preg_replace(pattern:'/(?<=_)\\\\\*(?=_|$)/', replacement: '[^\*]*', subject: preg_quote($role), count: $count);
136+
return ($count > 0) ? \sprintf('/%s/', $placeholderPattern) : false;
137+
}
122138
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,19 @@ public function testGetReachableRoleNamesWithRecursivePlaceholders()
7070
$this->assertEquals(['ROLE_QUX_A', 'ROLE_QUX_BAZ'], $role->getReachableRoleNames(['ROLE_QUX_A']));
7171
$this->assertEquals(['ROLE_QUX_BAZ'], $role->getReachableRoleNames(['ROLE_QUX_BAZ']));
7272
}
73+
74+
public function testInvalidPlaceholderSyntaxAreNotResolved()
75+
{
76+
$role = new RoleHierarchy([
77+
'ROLE_FOO*' => ['ROLE_FOOBAR'],
78+
'ROLE_*FOO' => ['ROLE_FOOBAR'],
79+
'ROLE_FOO_*BAR' => ['ROLE_FOOBAR'],
80+
'ROLE_FOO*_BAR' => ['ROLE_FOOBAR'],
81+
]);
82+
83+
$this->assertEquals(['ROLE_FOOA'], $role->getReachableRoleNames(['ROLE_FOOA']));
84+
$this->assertEquals(['ROLE_AFOO'], $role->getReachableRoleNames(['ROLE_AFOO']));
85+
$this->assertEquals(['ROLE_FOO_ABAR'], $role->getReachableRoleNames(['ROLE_FOO_ABAR']));
86+
$this->assertEquals(['ROLE_FOOA_BAR'], $role->getReachableRoleNames(['ROLE_FOOA_BAR']));
87+
}
7388
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy