StoreFileName: close review findings (collisions, case, device stems, length, renderId)

- Pass-through ids may not end in "-<16 hex>", so a rewritten name fed back as an id cannot alias it; hash widened 32 -> 64 bits.
- Ids with uppercase are rewritten (case-insensitive filesystems).
- Windows device stems (CON PRN AUX NUL COM0-9 LPT0-9, any extension) are rewritten.
- Stem capped at 100 chars; longer ids carry the hash.
- Daemon samples/ write now goes through StoreFileName.From(renderId).
- Tests: rewritten-as-id, case-only, device stems, overlong; daemon test uses a compiling patch so the samples/ write is reached when Faust is present.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Aqua 2026-11-06 20:00:30 +00:00
commit abfc816804
3 changed files with 109 additions and 16 deletions

View file

@ -5,12 +5,19 @@ namespace AquaSynth.Dsl;
/// <summary>
/// The one mapping from a caller-chosen id to a single file-name component in a store.
/// Ids already within [A-Za-z0-9._-] pass through unchanged. Anything else is sanitised and
/// suffixed with the first 8 hex of SHA-256 of the original id, so distinct ids stay distinct.
/// The result is never a path: no separators, never "." or "..".
/// An id passes through unchanged only if it is already a fixed point: ASCII [A-Za-z0-9._-],
/// all lowercase (stores live on case-insensitive filesystems), at most <see cref="MaxStem"/> long,
/// no trailing dot, not a Windows device stem (CON, NUL, COM1, ...), and not ending in a
/// "-" plus 16 hex digits. Every other id is sanitised and suffixed with "-" plus the first
/// 16 hex of SHA-256 of the original id. Passed-through names never end in the hash shape and
/// rewritten names always do, so the two sets cannot meet; distinct ids stay distinct up to a 64-bit
/// hash. The result is never a path: no separators, never "." or "..".
/// </summary>
public static class StoreFileName
{
private const int MaxStem = 100;
private const int HashLength = 16;
public static string From(string id)
{
ArgumentNullException.ThrowIfNull(id);
@ -19,20 +26,56 @@ public static class StoreFileName
throw new ArgumentException($"Id '{id}' cannot name a store file.", nameof(id));
}
var builder = new StringBuilder(id.Length);
foreach (var c in id)
var builder = new StringBuilder(Math.Min(id.Length, MaxStem));
foreach (var c in id.Take(MaxStem))
{
builder.Append(c is >= 'a' and <= 'z' or >= 'A' and <= 'Z' or >= '0' and <= '9' or '-' or '_' or '.' ? c : '_');
}
var safe = builder.ToString();
// A trailing dot is dropped by Windows, so "a." would alias "a"; hash it like any other rewrite.
if (safe == id && !id.EndsWith('.'))
if (IsFixedPoint(id, safe))
{
return safe;
}
var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(id)))[..8].ToLowerInvariant();
var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(id)))[..HashLength].ToLowerInvariant();
return $"{safe}-{hash}";
}
private static bool IsFixedPoint(string id, string safe)
{
if (safe != id || id.EndsWith('.') || id.Any(char.IsAsciiLetterUpper) || IsDeviceStem(id) || EndsWithHashShape(id))
{
return false;
}
return true;
}
// Windows resolves CON, PRN, AUX, NUL, COM0-9, LPT0-9 as devices whatever the extension.
private static bool IsDeviceStem(string id)
{
var dot = id.IndexOf('.');
var stem = (dot < 0 ? id : id[..dot]).TrimEnd(' ');
return stem.Length switch
{
3 => stem.Equals("con", StringComparison.OrdinalIgnoreCase)
|| stem.Equals("prn", StringComparison.OrdinalIgnoreCase)
|| stem.Equals("aux", StringComparison.OrdinalIgnoreCase)
|| stem.Equals("nul", StringComparison.OrdinalIgnoreCase),
4 => char.IsAsciiDigit(stem[3])
&& (stem.StartsWith("com", StringComparison.OrdinalIgnoreCase) || stem.StartsWith("lpt", StringComparison.OrdinalIgnoreCase)),
_ => false,
};
}
private static bool EndsWithHashShape(string id)
{
if (id.Length <= HashLength || id[^(HashLength + 1)] != '-')
{
return false;
}
return id[^HashLength..].All(c => c is >= '0' and <= '9' or >= 'a' and <= 'f');
}
}