Files
termanager2/app/Jobs/ImportTerritoryPdfFolder.php
francesco 0502fad996 Correggi bug di sicurezza e integrita' dei dati
I due piu' gravi potevano causare perdita di dati silenziosa.

AppServiceProvider chiamava key:generate --force a ogni richiesta quando
APP_KEY risultava vuota. Poiche' nome e cognome dei proclamatori sono
cifrati con quella chiave, ogni riga scritta sotto una chiave poi
sostituita diventava irrecuperabile. La chiave e' ora risolta una sola
volta all'avvio del container e il provider solleva un'eccezione se manca,
invece di rigenerarla.

importTerritoryPdfFolder() caricava i PDF, costruiva l'elenco dei file e
poi lo scartava senza mai accodare il job: nessun errore, nessun territorio
aggiornato, e i file restavano su disco a crescita illimitata perche' la
pulizia viveva solo nel finally del job mai eseguito.

Sicurezza:
- i permessi non venivano riapplicati alle richieste /livewire/update, per
  cui un permesso revocato restava attivo fino al reload della pagina
- host header injection: nginx passava l'Host del client come
  X-Forwarded-Host e Laravel si fidava di tutti i proxy, abbastanza per far
  generare a route() link verso domini arbitrari, inclusi i PDF pubblici
  che contengono un codice di accesso valido
- lo schema forwarded e' ora deciso dal server e non dall'header del client
- campagna_id in Rientra e' #[Locked]: era derivato lato server ma
  manipolabile dal client, permettendo di conteggiare un rientro su una
  campagna arbitraria
- sortField del registro passava non validato a orderBy(), con 500 su
  colonna inesistente
- SESSION_SECURE_COOKIE ora vale true fuori da local
- limiti su dimensione e numero di voci negli import ZIP

Integrita' e prestazioni:
- il default di assignment_link_ttl_hours era rimasto 24 dopo la
  conversione da ore a mesi: i link PDF pubblici duravano due anni invece
  di uno mese
- il seeder usava syncPermissions(), che a un secondo giro azzerava i
  permessi personalizzati dei ruoli; ora e' additivo
- pdftoppm senza timeout poteva bloccare il worker indefinitamente
- nomi file deterministici nell'import: "1 A" e "1-A" scrivevano sullo
  stesso PDF, sovrascrivendosi
- Setting::flushCache() dopo le cancellazioni massive, che non emettono
  eventi e lasciavano la cache puntata a una riga inesistente
- riga settings creata dal seeder, per evitare due singleton concorrenti
- rimosse due migrazioni ridondanti che rompevano migrate:rollback
- indici su assigned_at, (attivo, deleted_at) e deleted_at
- risolti gli N+1 negli elenchi territori e proclamatori

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-19 16:49:42 +02:00

271 lines
11 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Territorio;
use App\Models\User;
use App\Services\TerritorioPdfImportState;
use App\Services\TerritorioThumbnailService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class ImportTerritoryPdfFolder implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public int $tries = 1;
// A bounded timeout, not 0. With no limit a single PDF that hangs the
// renderer blocks the worker forever and the import state never leaves
// "running", so the UI polls a job that will never finish.
public int $timeout = 900;
public function __construct(
public string $importId,
public array $files,
public int $actorId,
) {
}
public function handle(TerritorioPdfImportState $stateService, TerritorioThumbnailService $thumbnailService): void
{
$stateService->markRunning($this->importId);
$stateService->appendLog($this->importId, 'Worker avviato. Inizio elaborazione dei PDF.');
$territoriMap = [];
foreach (Territorio::withTrashed()->get() as $territorio) {
$territoriMap[$this->normalizeTerritoryNumber($territorio->numero)] = $territorio;
}
$actor = User::find($this->actorId);
$seenNumbers = [];
try {
foreach ($this->files as $file) {
$originalName = $file['original_name'] ?? 'file-sconosciuto.pdf';
$stateService->increment($this->importId, 'processed');
$territoryMatch = $this->resolveTerritoryFromFilename($originalName, $territoriMap);
if ($territoryMatch === null) {
$stateService->increment($this->importId, 'skipped');
$stateService->appendLog($this->importId, '[SKIP] ' . $originalName . ' - nessun numero territorio riconosciuto nel nome file.');
$stateService->addIssue($this->importId, [
'file' => $originalName,
'type' => 'no-match',
'message' => 'Nessun numero territorio riconosciuto nel nome file.',
]);
continue;
}
if (($territoryMatch['type'] ?? 'single') === 'ambiguous') {
$stateService->increment($this->importId, 'skipped');
$stateService->appendLog($this->importId, '[SKIP] ' . $originalName . ' - nome ambiguo, possibili territori: ' . implode(', ', $territoryMatch['matched_numbers']) . '.');
$stateService->addIssue($this->importId, [
'file' => $originalName,
'type' => 'ambiguous',
'message' => 'Nome ambiguo.',
'matched_numbers' => $territoryMatch['matched_numbers'],
]);
continue;
}
$normalizedNumber = $territoryMatch['normalized_number'];
$matchedNumber = $territoryMatch['matched_number'];
$territorio = $territoryMatch['territorio'];
if (isset($seenNumbers[$normalizedNumber])) {
$stateService->increment($this->importId, 'skipped');
$stateService->appendLog($this->importId, '[SKIP] ' . $originalName . ' - territorio ' . $matchedNumber . ' presente piu volte nella stessa importazione.');
$stateService->addIssue($this->importId, [
'file' => $originalName,
'type' => 'duplicate-in-batch',
'message' => 'Territorio presente piu volte nella stessa importazione.',
'matched_numbers' => [$matchedNumber],
]);
continue;
}
$seenNumbers[$normalizedNumber] = true;
$sourcePath = Storage::disk('local')->path($file['stored_path']);
if (! is_file($sourcePath)) {
$stateService->increment($this->importId, 'errors');
$stateService->appendLog($this->importId, '[ERR] ' . $originalName . ' - file temporaneo non trovato.');
$stateService->addIssue($this->importId, [
'file' => $originalName,
'type' => 'missing-temp-file',
'message' => 'File temporaneo non trovato.',
'matched_numbers' => [$matchedNumber],
]);
continue;
}
try {
if ($territorio->pdf_path) {
Storage::disk('public')->delete($territorio->pdf_path);
}
if ($territorio->thumbnail_path) {
$thumbnailService->delete($territorio->thumbnail_path);
}
// The territory id is part of the filename because the slug
// alone is not unique: "1 A" and "1-A" are distinct
// territory numbers that both slug to "territorio-1-a".
// Without it the second import overwrites the first's PDF
// and both rows point at one file — then removing the PDF
// from either territory breaks the other.
$storedFilename = Str::slug('territorio-' . $territorio->numero)
. '-' . $territorio->id . '.pdf';
$publicPath = 'territori-pdf/' . $storedFilename;
Storage::disk('public')->put($publicPath, file_get_contents($sourcePath));
$thumbnailPath = $thumbnailService->generate($publicPath);
$territorio->update([
'pdf_path' => $publicPath,
'thumbnail_path' => $thumbnailPath,
]);
if ($actor) {
activity()->causedBy($actor)
->performedOn($territorio)
->withProperties([
'numero' => $territorio->numero,
'pdf' => $originalName,
'bulk_import' => true,
])
->log('bulk_uploaded_pdf');
}
$stateService->increment($this->importId, 'updated');
$stateService->appendLog(
$this->importId,
'[OK] ' . $originalName . ' - aggiornato territorio ' . $territorio->numero . ($thumbnailPath ? ' con thumbnail generata.' : ' ma la thumbnail non e stata generata.')
);
} catch (\Throwable $exception) {
$stateService->increment($this->importId, 'errors');
$stateService->appendLog($this->importId, '[ERR] ' . $originalName . ' - ' . $exception->getMessage());
$stateService->addIssue($this->importId, [
'file' => $originalName,
'type' => 'processing-error',
'message' => $exception->getMessage(),
'matched_numbers' => [$matchedNumber],
]);
}
}
$stateService->appendLog($this->importId, 'Import completato.');
$stateService->markCompleted($this->importId);
} catch (\Throwable $exception) {
$stateService->increment($this->importId, 'errors');
$stateService->appendLog($this->importId, '[ERR] Errore fatale del job: ' . $exception->getMessage());
$stateService->markFailed($this->importId);
throw $exception;
} finally {
Storage::disk('local')->deleteDirectory('bulk-territori-imports/' . $this->importId);
}
}
/**
* Runs when the job exceeds $timeout or dies outside handle()'s try block.
*
* Without this the import state is stranded in "running" — the UI polls it
* until the 24h cache entry expires — and the uploaded temp directory is
* never reclaimed, since handle()'s finally never executes.
*/
public function failed(?\Throwable $exception): void
{
$stateService = app(TerritorioPdfImportState::class);
$stateService->appendLog(
$this->importId,
'[ERR] Job terminato senza completare: ' . ($exception?->getMessage() ?? 'timeout')
);
$stateService->markFailed($this->importId);
Storage::disk('local')->deleteDirectory('bulk-territori-imports/' . $this->importId);
}
protected function resolveTerritoryFromFilename(string $filename, array $territoriMap): ?array
{
if ($territoriMap === []) {
return null;
}
$basename = pathinfo($filename, PATHINFO_FILENAME);
$normalizedBasename = mb_strtoupper($basename);
$normalizedBasename = preg_replace('/[^A-Z0-9]+/u', ' ', $normalizedBasename);
$normalizedBasename = trim(preg_replace('/\s+/', ' ', $normalizedBasename));
$territoryKeys = array_keys($territoriMap);
usort($territoryKeys, function (string $left, string $right) {
$lengthComparison = mb_strlen($right) <=> mb_strlen($left);
if ($lengthComparison !== 0) {
return $lengthComparison;
}
return strnatcasecmp($left, $right);
});
$matches = [];
foreach ($territoryKeys as $normalizedNumber) {
if (! $this->filenameContainsTerritoryNumber($normalizedBasename, $normalizedNumber)) {
continue;
}
$matches[] = [
'normalized_number' => $normalizedNumber,
'matched_number' => $territoriMap[$normalizedNumber]->numero,
'territorio' => $territoriMap[$normalizedNumber],
];
}
if ($matches === []) {
return null;
}
if (count($matches) > 1) {
return [
'type' => 'ambiguous',
'matched_numbers' => array_values(array_map(fn(array $match) => $match['matched_number'], $matches)),
];
}
return $matches[0];
}
protected function filenameContainsTerritoryNumber(string $normalizedBasename, string $normalizedNumber): bool
{
$escapedNumber = preg_quote($normalizedNumber, '/');
if (preg_match('/^\d+$/', $normalizedNumber)) {
$escapedNumber = '0*' . preg_quote(ltrim($normalizedNumber, '0') ?: '0', '/');
}
return (bool) preg_match('/(^| )' . $escapedNumber . '(?= |$)/', $normalizedBasename);
}
protected function normalizeTerritoryNumber(string $number): string
{
$normalized = preg_replace('/\s+/', ' ', trim(mb_strtoupper($number)));
if ($normalized !== '' && preg_match('/^\d+$/', $normalized)) {
return ltrim($normalized, '0') ?: '0';
}
return $normalized;
}
}