teremy
12/29/2020, 2:06 PMYev Kanivets
12/29/2020, 2:33 PMUUID.randomUUID()
?Mark Leverentz
12/29/2020, 11:07 PMUUID.randomUUID()
is good about giving unique values. However, it gives easy-to-guess values, which makes it less than ideal for something like a session-cookie, because I could look at my session-cookie, and then write a simple test that guesses other "nearby" values and tries them until i can find some other poor unsuspecting user's session-cookie.
Something like the following will give hard-to-guess values, but lack a uniqueness guarantee.
val secureRandom = java.security.SecureRandom()
...
secureRandom.nextInt()
I think you could combine both approaches to get something that would give you a value that is both unique and hard to guess:
// during app startup
val secureRandom = java.security.SecureRandom()
...
//when creating an app session
val sessionIdentifier = UUID.randomUUID() + secureRandom.nextInt()
However, much has been written about all the ways that we can get session-management wrong, so my answer is probably pretty flawed too. If you're using Ktor, you might consider using their session management (I haven't used it, but I'd guess that it's implemented more carefully than my nonsense above):
https://ktor.io/docs/sessions.html
If you need to roll your own session management, be sure to review the OWASP guide to avoiding common pitfalls:
https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.htmlRobert MacLean
12/30/2020, 1:44 PMowever, it gives easy-to-guess valuesIs that not version specific and also hard to attack (like you need a few to start to find it which hopefully other security aspects protects you from). you could also use a salted hash over the UUID with the salt per user to help a lot. the risk of collisions should be acceptable for most
Mark Leverentz
12/30/2020, 8:36 PMrandomUUID()
is a type-4 UUID (although in hindsight it probably should've been obvious). The javadocs say the following about randomUUID()
:
TheI think the biggest flaw with it, then, is just that it's only 122 bits of randomness, which is still kind of easy to search a large space of that. https://neilmadden.blog/2018/08/30/moving-away-from-uuids/ The above article recommends using something like the following (translated into Kotlin):is generated using a cryptographically strong pseudo random number generator.UUID
class SessionIdGenerator {
val random = java.security.SecureRandom()
val encoder = java.util.Base64.getUrlEncoder().withoutPadding()
fun newId() : String {
val data = ByteArray(20) // 20 bytes = 160 bits of randomness
random.nextBytes(data)
return encoder.encodeToString(data)
}
}
Robert MacLean
12/31/2020, 9:33 AMteremy
01/01/2021, 7:15 PM