mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-17 06:15:20 +00:00
The AI chat assistant scoped tool queries by company but ignored the per-user Bouncer abilities the rest of the app enforces, so any `use ai` holder could read customers, invoices, payments, and company financials their role couldn't otherwise see. Each AiTool now declares a required ability (entity-aligned); the registry hides unauthorized tools from the model and refuses to execute them as a backstop. Separately, admin/owner-supplied URLs were fetched server-side with no guard against private/reserved targets (SSRF): the AI base URL, the CurrencyConverter "DEDICATED" exchange-rate URL, and S3/Spaces file-disk endpoints. A shared PrivateNetworkGuard now backs a PublicHttpUrl validation rule (save-time) and runtime guards in each driver. - AiTool::requiredAbility() + mapping across all 12 tools - AiToolRegistry filters schemas() by ability and re-checks in execute() - PrivateNetworkGuard / BlockedUrlException / PublicHttpUrl rule (new) - Rule wired into AI config (service + 3 controllers), exchange-rate, and file-disk endpoints; runtime guards in OpenRouterDriver, CurrencyConverterDriver, and FileDiskService - Tests for ability filtering, the guard, the rule, and 422 rejections
74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Ai\Tools;
|
|
|
|
use App\Models\Item;
|
|
|
|
class SearchItemsTool extends AiTool
|
|
{
|
|
private const DEFAULT_LIMIT = 10;
|
|
|
|
private const MAX_LIMIT = 50;
|
|
|
|
public function name(): string
|
|
{
|
|
return 'search_items';
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return 'Search catalog items (products/services) for the current company by free-text query (matches name and description). Returns id, name, unit price, and description.';
|
|
}
|
|
|
|
public function parameterSchema(): array
|
|
{
|
|
return [
|
|
'type' => 'object',
|
|
'properties' => [
|
|
'query' => [
|
|
'type' => 'string',
|
|
'description' => 'Free-text search against name and description.',
|
|
],
|
|
'limit' => [
|
|
'type' => 'integer',
|
|
'minimum' => 1,
|
|
'maximum' => self::MAX_LIMIT,
|
|
],
|
|
],
|
|
'required' => [],
|
|
];
|
|
}
|
|
|
|
public function requiredAbility(): ?array
|
|
{
|
|
return ['view-item', Item::class];
|
|
}
|
|
|
|
public function execute(array $arguments, int $companyId, int $userId): mixed
|
|
{
|
|
$limit = min((int) ($arguments['limit'] ?? self::DEFAULT_LIMIT), self::MAX_LIMIT);
|
|
|
|
$query = Item::query()
|
|
->where('company_id', $companyId)
|
|
->orderBy('name')
|
|
->limit($limit);
|
|
|
|
if (! empty($arguments['query'])) {
|
|
$q = $arguments['query'];
|
|
$query->where(function ($qb) use ($q) {
|
|
$qb->where('name', 'like', "%{$q}%")
|
|
->orWhere('description', 'like', "%{$q}%");
|
|
});
|
|
}
|
|
|
|
return [
|
|
'items' => $query->get()->map(fn (Item $item): array => [
|
|
'id' => $item->id,
|
|
'name' => $item->name,
|
|
'description' => $item->description,
|
|
'price' => $item->price,
|
|
])->all(),
|
|
];
|
|
}
|
|
}
|