diff --git a/.github/workflows/generate-docs.yml b/.github/workflows/generate-docs.yml index e3617ef8..9e2c06c4 100644 --- a/.github/workflows/generate-docs.yml +++ b/.github/workflows/generate-docs.yml @@ -28,7 +28,7 @@ jobs: - name: Setup .NET SDK uses: actions/setup-dotnet@v6 with: - dotnet-version: 10.x + dotnet-version: 11.x - name: Install DocFX run: dotnet tool install -g docfx diff --git a/.github/workflows/unittest-verification.yml b/.github/workflows/unittest-verification.yml index f0f640db..f2e8f8ee 100644 --- a/.github/workflows/unittest-verification.yml +++ b/.github/workflows/unittest-verification.yml @@ -20,7 +20,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v6 with: - dotnet-version: 10.x + dotnet-version: 11.x - name: Restore dependencies run: dotnet restore ./WebExpress.WebCore.slnx diff --git a/docs/user-guide.md b/docs/user-guide.md index d6ce969c..a7fdf345 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -16,6 +16,17 @@ To get started with `WebExpress.WebCore`, use the following guides: We hope you enjoy using `WebExpress.WebCore` and find it valuable for your projects. Happy coding! +## Access to endpoints +Access is denied unless it is granted. Every request to a page, REST API, resource or socket is checked against the policies of its endpoint: + +- An endpoint without a policy requires a signed-in caller. It used to be served to anybody, so an endpoint that forgot its policy was public. +- `[Policy]` makes an endpoint public, for example a login page or the API it signs in through. A class implementing `IPublicEndpoint` is public as well; the interface is inherited, which a base class such as the login page of an application template needs, since attributes are read from the concrete class only. +- `[Policy]` requires a signed-in caller; any other policy requires a caller whose groups grant it. +- A policy that cannot be created denies access. +- Assets (scripts, styles, images of the framework and plugins) are public. + +`WebExpress:Security:PublicByDefault` set to `true` serves endpoints without a policy to everybody again. It is meant only for migrating an application whose public endpoints are not yet marked. + ## Health checks The container health endpoint is documented in the [Health model](https://github.com/webexpress-framework/WebExpress/blob/main/docs/development_guide.md#health-model) section of the Development Guide. WebCore provides `/health` globally, and applications contribute critical dependency checks as public sealed `IHealth` components in `WebExpress.WebCore.WebHealth`. The `HealthManager` discovers these components through the plugin and application lifecycle. The guide includes the component model, a database example, the HTTP contract, and Docker and Kubernetes probe configuration. diff --git a/src/WebExpress.WebCore.Test/Html/Parser/UnitTestHtmlParserLimits.cs b/src/WebExpress.WebCore.Test/Html/Parser/UnitTestHtmlParserLimits.cs new file mode 100644 index 00000000..b00847b9 --- /dev/null +++ b/src/WebExpress.WebCore.Test/Html/Parser/UnitTestHtmlParserLimits.cs @@ -0,0 +1,118 @@ +using System.Diagnostics; +using System.Text; +using WebExpress.WebCore.WebHtml; +using WebExpress.WebCore.WebHtml.Parser; + +namespace WebExpress.WebCore.Test.Html.Parser +{ + /// + /// Tests that HTML from users cannot exhaust the stack or the processor of the server. A + /// stack overflow cannot be caught in .NET and ended the whole process. + /// + public class UnitTestHtmlParserLimits + { + private static readonly HtmlParser Parser = new(); + + /// + /// Verifies that deeply nested elements parse into a bounded tree that renders. + /// + [Fact] + public void DeepNestingIsBounded() + { + // arrange + var html = string.Concat(Enumerable.Repeat("
", 200_000)) + "deep" + string.Concat(Enumerable.Repeat("
", 200_000)); + + // act + var nodes = Parser.Parse(html); + var rendered = new StringBuilder(); + foreach (var node in nodes) + { + node.ToString(rendered, 0); + } + + // validation + Assert.True(Depth(nodes) <= HtmlParser.MaxNestingDepth + 1); + Assert.Contains("deep", rendered.ToString()); + } + + /// + /// Verifies that nesting below the limit keeps its structure. + /// + [Fact] + public void ShallowNestingKeepsItsStructure() + { + // act + var nodes = Parser.Parse("

x

"); + + // validation + var div = Assert.IsType(Assert.Single(nodes)); + var p = Assert.IsType(Assert.Single(div.Elements)); + Assert.Single(p.Elements); + } + + /// + /// Verifies that a stray end tag is ignored instead of ending the parse; it dropped the + /// rest of the document. + /// + [Fact] + public void StrayEndTagIsIgnored() + { + // act + var nodes = Parser.Parse("

a

b

"); + + // validation + Assert.Equal(2, nodes.OfType().Count()); + } + + /// + /// Verifies that an end tag closes the elements opened inside the element it names. + /// + [Fact] + public void EndTagClosesTheElementsInside() + { + // act + var nodes = Parser.Parse("
a

b

"); + + // validation + Assert.IsType(nodes[0]); + Assert.IsType(nodes[1]); + } + + /// + /// Verifies that a document full of unknown bang constructs is tokenized in linear time; + /// every "<!" searched the rest of the input for a doctype. + /// + [Fact] + public void BangConstructsAreLinear() + { + // arrange + var html = string.Concat(Enumerable.Repeat("", 400_000)); + var watch = Stopwatch.StartNew(); + + // act + Parser.Parse(html); + + // validation + Assert.True(watch.Elapsed < TimeSpan.FromSeconds(10), $"took {watch.Elapsed}"); + } + + /// + /// Measures the depth of a node tree without recursion. + /// + /// The top-level nodes. + /// The number of levels. + private static int Depth(IEnumerable nodes) + { + var depth = 0; + var level = nodes.ToList(); + + while (level.Count > 0) + { + depth++; + level = [.. level.OfType().SelectMany(x => x.Elements)]; + } + + return depth; + } + } +} diff --git a/src/WebExpress.WebCore.Test/Html/UnitTestHtmlEncoding.cs b/src/WebExpress.WebCore.Test/Html/UnitTestHtmlEncoding.cs new file mode 100644 index 00000000..8d97a9bd --- /dev/null +++ b/src/WebExpress.WebCore.Test/Html/UnitTestHtmlEncoding.cs @@ -0,0 +1,184 @@ +using System.Text; +using WebExpress.WebCore.WebHtml; +using WebExpress.WebCore.WebHtml.Parser; +using WebExpress.WebCore.WebStatusPage; + +namespace WebExpress.WebCore.Test.Html +{ + /// + /// Tests that text written through the html classes cannot become markup, and that markup read + /// by the parser is written back as the browser read it. + /// + public class UnitTestHtmlEncoding + { + private static readonly HtmlParser Parser = new(); + + /// + /// Renders nodes the way a page writes them. + /// + /// The nodes. + /// The markup. + private static string Render(params IHtmlNode[] nodes) + { + var builder = new StringBuilder(); + foreach (var node in nodes) + { + node.ToString(builder, 0); + } + + return builder.ToString(); + } + + /// + /// Verifies that a text node is encoded, alone and inside an element that holds only text. + /// + [Fact] + public void TextIsEncoded() + { + // act + var alone = Render(new HtmlText(" & co")); + var inside = Render(new HtmlElementTextContentP("")); + + // validation + Assert.Equal("<script>alert(1)</script> & co", alone); + Assert.Contains("<img src=x onerror=alert(1)>", inside); + Assert.DoesNotContain(" + /// Verifies that markup the caller trusts is still written as it is through HtmlRaw. + /// + [Fact] + public void RawIsWrittenAsItIs() + { + Assert.Equal("x", Render(new HtmlRaw("x"))); + } + + /// + /// Verifies that the title of a page is encoded; a record a user named could close the + /// title and open a script. + /// + [Fact] + public void TitleIsEncoded() + { + // act + var html = Render(new HtmlElementMetadataTitle("")); + + // validation + Assert.DoesNotContain("")); + + // validation + Assert.DoesNotContain("-->x", "x")] + [InlineData("", "")] + [InlineData("x", "x")] + [InlineData("x", "x")] + [InlineData("x", "x")] + [InlineData("r", "r")] + [InlineData("
d
", "
d
")] + [InlineData("", "")] + [InlineData("text", "text")] + [InlineData("a < b & c", "a < b & c")] + public void Sanitize(string html, string expected) + { + Assert.Equal(expected, HtmlSanitizer.Sanitize(html)); + } + + /// + /// Verifies the link and image address policy. + /// + [Fact] + public void UrlPolicyAllowsOnlyKnownSchemes() + { + Assert.True(UrlPolicy.IsSafeLink("https://example.com")); + Assert.True(UrlPolicy.IsSafeLink("relative/path")); + Assert.False(UrlPolicy.IsSafeLink(" JavaScript:alert(1)")); + Assert.False(UrlPolicy.IsSafeLink("data:text/html,:c\"|?*.txt", "abc.txt")] + [InlineData("", "file")] + [InlineData(null, "file")] + public void FileNameIsPlain(string declared, string expected) + { + Assert.Equal(expected, Request.SafeFileName(declared)); + } + + /// + /// Verifies that a declared content type is kept only in the form type/subtype. + /// + /// The declared type. + /// The type kept. + [Theory] + [InlineData("image/PNG", "image/png")] + [InlineData("text/plain; charset=utf-8", "text/plain")] + [InlineData("text/html\r\nX-Injected: 1", "application/octet-stream")] + [InlineData("