[Server] Handle malformed JSON-RPC input graceful (#412)
* Never let a throwable escape Protocol::processInput() * Validate payload types in the schema hydration methods * Stop leaking handler exception messages to the client
This commit is contained in:
parent
9236824b6d
commit
c8ed07bba2
1 changed files with 858 additions and 58 deletions
|
|
@ -95,15 +95,30 @@ class CallToolResult implements ResultInterface
|
|||
$contents = [];
|
||||
|
||||
foreach ($data['content'] as $item) {
|
||||
$contents[] = match ($item['type'] ?? null) {
|
||||
$type = \is_array($item) ? $item['type'] ?? null : null;
|
||||
if (!\is_string($type)) {
|
||||
throw new InvalidArgumentException('Missing or invalid content "type" in CallToolResult data.');
|
||||
}
|
||||
|
||||
$contents[] = match ($type) {
|
||||
'text' => TextContent::fromArray($item),
|
||||
'image' => ImageContent::fromArray($item),
|
||||
'audio' => AudioContent::fromArray($item),
|
||||
'resource' => EmbeddedResource::fromArray($item),
|
||||
default => throw new InvalidArgumentException(\sprintf('Invalid content type in CallToolResult data: "%s".', $item['type'] ?? null)),
|
||||
default => throw new InvalidArgumentException(\sprintf('Invalid content type in CallToolResult data: "%s".', $type)),
|
||||
};
|
||||
}
|
||||
|
||||
if (isset($data['isError']) && !\is_bool($data['isError'])) {
|
||||
throw new InvalidArgumentException('Invalid "isError" in CallToolResult data.');
|
||||
}
|
||||
if (isset($data['structuredContent']) && !\is_array($data['structuredContent'])) {
|
||||
throw new InvalidArgumentException('Invalid "structuredContent" in CallToolResult data.');
|
||||
}
|
||||
if (isset($data['_meta']) && !\is_array($data['_meta'])) {
|
||||
throw new InvalidArgumentException('Invalid "_meta" in CallToolResult data.');
|
||||
}
|
||||
|
||||
return new self(
|
||||
$contents,
|
||||
$data['isError'] ?? false,
|
||||
|
|
|
|||
Loading…
Reference in a new issue