← Java / Spring · Contents

JAVA-051 / Advanced

Plaintext password-reset tokens with no expiry

A07:2021 Identification and Authentication Failures · CWE-640 Weak Password Recovery

Why is this a problem?

A DB leak enables immediate account takeover, and tokens without expiry can be reused for a long time.

How to fix it

java
String raw = randomToken(32);
resetRepo.save(new ResetToken(email, sha256(raw), Instant.now().plus(Duration.ofMinutes(15))));
// Include only the raw token in email; delete immediately after use

Further reading