Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
5885996
feat: move to .NET 11 and version 2.0.2-alpha
ReneSchwarzer Nov 15, 2026
cf50c73
fix: give identity validators the claims of the verified token
ReneSchwarzer Nov 17, 2026
6e9e22f
fix: drain the console on a thread of its own
ReneSchwarzer Nov 17, 2026
f0b8c64
feat: update xunit.v3 to 4.0.3 and the TRX report extension to 2.5.3
ReneSchwarzer Nov 17, 2026
b542213
fix: keep the slow-console log test independent of other tests' conso…
ReneSchwarzer Nov 19, 2026
676cdcf
fix: harden the http server and request handling
ReneSchwarzer Nov 20, 2026
6e860e6
fix: render fragments on their own instance and lock the fragment reg…
ReneSchwarzer Nov 24, 2026
b7d6e76
fix: lock the component registries and announce what they remove
ReneSchwarzer Nov 24, 2026
08c00d1
fix: lock the setting page, plugin and component registries
ReneSchwarzer Nov 24, 2026
2292f83
fix: build the HTML tree iteratively with a nesting limit
ReneSchwarzer Nov 24, 2026
6dc8f4e
fix!: encode text nodes, titles and status messages; parse HTML like …
ReneSchwarzer Nov 24, 2026
a276ffd
feat: add an allowlist HtmlSanitizer and a UrlPolicy for user HTML
ReneSchwarzer Nov 24, 2026
149153c
fix: reduce declared upload names to plain names and types to media t…
ReneSchwarzer Nov 25, 2026
a58ea2c
fix!: deny access to endpoints without a policy unless they are marke…
ReneSchwarzer Nov 25, 2026
fd745fe
fix: answer an enabled metrics endpoint without a token only from thi…
ReneSchwarzer Nov 25, 2026
ee094d2
fix: return an untranslated key with a brace instead of failing in st…
ReneSchwarzer Nov 25, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/generate-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unittest-verification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicAccessPolicy>]` 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<AuthenticatedAccessPolicy>]` 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.
Expand Down
118 changes: 118 additions & 0 deletions src/WebExpress.WebCore.Test/Html/Parser/UnitTestHtmlParserLimits.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
public class UnitTestHtmlParserLimits
{
private static readonly HtmlParser Parser = new();

/// <summary>
/// Verifies that deeply nested elements parse into a bounded tree that renders.
/// </summary>
[Fact]
public void DeepNestingIsBounded()
{
// arrange
var html = string.Concat(Enumerable.Repeat("<div>", 200_000)) + "deep" + string.Concat(Enumerable.Repeat("</div>", 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());
}

/// <summary>
/// Verifies that nesting below the limit keeps its structure.
/// </summary>
[Fact]
public void ShallowNestingKeepsItsStructure()
{
// act
var nodes = Parser.Parse("<div><p><b>x</b></p></div>");

// validation
var div = Assert.IsType<HtmlElementTextContentDiv>(Assert.Single(nodes));
var p = Assert.IsType<HtmlElementTextContentP>(Assert.Single(div.Elements));
Assert.Single(p.Elements);
}

/// <summary>
/// Verifies that a stray end tag is ignored instead of ending the parse; it dropped the
/// rest of the document.
/// </summary>
[Fact]
public void StrayEndTagIsIgnored()
{
// act
var nodes = Parser.Parse("<p>a</p></b><p>b</p>");

// validation
Assert.Equal(2, nodes.OfType<HtmlElementTextContentP>().Count());
}

/// <summary>
/// Verifies that an end tag closes the elements opened inside the element it names.
/// </summary>
[Fact]
public void EndTagClosesTheElementsInside()
{
// act
var nodes = Parser.Parse("<div><span>a</div><p>b</p>");

// validation
Assert.IsType<HtmlElementTextContentDiv>(nodes[0]);
Assert.IsType<HtmlElementTextContentP>(nodes[1]);
}

/// <summary>
/// Verifies that a document full of unknown bang constructs is tokenized in linear time;
/// every "&lt;!" searched the rest of the input for a doctype.
/// </summary>
[Fact]
public void BangConstructsAreLinear()
{
// arrange
var html = string.Concat(Enumerable.Repeat("<!x>", 400_000));
var watch = Stopwatch.StartNew();

// act
Parser.Parse(html);

// validation
Assert.True(watch.Elapsed < TimeSpan.FromSeconds(10), $"took {watch.Elapsed}");
}

/// <summary>
/// Measures the depth of a node tree without recursion.
/// </summary>
/// <param name="nodes">The top-level nodes.</param>
/// <returns>The number of levels.</returns>
private static int Depth(IEnumerable<IHtmlNode> nodes)
{
var depth = 0;
var level = nodes.ToList();

while (level.Count > 0)
{
depth++;
level = [.. level.OfType<HtmlElement>().SelectMany(x => x.Elements)];
}

return depth;
}
}
}
184 changes: 184 additions & 0 deletions src/WebExpress.WebCore.Test/Html/UnitTestHtmlEncoding.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
public class UnitTestHtmlEncoding
{
private static readonly HtmlParser Parser = new();

/// <summary>
/// Renders nodes the way a page writes them.
/// </summary>
/// <param name="nodes">The nodes.</param>
/// <returns>The markup.</returns>
private static string Render(params IHtmlNode[] nodes)
{
var builder = new StringBuilder();
foreach (var node in nodes)
{
node.ToString(builder, 0);
}

return builder.ToString();
}

/// <summary>
/// Verifies that a text node is encoded, alone and inside an element that holds only text.
/// </summary>
[Fact]
public void TextIsEncoded()
{
// act
var alone = Render(new HtmlText("<script>alert(1)</script> & co"));
var inside = Render(new HtmlElementTextContentP("<img src=x onerror=alert(1)>"));

// validation
Assert.Equal("&lt;script&gt;alert(1)&lt;/script&gt; &amp; co", alone);
Assert.Contains("&lt;img src=x onerror=alert(1)&gt;", inside);
Assert.DoesNotContain("<img", inside);
}

/// <summary>
/// Verifies that markup the caller trusts is still written as it is through HtmlRaw.
/// </summary>
[Fact]
public void RawIsWrittenAsItIs()
{
Assert.Equal("<b>x</b>", Render(new HtmlRaw("<b>x</b>")));
}

/// <summary>
/// Verifies that the title of a page is encoded; a record a user named could close the
/// title and open a script.
/// </summary>
[Fact]
public void TitleIsEncoded()
{
// act
var html = Render(new HtmlElementMetadataTitle("</title><script>alert(1)</script>"));

// validation
Assert.DoesNotContain("<script>", html);
Assert.Contains("&lt;/title&gt;", html);
}

/// <summary>
/// Verifies that parsed text keeps its entities when it is written again, instead of being
/// encoded twice or decoded into markup.
/// </summary>
/// <param name="html">The markup read.</param>
/// <param name="expected">The markup expected back.</param>
[Theory]
[InlineData("<p>a &amp; b</p>", "<p>a &amp; b</p>")]
[InlineData("<p>&lt;script&gt;</p>", "<p>&lt;script&gt;</p>")]
[InlineData("<p>AT&T</p>", "<p>AT&amp;T</p>")]
public void ParsedTextRoundTrips(string html, string expected)
{
Assert.Equal(expected, Render([.. Parser.Parse(html)]).Trim());
}

/// <summary>
/// Verifies that the content of a raw text element is neither decoded nor encoded.
/// </summary>
[Fact]
public void RawTextContentStaysRaw()
{
// act
var html = Render([.. Parser.Parse("<xmp>a > b &amp; c</xmp>")]);

// validation
Assert.Contains("a > b &amp; c", html);
}

/// <summary>
/// Verifies that a no-break space is no separator inside a tag. A browser reads
/// "img src=x onerror=..." as the name of one inert element; splitting it here wrote
/// an image with an event handler.
/// </summary>
[Fact]
public void NoBreakSpaceIsNoSeparator()
{
// act
var html = Render([.. Parser.Parse("<img src=x onerror=alert(1)>")]);

// validation
Assert.DoesNotContain("onerror=\"", html);
Assert.DoesNotContain("<img ", html);
}

/// <summary>
/// Verifies that a comment ends where a browser ends it, so markup after "--!&gt;" is parsed
/// as markup and not hidden in a comment.
/// </summary>
[Fact]
public void CommentEndsAtBangClose()
{
// act
var nodes = Parser.Parse("<!-- a --!><b>after</b>");

// validation
Assert.Contains(nodes, x => x is HtmlElementTextSemanticsB);
}

/// <summary>
/// Verifies that the text of a comment cannot end the comment it is written into.
/// </summary>
[Fact]
public void CommentTextCannotEndTheComment()
{
// act
var html = Render(new HtmlComment("x --><script>alert(1)</script>"));

// validation
Assert.DoesNotContain("--><script>", html);
Assert.EndsWith(" -->", html);
Assert.Equal(1, html.Split("-->").Length - 1);
}

/// <summary>
/// Verifies that an attribute whose name could end the tag or open another attribute is not
/// written.
/// </summary>
/// <param name="name">The attribute name.</param>
[Theory]
[InlineData("a\"onmouseover")]
[InlineData("x><script")]
[InlineData("a b")]
[InlineData("")]
public void InvalidAttributeNamesAreNotWritten(string name)
{
// arrange
var element = new HtmlElementTextContentDiv();
element.AddUserAttribute(name, "v");
element.AddUserAttribute("data-ok", "1");

// act
var html = Render(element);

// validation
Assert.Contains("data-ok=\"1\"", html);
Assert.DoesNotContain("=\"v\"", html);
}

/// <summary>
/// Verifies that a status message is text written encoded into the body.
/// </summary>
[Fact]
public void StatusMessageIsEncoded()
{
// act
var html = StatusMessage.ToHtml(new StatusMessage("bad <script>\nline"), "fallback");

// validation
Assert.Equal("<html><body><p>bad &lt;script&gt;<br/>line</p></body></html>", html);
Assert.Equal("fallback", StatusMessage.ToHtml(null, "fallback"));
}
}
}
46 changes: 46 additions & 0 deletions src/WebExpress.WebCore.Test/Html/UnitTestHtmlSanitizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using WebExpress.WebCore.WebHtml;

namespace WebExpress.WebCore.Test.Html
{
/// <summary>
/// Tests that user HTML is reduced to markup that cannot run script.
/// </summary>
public class UnitTestHtmlSanitizer
{
/// <summary>
/// Verifies that script vectors are removed and harmless markup is kept.
/// </summary>
/// <param name="html">The user HTML.</param>
/// <param name="expected">The sanitized markup.</param>
[Theory]
[InlineData("<b>bold</b>", "<b>bold</b>")]
[InlineData("<script>alert(1)</script>x", "x")]
[InlineData("<img src=x onerror=alert(1)>", "<img src=\"x\">")]
[InlineData("<a href=\"javascript:alert(1)\">x</a>", "<a>x</a>")]
[InlineData("<a href=\"java&#9;script:alert(1)\">x</a>", "<a>x</a>")]
[InlineData("<a href=\"/page\">x</a>", "<a href=\"/page\" rel=\"nofollow noopener noreferrer\">x</a>")]
[InlineData("<span style=\"color: red; position: fixed\">r</span>", "<span style=\"color: red;\">r</span>")]
[InlineData("<div data-wx-primary-uri=\"/x\" id=\"y\">d</div>", "<div>d</div>")]
[InlineData("<!-- a --!><svg onload=alert(1)>", "")]
[InlineData("<unknown>text</unknown>", "text")]
[InlineData("a < b & c", "a &lt; b &amp; c")]
public void Sanitize(string html, string expected)
{
Assert.Equal(expected, HtmlSanitizer.Sanitize(html));
}

/// <summary>
/// Verifies the link and image address policy.
/// </summary>
[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,<script>"));
Assert.True(UrlPolicy.IsSafeImage("data:image/png;base64,AA=="));
Assert.False(UrlPolicy.IsSafeImage("data:image/svg+xml,<svg onload=alert(1)>"));
}
}
}
Loading