mirror of
https://github.com/k1tbyte/Wand-Enhancer.git
synced 2026-09-04 08:13:18 +00:00
fix(asar): correct archive tree lookups and fail loudly on unreadable input
InsertFile resolved the grandparent node instead of the parent. Reads no longer create phantom directories in the header. Bound symlink traversal and skip reparse points when crawling. Locked or unreadable files now abort packing instead of being dropped. Read headers and integrity blocks with a full-read loop. Assert the header keeps its size before overwriting the placeholder. Validate Pickle buffer sizes, payload overflow and negative lengths. Check CreateSymbolicLink and external tool exit codes. Drop unused Pickle accessors, TransformedFile and FilesystemFilesAndLinks.Links.
This commit is contained in:
@@ -12,8 +12,6 @@ namespace AsarSharp.AsarFileSystem
|
||||
public static class Disk
|
||||
{
|
||||
private const int StreamBufferSize = 1024 * 1024;
|
||||
private static readonly ConcurrentDictionary<string, Filesystem> _filesystemCache =
|
||||
new ConcurrentDictionary<string, Filesystem>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public class ArchiveHeader
|
||||
{
|
||||
@@ -25,7 +23,6 @@ namespace AsarSharp.AsarFileSystem
|
||||
public class FilesystemFilesAndLinks
|
||||
{
|
||||
public List<BasicFileInfo> Files { get; set; } = new List<BasicFileInfo>();
|
||||
public List<BasicFileInfo> Links { get; set; } = new List<BasicFileInfo>();
|
||||
}
|
||||
|
||||
public class BasicFileInfo
|
||||
@@ -42,14 +39,14 @@ namespace AsarSharp.AsarFileSystem
|
||||
65536, FileOptions.SequentialScan))
|
||||
{
|
||||
byte[] sizeBuf = new byte[8];
|
||||
if (fs.Read(sizeBuf, 0, 8) != 8)
|
||||
if (fs.ReadFull(sizeBuf, 0, 8) != 8)
|
||||
throw new Exception("Unable to read header size");
|
||||
|
||||
var sizePickle = Pickle.CreateFromBuffer(sizeBuf);
|
||||
var size = sizePickle.CreateIterator().ReadUInt32();
|
||||
|
||||
var headerBuf = new byte[size];
|
||||
if (fs.Read(headerBuf, 0, (int)size) != size)
|
||||
if (fs.ReadFull(headerBuf, 0, (int)size) != size)
|
||||
throw new Exception("Unable to read header");
|
||||
|
||||
var headerPickle = Pickle.CreateFromBuffer(headerBuf);
|
||||
@@ -65,62 +62,28 @@ namespace AsarSharp.AsarFileSystem
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the header fresh every time: an archive is repacked in place during a patch run,
|
||||
/// so a cached header would hand out stale offsets on the next read of the same path.
|
||||
/// </summary>
|
||||
public static Filesystem ReadFilesystemSync(string archivePath)
|
||||
{
|
||||
return _filesystemCache.GetOrAdd(archivePath, key =>
|
||||
{
|
||||
var header = ReadArchiveHeaderSync(key);
|
||||
var filesystem = new Filesystem(key);
|
||||
filesystem.SetHeader(header.Header, header.HeaderSize);
|
||||
return filesystem;
|
||||
});
|
||||
}
|
||||
|
||||
public static byte[] ReadFileSync(Filesystem filesystem, string filename, FilesystemEntry info)
|
||||
{
|
||||
if (!info.IsFile || !info.Size.HasValue)
|
||||
throw new ArgumentException("Entry is not a file", nameof(info));
|
||||
|
||||
long size = info.Size.Value;
|
||||
byte[] buffer = new byte[size];
|
||||
|
||||
if (size <= 0) return buffer;
|
||||
|
||||
if (info.Unpacked == true)
|
||||
{
|
||||
string filePath = Path.Combine($"{filesystem.GetRootPath()}.unpacked", filename);
|
||||
return File.ReadAllBytes(filePath);
|
||||
}
|
||||
|
||||
using (var fs = new FileStream(filesystem.GetRootPath(), FileMode.Open, FileAccess.Read,
|
||||
FileShare.Read, 65536, FileOptions.RandomAccess))
|
||||
{
|
||||
long offset = 8 + filesystem.GetHeaderSize() + long.Parse(info.Offset);
|
||||
fs.Position = offset;
|
||||
int bytesRead = fs.Read(buffer, 0, (int)size);
|
||||
if (bytesRead != size)
|
||||
throw new Exception($"Failed to read entire file, got {bytesRead} bytes instead of {size}");
|
||||
}
|
||||
|
||||
return buffer;
|
||||
var header = ReadArchiveHeaderSync(archivePath);
|
||||
var filesystem = new Filesystem(archivePath);
|
||||
filesystem.SetHeader(header.Header, header.HeaderSize);
|
||||
return filesystem;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public static bool UncacheFilesystem(string archivePath)
|
||||
{
|
||||
return _filesystemCache.TryRemove(archivePath, out _);
|
||||
}
|
||||
|
||||
public static void UncacheAll()
|
||||
{
|
||||
_filesystemCache.Clear();
|
||||
}
|
||||
|
||||
public static void CopyFile(string dest, string rootPath, string filename)
|
||||
{
|
||||
if (dest == null || rootPath == null || filename == null)
|
||||
throw new ArgumentNullException();
|
||||
if (dest == null)
|
||||
throw new ArgumentNullException(nameof(dest));
|
||||
if (rootPath == null)
|
||||
throw new ArgumentNullException(nameof(rootPath));
|
||||
if (filename == null)
|
||||
throw new ArgumentNullException(nameof(filename));
|
||||
|
||||
string normalizedDestRoot = Path.GetFullPath(dest)
|
||||
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
@@ -192,6 +155,18 @@ namespace AsarSharp.AsarFileSystem
|
||||
var patchedSizePickle = Pickle.CreateEmpty();
|
||||
patchedSizePickle.WriteUInt32((uint)patchedPickle.GetTotalSize());
|
||||
|
||||
// The rewrite lands on top of the placeholder header, so it must be exactly as
|
||||
// long. Placeholder hashes are the same width as real ones, so this holds unless
|
||||
// a file changed size between crawl and write - which would silently shred the
|
||||
// payload that follows.
|
||||
if (patchedPickle.GetTotalSize() != headerPickle.GetTotalSize() ||
|
||||
patchedSizePickle.GetTotalSize() != sizePickleSize)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"ASAR header changed size while packing (a source file was modified mid-build). " +
|
||||
"Aborting rather than writing a corrupt archive.");
|
||||
}
|
||||
|
||||
fs.Position = 0;
|
||||
patchedSizePickle.WriteTo(fs);
|
||||
patchedPickle.WriteTo(fs);
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace AsarSharp.AsarFileSystem
|
||||
_headerSize = headerSize;
|
||||
}
|
||||
|
||||
public FilesystemEntry SearchNodeFromDirectory(string p)
|
||||
public FilesystemEntry SearchNodeFromDirectory(string p, bool create = true)
|
||||
{
|
||||
FilesystemEntry json = _header;
|
||||
|
||||
@@ -59,12 +59,31 @@ namespace AsarSharp.AsarFileSystem
|
||||
string seg = p.Substring(start, segLen);
|
||||
|
||||
if (!json.IsDirectory)
|
||||
throw new Exception($"Unexpected directory state while traversing: {p}");
|
||||
{
|
||||
if (create)
|
||||
throw new Exception($"Unexpected directory state while traversing: {p}");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (json.Files == null)
|
||||
{
|
||||
if (create)
|
||||
json.Files = new Dictionary<string, FilesystemEntry>(StringComparer.Ordinal);
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!json.Files.TryGetValue(seg, out var child))
|
||||
{
|
||||
child = new FilesystemEntry { Files = new Dictionary<string, FilesystemEntry>(StringComparer.Ordinal) };
|
||||
json.Files[seg] = child;
|
||||
if (create)
|
||||
{
|
||||
child = new FilesystemEntry { Files = new Dictionary<string, FilesystemEntry>(StringComparer.Ordinal) };
|
||||
json.Files[seg] = child;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
json = child;
|
||||
start = end + 1;
|
||||
@@ -81,7 +100,7 @@ namespace AsarSharp.AsarFileSystem
|
||||
|
||||
string name = Path.GetFileName(rel);
|
||||
string dir = Extensions.GetDirectoryName(rel);
|
||||
var parent = SearchNodeFromDirectory(dir);
|
||||
var parent = SearchNodeFromDirectory(dir, true);
|
||||
|
||||
if (parent.Files == null)
|
||||
parent.Files = new Dictionary<string, FilesystemEntry>(StringComparer.Ordinal);
|
||||
@@ -111,18 +130,23 @@ namespace AsarSharp.AsarFileSystem
|
||||
}
|
||||
}
|
||||
|
||||
public FilesystemEntry GetNode(string p, bool followLinks = true)
|
||||
public FilesystemEntry GetNode(string p, bool followLinks = true, int linkDepth = 0)
|
||||
{
|
||||
if (linkDepth > 40)
|
||||
throw new Exception($"Symlink loop detected at {p}");
|
||||
|
||||
p = p.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
|
||||
FilesystemEntry node = SearchNodeFromDirectory(Extensions.GetDirectoryName(p));
|
||||
FilesystemEntry node = SearchNodeFromDirectory(Extensions.GetDirectoryName(p), false);
|
||||
if (node == null)
|
||||
return null;
|
||||
string name = Path.GetFileName(p);
|
||||
|
||||
if (node.IsLink && followLinks)
|
||||
return GetNode(Path.Combine(node.Link, name));
|
||||
return GetNode(Path.Combine(node.Link, name), followLinks, linkDepth + 1);
|
||||
|
||||
if (!string.IsNullOrEmpty(name))
|
||||
{
|
||||
if (node.IsDirectory && node.Files.TryGetValue(name, out var entry))
|
||||
if (node.IsDirectory && node.Files != null && node.Files.TryGetValue(name, out var entry))
|
||||
return entry;
|
||||
return null;
|
||||
}
|
||||
@@ -130,16 +154,17 @@ namespace AsarSharp.AsarFileSystem
|
||||
return node;
|
||||
}
|
||||
|
||||
public FilesystemEntry GetFile(string p, bool followLinks = true)
|
||||
public FilesystemEntry GetFile(string p, bool followLinks = true, int linkDepth = 0)
|
||||
{
|
||||
FilesystemEntry info = GetNode(p, followLinks);
|
||||
if (linkDepth > 40)
|
||||
throw new Exception($"Symlink loop detected at {p}");
|
||||
|
||||
FilesystemEntry info = GetNode(p, followLinks, linkDepth);
|
||||
if (info == null) throw new Exception($"\"{p}\" was not found in this archive");
|
||||
if (info.IsLink && followLinks) return GetFile(info.Link, followLinks);
|
||||
if (info.IsLink && followLinks) return GetFile(info.Link, followLinks, linkDepth + 1);
|
||||
return info;
|
||||
}
|
||||
|
||||
public static string ReadLink(string path) => throw new NotImplementedException();
|
||||
|
||||
#region Writing
|
||||
|
||||
public FilesystemEntry SearchNodeFromPath(string p)
|
||||
@@ -159,7 +184,7 @@ namespace AsarSharp.AsarFileSystem
|
||||
public void InsertFile(string path, bool shouldUnpack, CrawledFileType file,
|
||||
IntegrityHelper.FileIntegrity precomputedIntegrity = null)
|
||||
{
|
||||
var (dirNode, _) = SearchNodeFromPathWithParent(Path.GetDirectoryName(path) ?? path);
|
||||
var (dirNode, _) = SearchNodeFromPathWithParent(path);
|
||||
var node = SearchNodeFromPath(path);
|
||||
|
||||
long size;
|
||||
|
||||
@@ -9,13 +9,6 @@ namespace AsarSharp.AsarFileSystem
|
||||
{
|
||||
public FileType Type { get; set; }
|
||||
public FileSystemInfo Stat { get; set; }
|
||||
public TransformedFile Transformed { get; set; }
|
||||
}
|
||||
|
||||
public class TransformedFile
|
||||
{
|
||||
public string Path { get; set; }
|
||||
public FileSystemInfo Stat { get; set; }
|
||||
}
|
||||
|
||||
public enum FileType
|
||||
@@ -36,7 +29,7 @@ namespace AsarSharp.AsarFileSystem
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
throw new IOException($"Failed to read attributes for '{filename}'", ex);
|
||||
}
|
||||
|
||||
bool isDirectory = (attributes & FileAttributes.Directory) == FileAttributes.Directory;
|
||||
@@ -59,7 +52,6 @@ namespace AsarSharp.AsarFileSystem
|
||||
foreach (var fullPath in CrawlIterative(dir))
|
||||
{
|
||||
var type = DetermineFileType(fullPath);
|
||||
if (type == null) continue;
|
||||
metadata[fullPath] = type;
|
||||
if (type.Type == FileType.Link) links.Add(fullPath);
|
||||
filenames.Add(fullPath);
|
||||
@@ -77,7 +69,8 @@ namespace AsarSharp.AsarFileSystem
|
||||
{
|
||||
if (string.Equals(filename, link, StringComparison.OrdinalIgnoreCase)) continue;
|
||||
|
||||
if (filename.StartsWith(link, StringComparison.OrdinalIgnoreCase))
|
||||
// Require a separator after the prefix so "…/foobar" does not match link "…/foo".
|
||||
if (filename.StartsWith(link + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string rel = Extensions.GetRelativePath(link, fileDir);
|
||||
if (!rel.StartsWith("..", StringComparison.Ordinal))
|
||||
@@ -120,7 +113,7 @@ namespace AsarSharp.AsarFileSystem
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
result.Add(entry.FullName);
|
||||
if (entry is DirectoryInfo subDir)
|
||||
if (entry is DirectoryInfo subDir && (subDir.Attributes & FileAttributes.ReparsePoint) == 0)
|
||||
stack.Push(subDir);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user