Files
InvoiceShelf/app/Services/Ai/Tools/ListOverdueInvoicesTool.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

60 lines
1.7 KiB
PHP

<?php
namespace App\Services\Ai\Tools;
use App\Models\Invoice;
class ListOverdueInvoicesTool extends AiTool
{
public function name(): string
{
return 'list_overdue_invoices';
}
public function description(): string
{
return 'List all invoices for the current company that are currently overdue (past their due date and unpaid or partially paid). Sorted by oldest-due-first.';
}
public function parameterSchema(): array
{
return [
'type' => 'object',
'properties' => (object) [],
'required' => [],
];
}
public function requiredAbility(): ?array
{
return ['view-invoice', Invoice::class];
}
public function execute(array $arguments, int $companyId, int $userId): mixed
{
$invoices = Invoice::query()
->where('company_id', $companyId)
->where('overdue', true)
->with('customer:id,name')
->orderBy('due_date')
->limit(100)
->get();
$totalOutstanding = (float) $invoices->sum('due_amount');
return [
'count' => $invoices->count(),
'total_outstanding' => $totalOutstanding,
'invoices' => $invoices->map(fn (Invoice $inv): array => [
'id' => $inv->id,
'invoice_number' => $inv->invoice_number,
'customer_id' => $inv->customer_id,
'customer_name' => $inv->customer?->name,
'due_date' => $this->asDate($inv->due_date),
'due_amount' => $inv->due_amount,
'total' => $inv->total,
])->all(),
];
}
}