From 012e433ef67ea9eb35a112cfeb5523e2ffc289e8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Oct 2022 16:57:22 -0500 Subject: add ShareValidationException --- .../Common/Exceptions/ShareValidationException.cs | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/slskd/Common/Exceptions/ShareValidationException.cs (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Exceptions/ShareValidationException.cs b/src/slskd/Common/Exceptions/ShareValidationException.cs new file mode 100644 index 00000000..97a3e126 --- /dev/null +++ b/src/slskd/Common/Exceptions/ShareValidationException.cs @@ -0,0 +1,72 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System; + using System.Diagnostics.CodeAnalysis; + using System.Runtime.Serialization; + + /// + /// Represents an error that occurs when multiple share scans are attempted concurrently. + /// + [ExcludeFromCodeCoverage] + [Serializable] + public class ShareValidationException : SlskdException + { + /// + /// Initializes a new instance of the class. + /// + public ShareValidationException() + : base() + { + } + + /// + /// Initializes a new instance of the class with a specified error message. + /// + /// The message that describes the error. + public ShareValidationException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a + /// reference to the inner exception that is the cause of this exception. + /// + /// The message that describes the error. + /// + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no + /// inner exception is specified. + /// + public ShareValidationException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class with serialized data. + /// + /// The SerializationInfo that holds the serialized object data about the exception being thrown. + /// The StreamingContext that contains contextual information about the source or destination. + protected ShareValidationException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} \ No newline at end of file -- cgit v1.2.3 From 845937dede800928f380285dad47a4c48f0ea731 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Oct 2022 09:11:02 -0500 Subject: redact dictionaries --- src/slskd/Common/Redactor.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Redactor.cs b/src/slskd/Common/Redactor.cs index b2069b12..e819a6a8 100644 --- a/src/slskd/Common/Redactor.cs +++ b/src/slskd/Common/Redactor.cs @@ -59,7 +59,14 @@ namespace slskd } else { - if (value.GetType().IsAssignableTo(typeof(IEnumerable))) + if (value.IsDictionary()) + { + foreach (string key in ((IDictionary)value).Keys) + { + Redact(((IDictionary)value)[key]); + } + } + else if (value.GetType().IsAssignableTo(typeof(IEnumerable))) { foreach (var element in (IEnumerable)value) { -- cgit v1.2.3 From 192db033cb76f129e9c71a1d82e0e71a291ad001 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Oct 2022 09:11:39 -0500 Subject: allow PBKDF2 keys to have variable length --- src/slskd/Common/Cryptography/Pbkdf2.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Cryptography/Pbkdf2.cs b/src/slskd/Common/Cryptography/Pbkdf2.cs index 3c3246d6..eea5c966 100644 --- a/src/slskd/Common/Cryptography/Pbkdf2.cs +++ b/src/slskd/Common/Cryptography/Pbkdf2.cs @@ -29,15 +29,16 @@ namespace slskd.Cryptography /// Gets a 256 bit (32 byte) key derived from the specified using PBKDF2/RFC 2898. /// /// The password from which to derive the key. + /// The desired length of the key, in bytes. /// The derived key. - public static byte[] GetKey(string password) + public static byte[] GetKey(string password, int length = 32) { byte[] salt = new byte[16]; int iterations = 1000; using var rng = RandomNumberGenerator.Create(); rng.GetBytes(salt); - return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32); + return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, length); } } } -- cgit v1.2.3 From 923627328c8e40a97f0d66be7c7d5dbaba949e6e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Oct 2022 09:36:37 -0500 Subject: add UrlAttribute --- src/slskd/Common/Validation/UrlAttribute.cs | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/slskd/Common/Validation/UrlAttribute.cs (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Validation/UrlAttribute.cs b/src/slskd/Common/Validation/UrlAttribute.cs new file mode 100644 index 00000000..9c980c9b --- /dev/null +++ b/src/slskd/Common/Validation/UrlAttribute.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Common.Validation +{ + using System; + using System.ComponentModel.DataAnnotations; + + /// + /// Validates that the value is a valid URL. + /// + public class UrlAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (!Uri.TryCreate((string)value, default(UriCreationOptions), out _)) + { + return new ValidationResult($"The {validationContext.DisplayName} field must contain a valid URL"); + } + + return ValidationResult.Success; + } + } +} -- cgit v1.2.3 From 2fd9253cedb5cbb871783f30921232e500aa6459 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Oct 2022 17:48:30 -0500 Subject: add variable length key gen --- src/slskd/Common/Cryptography/Pbkdf2.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/slskd/Common') diff --git a/src/slskd/Common/Cryptography/Pbkdf2.cs b/src/slskd/Common/Cryptography/Pbkdf2.cs index eea5c966..15b4a7ed 100644 --- a/src/slskd/Common/Cryptography/Pbkdf2.cs +++ b/src/slskd/Common/Cryptography/Pbkdf2.cs @@ -18,6 +18,7 @@ namespace slskd.Cryptography { using System.Security.Cryptography; + using System.Text; using Microsoft.AspNetCore.Cryptography.KeyDerivation; /// @@ -29,16 +30,29 @@ namespace slskd.Cryptography /// Gets a 256 bit (32 byte) key derived from the specified using PBKDF2/RFC 2898. /// /// The password from which to derive the key. - /// The desired length of the key, in bytes. /// The derived key. - public static byte[] GetKey(string password, int length = 32) + public static byte[] GetKey(string password) { byte[] salt = new byte[16]; int iterations = 1000; using var rng = RandomNumberGenerator.Create(); rng.GetBytes(salt); - return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, length); + return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32); + } + + /// + /// Gets a variable key derived from the specified and using PBKDF2/RFC 2898. + /// + /// The password from which to derive the key. + /// The value with which to salt the key. + /// The desired length of the key, in bytes. + /// The derived key. + public static byte[] GetKey(string password, string salt, int length) + { + int iterations = 1000; + + return KeyDerivation.Pbkdf2(password, Encoding.UTF8.GetBytes(salt), KeyDerivationPrf.HMACSHA256, iterations, length); } } } -- cgit v1.2.3