From 2b66cb27629726480c00ffae4775810a35ee4fcf Mon Sep 17 00:00:00 2001 From: whoswhip Date: Sat, 7 Mar 2026 10:44:26 -0500 Subject: [PATCH] fix(email): validate settings before sending; use hmac for tokens --- SharpbinV3.Server/Services/AuthService.cs | 16 ++++++++++++---- SharpbinV3.Server/Services/EmailService.cs | 5 +++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/SharpbinV3.Server/Services/AuthService.cs b/SharpbinV3.Server/Services/AuthService.cs index 7ddff65..aa9c825 100644 --- a/SharpbinV3.Server/Services/AuthService.cs +++ b/SharpbinV3.Server/Services/AuthService.cs @@ -1,5 +1,4 @@ -using System.Data; -using System.IdentityModel.Tokens.Jwt; +using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using Microsoft.EntityFrameworkCore; @@ -30,6 +29,7 @@ namespace SharpbinV3.Server.Services IOptions jwtOptions, IOptions authOptions, IOptions appSettings, + IOptions emailSettings, ILogger logger ) { @@ -38,6 +38,7 @@ namespace SharpbinV3.Server.Services private readonly JWTSettings _jwtSettings = jwtOptions.Value; private readonly AuthSettings _authSettings = authOptions.Value; private readonly AppSettings _appSettings = appSettings.Value; + private readonly EmailSettings _emailSettings = emailSettings.Value; private readonly ILogger _logger = logger; private const int EmailVerificationTokenLength = 64; @@ -225,9 +226,14 @@ namespace SharpbinV3.Server.Services { if (string.IsNullOrWhiteSpace(user.Email)) return; + if (string.IsNullOrWhiteSpace(_emailSettings.Verification_HMAC_Secret)) + { + _logger.LogWarning($"Email verification is not configured. Cannot send verification email to {user.Email}"); + return; + } string token = Utilities.GenerateSecureRandomString(EmailVerificationTokenLength); - string tokenHash = Utilities.ComputeSha256(token); + string tokenHash = Utilities.ComputeHmacSha256(token, _emailSettings.Verification_HMAC_Secret); await _email.SendAsync( user.Email, @@ -250,8 +256,10 @@ namespace SharpbinV3.Server.Services { if (token.Length != EmailVerificationTokenLength) return (false, "Invalid token."); + if (string.IsNullOrWhiteSpace(_emailSettings.Verification_HMAC_Secret)) + return (false, "Email verification is not configured."); - string tokenHash = Utilities.ComputeSha256(token); + string tokenHash = Utilities.ComputeHmacSha256(token, _emailSettings.Verification_HMAC_Secret); var verificationToken = await _db.EmailVerificationTokens.Include(t => t.User).FirstOrDefaultAsync(t => t.TokenHash == tokenHash); if ( diff --git a/SharpbinV3.Server/Services/EmailService.cs b/SharpbinV3.Server/Services/EmailService.cs index c7befdd..6085194 100644 --- a/SharpbinV3.Server/Services/EmailService.cs +++ b/SharpbinV3.Server/Services/EmailService.cs @@ -16,6 +16,11 @@ namespace SharpbinV3.Server.Services public async Task SendAsync(string to, string subject, string body) { + if (string.IsNullOrWhiteSpace(settings.Host) || string.IsNullOrWhiteSpace(settings.User) || string.IsNullOrWhiteSpace(settings.Password)) + { + logger.LogWarning($"Email settings are not fully configured. Skipping sending email to {to}"); + return; + } var email = new QueuedEmail(to, subject, body); if (queue.Writer.TryWrite(email))