Reconcile pre-packaged meta.json against manifest on install

This commit is contained in:
AmbulantRex 2023-04-09 10:53:09 -06:00
parent 3a731051ad
commit 7dd4201971
9 changed files with 341 additions and 62 deletions

View File

@ -32,6 +32,8 @@ namespace Emby.Server.Implementations.Plugins
/// </summary>
public class PluginManager : IPluginManager
{
private const string MetafileName = "meta.json";
private readonly string _pluginsPath;
private readonly Version _appVersion;
private readonly List<AssemblyLoadContext> _assemblyLoadContexts;
@ -374,7 +376,7 @@ namespace Emby.Server.Implementations.Plugins
try
{
var data = JsonSerializer.Serialize(manifest, _jsonOptions);
File.WriteAllText(Path.Combine(path, "meta.json"), data);
File.WriteAllText(Path.Combine(path, MetafileName), data);
return true;
}
catch (ArgumentException e)
@ -385,7 +387,7 @@ namespace Emby.Server.Implementations.Plugins
}
/// <inheritdoc/>
public async Task<bool> GenerateManifest(PackageInfo packageInfo, Version version, string path, PluginStatus status)
public async Task<bool> PopulateManifest(PackageInfo packageInfo, Version version, string path, PluginStatus status)
{
var versionInfo = packageInfo.Versions.First(v => v.Version == version.ToString());
var imagePath = string.Empty;
@ -427,13 +429,75 @@ namespace Emby.Server.Implementations.Plugins
Version = versionInfo.Version,
Status = status == PluginStatus.Disabled ? PluginStatus.Disabled : PluginStatus.Active, // Keep disabled state.
AutoUpdate = true,
ImagePath = imagePath,
Assemblies = versionInfo.Assemblies
ImagePath = imagePath
};
var metafile = Path.Combine(Path.Combine(path, MetafileName));
if (File.Exists(metafile))
{
var data = File.ReadAllBytes(metafile);
var localManifest = JsonSerializer.Deserialize<PluginManifest>(data, _jsonOptions) ?? new PluginManifest();
// Plugin installation is the typical cause for populating a manifest. Activate.
localManifest.Status = status == PluginStatus.Disabled ? PluginStatus.Disabled : PluginStatus.Active;
if (!Equals(localManifest.Id, manifest.Id))
{
_logger.LogError("The manifest ID {LocalUUID} did not match the package info ID {PackageUUID}.", localManifest.Id, manifest.Id);
localManifest.Status = PluginStatus.Malfunctioned;
}
if (localManifest.Version != manifest.Version)
{
_logger.LogWarning("The version of the local manifest was {LocalVersion}, but {PackageVersion} was expected. The value will be replaced.", localManifest.Version, manifest.Version);
// Correct the local version.
localManifest.Version = manifest.Version;
}
// Reconcile missing data against repository manifest.
ReconcileManifest(localManifest, manifest);
manifest = localManifest;
}
else
{
_logger.LogInformation("No local manifest exists for plugin {Plugin}. Populating from repository manifest.", manifest.Name);
}
return SaveManifest(manifest, path);
}
/// <summary>
/// Resolve the target plugin manifest against the source. Values are mapped onto the
/// target only if they are default values or empty strings. ID and status fields are ignored.
/// </summary>
/// <param name="baseManifest">The base <see cref="PluginManifest"/> to be reconciled.</param>
/// <param name="projector">The <see cref="PluginManifest"/> to reconcile against.</param>
private void ReconcileManifest(PluginManifest baseManifest, PluginManifest projector)
{
var ignoredFields = new string[]
{
nameof(baseManifest.Id),
nameof(baseManifest.Status)
};
foreach (var property in baseManifest.GetType().GetProperties())
{
var localValue = property.GetValue(baseManifest);
if (property.PropertyType == typeof(bool) || ignoredFields.Any(s => Equals(s, property.Name)))
{
continue;
}
if (property.PropertyType.IsNullOrDefault(localValue) || (property.PropertyType == typeof(string) && (string)localValue! == string.Empty))
{
property.SetValue(baseManifest, property.GetValue(projector));
}
}
}
/// <summary>
/// Changes a plugin's load status.
/// </summary>
@ -598,7 +662,7 @@ namespace Emby.Server.Implementations.Plugins
{
Version? version;
PluginManifest? manifest = null;
var metafile = Path.Combine(dir, "meta.json");
var metafile = Path.Combine(dir, MetafileName);
if (File.Exists(metafile))
{
// Only path where this stays null is when File.ReadAllBytes throws an IOException

View File

@ -183,7 +183,7 @@ namespace Emby.Server.Implementations.Updates
var plugin = _pluginManager.GetPlugin(package.Id, version.VersionNumber);
if (plugin is not null)
{
await _pluginManager.GenerateManifest(package, version.VersionNumber, plugin.Path, plugin.Manifest.Status).ConfigureAwait(false);
await _pluginManager.PopulateManifest(package, version.VersionNumber, plugin.Path, plugin.Manifest.Status).ConfigureAwait(false);
}
// Remove versions with a target ABI greater then the current application version.
@ -555,7 +555,10 @@ namespace Emby.Server.Implementations.Updates
stream.Position = 0;
using var reader = new ZipArchive(stream);
reader.ExtractToDirectory(targetDir, true);
await _pluginManager.GenerateManifest(package.PackageInfo, package.Version, targetDir, status).ConfigureAwait(false);
// Ensure we create one or populate existing ones with missing data.
await _pluginManager.PopulateManifest(package.PackageInfo, package.Version, targetDir, status);
_pluginManager.ImportPluginFrom(targetDir);
}

View File

@ -57,7 +57,7 @@ namespace MediaBrowser.Common.Plugins
/// <param name="path">The path where to save the manifest.</param>
/// <param name="status">Initial status of the plugin.</param>
/// <returns>True if successful.</returns>
Task<bool> GenerateManifest(PackageInfo packageInfo, Version version, string path, PluginStatus status);
Task<bool> PopulateManifest(PackageInfo packageInfo, Version version, string path, PluginStatus status);
/// <summary>
/// Imports plugin details from a folder.

View File

@ -75,11 +75,5 @@ namespace MediaBrowser.Model.Updates
/// </summary>
[JsonPropertyName("repositoryUrl")]
public string RepositoryUrl { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the assemblies whitelist for this version.
/// </summary>
[JsonPropertyName("assemblies")]
public IReadOnlyList<string> Assemblies { get; set; } = Array.Empty<string>();
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Globalization;
namespace Jellyfin.Extensions;
/// <summary>
/// Provides extensions methods for <see cref="Type" />.
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Checks if the supplied value is the default or null value for that type.
/// </summary>
/// <typeparam name="T">The type of the value to compare.</typeparam>
/// <param name="type">The type.</param>
/// <param name="value">The value to check.</param>
/// <returns><see langword="true"/> if the value is the default for the type. Otherwise, <see langword="false"/>.</returns>
public static bool IsNullOrDefault<T>(this Type type, T value)
{
if (value is null)
{
return true;
}
object? tmp = value;
object? defaultValue = type.IsValueType ? Activator.CreateInstance(type) : null;
if (type.IsAssignableTo(typeof(IConvertible)))
{
tmp = Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
return Equals(tmp, defaultValue);
}
/// <summary>
/// Checks if the object is currently a default or null value. Boxed types will be unboxed prior to comparison.
/// </summary>
/// <param name="obj">The object to check.</param>
/// <returns><see langword="true"/> if the value is the default for the type. Otherwise, <see langword="false"/>.</returns>
public static bool IsNullOrDefault(this object? obj)
{
// Unbox the type and check.
return obj?.GetType().IsNullOrDefault(obj) ?? true;
}
}

View File

@ -0,0 +1,68 @@
using System;
using Xunit;
namespace Jellyfin.Extensions.Tests
{
public class TypeExtensionsTests
{
[Theory]
[InlineData(typeof(byte), byte.MaxValue, false)]
[InlineData(typeof(short), short.MinValue, false)]
[InlineData(typeof(ushort), ushort.MaxValue, false)]
[InlineData(typeof(int), int.MinValue, false)]
[InlineData(typeof(uint), uint.MaxValue, false)]
[InlineData(typeof(long), long.MinValue, false)]
[InlineData(typeof(ulong), ulong.MaxValue, false)]
[InlineData(typeof(decimal), -1.0, false)]
[InlineData(typeof(bool), true, false)]
[InlineData(typeof(char), 'a', false)]
[InlineData(typeof(string), "", false)]
[InlineData(typeof(object), 1, false)]
[InlineData(typeof(byte), 0, true)]
[InlineData(typeof(short), 0, true)]
[InlineData(typeof(ushort), 0, true)]
[InlineData(typeof(int), 0, true)]
[InlineData(typeof(uint), 0, true)]
[InlineData(typeof(long), 0, true)]
[InlineData(typeof(ulong), 0, true)]
[InlineData(typeof(decimal), 0, true)]
[InlineData(typeof(bool), false, true)]
[InlineData(typeof(char), '\x0000', true)]
[InlineData(typeof(string), null, true)]
[InlineData(typeof(object), null, true)]
[InlineData(typeof(PhonyClass), null, true)]
[InlineData(typeof(DateTime), null, true)] // Special case handled within the test.
[InlineData(typeof(DateTime), null, false)] // Special case handled within the test.
[InlineData(typeof(byte?), null, true)]
[InlineData(typeof(short?), null, true)]
[InlineData(typeof(ushort?), null, true)]
[InlineData(typeof(int?), null, true)]
[InlineData(typeof(uint?), null, true)]
[InlineData(typeof(long?), null, true)]
[InlineData(typeof(ulong?), null, true)]
[InlineData(typeof(decimal?), null, true)]
[InlineData(typeof(bool?), null, true)]
[InlineData(typeof(char?), null, true)]
public void IsNullOrDefault_Matches_Expected(Type type, object? value, bool expectedResult)
{
if (type == typeof(DateTime))
{
if (expectedResult)
{
value = default(DateTime);
}
else
{
value = DateTime.Now;
}
}
Assert.Equal(expectedResult, type.IsNullOrDefault(value));
Assert.Equal(expectedResult, value.IsNullOrDefault());
}
private class PhonyClass
{
}
}
}

View File

@ -1,12 +1,16 @@
using System;
using System.Globalization;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using AutoFixture;
using Emby.Server.Implementations.Library;
using Emby.Server.Implementations.Plugins;
using Jellyfin.Extensions.Json;
using Jellyfin.Extensions.Json.Converters;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Model.Updates;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
@ -16,6 +20,21 @@ namespace Jellyfin.Server.Implementations.Tests.Plugins
{
private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data");
private string _tempPath = string.Empty;
private string _pluginPath = string.Empty;
private JsonSerializerOptions _options;
public PluginManagerTests()
{
(_tempPath, _pluginPath) = GetTestPaths("plugin-" + Path.GetRandomFileName());
Directory.CreateDirectory(_pluginPath);
_options = GetTestSerializerOptions();
}
[Fact]
public void SaveManifest_RoundTrip_Success()
{
@ -25,12 +44,9 @@ namespace Jellyfin.Server.Implementations.Tests.Plugins
Version = "1.0"
};
var tempPath = Path.Combine(_testPathRoot, "manifest-" + Path.GetRandomFileName());
Directory.CreateDirectory(tempPath);
Assert.True(pluginManager.SaveManifest(manifest, _pluginPath));
Assert.True(pluginManager.SaveManifest(manifest, tempPath));
var res = pluginManager.LoadManifest(tempPath);
var res = pluginManager.LoadManifest(_pluginPath);
Assert.Equal(manifest.Category, res.Manifest.Category);
Assert.Equal(manifest.Changelog, res.Manifest.Changelog);
@ -66,28 +82,25 @@ namespace Jellyfin.Server.Implementations.Tests.Plugins
};
var filename = Path.GetFileName(dllFile)!;
var (tempPath, pluginPath) = GetTestPaths("safe");
var dllPath = Path.GetDirectoryName(Path.Combine(_pluginPath, dllFile))!;
Directory.CreateDirectory(Path.Combine(pluginPath, dllFile.Replace(filename, string.Empty, StringComparison.OrdinalIgnoreCase)));
File.Create(Path.Combine(pluginPath, dllFile));
Directory.CreateDirectory(dllPath);
File.Create(Path.Combine(dllPath, filename));
var metafilePath = Path.Combine(_pluginPath, "meta.json");
var options = GetTestSerializerOptions();
var data = JsonSerializer.Serialize(manifest, options);
var metafilePath = Path.Combine(tempPath, "safe", "meta.json");
File.WriteAllText(metafilePath, JsonSerializer.Serialize(manifest, _options));
File.WriteAllText(metafilePath, data);
var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, _tempPath, new Version(1, 0));
var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, tempPath, new Version(1, 0));
var res = JsonSerializer.Deserialize<PluginManifest>(File.ReadAllText(metafilePath), _options);
var res = JsonSerializer.Deserialize<PluginManifest>(File.ReadAllText(metafilePath), options);
var expectedFullPath = Path.Combine(pluginPath, dllFile).Canonicalize();
var expectedFullPath = Path.Combine(_pluginPath, dllFile).Canonicalize();
Assert.NotNull(res);
Assert.NotEmpty(pluginManager.Plugins);
Assert.Equal(PluginStatus.Active, res!.Status);
Assert.Equal(expectedFullPath, pluginManager.Plugins[0].DllFiles[0]);
Assert.StartsWith(Path.Combine(tempPath, "safe"), expectedFullPath, StringComparison.InvariantCulture);
Assert.StartsWith(_pluginPath, expectedFullPath, StringComparison.InvariantCulture);
}
/// <summary>
@ -109,7 +122,6 @@ namespace Jellyfin.Server.Implementations.Tests.Plugins
[InlineData("..\\.\\..\\.\\..\\some.dll")] // Windows traversal with current and parent
[InlineData("\\\\network\\resource.dll")] // UNC Path
[InlineData("https://jellyfin.org/some.dll")] // URL
[InlineData("....//....//some.dll")] // Path replacement risk if a single "../" replacement occurs.
[InlineData("~/some.dll")] // Tilde poses a shell expansion risk, but is a valid path character.
public void Constructor_DiscoversUnsafePluginAssembly_Status_Malfunctioned(string unsafePath)
{
@ -120,10 +132,7 @@ namespace Jellyfin.Server.Implementations.Tests.Plugins
Assemblies = new string[] { unsafePath }
};
var (tempPath, pluginPath) = GetTestPaths("unsafe");
Directory.CreateDirectory(pluginPath);
// Only create very specific files. Otherwise the test will be exploiting path traversal.
var files = new string[]
{
"../other.dll",
@ -132,24 +141,136 @@ namespace Jellyfin.Server.Implementations.Tests.Plugins
foreach (var file in files)
{
File.Create(Path.Combine(pluginPath, file));
File.Create(Path.Combine(_pluginPath, file));
}
var options = GetTestSerializerOptions();
var data = JsonSerializer.Serialize(manifest, options);
var metafilePath = Path.Combine(tempPath, "unsafe", "meta.json");
var metafilePath = Path.Combine(_pluginPath, "meta.json");
File.WriteAllText(metafilePath, data);
File.WriteAllText(metafilePath, JsonSerializer.Serialize(manifest, _options));
var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, tempPath, new Version(1, 0));
var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, _tempPath, new Version(1, 0));
var res = JsonSerializer.Deserialize<PluginManifest>(File.ReadAllText(metafilePath), options);
var res = JsonSerializer.Deserialize<PluginManifest>(File.ReadAllText(metafilePath), _options);
Assert.NotNull(res);
Assert.Empty(pluginManager.Plugins);
Assert.Equal(PluginStatus.Malfunctioned, res!.Status);
}
[Fact]
public async Task PopulateManifest_ExistingMetafilePlugin_PopulatesMissingFields()
{
var packageInfo = GenerateTestPackage();
// Partial plugin without a name, but matching version and package ID
var partial = new PluginManifest
{
Id = packageInfo.Id,
AutoUpdate = false, // Turn off AutoUpdate
Status = PluginStatus.Restart,
Version = new Version(1, 0, 0).ToString(),
Assemblies = new[] { "Jellyfin.Test.dll" }
};
var metafilePath = Path.Combine(_pluginPath, "meta.json");
File.WriteAllText(metafilePath, JsonSerializer.Serialize(partial, _options));
var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, _tempPath, new Version(1, 0));
await pluginManager.PopulateManifest(packageInfo, new Version(1, 0), _pluginPath, PluginStatus.Active);
var resultBytes = File.ReadAllBytes(metafilePath);
var result = JsonSerializer.Deserialize<PluginManifest>(resultBytes, _options);
Assert.NotNull(result);
Assert.Equal(packageInfo.Name, result.Name);
Assert.Equal(packageInfo.Owner, result.Owner);
Assert.Equal(PluginStatus.Active, result.Status);
Assert.NotEmpty(result.Assemblies);
Assert.False(result.AutoUpdate);
// Preserved
Assert.Equal(packageInfo.Category, result.Category);
Assert.Equal(packageInfo.Description, result.Description);
Assert.Equal(packageInfo.Id, result.Id);
Assert.Equal(packageInfo.Overview, result.Overview);
Assert.Equal(partial.Assemblies[0], result.Assemblies[0]);
Assert.Equal(packageInfo.Versions[0].TargetAbi, result.TargetAbi);
Assert.Equal(DateTime.Parse(packageInfo.Versions[0].Timestamp!, CultureInfo.InvariantCulture), result.Timestamp);
Assert.Equal(packageInfo.Versions[0].Changelog, result.Changelog);
Assert.Equal(packageInfo.Versions[0].Version, result.Version);
}
[Fact]
public async Task PopulateManifest_ExistingMetafileMismatchedIds_Status_Malfunctioned()
{
var packageInfo = GenerateTestPackage();
// Partial plugin without a name, but matching version and package ID
var partial = new PluginManifest
{
Id = Guid.NewGuid(),
Version = new Version(1, 0, 0).ToString()
};
var metafilePath = Path.Combine(_pluginPath, "meta.json");
File.WriteAllText(metafilePath, JsonSerializer.Serialize(partial, _options));
var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, _tempPath, new Version(1, 0));
await pluginManager.PopulateManifest(packageInfo, new Version(1, 0), _pluginPath, PluginStatus.Active);
var resultBytes = File.ReadAllBytes(metafilePath);
var result = JsonSerializer.Deserialize<PluginManifest>(resultBytes, _options);
Assert.NotNull(result);
Assert.Equal(packageInfo.Name, result.Name);
Assert.Equal(PluginStatus.Malfunctioned, result.Status);
}
[Fact]
public async Task PopulateManifest_ExistingMetafileMismatchedVersions_Updates_Version()
{
var packageInfo = GenerateTestPackage();
var partial = new PluginManifest
{
Id = packageInfo.Id,
Version = new Version(2, 0, 0).ToString()
};
var metafilePath = Path.Combine(_pluginPath, "meta.json");
File.WriteAllText(metafilePath, JsonSerializer.Serialize(partial, _options));
var pluginManager = new PluginManager(new NullLogger<PluginManager>(), null!, null!, _tempPath, new Version(1, 0));
await pluginManager.PopulateManifest(packageInfo, new Version(1, 0), _pluginPath, PluginStatus.Active);
var resultBytes = File.ReadAllBytes(metafilePath);
var result = JsonSerializer.Deserialize<PluginManifest>(resultBytes, _options);
Assert.NotNull(result);
Assert.Equal(packageInfo.Name, result.Name);
Assert.Equal(PluginStatus.Active, result.Status);
Assert.Equal(packageInfo.Versions[0].Version, result.Version);
}
private PackageInfo GenerateTestPackage()
{
var fixture = new Fixture();
fixture.Customize<PackageInfo>(c => c.Without(x => x.Versions).Without(x => x.ImageUrl));
fixture.Customize<VersionInfo>(c => c.Without(x => x.Version).Without(x => x.Timestamp));
var versionInfo = fixture.Create<VersionInfo>();
versionInfo.Version = new Version(1, 0).ToString();
versionInfo.Timestamp = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture);
var packageInfo = fixture.Create<PackageInfo>();
packageInfo.Versions = new[] { versionInfo };
return packageInfo;
}
private JsonSerializerOptions GetTestSerializerOptions()
{
var options = new JsonSerializerOptions(JsonDefaults.Options)
@ -171,7 +292,7 @@ namespace Jellyfin.Server.Implementations.Tests.Plugins
private (string TempPath, string PluginPath) GetTestPaths(string pluginFolderName)
{
var tempPath = Path.Combine(_testPathRoot, "plugins-" + Path.GetRandomFileName());
var tempPath = Path.Combine(_testPathRoot, "plugin-manager" + Path.GetRandomFileName());
var pluginPath = Path.Combine(tempPath, pluginFolderName);
return (tempPath, pluginPath);

View File

@ -13,8 +13,7 @@
"targetAbi": "10.6.0.0",
"sourceUrl": "https://repo.jellyfin.org/releases/plugin/anime/anime_10.0.0.0.zip",
"checksum": "93e969adeba1050423fc8817ed3c36f8",
"timestamp": "2020-08-17T01:41:13Z",
"assemblies": [ "Jellyfin.Plugin.Anime.dll" ]
"timestamp": "2020-08-17T01:41:13Z"
},
{
"version": "9.0.0.0",
@ -22,8 +21,7 @@
"targetAbi": "10.6.0.0",
"sourceUrl": "https://repo.jellyfin.org/releases/plugin/anime/anime_9.0.0.0.zip",
"checksum": "9b1cebff835813e15f414f44b40c41c8",
"timestamp": "2020-07-20T01:30:16Z",
"assemblies": [ "Jellyfin.Plugin.Anime.dll" ]
"timestamp": "2020-07-20T01:30:16Z"
}
]
},

View File

@ -106,19 +106,5 @@ namespace Jellyfin.Server.Implementations.Tests.Updates
var ex = await Record.ExceptionAsync(() => _installationManager.InstallPackage(packageInfo, CancellationToken.None)).ConfigureAwait(false);
Assert.Null(ex);
}
[Fact]
public async Task InstallPackage_WithAssemblies_Success()
{
PackageInfo[] packages = await _installationManager.GetPackages(
"Jellyfin Stable",
"https://repo.jellyfin.org/releases/plugin/manifest-stable.json",
false);
packages = _installationManager.FilterPackages(packages, "Anime").ToArray();
Assert.Single(packages);
Assert.NotEmpty(packages[0].Versions[0].Assemblies);
Assert.Equal("Jellyfin.Plugin.Anime.dll", packages[0].Versions[0].Assemblies[0]);
}
}
}