mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 10:11:10 +00:00
Renames project paths from Lumina to Nemera
This commit is contained in:
1 parent
ce6fddb4c9
commit
0f659a4877
6450 files changed
No files matched your search
+115
@@ -0,0 +1,115 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.DTO.Common;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Plugins.Calibre.Core;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Calibre.SecurityTests.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="CalibreArtworkProvider"/> class, exercised through the public artwork provider surface.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CalibreArtworkProviderSecurityTests
|
||||
{
|
||||
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public async Task GetArtworkAsync_WhenCoverHrefIsABareFileName_ShouldReturnTheCoverInsideTheBookDirectory()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = WriteBookWithCoverReference(bookDirectory, "cover.jpg");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "cover.jpg"), "image-bytes");
|
||||
IArtworkProvider provider = CreateArtworkProvider();
|
||||
|
||||
// Act
|
||||
IReadOnlyList<ArtworkDto> result = await provider.GetArtworkAsync(_bookMetadataLookupDtoFixture.Create(path: bookPath), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
ArtworkDto artwork = Assert.Single(result);
|
||||
Assert.Equal(Path.Combine(bookDirectory, "cover.jpg"), artwork.LocalPath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("../cover.jpg")] // Parent directory escape.
|
||||
[InlineData("../../cover.jpg")] // Multiple parent directory escape.
|
||||
[InlineData("sub/cover.jpg")] // Forward slash path traversal.
|
||||
[InlineData("sub\\cover.jpg")] // Backslash path traversal.
|
||||
[InlineData(@"C:\Windows\System32\drivers\etc\hosts")] // Absolute Windows path.
|
||||
[InlineData("/etc/passwd")] // Absolute Unix path.
|
||||
[InlineData("http://evil.example/cover.jpg")] // Remote URL.
|
||||
[InlineData("file:///etc/passwd")] // File URI.
|
||||
public async Task GetArtworkAsync_WhenCoverHrefEscapesTheBookDirectory_ShouldReturnNull(string coverHref)
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = WriteBookWithCoverReference(bookDirectory, coverHref);
|
||||
IArtworkProvider provider = CreateArtworkProvider();
|
||||
|
||||
// Act
|
||||
IReadOnlyList<ArtworkDto> result = await provider.GetArtworkAsync(_bookMetadataLookupDtoFixture.Create(path: bookPath), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the Calibre artwork provider by registering the plugin services and resolving the keyed artwork provider.
|
||||
/// </summary>
|
||||
/// <returns>The created Calibre artwork provider.</returns>
|
||||
private static IArtworkProvider CreateArtworkProvider()
|
||||
{
|
||||
ServiceCollection services = new();
|
||||
new CalibrePlugin().RegisterServices(services);
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
return serviceProvider.GetKeyedService<IArtworkProvider>(CalibrePlugin.s_pluginId)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes an OPF file that references the given cover into the book directory, and returns the path of the book.
|
||||
/// </summary>
|
||||
/// <param name="bookDirectory">The file system path of the directory the OPF file is written into.</param>
|
||||
/// <param name="coverHref">The href of the cover the OPF file references.</param>
|
||||
/// <returns>The file system path of the book.</returns>
|
||||
private static string WriteBookWithCoverReference(string bookDirectory, string coverHref)
|
||||
{
|
||||
string opf = $$"""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>Test</dc:title></metadata><guide><reference type="cover" href="{{coverHref}}"/></guide></package>""";
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), opf);
|
||||
return bookPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a unique temporary directory for the book.
|
||||
/// </summary>
|
||||
/// <returns>The file system path of the created temporary directory.</returns>
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
string directoryPath = Path.Combine(Path.GetTempPath(), $"lumina-security-artwork-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
return directoryPath;
|
||||
}
|
||||
}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.DTO.Common;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Plugins.Calibre.Core;
|
||||
using Lumina.Plugins.Calibre.Core.Opf;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.Calibre.SecurityTests.Core.Opf;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <see cref="OpfReader"/> class, exercised through the public metadata provider surface.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class OpfReaderSecurityTests
|
||||
{
|
||||
private const int MAX_OPF_FILE_SIZE_BYTES = 5 * 1024 * 1024;
|
||||
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfContainsADoctypeWithAnExternalEntity_ShouldNotExpandTheEntity()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
// The OPF is untrusted XML, so a DTD with an external entity must be rejected instead of being read and expanded.
|
||||
string opfContent = """<?xml version="1.0" encoding="utf-8"?><!DOCTYPE package [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>&xxe;</dc:title></metadata></package>""";
|
||||
string bookPath = WriteBookWithOpf(bookDirectory, opfContent);
|
||||
IMetadataProvider provider = CreateMetadataProvider();
|
||||
|
||||
// Act
|
||||
MetadataDto? result = await provider.GetMetadataAsync(_bookMetadataLookupDtoFixture.Create(path: bookPath), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Null(((BookMetadataDto)result!).Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfContainsBillionLaughsStyleEntityExpansion_ShouldNotExpandTheEntities()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string opfContent = """<?xml version="1.0" encoding="utf-8"?><!DOCTYPE lolz [<!ENTITY lol "lol"><!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;"><!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">]><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>&lol3;</dc:title></metadata></package>""";
|
||||
string bookPath = WriteBookWithOpf(bookDirectory, opfContent);
|
||||
IMetadataProvider provider = CreateMetadataProvider();
|
||||
|
||||
// Act
|
||||
MetadataDto? result = await provider.GetMetadataAsync(_bookMetadataLookupDtoFixture.Create(path: bookPath), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Null(((BookMetadataDto)result!).Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfIsOversized_ShouldNotParseIt()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string oversizedContent = $"""<?xml version="1.0" encoding="utf-8"?><package xmlns="http://www.idpf.org/2007/opf"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:title>{new string('a', MAX_OPF_FILE_SIZE_BYTES + 100)}</dc:title></metadata></package>""";
|
||||
string bookPath = WriteBookWithOpf(bookDirectory, oversizedContent);
|
||||
IMetadataProvider provider = CreateMetadataProvider();
|
||||
|
||||
// Act
|
||||
MetadataDto? result = await provider.GetMetadataAsync(_bookMetadataLookupDtoFixture.Create(path: bookPath), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Null(((BookMetadataDto)result!).Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMetadataAsync_WhenOpfIsMalformedXml_ShouldNotExposeAnException()
|
||||
{
|
||||
// Arrange
|
||||
string bookDirectory = CreateTempDirectory();
|
||||
try
|
||||
{
|
||||
string bookPath = WriteBookWithOpf(bookDirectory, """<package xmlns="http://www.idpf.org/2007/opf"><metadata><dc:title>Unclosed""");
|
||||
IMetadataProvider provider = CreateMetadataProvider();
|
||||
|
||||
// Act
|
||||
MetadataDto? result = await provider.GetMetadataAsync(_bookMetadataLookupDtoFixture.Create(path: bookPath), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
// A malformed OPF must be swallowed and must not leak an exception to the caller.
|
||||
Assert.NotNull(result);
|
||||
Assert.Null(((BookMetadataDto)result!).Title);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(bookDirectory, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the Calibre metadata provider by registering the plugin services and resolving the keyed metadata provider.
|
||||
/// </summary>
|
||||
/// <returns>The created Calibre metadata provider.</returns>
|
||||
private static IMetadataProvider CreateMetadataProvider()
|
||||
{
|
||||
ServiceCollection services = new();
|
||||
new CalibrePlugin().RegisterServices(services);
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
return serviceProvider.GetKeyedService<IMetadataProvider>(CalibrePlugin.s_pluginId)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes an OPF file with the given content into the book directory, and returns the path of the book.
|
||||
/// </summary>
|
||||
/// <param name="bookDirectory">The file system path of the directory the OPF file is written into.</param>
|
||||
/// <param name="opfContent">The content of the OPF file to write.</param>
|
||||
/// <returns>The file system path of the book.</returns>
|
||||
private static string WriteBookWithOpf(string bookDirectory, string opfContent)
|
||||
{
|
||||
string bookPath = Path.Combine(bookDirectory, "book.epub");
|
||||
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), opfContent);
|
||||
return bookPath;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a unique temporary directory for the book.
|
||||
/// </summary>
|
||||
/// <returns>The file system path of the created temporary directory.</returns>
|
||||
private static string CreateTempDirectory()
|
||||
{
|
||||
string directoryPath = Path.Combine(Path.GetTempPath(), $"lumina-security-opf-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
return directoryPath;
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="Xunit.CollectionBehaviorAttribute">
|
||||
<_Parameter1>DisableTestParallelization = true</_Parameter1>
|
||||
<_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture.AutoNSubstitute" />
|
||||
<PackageReference Include="AutoFixture.Xunit2" />
|
||||
<PackageReference Include="Bogus" />
|
||||
<PackageReference Include="coverlet.collector">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="NSubstitute" />
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\plugins\Lumina.Plugins.Calibre\Lumina.Plugins.Calibre.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Lumina.Plugins.Contracts\Lumina.Plugins.Contracts.csproj" />
|
||||
<ProjectReference Include="..\..\Common\Lumina.Plugins.Calibre.Fixtures\Lumina.Plugins.Calibre.Fixtures.csproj" />
|
||||
<ProjectReference Include="..\..\Common\Lumina.Contracts.Fixtures\Lumina.Contracts.Fixtures.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in new issue
Block a user