Files
nis2-agile/public/sw.js
T
DevEnv nis2-agileandClaude Opus 4.8 4f386faae5 [FEAT] Epic C / C5.2b — Portale esterno stakeholder + sotto-dashboard feedback (mig.053)
Completa C5 (Epic C). Gli stakeholder rispondono in self-service tramite magic-link
(token SHA-256 per destinatario, NESSUN account/JWT); il compliance manager vede gli
esiti, i commenti e gli allegati nel dettaglio dell'attività.

- StakeholderPortalController (non-JWT, token-only): access / respond (questionario) /
  acknowledge (firma di avvenuta lettura) / comment / attachment. Submit one-shot (409),
  validazione risposte obbligatorie, anti-IDOR (un token = un solo destinatario),
  auto-completamento attività quando tutti hanno risposto/firmato.
- StakeholderActivityController: feedback (risposte per destinatario), comments
  (GET/POST), attachments (upload interno + lista; riuso evidence_files entity_type=
  'stk_activity', file sotto public/uploads/stk_activity/{org}/).
- mig.053: stk_activity_responses (answers JSON / acknowledged_at), stk_activity_comments
  (interni/esterni). Estende il seeder idempotente.
- Frontend: stk-portal.html (pagina pubblica dependency-free: questionario per tipo di
  domanda o testo+firma, commento, upload); dettaglio attività in stakeholder-activities.html
  con esiti, thread commenti e allegati.

Email disattivate (kill-switch) → i magic-link si condividono manualmente. Smoke prod OK
(access no-auth, respond+required+409, acknowledge+WRONG_TYPE, comment esterno/interno,
bad-token 404, feedback interno, auto-complete; cleanup org 151 pulita). Additivo. v1.21.0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 22:20:14 +02:00

97 lines
3.4 KiB
JavaScript

/*
* NIS2 Agile — Service Worker (PWA)
* -----------------------------------------------------------------------------
* Strategia (SaaS autenticato multi-tenant — sicurezza prima di tutto):
* - /api/* -> NETWORK-ONLY, MAI in cache. Nessun dato autenticato
* viene cacheato => zero leak cross-utente su un
* dispositivo condiviso. La sessione resta gestita da
* JWT / auth-gate.js, fuori dal service worker.
* - navigazioni HTML -> NETWORK-FIRST, fallback alla cache e infine /offline.html.
* - asset statici -> STALE-WHILE-REVALIDATE (risposta immediata dalla cache,
* aggiornamento in background).
*
* Il nome cache include la release: va BUMPATO ad ogni rilascio (vedi version.json),
* cosi' activate elimina automaticamente la shell vecchia.
*/
const CACHE = 'nis2-shell-v1.21.0';
const PRECACHE = [
'/offline.html',
'/manifest.webmanifest',
'/css/style.css',
'/js/common.js',
'/js/common-bi.js',
'/js/api.js',
'/js/i18n.js',
'/js/pwa.js',
'/vendor/bootstrap-italia/dist/css/bootstrap-italia.min.css',
'/vendor/bootstrap-italia/dist/js/bootstrap-italia.bundle.min.js',
'/vendor/bootstrap-italia/dist/svg/sprites.svg',
'/assets/icons/icon-192.png',
'/assets/icons/icon-512.png'
];
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE);
// add uno-a-uno: un singolo asset mancante non deve far fallire l'install
await Promise.all(PRECACHE.map((url) =>
cache.add(new Request(url, { cache: 'reload' })).catch((err) =>
console.warn('[sw] precache skip', url, err && err.message))
));
await self.skipWaiting();
})());
});
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
const keys = await caches.keys();
await Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)));
await self.clients.claim();
})());
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return; // non-GET: rete normale
const url = new URL(req.url);
if (url.origin !== self.location.origin) return; // cross-origin: rete normale
// /api/* -> network-only, mai in cache
if (url.pathname.startsWith('/api/')) {
event.respondWith(
fetch(req).catch(() => new Response(
JSON.stringify({ error: 'offline', message: 'Connessione di rete assente' }),
{ status: 503, headers: { 'Content-Type': 'application/json' } }
))
);
return;
}
// navigazioni HTML -> network-first, fallback cache -> offline
if (req.mode === 'navigate') {
event.respondWith((async () => {
try {
return await fetch(req);
} catch (e) {
const cached = await caches.match(req);
return cached || (await caches.match('/offline.html'));
}
})());
return;
}
// asset statici stesso-origin -> stale-while-revalidate
event.respondWith((async () => {
const cache = await caches.open(CACHE);
const cached = await cache.match(req);
const network = fetch(req).then((res) => {
if (res && res.status === 200 && res.type === 'basic') {
cache.put(req, res.clone());
}
return res;
}).catch(() => null);
return cached || (await network) || (await caches.match('/offline.html'));
})());
});