59 lines
3.5 KiB
C#
59 lines
3.5 KiB
C#
using System.IO.Compression;
|
|
using System.Security.Cryptography;
|
|
using System.Text.Json;
|
|
using MsPrivate.Launcher;
|
|
|
|
var passed = 0;
|
|
void Assert(bool condition, string message) { if (!condition) throw new Exception(message); passed++; }
|
|
byte[] CreateArchive(string path, byte[] content, string? declaredPath = null, string? declaredHash = null, bool extra = false)
|
|
{
|
|
var manifest = new PayloadManifest
|
|
{
|
|
Schema = 1,
|
|
Version = "test",
|
|
EntryPoint = declaredPath ?? path,
|
|
Files = [new PayloadFile { Path = declaredPath ?? path, Length = content.Length, Sha256 = declaredHash ?? Convert.ToHexString(SHA256.HashData(content)).ToLowerInvariant() }]
|
|
};
|
|
using var memory = new MemoryStream();
|
|
using (var archive = new ZipArchive(memory, ZipArchiveMode.Create, leaveOpen: true))
|
|
{
|
|
var file = archive.CreateEntry(path);
|
|
using (var output = file.Open()) output.Write(content);
|
|
var metadata = archive.CreateEntry(PayloadInstaller.ManifestName);
|
|
using (var output = metadata.Open()) JsonSerializer.Serialize(output, manifest);
|
|
if (extra) archive.CreateEntry("undeclared.txt");
|
|
}
|
|
return memory.ToArray();
|
|
}
|
|
|
|
var root = Path.Combine(Path.GetTempPath(), "ms-private-launcher-tests-" + Guid.NewGuid().ToString("N"));
|
|
try
|
|
{
|
|
var content = "Write-Output test"u8.ToArray();
|
|
var valid = CreateArchive("ms-private-GUI.ps1", content);
|
|
var manifest = PayloadInstaller.ReadAndValidateArchive(valid);
|
|
Assert(manifest.Files.Count == 1, "Valid embedded archive was rejected.");
|
|
var install = Path.Combine(root, "runtime");
|
|
PayloadInstaller.EnsureInstalled(valid, install);
|
|
Assert(PayloadInstaller.IsValidInstallation(install, manifest), "Installed payload did not verify.");
|
|
File.WriteAllText(Path.Combine(install, "ms-private-GUI.ps1"), "tampered");
|
|
PayloadInstaller.EnsureInstalled(valid, install);
|
|
Assert(PayloadInstaller.IsValidInstallation(install, manifest), "Tampered installed payload was not repaired.");
|
|
File.WriteAllText(Path.Combine(install, "unexpected.ps1"), "not declared");
|
|
Assert(!PayloadInstaller.IsValidInstallation(install, manifest), "Unexpected installed file was accepted.");
|
|
PayloadInstaller.EnsureInstalled(valid, install);
|
|
Assert(PayloadInstaller.IsValidInstallation(install, manifest), "Installation with an unexpected file was not repaired.");
|
|
File.WriteAllText(Path.Combine(install, PayloadInstaller.ManifestName), "{}");
|
|
Assert(!PayloadInstaller.IsValidInstallation(install, manifest), "Modified installed manifest was accepted.");
|
|
PayloadInstaller.EnsureInstalled(valid, install);
|
|
Assert(PayloadInstaller.IsValidInstallation(install, manifest), "Modified installed manifest was not repaired.");
|
|
try { PayloadInstaller.ReadAndValidateArchive(CreateArchive("safe.ps1", content, "../escape.ps1")); throw new Exception("Traversal manifest was accepted."); } catch (InvalidDataException) { passed++; }
|
|
try { PayloadInstaller.ReadAndValidateArchive(CreateArchive("ms-private-GUI.ps1", content, declaredHash: new string('0', 64))); throw new Exception("Bad hash was accepted."); } catch (InvalidDataException) { passed++; }
|
|
try { PayloadInstaller.ReadAndValidateArchive(CreateArchive("ms-private-GUI.ps1", content, extra: true)); throw new Exception("Undeclared archive entry was accepted."); } catch (InvalidDataException) { passed++; }
|
|
}
|
|
finally
|
|
{
|
|
if (Directory.Exists(root)) Directory.Delete(root, recursive: true);
|
|
}
|
|
Console.WriteLine($"{passed} native launcher tests passed.");
|