fix(script-analysis): key the trust verdict cache on every verdict input (CMN-5, WP1 task 1.3)
TrustVerdictCache keyed on the code hash alone while the verdict also depends on the analysis reference set (and its degraded-fallback mode). The key is now (code SHA-256, degraded flag, base-reference fingerprint, extra-reference digest), length-prefixed. File-backed extra references are digested (path/size/mtime/kind/aliases/embed) so the Central UI run gate's calls are cached under their own key; an extra reference with no stable identity stays uncacheable. The old extras test passed a PortableExecutableReference[] positionally, which binds to the explicit-base overload, so it never exercised the extras path; the tests now use the named argument.
This commit is contained in:
@@ -36,22 +36,75 @@ public class ScriptTrustValidatorCacheTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that a call with extra references is never served from (or stored in)
|
||||
/// the cache — extra references can change symbol resolution, so the verdict is a
|
||||
/// pure function of the code only for the default reference set.
|
||||
/// Code review 2026-12-02 CMN-5: the extra references are part of the cache key, so a
|
||||
/// call with extras is never served the no-extras verdict (or vice versa), while a
|
||||
/// repeat call with the same file-backed extras is a cache hit.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void FindViolations_WithExtraReferences_BypassesCache()
|
||||
public void FindViolations_WithExtraReferences_KeyedSeparatelyFromNoExtras()
|
||||
{
|
||||
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
|
||||
var extra = new[] { Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location) };
|
||||
var a = ScriptTrustValidator.FindViolations(code, extra);
|
||||
var b = ScriptTrustValidator.FindViolations(code, extra);
|
||||
// Named argument: a PortableExecutableReference[] positional argument binds to the
|
||||
// explicit-base-references overload instead (the pre-CMN-5 version of this test did
|
||||
// exactly that, so it never exercised the extras path).
|
||||
var a = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
||||
var b = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
||||
var cached = ScriptTrustValidator.FindViolations(code);
|
||||
|
||||
Assert.Same(a, b); // same extras → same key
|
||||
Assert.NotSame(a, cached); // extras vs none → different keys
|
||||
Assert.NotEmpty(a);
|
||||
Assert.NotEmpty(cached);
|
||||
}
|
||||
|
||||
/// <summary>An extra reference with no stable identity (an in-memory image) is never cached.</summary>
|
||||
[Fact]
|
||||
public void FindViolations_WithInMemoryExtraReference_BypassesCache()
|
||||
{
|
||||
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
|
||||
var image = File.ReadAllBytes(typeof(ScriptCompileSurface).Assembly.Location);
|
||||
var extra = new[] { Microsoft.CodeAnalysis.MetadataReference.CreateFromImage(image) };
|
||||
|
||||
Assert.Null(ScriptTrustValidator.DigestExtraReferences(extra));
|
||||
var a = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
||||
var b = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
|
||||
Assert.NotSame(a, b);
|
||||
Assert.NotSame(a, cached);
|
||||
Assert.NotSame(b, cached);
|
||||
}
|
||||
|
||||
/// <summary>Distinct extra-reference sets digest differently; the same set digests the same.</summary>
|
||||
[Fact]
|
||||
public void DigestExtraReferences_DistinguishesSets()
|
||||
{
|
||||
var surface = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location);
|
||||
var surfaceAgain = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location);
|
||||
var linq = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).Assembly.Location);
|
||||
var aliased = surface.WithAliases(["other"]);
|
||||
|
||||
var one = ScriptTrustValidator.DigestExtraReferences([surface]);
|
||||
Assert.NotNull(one);
|
||||
Assert.Equal(one, ScriptTrustValidator.DigestExtraReferences([surfaceAgain]));
|
||||
Assert.NotEqual(one, ScriptTrustValidator.DigestExtraReferences([surface, linq]));
|
||||
Assert.NotEqual(one, ScriptTrustValidator.DigestExtraReferences([aliased]));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The key carries every verdict input: code, the degraded flag, the base reference
|
||||
/// fingerprint and the extras digest. Changing any one of them changes the key.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void BuildKey_ChangesWithEveryInput()
|
||||
{
|
||||
var baseKey = TrustVerdictCache.BuildKey("code", degraded: false, "fp", "");
|
||||
Assert.Equal(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp", ""));
|
||||
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code2", degraded: false, "fp", ""));
|
||||
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: true, "fp", ""));
|
||||
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp2", ""));
|
||||
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp", "x"));
|
||||
// Length-prefixing: moving characters between components cannot collide.
|
||||
Assert.NotEqual(
|
||||
TrustVerdictCache.BuildKey("code", degraded: false, "ab", "c"),
|
||||
TrustVerdictCache.BuildKey("code", degraded: false, "a", "bc"));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that the shared verdict cannot be mutated by a caller to poison the cache.</summary>
|
||||
|
||||
Reference in New Issue
Block a user