mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-08-24 20:52:11 +00:00
extract ZIP handling logic into PluginArchive utility class for reuse and simplify PluginFactory implementation
This commit is contained in:
parent
c3f91f6828
commit
6f17856604
69
app/MindWork AI Studio/Tools/PluginSystem/PluginArchive.cs
Normal file
69
app/MindWork AI Studio/Tools/PluginSystem/PluginArchive.cs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
using System.IO.Compression;
|
||||||
|
|
||||||
|
namespace AIStudio.Tools.PluginSystem;
|
||||||
|
|
||||||
|
public static class PluginArchive
|
||||||
|
{
|
||||||
|
// Compatibility shim for Windows-created ZIPs with backslashes in entry names (dotnet/runtime#27620).
|
||||||
|
// See documentation/compatibility-shims/2026-07-enterprise-config-zip-backslashes.md.
|
||||||
|
public static void Extract(string sourceArchiveFileName, string destinationDirectory)
|
||||||
|
{
|
||||||
|
using var archive = ZipFile.OpenRead(sourceArchiveFileName);
|
||||||
|
Directory.CreateDirectory(destinationDirectory);
|
||||||
|
|
||||||
|
var destinationDirectoryFullPath = Path.GetFullPath(destinationDirectory);
|
||||||
|
if (!destinationDirectoryFullPath.EndsWith(Path.DirectorySeparatorChar))
|
||||||
|
destinationDirectoryFullPath += Path.DirectorySeparatorChar;
|
||||||
|
|
||||||
|
foreach (var entry in archive.Entries)
|
||||||
|
{
|
||||||
|
var normalizedEntryName = NormalizeEntryName(entry.FullName);
|
||||||
|
var destinationPath = GetEntryDestinationPath(destinationDirectoryFullPath, normalizedEntryName);
|
||||||
|
|
||||||
|
if (normalizedEntryName.EndsWith('/'))
|
||||||
|
{
|
||||||
|
if (entry.Length != 0)
|
||||||
|
throw new InvalidDataException($"The plugin archive contains a directory entry with data: '{entry.FullName}'.");
|
||||||
|
|
||||||
|
Directory.CreateDirectory(destinationPath);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!);
|
||||||
|
entry.ExtractToFile(destinationPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string NormalizeEntryName(string entryName)
|
||||||
|
{
|
||||||
|
var normalizedEntryName = entryName.Replace('\\', '/');
|
||||||
|
if (string.IsNullOrWhiteSpace(normalizedEntryName))
|
||||||
|
throw new InvalidDataException("The plugin archive contains an empty entry name.");
|
||||||
|
|
||||||
|
if (normalizedEntryName.Contains('\0'))
|
||||||
|
throw new InvalidDataException($"The plugin archive contains an invalid entry name: '{entryName}'.");
|
||||||
|
|
||||||
|
if (normalizedEntryName.StartsWith('/'))
|
||||||
|
throw new InvalidDataException($"The plugin archive contains a rooted entry name: '{entryName}'.");
|
||||||
|
|
||||||
|
if (normalizedEntryName is [_, ':', ..])
|
||||||
|
throw new InvalidDataException($"The plugin archive contains a drive-qualified entry name: '{entryName}'.");
|
||||||
|
|
||||||
|
var pathSegments = normalizedEntryName.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (pathSegments.Length == 0 || pathSegments.Any(segment => segment is "." or ".."))
|
||||||
|
throw new InvalidDataException($"The plugin archive contains an unsafe entry name: '{entryName}'.");
|
||||||
|
|
||||||
|
return normalizedEntryName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetEntryDestinationPath(string destinationDirectoryFullPath, string normalizedEntryName)
|
||||||
|
{
|
||||||
|
var pathSegments = normalizedEntryName.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
var relativePath = Path.Combine(pathSegments);
|
||||||
|
var destinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, relativePath));
|
||||||
|
if (!destinationPath.StartsWith(destinationDirectoryFullPath, StringComparison.Ordinal))
|
||||||
|
throw new InvalidDataException($"The plugin archive contains an entry outside the destination directory: '{normalizedEntryName}'.");
|
||||||
|
|
||||||
|
return destinationPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,3 @@
|
|||||||
using System.IO.Compression;
|
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
|
||||||
namespace AIStudio.Tools.PluginSystem;
|
namespace AIStudio.Tools.PluginSystem;
|
||||||
@ -130,69 +129,11 @@ public static partial class PluginFactory
|
|||||||
return wasSuccessful;
|
return wasSuccessful;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compatibility shim for Windows-created ZIPs with backslashes in entry names (dotnet/runtime#27620).
|
|
||||||
// See documentation/compatibility-shims/2026-07-enterprise-config-zip-backslashes.md.
|
|
||||||
private static void ExtractConfigPluginArchive(string sourceArchiveFileName, string destinationDirectory)
|
private static void ExtractConfigPluginArchive(string sourceArchiveFileName, string destinationDirectory)
|
||||||
{
|
{
|
||||||
using var archive = ZipFile.OpenRead(sourceArchiveFileName);
|
PluginArchive.Extract(sourceArchiveFileName, destinationDirectory);
|
||||||
Directory.CreateDirectory(destinationDirectory);
|
|
||||||
|
|
||||||
var destinationDirectoryFullPath = Path.GetFullPath(destinationDirectory);
|
|
||||||
if (!destinationDirectoryFullPath.EndsWith(Path.DirectorySeparatorChar))
|
|
||||||
destinationDirectoryFullPath += Path.DirectorySeparatorChar;
|
|
||||||
|
|
||||||
foreach (var entry in archive.Entries)
|
|
||||||
{
|
|
||||||
var normalizedEntryName = NormalizeConfigPluginZipEntryName(entry.FullName);
|
|
||||||
var destinationPath = GetConfigPluginZipEntryDestinationPath(destinationDirectoryFullPath, normalizedEntryName);
|
|
||||||
|
|
||||||
if (normalizedEntryName.EndsWith('/'))
|
|
||||||
{
|
|
||||||
if (entry.Length != 0)
|
|
||||||
throw new InvalidDataException($"The enterprise configuration plugin archive contains a directory entry with data: '{entry.FullName}'.");
|
|
||||||
|
|
||||||
Directory.CreateDirectory(destinationPath);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!);
|
|
||||||
entry.ExtractToFile(destinationPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Directory.EnumerateFiles(destinationDirectory, "plugin.lua", SearchOption.AllDirectories).Any())
|
if (!Directory.EnumerateFiles(destinationDirectory, "plugin.lua", SearchOption.AllDirectories).Any())
|
||||||
throw new InvalidDataException("The enterprise configuration plugin archive does not contain a plugin.lua file.");
|
throw new InvalidDataException("The enterprise configuration plugin archive does not contain a plugin.lua file.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private static string NormalizeConfigPluginZipEntryName(string entryName)
|
|
||||||
{
|
|
||||||
var normalizedEntryName = entryName.Replace('\\', '/');
|
|
||||||
if (string.IsNullOrWhiteSpace(normalizedEntryName))
|
|
||||||
throw new InvalidDataException("The enterprise configuration plugin archive contains an empty entry name.");
|
|
||||||
|
|
||||||
if (normalizedEntryName.Contains('\0'))
|
|
||||||
throw new InvalidDataException($"The enterprise configuration plugin archive contains an invalid entry name: '{entryName}'.");
|
|
||||||
|
|
||||||
if (normalizedEntryName.StartsWith('/'))
|
|
||||||
throw new InvalidDataException($"The enterprise configuration plugin archive contains a rooted entry name: '{entryName}'.");
|
|
||||||
|
|
||||||
if (normalizedEntryName is [_, ':', ..])
|
|
||||||
throw new InvalidDataException($"The enterprise configuration plugin archive contains a drive-qualified entry name: '{entryName}'.");
|
|
||||||
|
|
||||||
var pathSegments = normalizedEntryName.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
if (pathSegments.Length == 0 || pathSegments.Any(segment => segment is "." or ".."))
|
|
||||||
throw new InvalidDataException($"The enterprise configuration plugin archive contains an unsafe entry name: '{entryName}'.");
|
|
||||||
|
|
||||||
return normalizedEntryName;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetConfigPluginZipEntryDestinationPath(string destinationDirectoryFullPath, string normalizedEntryName)
|
|
||||||
{
|
|
||||||
var pathSegments = normalizedEntryName.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
var relativePath = Path.Combine(pathSegments);
|
|
||||||
var destinationPath = Path.GetFullPath(Path.Combine(destinationDirectoryFullPath, relativePath));
|
|
||||||
if (!destinationPath.StartsWith(destinationDirectoryFullPath, StringComparison.Ordinal))
|
|
||||||
throw new InvalidDataException($"The enterprise configuration plugin archive contains an entry outside the destination directory: '{normalizedEntryName}'.");
|
|
||||||
|
|
||||||
return destinationPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user