Files
InvoiceShelf/app/Services/Ai/Tools/GetCustomerTool.php
Darko Gjorgjijoski ac2a8ca939 fix(security): gate AI tools by user ability and block admin-URL SSRF
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
2026-06-05 00:01:09 +02:00

84 lines
2.6 KiB
PHP

<?php
namespace App\Services\Ai\Tools;
use App\Models\Customer;
use App\Models\Invoice;
class GetCustomerTool extends AiTool
{
public function name(): string
{
return 'get_customer';
}
public function description(): string
{
return 'Fetch full details for a single customer by ID, including contact info, billing/shipping address, and aggregate totals (invoice count, outstanding balance).';
}
public function parameterSchema(): array
{
return [
'type' => 'object',
'properties' => [
'customer_id' => [
'type' => 'integer',
'description' => 'The customer ID.',
],
],
'required' => ['customer_id'],
];
}
public function requiredAbility(): ?array
{
return ['view-customer', Customer::class];
}
public function execute(array $arguments, int $companyId, int $userId): mixed
{
$customer = Customer::query()
->where('company_id', $companyId)
->where('id', (int) ($arguments['customer_id'] ?? 0))
->with(['billingAddress', 'shippingAddress'])
->first();
if (! $customer) {
return ['error' => 'customer_not_found'];
}
// Aggregate totals — done with lightweight queries rather than loading every invoice.
$invoiceCount = Invoice::query()
->where('company_id', $companyId)
->where('customer_id', $customer->id)
->count();
$outstanding = (float) Invoice::query()
->where('company_id', $companyId)
->where('customer_id', $customer->id)
->whereIn('paid_status', ['UNPAID', 'PARTIALLY_PAID'])
->sum('due_amount');
return [
'customer' => [
'id' => $customer->id,
'name' => $customer->name,
'display_name' => $customer->display_name,
'email' => $customer->email,
'phone' => $customer->phone,
'contact_name' => $customer->contact_name,
'company_name' => $customer->company_name,
'website' => $customer->website,
'enable_portal' => (bool) $customer->enable_portal,
'billing_address' => $customer->billingAddress,
'shipping_address' => $customer->shippingAddress,
'totals' => [
'invoice_count' => $invoiceCount,
'outstanding_amount' => $outstanding,
],
],
];
}
}