This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
feat: Add #[IsGrantedTool] for tool access control
#382
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| namespace PhpLlm\LlmChain\Chain\Toolbox\Security\Attribute; | ||
|
|
||
| use PhpLlm\LlmChain\Platform\Tool\Tool; | ||
| use Symfony\Component\ExpressionLanguage\Expression; | ||
|
|
||
| /** | ||
| * Checks if user has permission to access to some tool resource using security roles and voters. | ||
| * | ||
| * @see https://symfony.com/doc/current/security.html#roles | ||
| * | ||
| * @author Valtteri R <[email protected]> | ||
| */ | ||
| #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)] | ||
| final class IsGrantedTool | ||
| { | ||
| /** | ||
| * @param string|Expression $attribute The attribute that will be checked against a given authentication token and optional subject | ||
| * @param array<mixed>|string|Expression|\Closure(array<string,mixed>, Tool):mixed|null $subject An optional subject - e.g. the current object being voted on | ||
| * @param string|null $message A custom message when access is not granted | ||
| * @param int|null $exceptionCode If set, will add the exception code to thrown exception | ||
| */ | ||
| public function __construct( | ||
| public string|Expression $attribute, | ||
| public array|string|Expression|\Closure|null $subject = null, | ||
| public ?string $message = null, | ||
| public ?int $exceptionCode = null, | ||
| ) { | ||
| } | ||
| } |
105 changes: 105 additions & 0 deletions
105
src/Chain/Toolbox/Security/EventListener/IsGrantedToolAttributeListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| namespace PhpLlm\LlmChain\Chain\Toolbox\Security\EventListener; | ||
|
|
||
| use PhpLlm\LlmChain\Chain\Toolbox\Event\ToolCallArgumentsResolved; | ||
| use PhpLlm\LlmChain\Chain\Toolbox\Security\Attribute\IsGrantedTool; | ||
| use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
| use Symfony\Component\ExpressionLanguage\Expression; | ||
| use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
| use Symfony\Component\Security\Core\Authorization\AccessDecision; | ||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
| use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
| use Symfony\Component\Security\Core\Exception\RuntimeException; | ||
|
|
||
| /** | ||
| * Checks {@see IsGrantedTool} attributes on tools just before they are called. | ||
| * | ||
| * @author Valtteri R <[email protected]> | ||
| */ | ||
| #[AsEventListener] | ||
| class IsGrantedToolAttributeListener | ||
| { | ||
| public function __construct( | ||
| private readonly AuthorizationCheckerInterface $authChecker, | ||
| private ?ExpressionLanguage $expressionLanguage = null, | ||
| ) { | ||
| } | ||
|
|
||
| public function __invoke(ToolCallArgumentsResolved $event): void | ||
| { | ||
| $tool = $event->tool; | ||
| $class = new \ReflectionClass($tool); | ||
| $method = $class->getMethod($event->metadata->reference->method); | ||
| $classAttributes = $class->getAttributes(IsGrantedTool::class); | ||
| $methodAttributes = $method->getAttributes(IsGrantedTool::class); | ||
|
|
||
| if (!$classAttributes && !$methodAttributes) { | ||
| return; | ||
| } | ||
|
|
||
| $arguments = $event->arguments; | ||
|
|
||
| foreach (array_merge($classAttributes, $methodAttributes) as $attr) { | ||
| /** @var IsGrantedTool $attribute */ | ||
| $attribute = $attr->newInstance(); | ||
| $subject = null; | ||
|
|
||
| if ($subjectRef = $attribute->subject) { | ||
| if (\is_array($subjectRef)) { | ||
| foreach ($subjectRef as $refKey => $ref) { | ||
| $subject[\is_string($refKey) ? $refKey : (string) $ref] = $this->getIsGrantedSubject($ref, $tool, $arguments); | ||
| } | ||
| } else { | ||
| $subject = $this->getIsGrantedSubject($subjectRef, $tool, $arguments); | ||
| } | ||
| } | ||
|
|
||
| $accessDecision = null; | ||
| // bc layer | ||
| if (class_exists(AccessDecision::class)) { | ||
| $accessDecision = new AccessDecision(); | ||
| $accessDecision->isGranted = false; | ||
| $decision = &$accessDecision->isGranted; | ||
| } | ||
|
|
||
| if (!$decision = $this->authChecker->isGranted($attribute->attribute, $subject, $accessDecision)) { | ||
| $message = $attribute->message ?: $accessDecision->getMessage(); | ||
|
|
||
| $e = new AccessDeniedException($message, code: $attribute->exceptionCode ?? 403); | ||
| $e->setAttributes([$attribute->attribute]); | ||
| $e->setSubject($subject); | ||
| if ($accessDecision) { | ||
| $e->setAccessDecision($accessDecision); | ||
| } | ||
|
|
||
| throw $e; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $arguments | ||
| */ | ||
| private function getIsGrantedSubject(string|Expression|\Closure $subjectRef, object $tool, array $arguments): mixed | ||
| { | ||
| if ($subjectRef instanceof \Closure) { | ||
| return $subjectRef($arguments, $tool); | ||
| } | ||
|
|
||
| if ($subjectRef instanceof Expression) { | ||
| $this->expressionLanguage ??= new ExpressionLanguage(); | ||
|
|
||
| return $this->expressionLanguage->evaluate($subjectRef, [ | ||
| 'tool' => $tool, | ||
| 'args' => $arguments, | ||
| ]); | ||
| } | ||
|
|
||
| if (!\array_key_exists($subjectRef, $arguments)) { | ||
| throw new RuntimeException(\sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your tool method.', $subjectRef, $subjectRef)); | ||
| } | ||
|
|
||
| return $arguments[$subjectRef]; | ||
| } | ||
| } | ||
83 changes: 83 additions & 0 deletions
83
tests/Chain/Toolbox/Security/IsGrantedAttributeListenerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpLlm\LlmChain\Tests\Chain\Toolbox\Security; | ||
|
|
||
| use PhpLlm\LlmChain\Chain\Toolbox\Event\ToolCallArgumentsResolved; | ||
| use PhpLlm\LlmChain\Chain\Toolbox\Security\EventListener\IsGrantedToolAttributeListener; | ||
| use PhpLlm\LlmChain\Platform\Tool\ExecutionReference; | ||
| use PhpLlm\LlmChain\Platform\Tool\Tool; | ||
| use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolWithIsGrantedOnClass; | ||
| use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolWithIsGrantedOnMethod; | ||
| use PHPUnit\Framework\Attributes\Before; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use PHPUnit\Framework\Attributes\TestWith; | ||
| use PHPUnit\Framework\Attributes\UsesClass; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\EventDispatcher\EventDispatcher; | ||
| use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
| use Symfony\Component\ExpressionLanguage\Expression; | ||
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
| use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
|
|
||
| #[CoversClass(IsGrantedToolAttributeListener::class)] | ||
| #[UsesClass(EventDispatcher::class)] | ||
| #[UsesClass(ToolCallArgumentsResolved::class)] | ||
| #[UsesClass(Expression::class)] | ||
| #[UsesClass(AccessDeniedException::class)] | ||
| #[UsesClass(Tool::class)] | ||
| #[UsesClass(ExecutionReference::class)] | ||
| class IsGrantedAttributeListenerTest extends TestCase | ||
| { | ||
| private EventDispatcherInterface $dispatcher; | ||
| private AuthorizationCheckerInterface&MockObject $authChecker; | ||
|
|
||
| #[Before] | ||
| protected function setupTool(): void | ||
| { | ||
| $this->dispatcher = new EventDispatcher(); | ||
| $this->authChecker = $this->createMock(AuthorizationCheckerInterface::class); | ||
| $this->dispatcher->addListener(ToolCallArgumentsResolved::class, new IsGrantedToolAttributeListener($this->authChecker)); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'simple'), 'simple', '')])] | ||
| #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'expressionAsSubject'), 'expressionAsSubject', '')])] | ||
| #[TestWith([new ToolWithIsGrantedOnClass(), new Tool(new ExecutionReference(ToolWithIsGrantedOnClass::class, '__invoke'), 'ToolWithIsGrantedOnClass', '')])] | ||
| public function itWillThrowWhenNotGranted(object $tool, Tool $metadata): void | ||
| { | ||
| $this->authChecker->expects(self::once())->method('isGranted')->willReturn(false); | ||
|
|
||
| self::expectException(AccessDeniedException::class); | ||
| self::expectExceptionMessage(\sprintf('No access to %s tool.', $metadata->name)); | ||
| $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, [])); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'simple'), '', '')], 'method')] | ||
| public function itWillNotThrowWhenGranted(object $tool, Tool $metadata): void | ||
| { | ||
| $this->authChecker->expects(self::once())->method('isGranted')->with('ROLE_USER')->willReturn(true); | ||
| $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, [])); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'argumentAsSubject'), '', '')], 'method')] | ||
| #[TestWith([new ToolWithIsGrantedOnClass(), new Tool(new ExecutionReference(ToolWithIsGrantedOnClass::class, '__invoke'), '', '')], 'class')] | ||
| public function itWillProvideArgumentAsSubject(object $tool, Tool $metadata): void | ||
| { | ||
| $this->authChecker->expects(self::once())->method('isGranted')->with('test:permission', 44)->willReturn(true); | ||
| $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, ['itemId' => 44])); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'expressionAsSubject'), '', '')], 'method')] | ||
| public function itWillEvaluateSubjectExpression(object $tool, Tool $metadata): void | ||
| { | ||
| $this->authChecker->expects(self::once())->method('isGranted')->with('test:permission', 44)->willReturn(true); | ||
| $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, ['itemId' => 44])); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace PhpLlm\LlmChain\Tests\Fixture\Tool; | ||
|
|
||
| use PhpLlm\LlmChain\Chain\Toolbox\Security\Attribute\IsGrantedTool; | ||
| use Symfony\Component\ExpressionLanguage\Expression; | ||
|
|
||
| #[IsGrantedTool('test:permission', new Expression('args["itemId"] ?? 0'), message: 'No access to ToolWithIsGrantedOnClass tool.')] | ||
| final class ToolWithIsGrantedOnClass | ||
| { | ||
| public function __invoke(int $itemId): void | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace PhpLlm\LlmChain\Tests\Fixture\Tool; | ||
|
|
||
| use PhpLlm\LlmChain\Chain\Toolbox\Security\Attribute\IsGrantedTool; | ||
| use Symfony\Component\ExpressionLanguage\Expression; | ||
|
|
||
| final class ToolWithIsGrantedOnMethod | ||
| { | ||
| #[IsGrantedTool('ROLE_USER', message: 'No access to simple tool.')] | ||
| public function simple(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| #[IsGrantedTool('test:permission', 'itemId', message: 'No access to argumentAsSubject tool.')] | ||
| public function argumentAsSubject(int $itemId): int | ||
| { | ||
| return $itemId; | ||
| } | ||
|
|
||
| #[IsGrantedTool('test:permission', new Expression('args["itemId"]'), message: 'No access to expressionAsSubject tool.')] | ||
| public function expressionAsSubject(int $itemId): int | ||
| { | ||
| return $itemId; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.