- Pure WebsitePathResolver (canonieke 301's, echte 404, dotfiles geweigerd, gereserveerde prefixes) - PrerenderedSiteMiddleware, 404-fallback na de API-routes, gzip, Cache-Control, NoIndex/robots.txt - Beleid StrictSite met eigen AllowedSite*Origins; volledige headerset op alle websiteresponses - Tests: voorbeelden, FsCheck-properties en integratietests op een in-memory server Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
146 lines
5.1 KiB
C#
146 lines
5.1 KiB
C#
using FsCheck;
|
|
using FsCheck.Fluent;
|
|
using SlpModularCms.Core.Hosting.Website;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace SlpModularCms.Core.Tests.Hosting.Website;
|
|
|
|
/// <summary>
|
|
/// Property-based tests for the pure resolver (PBT-03 invariants, idempotence).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A failing run prints FsCheck's replay seed in the failure message (PBT-08); once a shrunk
|
|
/// counterexample is understood it is added to <see cref="WebsitePathResolverTests"/> as a plain
|
|
/// example test so the regression stays pinned.
|
|
/// </remarks>
|
|
public class WebsitePathResolverPropertyTests(ITestOutputHelper output)
|
|
{
|
|
private static readonly string[] KnownFiles =
|
|
[
|
|
"index.html", "over-mij.html", "contact.html", "404.html", "robots.txt",
|
|
"assets/app.abc123.js", "blog/index.html", ".well-known/security.txt"
|
|
];
|
|
|
|
private static bool Exists(string candidate) => KnownFiles.Contains(candidate);
|
|
|
|
// PBT-07 generators: path-ish strings that stress slashes, dots, case, encoding and control characters.
|
|
private static readonly string[] Fragments =
|
|
[
|
|
"/", "/", "//", "\\", ".", "..", "...", ".git", ".well-known", "%2e", "%2E", "%2f", "%5c", "%",
|
|
"over-mij", "Over-Mij", "contact", "index", "Index", ".html", ".HTML", "404", "assets", "app.abc123.js",
|
|
"blog", "admin", "api", "v1", "health", "a", "é", "ß", "İ", "\0", "\n", " ", "?", "#", "~", "x"
|
|
];
|
|
|
|
private static Arbitrary<string> PathArb()
|
|
{
|
|
var gen = Gen.Elements(Fragments)
|
|
.ArrayOf()
|
|
.Select(parts => string.Concat(parts))
|
|
.Select(tail => "/" + tail);
|
|
|
|
// Also exercise inputs that do not start with a slash and the empty string.
|
|
return Gen.OneOf(gen, Gen.Elements("", "x", "..", "\\x").Select(s => s))
|
|
.ToArbitrary();
|
|
}
|
|
|
|
private static Arbitrary<string?> QueryArb() =>
|
|
Gen.Elements(null, "", "?", "?a=1", "?a=1&b=%2e%2e", "a=1", "?x\r\ny")
|
|
.ToArbitrary();
|
|
|
|
private void Check(Func<string, string?, bool> property)
|
|
{
|
|
var config = Config.QuickThrowOnFailure.WithMaxTest(2000);
|
|
|
|
Prop.ForAll(PathArb(), QueryArb(), (path, query) => property(path, query))
|
|
.Check(config);
|
|
|
|
output.WriteLine("Property held for 2000 generated inputs.");
|
|
}
|
|
|
|
[Fact]
|
|
public void Resolve_NeverThrows() =>
|
|
Check((path, query) =>
|
|
{
|
|
WebsitePathResolver.Resolve(path, query, Exists);
|
|
return true;
|
|
});
|
|
|
|
[Fact]
|
|
public void ServeFile_IsAlwaysASafeExistingRelativePath() =>
|
|
Check((path, query) =>
|
|
{
|
|
if (WebsitePathResolver.Resolve(path, query, Exists) is not WebsiteResolution.ServeFile serve)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var p = serve.RelativePath;
|
|
var segments = p.Split('/');
|
|
|
|
return Exists(p)
|
|
&& p.Length > 0
|
|
&& !p.StartsWith('/')
|
|
&& !p.Contains('\\')
|
|
&& !p.Contains('\0')
|
|
&& segments.All(s => s.Length > 0 && s != "." && s != "..")
|
|
&& segments.Skip(1).All(s => !s.StartsWith('.'))
|
|
&& (!segments[0].StartsWith('.') || segments[0] == ".well-known");
|
|
});
|
|
|
|
[Fact]
|
|
public void Redirect_LocationIsRelativeAndNeverAnOpenRedirect() =>
|
|
Check((path, query) =>
|
|
{
|
|
if (WebsitePathResolver.Resolve(path, query, Exists) is not WebsiteResolution.Redirect redirect)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var location = redirect.Location;
|
|
var pathPart = location.Split('?')[0];
|
|
|
|
return location.StartsWith('/')
|
|
&& !location.StartsWith("//", StringComparison.Ordinal)
|
|
&& !location.StartsWith("/\\", StringComparison.Ordinal)
|
|
&& !location.Any(c => c < 0x20 || c == 0x7f || c == '\\')
|
|
&& (pathPart == "/" || !pathPart.EndsWith('/'));
|
|
});
|
|
|
|
[Fact]
|
|
public void Redirect_TargetNeverRedirectsAgain() =>
|
|
Check((path, query) =>
|
|
{
|
|
if (WebsitePathResolver.Resolve(path, query, Exists) is not WebsiteResolution.Redirect first)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var split = first.Location.IndexOf('?');
|
|
var targetPath = split < 0 ? first.Location : first.Location[..split];
|
|
var targetQuery = split < 0 ? null : first.Location[split..];
|
|
|
|
return WebsitePathResolver.Resolve(targetPath, targetQuery, Exists) is not WebsiteResolution.Redirect;
|
|
});
|
|
|
|
[Fact]
|
|
public void ReservedPaths_AreNeverResolved() =>
|
|
Check((path, query) =>
|
|
!WebsitePathResolver.IsReservedPath(path)
|
|
|| WebsitePathResolver.Resolve(path, query, Exists) is WebsiteResolution.NotFound);
|
|
|
|
[Fact]
|
|
public void Resolve_DoesAtMostTwoFileChecks() =>
|
|
Check((path, query) =>
|
|
{
|
|
var calls = 0;
|
|
WebsitePathResolver.Resolve(path, query, c =>
|
|
{
|
|
calls++;
|
|
return Exists(c);
|
|
});
|
|
|
|
return calls <= 2;
|
|
});
|
|
}
|