diff --git a/backend/package-lock.json b/backend/package-lock.json index e4505bf..8eae119 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -8,9 +8,9 @@ "name": "taboada-becarios-api", "version": "0.1.0", "dependencies": { + "@fastify/cookie": "^11.0.2", "@fastify/cors": "^11.2.0", "@fastify/rate-limit": "^11.1.0", - "@fastify/secure-session": "^8.3.0", "argon2": "^0.44.0", "dotenv": "^17.4.2", "fastify": "^5.9.0", @@ -217,30 +217,6 @@ ], "license": "MIT" }, - "node_modules/@fastify/secure-session": { - "version": "8.3.0", - "resolved": "https://registry.npmjs.org/@fastify/secure-session/-/secure-session-8.3.0.tgz", - "integrity": "sha512-jyXbYWAp5MCaCA+7idsV+Op3kMN/oDtskZ0SE3QPl53Lo0GFF7p/lk1UEliGaT0OSX+RGSrd2gQlUu1ta1t7yQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/fastify" - }, - { - "type": "opencollective", - "url": "https://opencollective.com/fastify" - } - ], - "license": "MIT", - "dependencies": { - "@fastify/cookie": "^11.0.1", - "fastify-plugin": "^5.0.0", - "sodium-native": "^5.0.1" - }, - "bin": { - "secure-session": "genkey.js" - } - }, "node_modules/@lukeed/ms": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/@lukeed/ms/-/ms-2.0.2.tgz", @@ -349,150 +325,6 @@ "fastq": "^1.17.1" } }, - "node_modules/b4a": { - "version": "1.8.1", - "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.8.1.tgz", - "integrity": "sha512-aiqre1Nr0B/6DgE2N5vwTc+2/oQZ4Wh1t4NznYY4E00y8LCt6NqdRv81so00oo27D8MVKTpUa/MwUUtBLXCoDw==", - "license": "Apache-2.0", - "peerDependencies": { - "react-native-b4a": "*" - }, - "peerDependenciesMeta": { - "react-native-b4a": { - "optional": true - } - } - }, - "node_modules/bare-addon-resolve": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/bare-addon-resolve/-/bare-addon-resolve-1.10.0.tgz", - "integrity": "sha512-sSd0jieRJlDaODOzj0oe0RjFVC1QI0ZIjGIdPkbrTXsdVVtENg14c+lHHAhHwmWCZ2nQlMhy8jA3Y5LYPc/isA==", - "license": "Apache-2.0", - "dependencies": { - "bare-module-resolve": "^1.10.0", - "bare-semver": "^1.0.0" - }, - "peerDependencies": { - "bare-url": "*" - }, - "peerDependenciesMeta": { - "bare-url": { - "optional": true - } - } - }, - "node_modules/bare-ansi-escapes": { - "version": "2.2.3", - "resolved": "https://registry.npmjs.org/bare-ansi-escapes/-/bare-ansi-escapes-2.2.3.tgz", - "integrity": "sha512-02ES4/E2RbrtZSnHJ9LntBhYkLA6lPpSEeP8iqS3MccBIVhVBlEmruF1I7HZqx5Q8aiTeYfQVeqmrU9YO2yYoQ==", - "license": "Apache-2.0", - "dependencies": { - "bare-stream": "^2.6.5" - }, - "peerDependencies": { - "bare-buffer": "*" - }, - "peerDependenciesMeta": { - "bare-buffer": { - "optional": true - } - } - }, - "node_modules/bare-assert": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/bare-assert/-/bare-assert-1.2.0.tgz", - "integrity": "sha512-c6uvgvTJBspTDxtVnPgrBKmLgcpW3Fp72NVKDLg6oT4QjQbhGtvrkHMhGYMK1sh4vjBHOBmuUalyt9hSzV37fQ==", - "license": "Apache-2.0", - "dependencies": { - "bare-inspect": "^3.1.2" - } - }, - "node_modules/bare-events": { - "version": "2.9.1", - "resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.9.1.tgz", - "integrity": "sha512-Z0oHEHAFDZkffN8Qc39zNZjQlMDkPJRyyyZieU1VH7u8c5S+qHZ2S8ixdKIAxEjfHO7FJxXmJWgteOghVanIsg==", - "license": "Apache-2.0", - "peerDependencies": { - "bare-abort-controller": "*" - }, - "peerDependenciesMeta": { - "bare-abort-controller": { - "optional": true - } - } - }, - "node_modules/bare-inspect": { - "version": "3.1.4", - "resolved": "https://registry.npmjs.org/bare-inspect/-/bare-inspect-3.1.4.tgz", - "integrity": "sha512-jfW5KRA84o3REpI6Vr4nbvMn+hqVAw8GU1mMdRwUsY5yJovQamxYeKGVKGqdzs+8ZbG4jRzGUXP/3Ji/DnqfPg==", - "license": "Apache-2.0", - "dependencies": { - "bare-ansi-escapes": "^2.1.0", - "bare-type": "^1.0.0" - }, - "engines": { - "bare": ">=1.18.0" - } - }, - "node_modules/bare-module-resolve": { - "version": "1.12.2", - "resolved": "https://registry.npmjs.org/bare-module-resolve/-/bare-module-resolve-1.12.2.tgz", - "integrity": "sha512-j+hiD5k99qec4KjJvYsI67q5AOBifmy9JG3oeMVxTmvrhn2sIdp8StrUvZu4YNgwTpO+NhniQG16N1ETDe1k5w==", - "license": "Apache-2.0", - "dependencies": { - "bare-semver": "^1.0.0" - }, - "peerDependencies": { - "bare-url": "*" - }, - "peerDependenciesMeta": { - "bare-url": { - "optional": true - } - } - }, - "node_modules/bare-semver": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/bare-semver/-/bare-semver-1.1.0.tgz", - "integrity": "sha512-1Hw5qJ7hXdVt3uPUqjeFTuxyvBUJauvz5A1I2jk8gzjZMHp04n//6nV9MDbG9CMw78JHY2lGV0w6s//LrASm2w==", - "license": "Apache-2.0" - }, - "node_modules/bare-stream": { - "version": "2.13.3", - "resolved": "https://registry.npmjs.org/bare-stream/-/bare-stream-2.13.3.tgz", - "integrity": "sha512-Kc+brLqvEqGkjyfiwJmImAOqLZL7OsoLKuavx+hJjgVV3nLTOjloJyPMFxjUPerGGHrNH0fLU06jjykMLWrERQ==", - "license": "Apache-2.0", - "dependencies": { - "b4a": "^1.8.1", - "streamx": "^2.25.0", - "teex": "^1.0.1" - }, - "peerDependencies": { - "bare-abort-controller": "*", - "bare-buffer": "*", - "bare-events": "*" - }, - "peerDependenciesMeta": { - "bare-abort-controller": { - "optional": true - }, - "bare-buffer": { - "optional": true - }, - "bare-events": { - "optional": true - } - } - }, - "node_modules/bare-type": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/bare-type/-/bare-type-1.1.0.tgz", - "integrity": "sha512-LdtnnEEYldOc87Dr4GpsKnStStZk3zfgoEMXy8yvEZkXrcCv9RtYDrUYWFsBQHtaB0s1EUWmcvS6XmEZYIj3Bw==", - "license": "Apache-2.0", - "engines": { - "bare": ">=1.2.0" - } - }, "node_modules/colorette": { "version": "2.0.20", "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz", @@ -585,15 +417,6 @@ "once": "^1.4.0" } }, - "node_modules/events-universal": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/events-universal/-/events-universal-1.0.1.tgz", - "integrity": "sha512-LUd5euvbMLpwOF8m6ivPCbhQeSiYVNb8Vs0fQ8QjXo0JTkEHpz8pxdQf0gStltaPpw0Cca8b39KxvK9cfKRiAw==", - "license": "Apache-2.0", - "dependencies": { - "bare-events": "^2.7.0" - } - }, "node_modules/fast-copy": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/fast-copy/-/fast-copy-4.0.3.tgz", @@ -613,12 +436,6 @@ "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", "license": "MIT" }, - "node_modules/fast-fifo": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.3.2.tgz", - "integrity": "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ==", - "license": "MIT" - }, "node_modules/fast-json-stringify": { "version": "6.4.0", "resolved": "https://registry.npmjs.org/fast-json-stringify/-/fast-json-stringify-6.4.0.tgz", @@ -1131,18 +948,6 @@ "node": ">= 12.13.0" } }, - "node_modules/require-addon": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/require-addon/-/require-addon-1.2.0.tgz", - "integrity": "sha512-VNPDZlYgIYQwWp9jMTzljx+k0ZtatKlcvOhktZ/anNPI3dQ9NXk7cq2U4iJ1wd9IrytRnYhyEocFWbkdPb+MYA==", - "license": "Apache-2.0", - "dependencies": { - "bare-addon-resolve": "^1.3.0" - }, - "engines": { - "bare": ">=1.10.0" - } - }, "node_modules/require-from-string": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", @@ -1263,20 +1068,6 @@ "node": ">=8" } }, - "node_modules/sodium-native": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/sodium-native/-/sodium-native-5.1.0.tgz", - "integrity": "sha512-3RxgyWyJlhTsABPnJVpCI5CoTDANZTqqFrEPqr+kjfnRaBihpVtMUE3yTF40ukdoB1APXeoBNKF3MzZAIHg39g==", - "license": "MIT", - "dependencies": { - "bare-assert": "^1.2.0", - "require-addon": "^1.1.0", - "which-runtime": "^1.2.1" - }, - "engines": { - "bare": ">=1.16.0" - } - }, "node_modules/sonic-boom": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-4.2.1.tgz", @@ -1295,17 +1086,6 @@ "node": ">= 10.x" } }, - "node_modules/streamx": { - "version": "2.28.0", - "resolved": "https://registry.npmjs.org/streamx/-/streamx-2.28.0.tgz", - "integrity": "sha512-1Yowhzjf0ivGMrTIkY9hav5TxobO9qIVqUE41fiCGMGgc3CLlf4MY+9AHmZqBWgDTue0fY9zWjYFVyf6Diuobw==", - "license": "MIT", - "dependencies": { - "events-universal": "^1.0.0", - "fast-fifo": "^1.3.2", - "text-decoder": "^1.1.0" - } - }, "node_modules/strip-json-comments": { "version": "5.0.3", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-5.0.3.tgz", @@ -1319,24 +1099,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/teex": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/teex/-/teex-1.0.1.tgz", - "integrity": "sha512-eYE6iEI62Ni1H8oIa7KlDU6uQBtqr4Eajni3wX7rpfXD8ysFx8z0+dri+KWEPWpBsxXfxu58x/0jvTVT1ekOSg==", - "license": "MIT", - "dependencies": { - "streamx": "^2.12.5" - } - }, - "node_modules/text-decoder": { - "version": "1.2.7", - "resolved": "https://registry.npmjs.org/text-decoder/-/text-decoder-1.2.7.tgz", - "integrity": "sha512-vlLytXkeP4xvEq2otHeJfSQIRyWxo/oZGEbXrtEEF9Hnmrdly59sUbzZ/QgyWuLYHctCHxFF4tRQZNQ9k60ExQ==", - "license": "Apache-2.0", - "dependencies": { - "b4a": "^1.6.4" - } - }, "node_modules/thread-stream": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-4.2.0.tgz", @@ -1379,12 +1141,6 @@ "node": ">= 8" } }, - "node_modules/which-runtime": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/which-runtime/-/which-runtime-1.4.0.tgz", - "integrity": "sha512-0ugbP4CJW4e2D20jvEcC4973dCgIaHI4Rw1PT+26U9zEve7FyYdWAIwUnoeOYvoCfn+wXHoHTKb1KhkYlb60Pw==", - "license": "Apache-2.0" - }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/backend/package.json b/backend/package.json index 6451994..1b7cebc 100644 --- a/backend/package.json +++ b/backend/package.json @@ -12,9 +12,9 @@ "node": ">=20" }, "dependencies": { + "@fastify/cookie": "^11.0.2", "@fastify/cors": "^11.2.0", "@fastify/rate-limit": "^11.1.0", - "@fastify/secure-session": "^8.3.0", "argon2": "^0.44.0", "dotenv": "^17.4.2", "fastify": "^5.9.0", diff --git a/backend/src/config.js b/backend/src/config.js index 58bed3d..c691d6c 100644 --- a/backend/src/config.js +++ b/backend/src/config.js @@ -46,9 +46,6 @@ export const config = { }, session: { secret: process.env.SESSION_SECRET, - // Salt de derivación de clave de @fastify/secure-session (16 bytes exactos). - // No es secreto: la seguridad depende de SESSION_SECRET. - salt: 'taboada-becarios', cookieName: 'taboada_session', ttlSeconds: 12 * 60 * 60, cookieSecure: process.env.COOKIE_SECURE !== 'false', diff --git a/backend/src/index.js b/backend/src/index.js index 18a7ab2..c285505 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -1,7 +1,7 @@ import Fastify from 'fastify'; +import cookie from '@fastify/cookie'; import cors from '@fastify/cors'; import rateLimit from '@fastify/rate-limit'; -import secureSession from '@fastify/secure-session'; import { config } from './config.js'; import { db } from './db/client.js'; import { runMigrations, seedSupervisora } from './db/migrate.js'; @@ -33,19 +33,8 @@ await fastify.register(cors, { credentials: true, }); -// Cookie de sesión de la supervisora: cifrada+firmada, httpOnly, secure (§6) -await fastify.register(secureSession, { - secret: config.session.secret, - salt: config.session.salt, - cookieName: config.session.cookieName, - cookie: { - path: '/', - httpOnly: true, - secure: config.session.cookieSecure, - sameSite: 'lax', - maxAge: config.session.ttlSeconds, - }, -}); +// Cookies firmadas con HMAC (sesión de supervisora, §6) +await fastify.register(cookie, { secret: config.session.secret }); // global: false → solo limita rutas que lo pidan en su config (login) await fastify.register(rateLimit, { global: false }); diff --git a/backend/src/routes/auth.js b/backend/src/routes/auth.js index e67da6e..26cb37c 100644 --- a/backend/src/routes/auth.js +++ b/backend/src/routes/auth.js @@ -1,4 +1,6 @@ +import { config } from '../config.js'; import { verifyPassword } from '../services/passwords.js'; +import { serializeSession, sessionCookieOptions } from '../services/sessions.js'; // Hash argon2id de un valor aleatorio descartado. Cuando el usuario no // existe se verifica contra esto para que la respuesta tarde lo mismo @@ -41,15 +43,19 @@ export default async function authRoutes(fastify) { code: 'CREDENCIALES_INVALIDAS', }); } - req.session.set('supervisora', { id: supervisora.id, usuario: supervisora.usuario }); + reply.setCookie( + config.session.cookieName, + serializeSession(supervisora), + sessionCookieOptions + ); req.log.info({ supervisoraId: supervisora.id }, 'Login de supervisora'); return { ok: true, usuario: supervisora.usuario }; } ); // ──────────────── POST /auth/logout ──────────────── - fastify.post('/auth/logout', async (req) => { - req.session.delete(); + fastify.post('/auth/logout', async (req, reply) => { + reply.clearCookie(config.session.cookieName, { path: '/' }); return { ok: true }; }); } diff --git a/backend/src/services/sessions.js b/backend/src/services/sessions.js new file mode 100644 index 0000000..1b6724b --- /dev/null +++ b/backend/src/services/sessions.js @@ -0,0 +1,36 @@ +import { config } from '../config.js'; + +// Sesión de supervisora como cookie firmada (§6: @fastify/cookie + firma). +// La firma HMAC la pone/verifica @fastify/cookie (signed: true) con +// SESSION_SECRET; aquí solo se serializa el payload y se valida expiración. +// Nota: secure-session quedó descartado porque sodium-native no publica +// prebuilds musl y no carga en Alpine. + +export const sessionCookieOptions = { + path: '/', + httpOnly: true, + secure: config.session.cookieSecure, + sameSite: 'lax', + maxAge: config.session.ttlSeconds, + signed: true, +}; + +export function serializeSession(supervisora) { + const payload = { + id: supervisora.id, + usuario: supervisora.usuario, + exp: Date.now() + config.session.ttlSeconds * 1000, + }; + return Buffer.from(JSON.stringify(payload)).toString('base64url'); +} + +// Devuelve el payload si la cookie (ya des-firmada) es válida y no expiró. +export function deserializeSession(value) { + try { + const payload = JSON.parse(Buffer.from(value, 'base64url').toString('utf8')); + if (!payload?.id || !payload?.exp || Date.now() > payload.exp) return null; + return payload; + } catch { + return null; + } +}