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

[7.x] Replace placeholder for dots and asterisks in validator #33367

Merged
merged 3 commits into from Jun 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Illuminate/Validation/Validator.php
Expand Up @@ -277,7 +277,7 @@ public function __construct(Translator $translator, array $data, array $rules,
}

/**
* Parse the data array, converting dots to ->.
* Parse the data array, converting dots and asterisks.
*
* @param array $data
* @return array
Expand All @@ -303,6 +303,33 @@ public function parseData(array $data)
return $newData;
}

/**
* Replace the placeholder used in data keys.
*
* @param array $data
* @return array
*/
public function replacePlaceholders($data)
{
$originalData = [];

foreach ($data as $key => $value) {
if (is_array($value)) {
$value = $this->replacePlaceholders($value);
}

$key = str_replace(
[$this->dotPlaceholder, '__asterisk__'],
['.', '*'],
$key
);

$originalData[$key] = $value;
}

return $originalData;
}

/**
* Add an after validation callback.
*
Expand Down Expand Up @@ -464,7 +491,7 @@ public function validated()
}
}

return $results;
return $this->replacePlaceholders($results);
}

/**
Expand Down
57 changes: 28 additions & 29 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -3880,6 +3880,32 @@ public function testPassingSlashVulnerability()
$this->assertTrue($v->fails());
}

public function testPlaceholdersAreReplaced()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not actually covers the problem described in the related issue: it does not check whether validated data array contains the correct key or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to open a PR with more tests. That's always a good thing :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done: #33932

{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, [
'matrix' => ['\\' => ['invalid'], '1\\' => ['invalid']],
], [
'matrix.*.*' => 'integer',
]);
$this->assertTrue($v->fails());

$v = new Validator($trans, [
'matrix' => ['\\' => [1], '1\\' => [1]],
], [
'matrix.*.*' => 'integer',
]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [
'foo' => ['bar' => 'valid'], 'foo.bar' => 'invalid', 'foo->bar' => 'valid',
], [
'foo\.bar' => 'required|in:valid',
]);
$this->assertTrue($v->fails());
}

public function testCoveringEmptyKeys()
{
$trans = $this->getIlluminateArrayTranslator();
Expand All @@ -3891,35 +3917,8 @@ public function testImplicitEachWithAsterisksWithArrayValues()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'size:4']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => [1, 2, 3, 4]], ['foo' => 'size:4']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => [1, 2, 3, 4]], ['foo.*' => 'integer', 'foo.0' => 'required']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => [['bar' => [1, 2, 3]], ['bar' => [1, 2, 3]]]], ['foo.*.bar' => 'size:4']);
$this->assertFalse($v->passes());

$v = new Validator($trans,
['foo' => [['bar' => [1, 2, 3]], ['bar' => [1, 2, 3]]]], ['foo.*.bar' => 'min:3']);
$this->assertTrue($v->passes());

$v = new Validator($trans,
['foo' => [['bar' => [1, 2, 3]], ['bar' => [1, 2, 3]]]], ['foo.*.bar' => 'between:3,6']);
$this->assertTrue($v->passes());

$v = new Validator($trans,
['foo' => [['name' => 'first', 'votes' => [1, 2]], ['name' => 'second', 'votes' => ['something', 2]]]],
['foo.*.votes' => ['Required', 'Size:2']]);
$this->assertTrue($v->passes());

$v = new Validator($trans,
['foo' => [['name' => 'first', 'votes' => [1, 2, 3]], ['name' => 'second', 'votes' => ['something', 2]]]],
['foo.*.votes' => ['Required', 'Size:2']]);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['foo' => ['bar.baz' => '']], ['foo' => 'required']);
$this->assertEquals(['foo' => ['bar.baz' => '']], $v->validated());
}

public function testValidateNestedArrayWithCommonParentChildKey()
Expand Down