Is there a good way to rotate a session ID for a S...
# ktor
t
Is there a good way to rotate a session ID for a Session in the ktor Sessions plugin? According to the OWASP Guidelines (cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#…) It is recommended to rotate the session ID after privilege level changes (for example, unauthed => authed), in case an attacker has previously captured the session cookie. The only way with Ktor Sessions I have found this to work is by using
Copy code
val SESSION_PLUGIN_SESSION_ATTRIBUTE_KEY = AttributeKey<String>("SessionId")

val session = call.sessions.get<MySession>()
call.sessions.clear<MySession>()
call.attributes.remove(SESSION_PLUGIN_SESSION_ATTRIBUTE_KEY)
call.sessions.set(session)
Which is quite hacky and I have not even checked so far for side effects. Is there a better way for this? Previously reported here: youtrack.jetbrains.com/issue/KTOR-675/Regenerate-session-id?_gl=…. For reference: the attribute key is taken from here: github.com/ktorio/ktor/blob/…/SessionTrackerById.kt#…
a
I don't see any better options because the session ID manipulation is hidden from the consumer.