Skip to content

Commit 9cd047c

Browse files
[10.x] Escape forward slashes when exploding wildcard rules (#48936)
* Escape forward slashes when exploding wildcard rules Array keys with a forward slash '/' passed into the validator will cause a preg_match(): Unknown modifier exception if they are validated using a rule keyed with a wildcard '*' This is due to the pattern being terminated early by the existence of the '/' in the pattern variable, this same fix is also used in Validator.php * Add test * Fix for continuous integration style
1 parent fe2ac4a commit 9cd047c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Illuminate/Validation/ValidationRuleParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected function prepareRule($rule, $attribute)
144144
*/
145145
protected function explodeWildcardRules($results, $attribute, $rules)
146146
{
147-
$pattern = str_replace('\*', '[^\.]*', preg_quote($attribute));
147+
$pattern = str_replace('\*', '[^\.]*', preg_quote($attribute, '/'));
148148

149149
$data = ValidationData::initializeAndGatherData($attribute, $this->data);
150150

tests/Validation/ValidationRuleParserTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ public function testExplodeGeneratesNestedRulesForNonNestedData()
199199
$this->assertEquals([], $results->implicitAttributes);
200200
}
201201

202+
public function testExplodeHandlesForwardSlashesInWildcardRule()
203+
{
204+
$parser = (new ValidationRuleParser([
205+
'redirects' => [
206+
'directory/subdirectory/file' => [
207+
'directory/subdirectory/redirectedfile',
208+
],
209+
],
210+
]));
211+
212+
$results = $parser->explode([
213+
'redirects.directory/subdirectory/file.*' => 'string',
214+
]);
215+
216+
$this->assertEquals([
217+
'redirects.directory/subdirectory/file.0' => ['string'],
218+
], $results->rules);
219+
$this->assertEquals([
220+
'redirects.directory/subdirectory/file.*' => ['redirects.directory/subdirectory/file.0'],
221+
], $results->implicitAttributes);
222+
}
223+
202224
public function testExplodeHandlesArraysOfNestedRules()
203225
{
204226
$parser = (new ValidationRuleParser([

0 commit comments

Comments
 (0)