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

Validate arguments before passing to authorize #413

Merged
merged 1 commit into from
Jul 19, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ CHANGELOG
- Replace global helper `is_lumen` with static class call `\Rebing\GraphQL\Helpers::isLumen`

### Fixed
- Arguments are now validation before they're passed to `authorize()` [\#413](https://github.com/rebing/graphql-laravel/pull/413)
- File uploads now correctly work with batched requests [\#397](https://github.com/rebing/graphql-laravel/pull/397)
- Path multi-level support for Schemas works again [\#358](https://github.com/rebing/graphql-laravel/pull/358)
- SelectFields correctly passes field arguments to the custom query [\#327](https://github.com/rebing/graphql-laravel/pull/327)
Expand Down
10 changes: 5 additions & 5 deletions src/Support/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ protected function getResolver(): ?Closure
$arguments[1] = array_merge($arguments[1], $arguments[2]);
}

// Authorize
if (call_user_func($authorize, $arguments[1]) != true) {
throw new AuthorizationError('Unauthorized');
}

// Validate mutation arguments
if (method_exists($this, 'getRules')) {
$args = Arr::get($arguments, 1, []);
Expand All @@ -188,6 +183,11 @@ protected function getResolver(): ?Closure
}
}

// Authorize
if (call_user_func($authorize, $arguments[1]) != true) {
throw new AuthorizationError('Unauthorized');
}

// Add the 'selects and relations' feature as 5th arg
if (isset($arguments[3])) {
$arguments[] = function () use ($arguments): SelectFields {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rebing\GraphQL\Tests\Unit\ValidationAuthorizationTests;

use Illuminate\Support\MessageBag;
use Rebing\GraphQL\Tests\TestCase;

class ValidationAuthorizationTest extends TestCase
Expand All @@ -23,7 +24,16 @@ public function testAuthorizeArgumentsInvalid(): void
],
]);

$this->assertSame('Unauthorized', $result['errors'][0]['message']);
$this->assertSame('validation', $result['errors'][0]['message']);

/** @var MessageBag $messageBag */
$messageBag = $result['errors'][0]['extensions']['validation'];
$expectedErrors = [
'arg1' => [
'The selected arg1 is invalid.',
],
];
$this->assertSame($expectedErrors, $messageBag->messages());
}

public function testAuthorizeArgumentsValid(): void
Expand Down