mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Hardens the persisted settings of the MusicBrainz and Open Library plugins against malformed values
This commit is contained in:
1 parent
dd6bf594f0
commit
24526fd9a2
2 files changed
+26
-3
No files matched your search
@@ -17,6 +17,10 @@ namespace Lumina.Plugins.MusicBrainz.Core.Settings;
|
||||
/// </summary>
|
||||
internal sealed class MusicBrainzSettingsProvider
|
||||
{
|
||||
// The minimum request interval is clamped to a sane range, so a persisted value can neither be a non finite value that cannot be turned into a
|
||||
// duration, nor be so large that waiting for it overflows the delay of the requests.
|
||||
private const double MAXIMUM_REQUEST_INTERVAL_SECONDS = 60;
|
||||
|
||||
private readonly IPluginSettingsStore? _settingsStore;
|
||||
private readonly Guid _pluginId;
|
||||
private readonly MusicBrainzSettingsDto _defaults;
|
||||
@@ -112,9 +116,12 @@ internal sealed class MusicBrainzSettingsProvider
|
||||
int.TryParse(releaseLookupLimit, NumberStyles.Integer, CultureInfo.InvariantCulture, out int releaseLookupLimitValue))
|
||||
settings.ReleaseLookupLimit = Math.Max(1, releaseLookupLimitValue);
|
||||
|
||||
// A persisted value that is not a finite number is rejected, and a finite one is clamped, so that neither a NaN, nor an infinity, nor an
|
||||
// out of range value can make the conversion to a duration or the waiting for it throw.
|
||||
if (storedSettings.TryGetValue(MusicBrainzSettingsKeys.MINIMUM_REQUEST_INTERVAL_SECONDS, out string? minimumRequestInterval) &&
|
||||
double.TryParse(minimumRequestInterval, NumberStyles.Float, CultureInfo.InvariantCulture, out double minimumRequestIntervalValue))
|
||||
settings.MinimumRequestInterval = TimeSpan.FromSeconds(Math.Max(0, minimumRequestIntervalValue));
|
||||
double.TryParse(minimumRequestInterval, NumberStyles.Float, CultureInfo.InvariantCulture, out double minimumRequestIntervalValue) &&
|
||||
double.IsFinite(minimumRequestIntervalValue))
|
||||
settings.MinimumRequestInterval = TimeSpan.FromSeconds(Math.Clamp(minimumRequestIntervalValue, 0, MAXIMUM_REQUEST_INTERVAL_SECONDS));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,7 +20,10 @@ internal static class OpenLibrarySettingsLoader
|
||||
public static void Apply(OpenLibrarySettingsDto settings, IReadOnlyDictionary<string, string> storedSettings)
|
||||
{
|
||||
if (storedSettings.TryGetValue(OpenLibrarySettingsKeys.CONTACT_EMAIL, out string? contactEmail))
|
||||
settings.ContactEmail = string.IsNullOrWhiteSpace(contactEmail) ? null : contactEmail.Trim();
|
||||
{
|
||||
string? trimmedContactEmail = string.IsNullOrWhiteSpace(contactEmail) ? null : contactEmail.Trim();
|
||||
settings.ContactEmail = trimmedContactEmail is not null && IsSafeContactEmail(trimmedContactEmail) ? trimmedContactEmail : null;
|
||||
}
|
||||
|
||||
if (storedSettings.TryGetValue(OpenLibrarySettingsKeys.SEARCH_RESULT_LIMIT, out string? searchResultLimit) &&
|
||||
int.TryParse(searchResultLimit, NumberStyles.Integer, CultureInfo.InvariantCulture, out int searchResultLimitValue))
|
||||
@@ -34,4 +37,17 @@ internal static class OpenLibrarySettingsLoader
|
||||
double.TryParse(minimumRequestInterval, NumberStyles.Float, CultureInfo.InvariantCulture, out double minimumRequestIntervalValue))
|
||||
settings.MinimumRequestInterval = TimeSpan.FromSeconds(Math.Max(0, minimumRequestIntervalValue));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the persisted contact email only contains characters that are safe inside the request from header.
|
||||
/// </summary>
|
||||
/// <param name="contactEmail">The persisted contact email.</param>
|
||||
/// <returns><see langword="true"/> when the email is safe, otherwise <see langword="false"/>.</returns>
|
||||
private static bool IsSafeContactEmail(string contactEmail)
|
||||
{
|
||||
foreach (char character in contactEmail)
|
||||
if (!char.IsAsciiLetterOrDigit(character) && character is not ('@' or '.' or '_' or '-' or '+'))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user