aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-12-09 08:04:24 -0600
committerGitHub <noreply@github.com>2021-12-09 08:04:24 -0600
commit24ce3f1b74a26b04dd96be62c07d25da0158df0b (patch)
tree216be0fbdfdab5728cfcd14101cb85de39a4c3f1 /src/slskd/Common
parentec60cfbd397badca9a76d3923c653ad7cb6561c5 (diff)
parent2262e842f42ccef7f557cee4f7e05c9adf15b739 (diff)
downloadslskd-24ce3f1b74a26b04dd96be62c07d25da0158df0b.tar.gz
Merge pull request #299 from slskd/secrets
Explicitly redact secrets stored in options before exporting them
Diffstat (limited to 'src/slskd/Common')
-rw-r--r--src/slskd/Common/Extensions.cs16
-rw-r--r--src/slskd/Common/Redactor.cs85
2 files changed, 101 insertions, 0 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs
index d678541d..0f3f8362 100644
--- a/src/slskd/Common/Extensions.cs
+++ b/src/slskd/Common/Extensions.cs
@@ -308,6 +308,21 @@ namespace slskd
}
/// <summary>
+ /// Redacts this instance of Options, replacing properties marked with <see cref="SecretAttribute"/> with '*****'.
+ /// </summary>
+ /// <remarks>
+ /// Creates a deep clone before redacting.
+ /// </remarks>
+ /// <param name="options">The Options instance to redact.</param>
+ /// <returns>A redacted instance.</returns>
+ public static Options Redact(this Options options)
+ {
+ var redacted = options.ToJson().FromJson<Options>();
+ Redactor.Redact(redacted, redactWith: "*****");
+ return redacted;
+ }
+
+ /// <summary>
/// Creates a copy of this instance with the specified parameters changed.
/// </summary>
/// <param name="o">The options instance to copy.</param>
@@ -350,6 +365,7 @@ namespace slskd
var options = new JsonSerializerOptions();
options.Converters.Add(new IPAddressConverter());
options.Converters.Add(new JsonStringEnumConverter());
+ options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
return options;
}
diff --git a/src/slskd/Common/Redactor.cs b/src/slskd/Common/Redactor.cs
new file mode 100644
index 00000000..b2069b12
--- /dev/null
+++ b/src/slskd/Common/Redactor.cs
@@ -0,0 +1,85 @@
+// <copyright file="Redactor.cs" company="slskd Team">
+// 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/.
+// </copyright>
+
+namespace slskd
+{
+ using System;
+ using System.Collections;
+ using System.Linq;
+ using System.Reflection;
+
+ /// <summary>
+ /// Redacts secrets within objects.
+ /// </summary>
+ public static class Redactor
+ {
+ /// <summary>
+ /// Recursively scans the <paramref name="target"/> for properties marked with <see cref="SecretAttribute"/> and redacts the values by overwriting them.
+ /// </summary>
+ /// <remarks>
+ /// Only works on properties of type <see cref="string"/>.
+ /// </remarks>
+ /// <param name="target">The object to redact.</param>
+ /// <param name="redactWith">The string with which to replace redacted values.</param>
+ public static void Redact(object target, string redactWith = "*****")
+ {
+ var type = target.GetType();
+
+ if (type.IsPrimitive || type.IsValueType || type == typeof(string) || type.IsAssignableTo(typeof(IEnumerable)))
+ {
+ return;
+ }
+
+ foreach (var prop in target.GetType().GetProperties())
+ {
+ var value = prop.GetValue(target);
+
+ if (value == null)
+ {
+ continue;
+ }
+
+ if (prop.GetCustomAttributes().Any(attr => attr.GetType() == typeof(SecretAttribute)) && prop.PropertyType == typeof(string))
+ {
+ prop.SetValue(target, redactWith);
+ }
+ else
+ {
+ if (value.GetType().IsAssignableTo(typeof(IEnumerable)))
+ {
+ foreach (var element in (IEnumerable)value)
+ {
+ Redact(element);
+ }
+ }
+ else
+ {
+ Redact(value);
+ }
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Indicates that a property or field contains a secret, allowing it to be redacted.
+ /// </summary>
+ [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
+ public class SecretAttribute : Attribute
+ {
+ }
+}