From 862c76c00bbc8503481edff99a73fc8c6bf46de0 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sun, 23 Aug 2026 08:31:47 -0700 Subject: [PATCH] Fixes plugin upload path traversal on non-Windows platforms --- .../Core/Plugins/PluginInstaller.cs | 36 ++++++++++++++----- .../Core/Plugins/PluginInstallerTests.cs | 34 ++++++++++++++++++ 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/Lumina.Infrastructure/Core/Plugins/PluginInstaller.cs b/src/Lumina.Infrastructure/Core/Plugins/PluginInstaller.cs index c9f7b37d..eb897343 100644 --- a/src/Lumina.Infrastructure/Core/Plugins/PluginInstaller.cs +++ b/src/Lumina.Infrastructure/Core/Plugins/PluginInstaller.cs @@ -9,7 +9,6 @@ using System; using System.Collections.Concurrent; using System.IO; using System.IO.Compression; -using System.Linq; using System.Threading; using System.Threading.Tasks; #endregion @@ -56,7 +55,10 @@ internal sealed class PluginInstaller : IPluginInstaller string extension = Path.GetExtension(fileName); if (string.Equals(extension, ".dll", StringComparison.OrdinalIgnoreCase)) { - await CopySingleAssemblyAsync(archive, Path.GetFileName(fileName), cancellationToken).ConfigureAwait(false); + string? safeFileName = GetSafeFileName(fileName); + if (safeFileName is null) + return Errors.Plugins.PluginFileNameCannotBeEmpty; + await CopySingleAssemblyAsync(archive, safeFileName, cancellationToken).ConfigureAwait(false); return Result.Success; } @@ -105,21 +107,37 @@ internal sealed class PluginInstaller : IPluginInstaller private async Task> ExtractZipArchiveAsync(Stream archive, CancellationToken cancellationToken) { using ZipArchive zipArchive = new(archive); - ZipArchiveEntry[] assemblyEntries = [.. zipArchive.Entries.Where(entry => string.Equals(Path.GetExtension(entry.FullName), ".dll", StringComparison.OrdinalIgnoreCase))]; - if (assemblyEntries.Length == 0) - return Errors.Plugins.PluginArchiveContainsNoAssemblies; - Directory.CreateDirectory(_pluginsDirectory); - foreach (ZipArchiveEntry entry in assemblyEntries) + bool foundAssembly = false; + foreach (ZipArchiveEntry entry in zipArchive.Entries) { cancellationToken.ThrowIfCancellationRequested(); - // flatten the archive structure and use only the file name, so no entry can escape the plugin storage directory - string destinationPath = Path.Combine(_pluginsDirectory, Path.GetFileName(entry.FullName)); + string? safeFileName = GetSafeFileName(entry.FullName); + if (safeFileName is null || !string.Equals(Path.GetExtension(safeFileName), ".dll", StringComparison.OrdinalIgnoreCase)) + continue; + foundAssembly = true; + string destinationPath = Path.Combine(_pluginsDirectory, safeFileName); await using Stream entryStream = entry.Open(); await using FileStream outputStream = new(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None); await entryStream.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false); } + if (!foundAssembly) + return Errors.Plugins.PluginArchiveContainsNoAssemblies; return Result.Success; } + + /// + /// Extracts the bare file name of an uploaded plugin file or archive entry, stripping every directory component, + /// so that a path traversal attempt cannot write outside the plugin storage directory on any platform. + /// + /// The file path to sanitize. + /// The bare file name, or when the path contains no usable file name. + private static string? GetSafeFileName(string path) + { + // a backslash is not a directory separator on non-Windows platforms, so it is normalized to '/' first, + // after which Path.GetFileName can strip any directory component on every platform + string fileName = Path.GetFileName(path.Replace('\\', '/')); + return fileName is "" or "." or ".." ? null : fileName; + } } diff --git a/tests/UnitTests/Lumina.Infrastructure.UnitTests/Core/Plugins/PluginInstallerTests.cs b/tests/UnitTests/Lumina.Infrastructure.UnitTests/Core/Plugins/PluginInstallerTests.cs index e67fd71e..c744d920 100644 --- a/tests/UnitTests/Lumina.Infrastructure.UnitTests/Core/Plugins/PluginInstallerTests.cs +++ b/tests/UnitTests/Lumina.Infrastructure.UnitTests/Core/Plugins/PluginInstallerTests.cs @@ -72,6 +72,40 @@ public class PluginInstallerTests : IDisposable Assert.False(File.Exists(Path.Combine(_pluginsDirectory, "readme.txt"))); } + [Fact] + public async Task InstallAsync_WhenUploadingZipWithBackslashTraversalEntry_ShouldFlattenItInsidePluginsDirectory() + { + // Arrange + await using MemoryStream zipStream = new(); + // a backslash is not a directory separator on non-Windows platforms, so it must be neutralized explicitly + CreateZipArchive(zipStream, [(@"..\evil.dll", ReadTestAssembly())]); + zipStream.Position = 0; + + // Act + Result result = await _sut.InstallAsync(zipStream, "plugin.zip", CancellationToken.None); + + // Assert + Assert.False(result.IsFailure); + Assert.True(File.Exists(Path.Combine(_pluginsDirectory, "evil.dll"))); + Assert.False(File.Exists(Path.Combine(AppContext.BaseDirectory, "evil.dll"))); + } + + [Fact] + public async Task InstallAsync_WhenUploadingSingleAssemblyWithTraversalFileName_ShouldFlattenItInsidePluginsDirectory() + { + // Arrange + const string TRAVERSAL_FILE_NAME = "traversal-test-plugin.dll"; + await using FileStream stream = File.OpenRead(Path.Combine(AppContext.BaseDirectory, "Lumina.TestPlugin.dll")); + + // Act + Result result = await _sut.InstallAsync(stream, $@"..\{TRAVERSAL_FILE_NAME}", CancellationToken.None); + + // Assert + Assert.False(result.IsFailure); + Assert.True(File.Exists(Path.Combine(_pluginsDirectory, TRAVERSAL_FILE_NAME))); + Assert.False(File.Exists(Path.Combine(AppContext.BaseDirectory, TRAVERSAL_FILE_NAME))); + } + [Fact] public async Task InstallAsync_WhenUploadingUnsupportedFileType_ShouldReturnError() {