From 10e03ad8e8385f4185ba10f626a032a7d0d2a999 Mon Sep 17 00:00:00 2001 From: Heimdall Date: Wed, 11 Nov 2026 14:12:09 +0000 Subject: [PATCH] Asset guard: hx- matched only as an attribute; source scan of every view, C# page builder, css and js for cross-origin assets (Google Fonts allowance explicit) Co-Authored-By: Claude Sonnet 5.5 --- .../NoThirdPartyAssetTests.cs | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tests/Bifrost.Web.Tests/NoThirdPartyAssetTests.cs b/tests/Bifrost.Web.Tests/NoThirdPartyAssetTests.cs index db9e774..5deb1a8 100644 --- a/tests/Bifrost.Web.Tests/NoThirdPartyAssetTests.cs +++ b/tests/Bifrost.Web.Tests/NoThirdPartyAssetTests.cs @@ -17,6 +17,71 @@ public sealed partial class NoThirdPartyAssetTests(TestWebApplicationFactory fac [GeneratedRegex(@"<(?:script|link)\b[^>]*\b(?:src|href)\s*=\s*[""']?\s*(?:https?:)?//(?!fonts\.(?:googleapis|gstatic)\.com)", RegexOptions.IgnoreCase)] private static partial Regex CrossOriginAsset(); + // An htmx remnant is an attribute (` hx-get="..."`); a bare "hx-" can occur inside a random token. + [GeneratedRegex(@"\shx-[a-z-]+\s*=", RegexOptions.IgnoreCase)] + private static partial Regex HtmxAttribute(); + + // Source-level: any element that fetches a resource (script/link/img/iframe/...) from an absolute or protocol-relative + // URL outside the Google Fonts allowance, in markup that may sit in C# strings (escaped or doubled quotes), plus CSS @import / url(). + [GeneratedRegex(@"<(?:script|link|img|iframe|frame|source|embed|object|video|audio|input)\b[^>]*?\b(?:src|href|data|srcset)\s*=\s*(?:\\|"")?[""']?\s*(?:https?:)?//(?!fonts\.(?:googleapis|gstatic)\.com)|@import\s+(?:url\()?\s*(?:\\|"")?[""']?\s*(?:https?:)?//(?!fonts\.(?:googleapis|gstatic)\.com)|\burl\(\s*[""']?\s*(?:https?:)?//(?!fonts\.(?:googleapis|gstatic)\.com)", RegexOptions.IgnoreCase | RegexOptions.Singleline)] + private static partial Regex CrossOriginSource(); + + private static string RepoRoot() + { + var dir = new DirectoryInfo(AppContext.BaseDirectory); + while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "Bifrost.slnx"))) + { + dir = dir.Parent; + } + + return dir?.FullName ?? throw new InvalidOperationException("Bifrost.slnx not found above the test binary."); + } + + // Every file that can emit HTML or CSS: all .cshtml, all .cs under src (the C# page builders), and the site's own css/js. + private static IEnumerable MarkupSources(string root) + { + var src = Path.Combine(root, "src", "Bifrost.Web"); + var sep = Path.DirectorySeparatorChar; + return Directory.EnumerateFiles(src, "*", SearchOption.AllDirectories) + .Where(f => !f.Contains($"{sep}bin{sep}") && !f.Contains($"{sep}obj{sep}") && !f.Contains($"{sep}wwwroot{sep}lib{sep}")) + .Where(f => f.EndsWith(".cshtml") || f.EndsWith(".cs") || f.EndsWith(".css") || f.EndsWith(".js") || f.EndsWith(".html")); + } + + [Fact] + public void No_source_that_emits_markup_loads_a_cross_origin_asset() + { + var files = MarkupSources(RepoRoot()).ToList(); + Assert.True(files.Count(f => f.EndsWith(".cshtml")) >= 10, "the scan found too few views; the root is wrong"); + Assert.Contains(files, f => f.EndsWith("HeimdallCompletePage.cs")); + Assert.Contains(files, f => f.EndsWith(Path.Combine("Membership", "Status.cshtml"))); + + var offenders = files.Where(f => CrossOriginSource().IsMatch(File.ReadAllText(f))).ToList(); + Assert.True(offenders.Count == 0, "cross-origin asset in: " + string.Join(", ", offenders)); + Assert.DoesNotContain(files, f => HtmxAttribute().IsMatch(File.ReadAllText(f))); + } + + [Theory] + [InlineData("", true)] + [InlineData("", true)] + [InlineData("html.Append(\"", false)] + [InlineData("repo", false)] + public void The_source_pattern_catches_cross_origin_assets_and_allows_same_origin_and_fonts(string markup, bool flagged) => + Assert.Equal(flagged, CrossOriginSource().IsMatch(markup)); + + [Theory] + [InlineData("
", true)] + [InlineData("
", true)] + [InlineData("token=ab-hx-9f3", false)] + public void Htmx_is_matched_only_as_an_attribute(string markup, bool flagged) => + Assert.Equal(flagged, HtmxAttribute().IsMatch(markup)); + [Fact] public async Task Real_pages_load_no_third_party_asset() { @@ -60,7 +125,7 @@ public sealed partial class NoThirdPartyAssetTests(TestWebApplicationFactory fac Assert.False(string.IsNullOrWhiteSpace(html), path); Assert.DoesNotContain("unpkg.com", html, StringComparison.OrdinalIgnoreCase); Assert.DoesNotContain("htmx", html, StringComparison.OrdinalIgnoreCase); - Assert.DoesNotContain("hx-", html, StringComparison.OrdinalIgnoreCase); + Assert.False(HtmxAttribute().IsMatch(html), $"{path} carries an hx- attribute"); Assert.False(CrossOriginAsset().IsMatch(html), $"{path} loads a cross-origin asset"); } }