From 60d7ba1c39a294885a7fb7653a3229aece9d5f74 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Thu, 10 Sep 2026 19:48:15 +0200 Subject: [PATCH] =?UTF-8?q?Corrige=20la=20d=C3=A9connexion=20:=20accepter?= =?UTF-8?q?=20le=20POST=20de=20formulaire=20(urlencoded)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le
de déconnexion envoie `application/x-www-form-urlencoded` ; Fastify n'ayant aucun parseur pour ce type répondait 415 (FST_ERR_CTP_INVALID_MEDIA_TYPE) avant même d'atteindre le handler. Ajout d'un parseur `application/x-www-form-urlencoded` (URLSearchParams -> objet, sans dépendance). Le test de déconnexion envoie désormais l'en-tête du navigateur pour couvrir le cas. Co-Authored-By: Claude Sonnet 5 --- src/app.ts | 14 ++++++++++++++ tests/auth-flow.test.ts | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 5765a06..08eecd2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -68,6 +68,20 @@ export async function buildApp( taskDone(null, payload); }); + // Formulaires HTML (déconnexion) : sinon Fastify répond 415 sur le POST du . + app.addContentTypeParser( + 'application/x-www-form-urlencoded', + { parseAs: 'string' }, + (_request, body: string | Buffer, taskDone) => { + try { + const raw = typeof body === 'string' ? body : body.toString('utf8'); + taskDone(null, Object.fromEntries(new URLSearchParams(raw))); + } catch (error) { + taskDone(error instanceof Error ? error : new Error('Corps de formulaire invalide')); + } + }, + ); + await app.register(secureSession, { key: config.session.key, cookieName: SESSION_COOKIE_NAME, diff --git a/tests/auth-flow.test.ts b/tests/auth-flow.test.ts index 6b6c642..16acec6 100644 --- a/tests/auth-flow.test.ts +++ b/tests/auth-flow.test.ts @@ -71,11 +71,15 @@ describe('flux d’authentification', () => { expect(me.json()).toEqual({ authenticated: true, name: 'Alice' }); }); - it('POST /auth/logout vide la session', async () => { + it('POST /auth/logout vide la session (soumission de HTML)', async () => { const cookie = await login(app, idp); + // Le navigateur envoie un POST application/x-www-form-urlencoded : le serveur + // doit l'accepter (et non répondre 415). const out = await app.inject({ method: 'POST', url: '/auth/logout', + headers: { 'content-type': 'application/x-www-form-urlencoded' }, + payload: '', cookies: { [SESSION_COOKIE]: cookie }, }); expect(out.statusCode).toBe(302);