Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

Name the files of the script cache only from a key

A file of .tazel/scripts/ is named only through one helper that accepts
a key alone (64 lower-case hexadecimal digits, what ScriptCache.Key
returns) and takes the file name with Path.GetFileName; anything else is
an ArgumentException. Codacy (Semgrep unsafe-path-combine) pointed at
the joins.
Commit
4c5bbf13100e406f5c74d4062589477e26431dad
Author
Leitwolf11 ·
Parents
6470ee59ac08

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Project/ScriptCache.cs      | 19 +++++++++++++++----
 tests/Progmasoft.Tazel.Tests/ScriptCacheTests.cs | 18 +++++++++++++++---
 2 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/src/Progmasoft.Tazel/Project/ScriptCache.cs b/src/Progmasoft.Tazel/Project/ScriptCache.cs
index 941c09c..45914a6 100644
--- a/src/Progmasoft.Tazel/Project/ScriptCache.cs
+++ b/src/Progmasoft.Tazel/Project/ScriptCache.cs
@@ -69,15 +69,26 @@ internal static class ScriptCache
         return Convert.ToHexStringLower(hash.GetHashAndReset());
     }
 
+    // A file of the cache directory: a key is the hexadecimal SHA-256 that Key returns, so its name is never a path.
+    private static string FileOf(string directory, string key, string suffix)
+    {
+        if (key.Length != 64 || !key.All(char.IsAsciiHexDigitLower))
+        {
+            throw new ArgumentException($"'{key}' is not a key of the script cache", nameof(key));
+        }
+
+        return Path.Combine(directory, Path.GetFileName(key + suffix));
+    }
+
     /// <summary>Whether <paramref name="directory"/> keeps the compiled file named <paramref name="key"/>.</summary>
-    public static bool IsKept(string directory, string key) => File.Exists(Path.Combine(directory, key + ".dll"));
+    public static bool IsKept(string directory, string key) => File.Exists(FileOf(directory, key, ".dll"));
 
     /// <summary>The compiled file named <paramref name="key"/> in <paramref name="directory"/>, or null.</summary>
     public static byte[]? TryRead(string directory, string key)
     {
         try
         {
-            var file = Path.Combine(directory, key + ".dll");
+            var file = FileOf(directory, key, ".dll");
             return File.Exists(file) && new FileInfo(file).LinkTarget is null ? File.ReadAllBytes(file) : null;
         }
         catch (Exception error) when (error is IOException or UnauthorizedAccessException)
@@ -92,12 +103,12 @@ internal static class ScriptCache
     /// </summary>
     public static void TryWrite(string directory, string key, byte[] image)
     {
-        var partial = Path.Combine(directory, $"{key}.{Guid.NewGuid():N}.partial");
+        var partial = FileOf(directory, key, $".{Guid.NewGuid():N}.partial");
         try
         {
             System.IO.Directory.CreateDirectory(directory);
             File.WriteAllBytes(partial, image);
-            File.Move(partial, Path.Combine(directory, key + ".dll"), overwrite: true);
+            File.Move(partial, FileOf(directory, key, ".dll"), overwrite: true);
         }
         catch (Exception error) when (error is IOException or UnauthorizedAccessException)
         {
diff --git a/tests/Progmasoft.Tazel.Tests/ScriptCacheTests.cs b/tests/Progmasoft.Tazel.Tests/ScriptCacheTests.cs
index 4bd2473..7d5a9d7 100644
--- a/tests/Progmasoft.Tazel.Tests/ScriptCacheTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/ScriptCacheTests.cs
@@ -140,9 +140,21 @@ public sealed class ScriptCacheTests
         using var module = new TemporaryModule();
         var blocked = Path.Combine(module.Root, "file");
         File.WriteAllText(blocked, "");
-        ScriptCache.TryWrite(Path.Combine(blocked, "scripts"), "k", [1, 2, 3]);
-        Assert.Null(ScriptCache.TryRead(Path.Combine(blocked, "scripts"), "k"));
-        Assert.Null(ScriptCache.TryRead(module.Root, "missing"));
+        var key = new string('a', 64);
+        ScriptCache.TryWrite(Path.Combine(blocked, "scripts"), key, [1, 2, 3]);
+        Assert.Null(ScriptCache.TryRead(Path.Combine(blocked, "scripts"), key));
+        Assert.Null(ScriptCache.TryRead(module.Root, new string('b', 64)));
+    }
+
+    [Theory]
+    [InlineData("k")]
+    [InlineData("../../../../etc/passwd")]
+    [InlineData("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")]
+    public void OnlyAKeyNamesAFile(string key)
+    {
+        using var module = new TemporaryModule();
+        Assert.Throws<ArgumentException>(() => ScriptCache.IsKept(module.Root, key));
+        Assert.Throws<ArgumentException>(() => ScriptCache.TryWrite(module.Root, key, [1]));
     }
 
     /// <summary>The globals of the scripts above.</summary>