Skip to content
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

[8.x] Replace escaped dot with place holder in dependent rules parameters #39935

Merged
merged 4 commits into from
Dec 8, 2021
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
23 changes: 20 additions & 3 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,12 @@ protected function validateAttribute($attribute, $rule)
// First we will get the correct keys for the given attribute in case the field is nested in
// an array. Then we determine if the given rule accepts other field names as parameters.
// If so, we will replace any asterisks found in the parameters with the correct keys.
if (($keys = $this->getExplicitKeys($attribute)) &&
$this->dependsOnOtherFields($rule)) {
$parameters = $this->replaceAsterisksInParameters($parameters, $keys);
if ($this->dependsOnOtherFields($rule)) {
$parameters = $this->replaceDotInParameters($parameters);

if ($keys = $this->getExplicitKeys($attribute)) {
$parameters = $this->replaceAsterisksInParameters($parameters, $keys);
}
}

$value = $this->getValue($attribute);
Expand Down Expand Up @@ -660,6 +663,20 @@ protected function getPrimaryAttribute($attribute)
return $attribute;
}

/**
* Replace each field parameter which has an escaped dot with the dot placeholder.
*
* @param array $parameters
* @param array $keys
* @return array
*/
protected function replaceDotInParameters(array $parameters)
{
return array_map(function ($field) {
return str_replace('\.', $this->dotPlaceholder, $field);
}, $parameters);
}

/**
* Replace each field parameter which has asterisks with the given keys.
*
Expand Down
33 changes: 28 additions & 5 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4856,23 +4856,46 @@ public function testValidateImplicitEachWithAsterisksForRequiredNonExistingKey()
public function testParsingArrayKeysWithDot()
{
$trans = $this->getIlluminateArrayTranslator();

// Interpreted dot fails on empty value
$v = new Validator($trans, ['foo' => ['bar' => ''], 'foo.bar' => 'valid'], ['foo.bar' => 'required']);
$this->assertTrue($v->fails());

// Escaped dot fails on empty value
$v = new Validator($trans, ['foo' => ['bar' => 'valid'], 'foo.bar' => ''], ['foo\.bar' => 'required']);
$this->assertTrue($v->fails());

// Interpreted dot succeeds
$v = new Validator($trans, ['foo' => ['bar' => 'valid'], 'foo.bar' => 'zxc'], ['foo\.bar' => 'required']);
$this->assertFalse($v->fails());

// Interpreted dot followed by escaped dot fails on empty value
$v = new Validator($trans, ['foo' => ['bar.baz' => '']], ['foo.bar\.baz' => 'required']);
$this->assertTrue($v->fails());

// Interpreted dot followed by escaped dot fails on empty value
$v = new Validator($trans, ['foo' => [['bar.baz' => ''], ['bar.baz' => '']]], ['foo.*.bar\.baz' => 'required']);
$this->assertTrue($v->fails());
}

public function testParsingArrayKeysWithDotWhenTestingExistence()
{
$trans = $this->getIlluminateArrayTranslator();
// RequiredWith using escaped dot in a nested array
$v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_with:bar.foo\.bar']);
$this->assertFalse($v->passes());
// RequiredWithAll using escaped dot in a nested array
$v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_with_all:bar.foo\.bar']);
$this->assertFalse($v->passes());
// RequiredWithout using escaped dot in a nested array
$v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_without:bar.foo\.bar']);
$this->assertTrue($v->passes());
// RequiredWithoutAll using escaped dot in a nested array
$v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_without_all:bar.foo\.bar']);
$this->assertTrue($v->passes());
// Same using escaped dot in a nested array
$v = new Validator($trans, ['foo' => 'valid', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'same:bar.foo\.bar']);
$this->assertTrue($v->passes());
// RequiredUnless using escaped dot in a nested array
$v = new Validator($trans, ['foo' => '', 'bar' => ['foo.bar' => 'valid']], ['foo' => 'required_unless:bar.foo\.bar,valid']);
$this->assertTrue($v->passes());
}

public function testPassingSlashVulnerability()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down