|
| 1 | +import secureSession from "@fastify/secure-session"; |
| 2 | +import fp from "fastify-plugin"; |
| 3 | +import fastifyCookie from "@fastify/cookie"; |
| 4 | +import fastifySession from '@fastify/session'; |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +export const COOKIE_NAME_ENCRYPTION_KEY = "session_encrpytion_key"; |
| 9 | +export const COOKIE_NAME_SESSION = "session-cookie"; |
| 10 | + |
| 11 | +export const SECURE_SESSION_NAME = "encryptedSessionInternal"; |
| 12 | +export const UNDERLYING_SESSION_NAME = "underlyingSessionNotPerUserEncrypted"; |
| 13 | + |
| 14 | +// This is the key used to store the encryption key in the secure session cookie |
| 15 | +export const SECURE_COOKIE_KEY_ENCRYPTION_KEY = "encryptionKey"; |
| 16 | + |
| 17 | +export const REQUEST_DECORATOR = "encryptedSession"; |
| 18 | + |
| 19 | +async function encryptedSession(fastify) { |
| 20 | + const { COOKIE_SECRET, NODE_ENV } = fastify.config; |
| 21 | + |
| 22 | + await fastify.register(fastifyCookie); |
| 23 | + |
| 24 | + fastify.register(secureSession, { |
| 25 | + secret: Buffer.from(COOKIE_SECRET, "hex"), |
| 26 | + cookieName: COOKIE_NAME_ENCRYPTION_KEY, |
| 27 | + sessionName: SECURE_SESSION_NAME, |
| 28 | + cookie: { |
| 29 | + path: "/", |
| 30 | + httpOnly: true, |
| 31 | + sameSite: "lax", |
| 32 | + secure: NODE_ENV === "production", |
| 33 | + maxAge: 60 * 60 * 24 * 7, // 7 days |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + |
| 38 | + fastify.register(fastifySession, { |
| 39 | + secret: "test-secret-32-char-or-longerasdasdasdasdasdasdasdasdasdasd", |
| 40 | + cookieName: COOKIE_NAME_SESSION, |
| 41 | + // sessionName: UNDERLYING_SESSION_NAME, //NOT POSSIBLE to change the name it is decorated on the request object |
| 42 | + cookie: { |
| 43 | + path: "/", |
| 44 | + httpOnly: true, |
| 45 | + sameSite: "lax", |
| 46 | + secure: NODE_ENV === "production", |
| 47 | + maxAge: 60 * 60 * 24 * 7, // 7 days |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + fastify.addHook('onRequest', (request, _reply, next) => { |
| 52 | + //we use secure-session cookie to get the encryption key and decrypt the store |
| 53 | + if (request[SECURE_SESSION_NAME].get(SECURE_COOKIE_KEY_ENCRYPTION_KEY) === undefined) { |
| 54 | + console.log("encryption key not found, creating new one"); |
| 55 | + |
| 56 | + //TODO: create a new encrpytion key and set it in the secure session cookie |
| 57 | + request[SECURE_SESSION_NAME].set(SECURE_COOKIE_KEY_ENCRYPTION_KEY, "TODO_SHOULD_BE_RANDOM"); |
| 58 | + request[REQUEST_DECORATOR] = new Session() |
| 59 | + } else { |
| 60 | + console.log("encryption key found, using existing one"); |
| 61 | + const encryptedStore = request.session.get("encryptedStore"); |
| 62 | + if (encryptedStore) { |
| 63 | + try { |
| 64 | + //TODO: add decrypted step |
| 65 | + const decryptedStore = JSON.parse(encryptedStore); |
| 66 | + request[REQUEST_DECORATOR] = new Session(decryptedStore); |
| 67 | + } catch (error) { |
| 68 | + console.error("Failed to parse encrypted store:", error); |
| 69 | + request[REQUEST_DECORATOR] = new Session(); |
| 70 | + } |
| 71 | + } else { |
| 72 | + // we could not parse the encrypted store, so we create a new one and it would overwrite the previously stored store. |
| 73 | + console.log("No encrypted store found, creating new session"); |
| 74 | + request[REQUEST_DECORATOR] = new Session(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + next() |
| 79 | + }) |
| 80 | + |
| 81 | + //TODO maybe move to onResponse after res is send. Lifecycle Doc https://fastify.dev/docs/latest/Reference/Lifecycle/ |
| 82 | + // onSend is called before the response is send. Here we take encrypt the Session object and store it in the fastify-session. |
| 83 | + // Then we also want to make sure the unencrypted object is removed from memory |
| 84 | + fastify.addHook('onSend', (request, reply, _payload, next) => { |
| 85 | + console.log("onSend hook called", request[REQUEST_DECORATOR].data()); |
| 86 | + |
| 87 | + //on send we will encrypt the store and set it in the backend-side session store |
| 88 | + console.log("Encrypted store that will be set in session:", JSON.stringify(request[REQUEST_DECORATOR].data())); |
| 89 | + |
| 90 | + //TODO: encrypt the data here. |
| 91 | + //we store everything in one value in the session, that might be problematic for future redis with expiration times per key. we might want to split this |
| 92 | + const encryptedData = JSON.stringify(request[REQUEST_DECORATOR].data()) |
| 93 | + |
| 94 | + //remove unencrypted data from memory |
| 95 | + delete request[REQUEST_DECORATOR]; |
| 96 | + request[REQUEST_DECORATOR] = null; |
| 97 | + |
| 98 | + request.session.encryptedStore = encryptedData; |
| 99 | + next() |
| 100 | + }) |
| 101 | + |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +export default fp(encryptedSession); |
| 106 | + |
| 107 | +// maybe use a closure to encapsulate the session data so noone can reference it and we are the only ones keeping a reference |
| 108 | +function createEncryptedSession(previousValue) { |
| 109 | + let encryptedStore = {}; // Private variable |
| 110 | + if (previousValue) { |
| 111 | + encryptedStore = previousValue; |
| 112 | + } |
| 113 | + return { |
| 114 | + set(key, value) { |
| 115 | + encryptedStore[key] = value; |
| 116 | + }, |
| 117 | + get(key) { |
| 118 | + return encryptedStore[key]; |
| 119 | + }, |
| 120 | + delete(key) { |
| 121 | + delete encryptedStore[key]; |
| 122 | + }, |
| 123 | + clear() { |
| 124 | + encryptedStore = {}; // Clear all data |
| 125 | + }, |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +class Session { |
| 130 | + #data; |
| 131 | + |
| 132 | + constructor(obj) { |
| 133 | + this.#data = obj || {} |
| 134 | + } |
| 135 | + |
| 136 | + get(key) { |
| 137 | + return this.#data[key] |
| 138 | + } |
| 139 | + |
| 140 | + set(key, value) { |
| 141 | + this.#data[key] = value |
| 142 | + } |
| 143 | + |
| 144 | + delete(key) { |
| 145 | + this.#data[key] = undefined |
| 146 | + } |
| 147 | + |
| 148 | + data() { |
| 149 | + const copy = { |
| 150 | + ...this.#data |
| 151 | + } |
| 152 | + |
| 153 | + return copy |
| 154 | + } |
| 155 | +} |
0 commit comments