Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix WebResource handler matching/unregister semantics and harden WebView defaults - #57

#57
Merged
XuanchenLin merged 1 commit into
masterXuanchenLin/WinFormedge:masterfrom
copilot/analyze-project-issuesXuanchenLin/WinFormedge:copilot/analyze-project-issuesCopy head branch name to clipboard
Jul 13, 2026
Merged

Fix WebResource handler matching/unregister semantics and harden WebView defaults#57
XuanchenLin merged 1 commit into
masterXuanchenLin/WinFormedge:masterfrom
copilot/analyze-project-issuesXuanchenLin/WinFormedge:copilot/analyze-project-issuesCopy head branch name to clipboard

Conversation

Copilot AI commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

This PR addresses multiple correctness and hardening issues in WinFormedge’s WebResource and WebView initialization paths. It fixes incorrect handler resolution/unregistration behavior, makes WebView initialization failures observable, tightens default security posture, and aligns README API examples with the current public entrypoint.

  • WebResource routing correctness

    • Changed handler selection to prefer the most specific path prefix (longest AbsolutePath) instead of the least specific one.
    • Prevents broad handlers from shadowing narrower handlers (e.g. /api/ vs /api/v1/).
  • WebResource unregister correctness

    • Unregister now removes all matching handlers by scheme/host (case-insensitive).
    • Filter removal now uses each handler’s original WebResourceContext instead of hardcoded All, preventing stale filters.
  • WebView initialization failure handling

    • Replaced CreateWebView2 async void flow with CreateWebView2Async (Task) and awaited it from handle creation.
    • Captures initialization exceptions and routes them to Startup.OnApplicationException(...).
    • Controller access now throws a descriptive InvalidOperationException with captured initialization exception context.
  • Security baseline hardening

    • Enabled tracking prevention by default in CoreWebView2EnvironmentOptions.
    • Enabled reputation checking by default in CoreWebView2Settings.
    • Stopped returning raw exception messages in HTTP 500 responses for web resources.
  • Documentation/API alignment

    • Updated README examples to use WinFormedgeApp.CreateAppBuilder() and consistent WinFormedgeApp naming instead of outdated FormedgeApp.CreateBuilder() references.
// Before
var targetHandler = matchedHandlers.OrderBy(x => x.Uri.AbsolutePath.Length).FirstOrDefault();

// After
var targetHandler = matchedHandlers.OrderByDescending(x => x.Uri.AbsolutePath.Length).FirstOrDefault();

@XuanchenLin
XuanchenLin marked this pull request as ready for review July 13, 2026 10:14
Copilot AI review requested due to automatic review settings July 13, 2026 10:14
@XuanchenLin
XuanchenLin merged commit 44a4653 into master Jul 13, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves WinFormedge’s WebResource routing/unregistration correctness, makes WebView2 initialization failures observable to app startup hooks, and hardens default WebView2 security settings while aligning README examples with the current public API entrypoint.

Changes:

  • WebResource handler selection now prefers the most specific (longest) path-prefix match; unregister removes all matching handlers and removes filters using each handler’s original WebResourceContext.
  • WebView2 initialization is now awaited via CreateWebView2Async, with captured exceptions surfaced through Startup.OnApplicationException(...) and via a more descriptive Controller accessor.
  • Security defaults tightened (tracking prevention + reputation checking enabled), and README examples updated to WinFormedgeApp.CreateAppBuilder().

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/WinFormedge/WebResource/base/WebResourceManager.cs Fixes handler selection specificity and improves unregister/filter removal behavior.
src/WinFormedge/WebResource/base/WebResourceHandler.cs Hardens 500 responses to avoid leaking exception details.
src/WinFormedge/Classes.WebView/WebViewCore.cs Makes WebView2 initialization awaitable and exceptions observable; updates controller access semantics.
src/WinFormedge/Classes.Startup/WinFormedgeApp.cs Enables tracking prevention by default in environment options.
README.md Updates public docs to the current WinFormedgeApp builder API.
src/WinFormedge/README.md Updates package-level README examples to the current WinFormedgeApp builder API.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 78 to +80
matchedHandlers = matchedHandlers.Where(x => uri.AbsolutePath.StartsWith(x.Uri.AbsolutePath));

var targetHandler = matchedHandlers.OrderBy(x => x.Uri.AbsolutePath.Length).FirstOrDefault();
var targetHandler = matchedHandlers.OrderByDescending(x => x.Uri.AbsolutePath.Length).FirstOrDefault();
Comment on lines +163 to +167
if (_initialized)
{
var url = GetFilterUrl(handler.Scheme, handler.HostName);
_webView2!.RemoveWebResourceRequestedFilter(url + "*", handler.WebResourceContext);
}
Comment on lines +980 to 983
catch (Exception)
{
args.Response = webview.Environment.CreateWebResourceResponse(null, StatusCodes.Status500InternalServerError, ex.Message, string.Empty);
args.Response = webview.Environment.CreateWebResourceResponse(null, StatusCodes.Status500InternalServerError, StatusCodes.GetStatusPhrase(StatusCodes.Status500InternalServerError), string.Empty);

Comment on lines 132 to +133
/// <exception cref="NullReferenceException">Thrown if the controller is not initialized.</exception>
internal CoreWebView2Controller Controller => _controller ?? throw new NullReferenceException(nameof(Controller));
internal CoreWebView2Controller Controller => _controller ?? throw new InvalidOperationException("WebView2 controller is not initialized.", _initializationException);
Comment on lines +408 to 412
catch (Exception ex)
{
CreateWebView2();
_initializationException = ex;
WinFormedgeApp.Current.Startup?.OnApplicationException(ex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.