tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
Trust an unpacked marker only when it names one directory inside
- Commit
e8304ebb20d4843abfc2e1b043f59648cd548c2b- Author
- Leitwolf11 ·
- Parents
0647c27f7ae9
Changes
.github/workflows/ci.yml | 5 ++- Directory.Packages.props | 1 + src/Progmasoft.Tazel/Archive/ModuleArchive.cs | 25 ++++++++++++++ src/Progmasoft.Tazel/Project/LocalModules.cs | 9 ++--- src/Progmasoft.Tazel/Project/Plugins.cs | 7 ++-- src/Progmasoft.Tazel/Project/PortalDependencies.cs | 7 ++-- tests/Progmasoft.Tazel.Tests/MarkerTests.cs | 40 ++++++++++++++++++++++ .../Progmasoft.Tazel.Tests.csproj | 1 + .../Progmasoft.Tazel.Tests/QueryAndPortalTests.cs | 10 +++--- 9 files changed, 86 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f6ce205..fd7fa26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -99,7 +99,10 @@ jobs: run: dotnet build Tazel.slnx -c Release - name: Test - run: dotnet test --solution Tazel.slnx -c Release --no-build --coverlet --coverlet-output-format cobertura --results-directory artifacts/coverage + # A test that hangs for 15 minutes is dumped and named, instead of the job running into its timeout unnamed. + run: >- + dotnet test --solution Tazel.slnx -c Release --no-build --coverlet --coverlet-output-format cobertura + --results-directory artifacts/coverage --hangdump --hangdump-timeout 15m --hangdump-type Mini - name: Run the built Tazel run: dotnet src/Progmasoft.Tazel/bin/Release/net10.0/tazel.dll version diff --git a/Directory.Packages.props b/Directory.Packages.props index 284c1f6..c015251 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -15,6 +15,7 @@ <PackageVersion Include="CsCheck" Version="5.0.1" /> <PackageVersion Include="Snapshooter.Xunit3" Version="1.3.1" /> <PackageVersion Include="coverlet.MTP" Version="10.1.0" /> + <PackageVersion Include="Microsoft.Testing.Extensions.HangDump" Version="2.5.3" /> <PackageVersion Include="BenchmarkDotNet" Version="0.15.8" /> <PackageVersion Include="Microsoft.Build.Utilities.Core" Version="18.0.2" /> <PackageVersion Include="SharpFuzz" Version="2.3.0" /> diff --git a/src/Progmasoft.Tazel/Archive/ModuleArchive.cs b/src/Progmasoft.Tazel/Archive/ModuleArchive.cs index a5957a7..a68c21d 100644 --- a/src/Progmasoft.Tazel/Archive/ModuleArchive.cs +++ b/src/Progmasoft.Tazel/Archive/ModuleArchive.cs @@ -78,6 +78,31 @@ internal static class ModuleArchive public static void Write(string root, string baseName, string archive, IReadOnlyList<string> files) => ReproducibleArchive.Write(root, baseName, archive, files); + /// <summary> + /// The directory a <c>.unpacked</c> marker below <paramref name="destination"/> names, or null when there is no + /// marker. The marker holds one directory name that Unpack made; anything else, such as a path that leads out of the + /// destination, means the state was altered, and the unpacked files are not trusted. + /// </summary> + public static string? FromMarker(string destination) + { + ArgumentNullException.ThrowIfNull(destination); + var marker = Path.Combine(destination, ".unpacked"); + if (!System.IO.File.Exists(marker)) + { + return null; + } + + var name = System.IO.File.ReadAllText(marker).Trim(); + var directory = Path.Combine(destination, Path.GetFileName(name)); + return name.Length > 0 && name == Path.GetFileName(name) && name is not ("." or "..") && Directory.Exists(directory) + ? directory + : throw new ProjectException($"{destination} is damaged; remove it and run the command again"); + } + + /// <summary>Writes the marker that names the unpacked directory of <paramref name="destination"/>.</summary> + public static void WriteMarker(string destination, string directory) => + System.IO.File.WriteAllText(Path.Combine(destination, ".unpacked"), Path.GetFileName(directory)); + /// <summary>An unpacked archive has one top directory, and the file it must hold in it.</summary> private static void CheckUnpacked(string work, [System.Diagnostics.CodeAnalysis.NotNull] string? top, string requiredFile, string shown) { diff --git a/src/Progmasoft.Tazel/Project/LocalModules.cs b/src/Progmasoft.Tazel/Project/LocalModules.cs index 9e8199d..a6ea09e 100644 --- a/src/Progmasoft.Tazel/Project/LocalModules.cs +++ b/src/Progmasoft.Tazel/Project/LocalModules.cs @@ -41,12 +41,9 @@ internal static class LocalModules } var destination = Path.Combine(mainRoot, ModuleFiles.StateDirectory, "external", $"{local.Name}-{actual[..16]}"); - var marker = Path.Combine(destination, ".unpacked"); - if (File.Exists(marker)) + if (ModuleArchive.FromMarker(destination) is { } unpacked) { - return File.ReadAllText(marker).Trim() is { Length: > 0 } top - ? Path.Combine(destination, top) - : throw new ProjectException($"{destination} is damaged; remove it"); + return unpacked; } if (Directory.Exists(destination)) @@ -55,7 +52,7 @@ internal static class LocalModules } var root = ModuleArchive.Unpack(archive, destination); - File.WriteAllText(marker, Path.GetFileName(root)); + ModuleArchive.WriteMarker(destination, root); return root; } diff --git a/src/Progmasoft.Tazel/Project/Plugins.cs b/src/Progmasoft.Tazel/Project/Plugins.cs index e25a768..2bcfda0 100644 --- a/src/Progmasoft.Tazel/Project/Plugins.cs +++ b/src/Progmasoft.Tazel/Project/Plugins.cs @@ -147,10 +147,9 @@ internal static class Plugins { var sha = Sha256Sums.Sha256(archive); var destination = Path.Combine(mainRoot, ModuleFiles.StateDirectory, "plugins", sha[..32]); - var marker = Path.Combine(destination, ".unpacked"); - if (File.Exists(marker)) + if (ModuleArchive.FromMarker(destination) is { } unpacked) { - return Path.Combine(destination, File.ReadAllText(marker).Trim()); + return unpacked; } if (Directory.Exists(destination)) @@ -159,7 +158,7 @@ internal static class Plugins } var directory = ModuleArchive.Unpack(archive, destination, ManifestFile); - File.WriteAllText(marker, Path.GetFileName(directory)); + ModuleArchive.WriteMarker(destination, directory); return directory; } diff --git a/src/Progmasoft.Tazel/Project/PortalDependencies.cs b/src/Progmasoft.Tazel/Project/PortalDependencies.cs index 328ba50..72390c0 100644 --- a/src/Progmasoft.Tazel/Project/PortalDependencies.cs +++ b/src/Progmasoft.Tazel/Project/PortalDependencies.cs @@ -195,10 +195,9 @@ internal static class PortalDependencies } var destination = Path.Combine(mainRoot, ModuleFiles.StateDirectory, "external", $"{module.BaseName}-{sha[..16]}"); - var marker = Path.Combine(destination, ".unpacked"); - if (File.Exists(marker)) + if (ModuleArchive.FromMarker(destination) is { } unpacked) { - return Path.Combine(destination, (await File.ReadAllTextAsync(marker, cancel).ConfigureAwait(false)).Trim()); + return unpacked; } if (Directory.Exists(destination)) @@ -207,7 +206,7 @@ internal static class PortalDependencies } var root = ModuleArchive.Unpack(archive, destination); - await File.WriteAllTextAsync(marker, Path.GetFileName(root), cancel).ConfigureAwait(false); + ModuleArchive.WriteMarker(destination, root); return root; } } diff --git a/tests/Progmasoft.Tazel.Tests/MarkerTests.cs b/tests/Progmasoft.Tazel.Tests/MarkerTests.cs new file mode 100644 index 0000000..3fa0df3 --- /dev/null +++ b/tests/Progmasoft.Tazel.Tests/MarkerTests.cs @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.IO; +using Progmasoft.Tazel.Archive; +using Progmasoft.Tazel.Project; +using Xunit; + +namespace Progmasoft.Tazel.Tests; + +public sealed class MarkerTests +{ + [Fact] + public void AMarkerNamesTheDirectoryItWroteAndNothingElse() + { + using var state = new TemporaryModule(); + var destination = Path.Combine(state.Root, "external", "dep"); + Assert.Null(ModuleArchive.FromMarker(destination)); + Directory.CreateDirectory(Path.Combine(destination, "com.example.dep-1.0.0")); + ModuleArchive.WriteMarker(destination, Path.Combine(destination, "com.example.dep-1.0.0")); + Assert.Equal(Path.Combine(destination, "com.example.dep-1.0.0"), ModuleArchive.FromMarker(destination)); + } + + [Theory] + [InlineData("../../outside")] + [InlineData("..")] + [InlineData("")] + [InlineData("missing")] + public void AnAlteredMarkerIsNotTrusted(string written) + { + using var state = new TemporaryModule(); + var destination = Path.Combine(state.Root, "external", "dep"); + Directory.CreateDirectory(destination); + Directory.CreateDirectory(Path.Combine(state.Root, "outside")); + File.WriteAllText(Path.Combine(destination, ".unpacked"), written); + var error = Assert.Throws<ProjectException>(() => ModuleArchive.FromMarker(destination)); + Assert.Contains("is damaged", error.Message, StringComparison.Ordinal); + } +} diff --git a/tests/Progmasoft.Tazel.Tests/Progmasoft.Tazel.Tests.csproj b/tests/Progmasoft.Tazel.Tests/Progmasoft.Tazel.Tests.csproj index b1bec4c..86f0edd 100644 --- a/tests/Progmasoft.Tazel.Tests/Progmasoft.Tazel.Tests.csproj +++ b/tests/Progmasoft.Tazel.Tests/Progmasoft.Tazel.Tests.csproj @@ -19,5 +19,6 @@ <PackageReference Include="CsCheck" /> <PackageReference Include="Snapshooter.Xunit3" /> <PackageReference Include="coverlet.MTP" /> + <PackageReference Include="Microsoft.Testing.Extensions.HangDump" /> </ItemGroup> </Project> diff --git a/tests/Progmasoft.Tazel.Tests/QueryAndPortalTests.cs b/tests/Progmasoft.Tazel.Tests/QueryAndPortalTests.cs index e3abc2d..80d2a2d 100644 --- a/tests/Progmasoft.Tazel.Tests/QueryAndPortalTests.cs +++ b/tests/Progmasoft.Tazel.Tests/QueryAndPortalTests.cs @@ -246,10 +246,12 @@ internal sealed class FakePortal : IDisposable return; } - // The portal serves its own directory and nothing above it. - var root = Path.GetFullPath(Directory) + Path.DirectorySeparatorChar; - var file = Path.GetFullPath(Path.Combine(root, context.Request.Url!.AbsolutePath.TrimStart('/'))); - if (file.StartsWith(root, StringComparison.Ordinal) && File.Exists(file)) + // The portal serves the files of its own directory, found by listing it: what a request asks for is only + // compared with them, never made into a path. + var wanted = context.Request.Url!.AbsolutePath.TrimStart('/'); + var file = System.IO.Directory.EnumerateFiles(Directory, "*", SearchOption.AllDirectories) + .FirstOrDefault(path => Path.GetRelativePath(Directory, path).Replace('\\', '/') == wanted); + if (file is not null) { var bytes = await File.ReadAllBytesAsync(file); context.Response.StatusCode = 200;