Skip to content

Commit

Permalink
[10.x] Escape forward slashes when exploding wildcard rules (#48936)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
matt-farrugia authored Nov 7, 2023
1 parent fe2ac4a commit 9cd047c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function prepareRule($rule, $attribute)
*/
protected function explodeWildcardRules($results, $attribute, $rules)
{
$pattern = str_replace('\*', '[^\.]*', preg_quote($attribute));
$pattern = str_replace('\*', '[^\.]*', preg_quote($attribute, '/'));

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

Expand Down
22 changes: 22 additions & 0 deletions tests/Validation/ValidationRuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ public function testExplodeGeneratesNestedRulesForNonNestedData()
$this->assertEquals([], $results->implicitAttributes);
}

public function testExplodeHandlesForwardSlashesInWildcardRule()
{
$parser = (new ValidationRuleParser([
'redirects' => [
'directory/subdirectory/file' => [
'directory/subdirectory/redirectedfile',
],
],
]));

$results = $parser->explode([
'redirects.directory/subdirectory/file.*' => 'string',
]);

$this->assertEquals([
'redirects.directory/subdirectory/file.0' => ['string'],
], $results->rules);
$this->assertEquals([
'redirects.directory/subdirectory/file.*' => ['redirects.directory/subdirectory/file.0'],
], $results->implicitAttributes);
}

public function testExplodeHandlesArraysOfNestedRules()
{
$parser = (new ValidationRuleParser([
Expand Down

0 comments on commit 9cd047c

Please sign in to comment.