Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Make mandatory session properties immutable #54

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
15 changes: 14 additions & 1 deletion src/runtime/server/middleware/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ const getSession = async (event: H3Event): Promise<null | Session> => {
return session
}

const getImmutableSession = (session: Session) => {
const immutableSession = { ...session }
const properties = ['id', 'createdAt', 'ip']

properties.forEach((property) => {
Object.defineProperty(immutableSession, property, {
writable: false
})
})

return immutableSession as Session
}

const updateSessionExpirationDate = (session: Session, event: H3Event) => {
const now = new Date()
safeSetCookie(event, SESSION_COOKIE_NAME, session.id, now)
Expand All @@ -142,7 +155,7 @@ const ensureSession = async (event: H3Event) => {
}

event.context.sessionId = session.id
event.context.session = session
event.context.session = getImmutableSession(session)
return session
}

Expand Down