diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-12-17 16:44:39 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-17 16:44:39 -0600 |
| commit | 4e232315e3186608ff3678d4ee7c44b05ef8b09d (patch) | |
| tree | 29e7e27d4daf4a445f3b7367386e43c4035f45b8 /src | |
| parent | 4f4385475b100c7e86d2281ae685741c21abebc2 (diff) | |
| parent | 6916575a936c47a936e547ba6a9ee486703e16aa (diff) | |
| download | slskd-4e232315e3186608ff3678d4ee7c44b05ef8b09d.tar.gz | |
Merge pull request #1021 from slskd/jwt
FIX: Ensure JWT signing key is at least 32 characters (256 bits)
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Options.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 11 |
2 files changed, 11 insertions, 2 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 1eb92449..077ecf74 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1653,7 +1653,7 @@ namespace slskd [StringLength(255, MinimumLength = 16)] [Secret] [RequiresRestart] - public string Key { get; init; } = Cryptography.Random.GetBytes(16).ToBase62(); + public string Key { get; init; } = Cryptography.Random.GetBytes(32).ToBase62(); /// <summary> /// Gets the TTL for JWTs, in milliseconds. diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index ca991e3c..b17ba6aa 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -627,7 +627,16 @@ namespace slskd services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(DataDirectory, "misc", ".DataProtection-Keys"))); - var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key)); + var adjustedJwtKey = OptionsAtStartup.Web.Authentication.Jwt.Key; + + // the JWT key must be at least 256 bits in length, due to the HMACSHA256 algorithm being used for signing. + if (adjustedJwtKey.Length < 32) + { + adjustedJwtKey = adjustedJwtKey.PadRight(32, ' '); + Log.Warning("The configured JWT signing key is less than the required 32 characters and has been padded with spaces to make up the difference. The minimum length will be raised from 16 to 32 in a future release, and this will become an error."); + } + + var jwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(adjustedJwtKey)); services.AddSingleton(jwtSigningKey); services.AddSingleton<ISecurityService, SecurityService>(); |
