Files
adam-acs/inertia/helpers/fetch_with_csrf.ts
T
kevin.lhereec 8cddf98093
Build & Deploy / build (push) Successful in 30s
Build & Deploy / docker (push) Successful in 1m6s
Build & Deploy / deploy (push) Failing after 33s
fix: CSRF token sur les requetes mutantes /internal + IPv6 Docker + tests Connection Request
- Ajout de fetchWithCsrf() helper qui lit le cookie XSRF-TOKEN et injecte
  X-XSRF-TOKEN sur toutes les requetes POST/PUT/PATCH/DELETE vers /internal/*
- Remplacement de fetch() par fetchWithCsrf() dans tous les composants Vue
  concernes (Tr069Trace, DeviceDetail, SequenceViewer, Home, configurations,
  firmware_catalog)
- Activation IPv6 sur les reseaux Docker (enable_ipv6: true, subnet fd00:adam::/64)
  dans docker-compose.yml et docker-compose.prod.yml
- Ajout tests unitaires DeviceService.sendWakeOnWlan : IPv4, IPv6, ENETUNREACH,
  credentials fallback chain (11 tests)
2026-07-17 10:16:29 +02:00

39 lines
1.1 KiB
TypeScript

/**
* Reads the XSRF-TOKEN cookie set by AdonisJS Shield and returns its decoded value.
* Returns an empty string if the cookie is not present.
*/
function getCsrfToken(): string {
const match = document.cookie
.split('; ')
.find((row) => row.startsWith('XSRF-TOKEN='))
if (!match) return ''
return decodeURIComponent(match.split('=').slice(1).join('='))
}
/**
* Wrapper around the native fetch() that automatically adds the
* X-XSRF-TOKEN header for state-mutating requests (POST, PUT, PATCH, DELETE)
* to satisfy AdonisJS Shield CSRF protection on /internal/* routes.
*
* Usage: drop-in replacement for fetch() — same signature.
*/
export function fetchWithCsrf(input: RequestInfo | URL, init: RequestInit = {}): Promise<Response> {
const method = (init.method ?? 'GET').toUpperCase()
const mutating = ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method)
if (mutating) {
const token = getCsrfToken()
if (token) {
init = {
...init,
headers: {
...init.headers,
'X-XSRF-TOKEN': token,
},
}
}
}
return fetch(input, init)
}