Fixing security hotspots + issues

This commit is contained in:
2025-06-17 19:47:56 +02:00
parent ad1909ef57
commit 11a9696bdd
7 changed files with 13 additions and 12 deletions

View File

@@ -35,7 +35,7 @@
"ExpiredAfterMinsOfInactivity": 15 "ExpiredAfterMinsOfInactivity": 15
}, },
"EncryptionSettings": { "EncryptionSettings": {
"Secret": "S7VIidfXQf1tOQYX", "SaltKey": "S7VIidfXQf1tOQYX",
"Salt": "", "Salt": "",
"Iterations": 10 "Iterations": 10
} }

View File

@@ -35,7 +35,7 @@
"ExpiredAfterMinsOfInactivity": 15 "ExpiredAfterMinsOfInactivity": 15
}, },
"EncryptionSettings": { "EncryptionSettings": {
"Secret": "AAAAA", "SaltKey": "AAAAA",
"Salt": "", "Salt": "",
"Iterations": 10 "Iterations": 10
} }

View File

@@ -3,7 +3,7 @@ namespace BasicDotnetTemplate.MainProject.Models.Settings;
public class EncryptionSettings public class EncryptionSettings
{ {
#nullable enable #nullable enable
public string? Secret { get; set; } public string? SaltKey { get; set; }
public string? Salt { get; set; } public string? Salt { get; set; }
public int? Iterations { get; set; } public int? Iterations { get; set; }
#nullable disable #nullable disable

View File

@@ -22,15 +22,12 @@ public interface IUserService
public class UserService : BaseService, IUserService public class UserService : BaseService, IUserService
{ {
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
private readonly CryptUtils _cryptUtils;
public UserService( public UserService(
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
IConfiguration configuration, IConfiguration configuration,
SqlServerContext sqlServerContext SqlServerContext sqlServerContext
) : base(httpContextAccessor, configuration, sqlServerContext) ) : base(httpContextAccessor, configuration, sqlServerContext)
{ { }
this._cryptUtils = new(_appSettings);
}
private IQueryable<User> GetUsersQueryable() private IQueryable<User> GetUsersQueryable()
{ {

View File

@@ -6,7 +6,7 @@ using BasicDotnetTemplate.MainProject.Models.Settings;
namespace BasicDotnetTemplate.MainProject.Utils; namespace BasicDotnetTemplate.MainProject.Utils;
public class CryptUtils(AppSettings appSettings) public class CryptUtils(AppSettings appSettings)
{ {
private readonly string _secret = appSettings.EncryptionSettings?.Secret ?? String.Empty; private readonly string _saltKey = appSettings.EncryptionSettings?.SaltKey ?? String.Empty;
private const int _M = 16; private const int _M = 16;
private const int _N = 32; private const int _N = 32;
@@ -14,7 +14,7 @@ public class CryptUtils(AppSettings appSettings)
{ {
var decrypted = String.Empty; var decrypted = String.Empty;
if (String.IsNullOrEmpty(this._secret) || this._secret.Length < _M) if (String.IsNullOrEmpty(this._saltKey) || this._saltKey.Length < _M)
{ {
throw new ArgumentException("Unable to proceed with decryption due to invalid settings"); throw new ArgumentException("Unable to proceed with decryption due to invalid settings");
} }
@@ -28,7 +28,7 @@ public class CryptUtils(AppSettings appSettings)
using (var aes = Aes.Create()) using (var aes = Aes.Create())
{ {
aes.Key = Encoding.UTF8.GetBytes(this._secret); aes.Key = Encoding.UTF8.GetBytes(this._saltKey);
aes.IV = Encoding.UTF8.GetBytes(iv); aes.IV = Encoding.UTF8.GetBytes(iv);
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV)) using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV))

View File

@@ -29,7 +29,11 @@ public partial class PasswordUtils
[GeneratedRegex("[^a-zA-Z0-9]")] [GeneratedRegex("[^a-zA-Z0-9]")]
private static partial Regex RegexSpecial(); private static partial Regex RegexSpecial();
private static readonly Regex RegexIdenticalChars = new(@"(\S)\1{2,}", RegexOptions.IgnoreCase | RegexOptions.Compiled); private static readonly Regex RegexIdenticalChars = new(
@"(\S)\1{2,}",
RegexOptions.IgnoreCase | RegexOptions.Compiled,
TimeSpan.FromMilliseconds(100)
);
public static List<string> ValidatePassword(string password) public static List<string> ValidatePassword(string password)
{ {

View File

@@ -35,7 +35,7 @@
"ExpiredAfterMinsOfInactivity": 15 "ExpiredAfterMinsOfInactivity": 15
}, },
"EncryptionSettings": { "EncryptionSettings": {
"Secret": "S7VIidfXQf1tOQYX", "SaltKey": "S7VIidfXQf1tOQYX",
"Salt": "", "Salt": "",
"Iterations": 10 "Iterations": 10
}, },