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

[6.x] Type hinted arguments for Illuminate\Validation\Rules\RequiredIf #37688

Merged
merged 10 commits into from
Jun 14, 2021
Merged

Conversation

0xcrypto
Copy link
Contributor

Fixes a security issue as explained in https://www.huntr.dev/bounties/3-laravel/framework/

If the argument to Illuminate\Validation\Rules\RequiredIf is not boolean or callable, it will throw InvalidArgumentException.

Added tests to check

  1. that the provided input is either a callable or a boolean (as it was hinted in docblock)
  2. that the provided callable is not serializable.

@taylorotwell taylorotwell merged commit 814d6bc into laravel:6.x Jun 14, 2021
@0xcrypto
Copy link
Contributor Author

0xcrypto commented Jun 14, 2021

Thank you @taylorotwell, can you please validate the report and patch on huntr.dev as well (https://www.huntr.dev/bounties/3-laravel/framework/). It helps me earn a living for the contribution I do to open source projects.

@GrahamCampbell GrahamCampbell changed the title Type hinted arguments for Illuminate\Validation\Rules\RequiredIf [6.x] Type hinted arguments for Illuminate\Validation\Rules\RequiredIf Jun 14, 2021
@@ -29,4 +29,30 @@ public function testItClousureReturnsFormatsAStringVersionOfTheRule()

$this->assertSame('', (string) $rule);
}

public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule()
Copy link
Contributor

@nuernbergerA nuernbergerA Jun 15, 2021

Choose a reason for hiding this comment

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

This test would lead to false/positives.

expectException will succeed if one of the 3 rules hit
it would be better to test with a data provider or separate tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh my bad, will fix it.

@nuernbergerA
Copy link
Contributor

not sure what the security issues was, I assume it's related to __toString

I consider this as a breaking change because before it was possible to have something like this

Validator::make($request->all(), [
    'role_id' => Rule::requiredIf(optional($request->user())->is_admin),
]);

This will fail now if it's null

@0xcrypto
Copy link
Contributor Author

not sure what the security issues was, I assume it's related to __toString

I consider this as a breaking change because before it was possible to have something like this

Validator::make($request->all(), [
    'role_id' => Rule::requiredIf(optional($request->user())->is_admin),
]);

This will fail now if it's null

I agree this is a breaking change if people are using null to pass as false. But as the docblock says, the function only accepts a callable or boolean. I am unsure if allowing null would be better here or following the arguments description in the docblock. Maybe cast the null to boolean before passing? Or rather do it inside the function?

@ankurk91
Copy link
Contributor

Maybe cast the null to boolean before passing? Or rather do it inside the function?

Yes, i think it is ok to cast null to bool

if(!is_callable($condition)  || ! (bool)$condition){

}

@0xcrypto
Copy link
Contributor Author

if(!is_callable($condition) || ! (bool)$condition){

or maybe just check for argument not being a string as any literal other than string would make is_callable return false anyway.

if (! is_string($condition)) {
    $this->condition = $condition;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants