Add check for ProviderIds to prevent '=' from appearing in keys, also support '=' in the values. (#12274)

This commit is contained in:
Erwin de Haan 2024-07-15 14:44:22 +02:00 committed by GitHub
parent c666f9d050
commit 3262f8dc2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 23 deletions

View File

@ -1046,9 +1046,10 @@ namespace Emby.Server.Implementations.Data
foreach (var part in value.SpanSplit('|'))
{
var providerDelimiterIndex = part.IndexOf('=');
if (providerDelimiterIndex != -1 && providerDelimiterIndex == part.LastIndexOf('='))
// Don't let empty values through
if (providerDelimiterIndex != -1 && part.Length != providerDelimiterIndex + 1)
{
item.SetProviderId(part.Slice(0, providerDelimiterIndex).ToString(), part.Slice(providerDelimiterIndex + 1).ToString());
item.SetProviderId(part[..providerDelimiterIndex].ToString(), part[(providerDelimiterIndex + 1)..].ToString());
}
}
}

View File

@ -111,19 +111,21 @@ namespace MediaBrowser.Model.Entities
/// Sets a provider id.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="name">The name.</param>
/// <param name="name">The name, this should not contain a '=' character.</param>
/// <param name="value">The value.</param>
public static void SetProviderId(this IHasProviderIds instance, string name, string? value)
/// <remarks>Due to how deserialization from the database works the name can not contain '='.</remarks>
public static void SetProviderId(this IHasProviderIds instance, string name, string value)
{
ArgumentNullException.ThrowIfNull(instance);
ArgumentException.ThrowIfNullOrEmpty(name);
ArgumentException.ThrowIfNullOrEmpty(value);
// If it's null remove the key from the dictionary
if (string.IsNullOrEmpty(value))
// When name contains a '=' it can't be deserialized from the database
if (name.Contains('=', StringComparison.Ordinal))
{
instance.ProviderIds?.Remove(name);
throw new ArgumentException("Provider id name cannot contain '='", nameof(name));
}
else
{
// Ensure it exists
instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
@ -137,7 +139,6 @@ namespace MediaBrowser.Model.Entities
instance.ProviderIds[name] = value;
}
}
}
/// <summary>
/// Sets a provider id.
@ -149,5 +150,30 @@ namespace MediaBrowser.Model.Entities
{
instance.SetProviderId(provider.ToString(), value);
}
/// <summary>
/// Removes a provider id.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="name">The name.</param>
public static void RemoveProviderId(this IHasProviderIds instance, string name)
{
ArgumentNullException.ThrowIfNull(instance);
ArgumentException.ThrowIfNullOrEmpty(name);
instance.ProviderIds?.Remove(name);
}
/// <summary>
/// Removes a provider id.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="provider">The provider.</param>
public static void RemoveProviderId(this IHasProviderIds instance, MetadataProvider provider)
{
ArgumentNullException.ThrowIfNull(instance);
instance.ProviderIds?.Remove(provider.ToString());
}
}
}

View File

@ -141,7 +141,7 @@ namespace Jellyfin.Model.Tests.Entities
public void SetProviderId_Null_Remove()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.SetProviderId(MetadataProvider.Imdb, null!);
Assert.Throws<ArgumentNullException>(() => provider.SetProviderId(MetadataProvider.Imdb, null!));
Assert.Empty(provider.ProviderIds);
}
@ -150,8 +150,8 @@ namespace Jellyfin.Model.Tests.Entities
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
provider.SetProviderId(MetadataProvider.Imdb, string.Empty);
Assert.Empty(provider.ProviderIds);
Assert.Throws<ArgumentException>(() => provider.SetProviderId(MetadataProvider.Imdb, string.Empty));
Assert.Single(provider.ProviderIds);
}
[Fact]
@ -182,10 +182,20 @@ namespace Jellyfin.Model.Tests.Entities
ProviderIds = null!
};
nullProvider.SetProviderId(MetadataProvider.Imdb, string.Empty);
Assert.Throws<ArgumentException>(() => nullProvider.SetProviderId(MetadataProvider.Imdb, string.Empty));
Assert.Null(nullProvider.ProviderIds);
}
[Fact]
public void RemoveProviderId_Null_Remove()
{
var provider = new ProviderIdsExtensionsTestsObject();
provider.ProviderIds[MetadataProvider.Imdb.ToString()] = ExampleImdbId;
provider.RemoveProviderId(MetadataProvider.Imdb);
Assert.Empty(provider.ProviderIds);
}
private sealed class ProviderIdsExtensionsTestsObject : IHasProviderIds
{
public static readonly ProviderIdsExtensionsTestsObject Empty = new ProviderIdsExtensionsTestsObject();