Skip to content

Commit e8a4447

Browse files
explicitly save session to avoid asynchronous saving and therefore inconsistent session date
1 parent c1f1982 commit e8a4447

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

server/encrypted-session.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async function encryptedSession(fastify) {
8888
//TODO maybe move to onResponse after res is send. Lifecycle Doc https://fastify.dev/docs/latest/Reference/Lifecycle/
8989
// onSend is called before the response is send. Here we take encrypt the Session object and store it in the fastify-session.
9090
// Then we also want to make sure the unencrypted object is removed from memory
91-
fastify.addHook('onSend', (request, reply, _payload, next) => {
91+
fastify.addHook('onSend', async (request, reply, _payload) => {
9292
const encryptionKey = Buffer.from(request[SECURE_SESSION_NAME].get(SECURE_COOKIE_KEY_ENCRYPTION_KEY), "base64");
9393
if (!encryptionKey) {
9494
// if no encryption key is found in the secure session, we cannot encrypt the store. This should not happen since an encrption key is generated when the request arrived
@@ -109,11 +109,9 @@ async function encryptedSession(fastify) {
109109
iv,
110110
tag,
111111
});
112+
await request.session.save()
112113
request.log.info("store encrypted and set into request.session.encryptedStore");
113-
next()
114114
})
115-
116-
117115
}
118116

119117
export default fp(encryptedSession);

0 commit comments

Comments
 (0)