Skip to content

Commit

Permalink
[8.x] Replace escaped dot with place holder in dependent rules parame…
Browse files Browse the repository at this point in the history
…ters (laravel#39935)

* Replace escaped dot with place holder in dependent rules parameters

* Fix styling

* Fix styling 2

* formatting

Co-authored-by: Quentin JAMELOT <quentin.jamelot@groupe-salaun.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people authored Dec 8, 2021
1 parent fada746 commit 6fb7c44
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
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

0 comments on commit 6fb7c44

Please sign in to comment.