Skip to content

Commit fc3f033

Browse files
authored
Prevent password reset spam (#973)
* Remove password reset tokens after 1 day and only allow 1 at a time * Update expiration hint in email to reflect actual time
1 parent 1365c6f commit fc3f033

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

ProjectLighthouse/Database/DatabaseContext.WebTokens.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public async Task RemoveExpiredTokens()
104104
await this.WebTokens.RemoveWhere(t => DateTime.UtcNow > t.ExpiresAt);
105105
await this.EmailVerificationTokens.RemoveWhere(t => DateTime.UtcNow > t.ExpiresAt);
106106
await this.EmailSetTokens.RemoveWhere(t => DateTime.UtcNow > t.ExpiresAt);
107+
await this.PasswordResetTokens.RemoveWhere(t => DateTime.UtcNow > t.Created.AddDays(1));
107108
}
108109

109110
public async Task RemoveRegistrationToken(string? tokenString)

ProjectLighthouse/Helpers/EmailHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static async Task SendPasswordResetEmail(DatabaseContext database, IMailS
4646
{
4747
if (!CanSendMail(user)) return;
4848

49+
if (await database.PasswordResetTokens.CountAsync(t => t.UserId == user.UserId) > 0) return;
50+
4951
PasswordResetTokenEntity token = new()
5052
{
5153
Created = DateTime.UtcNow,
@@ -59,7 +61,8 @@ public static async Task SendPasswordResetEmail(DatabaseContext database, IMailS
5961
string messageBody = $"Hello, {user.Username}.\n\n" +
6062
"A request to reset your account's password was issued. If this wasn't you, this can probably be ignored.\n\n" +
6163
$"If this was you, your {ServerConfiguration.Instance.Customization.ServerName} password can be reset at the following link:\n" +
62-
$"{ServerConfiguration.Instance.ExternalUrl}/passwordReset?token={token.ResetToken}";
64+
$"{ServerConfiguration.Instance.ExternalUrl}/passwordReset?token={token.ResetToken}\n\n" +
65+
"This link will expire in 24 hours";
6366

6467
await mail.SendEmailAsync(user.EmailAddress, $"Project Lighthouse Password Reset Request for {user.Username}", messageBody);
6568

0 commit comments

Comments
 (0)