Skip to content

Conversation

@mattleibow
Copy link
Member

@mattleibow mattleibow commented Oct 21, 2025

This pull request introduces significant improvements to the HybridWebView's .NET invocation security and request handling, as well as updates to the related test infrastructure and JavaScript interop logic. The changes enforce stricter validation of incoming requests to the __hwvInvokeDotNet endpoint, ensuring only properly formed POST requests with required headers and bodies are processed. Comprehensive device tests and supporting HTML/JS files have been added to verify these behaviors across platforms.

HybridWebView .NET Invocation Security & Request Handling

  • The HybridWebView handler now strictly validates requests to the __hwvInvokeDotNet endpoint: only POST requests with the correct X-Maui-Invoke-Token and X-Maui-Request-Body headers and a non-empty body are accepted; others are blocked with appropriate HTTP status codes (400 Bad Request, 405 Method Not Allowed). [1] [2] [3]
  • Refactored the .NET invocation logic to deserialize request data from the POST body or header, removing reliance on query strings and improving platform compatibility. [1] [2] [3]

JavaScript Interop and API Updates

  • Updated the JavaScript and TypeScript logic to send POST requests with the required headers and body to the __hwvInvokeDotNet endpoint, matching the new server-side requirements. The request and response handling code was also improved for better error handling and data deserialization. [1] [2] [3] [4] [5]

Device Test Infrastructure

  • Added a new device test suite (HybridWebViewTests_InvokeDotNetFails.cs) and corresponding HTML test page (invokedotnetfails.html) to verify that invalid requests (wrong method, missing/invalid headers, empty body, iframe context) are blocked and only valid requests succeed. [1] [2]

Cross-Platform and Build System Improvements

  • Refactored handler code to use a platform-agnostic HeaderPairType for header handling, improving maintainability and cross-platform support. [1] [2]
  • Improved TypeScript build configuration to allow overriding the compile block condition via the TypeScriptCompileBlocked property.

Future Improvements

  • Right now, Windows, iOS and Mac Catalyst use a POST with a body, but Android is limited and has to use a header. In future, switching to a [JavascriptInterface] may be worth investigation.
  • This could also be expanded into a non-HTTP request and just use the same messaging system that the other invokes use.

Copilot AI review requested due to automatic review settings October 21, 2025 01:27
@mattleibow mattleibow added the p/0 Current heighest priority issues that we are targeting for a release. label Oct 21, 2025
@mattleibow mattleibow added this to the .NET 10.0 GA milestone Oct 21, 2025
Copy link
Contributor

Copilot AI left a comment

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 migrates the HybridWebView's .NET method invocation from GET requests with URL-encoded query strings to POST requests with JSON bodies. This change removes URL length limitations and enables future extensibility. The implementation differs slightly by platform: Windows, iOS, and macOS use standard POST bodies, while Android works around platform limitations by using a custom header to transmit the request body.

Key changes:

  • Changed JavaScript fetch calls from GET to POST with JSON body
  • Added security validation through custom headers (X-Maui-Invoke-Token)
  • Implemented platform-specific body handling (stream for iOS/Windows/Mac, header for Android)
  • Added comprehensive test coverage for validation failures

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
MauiHybridWebViewClient.cs Android implementation: validates POST requests and reads body from custom header
HybridWebViewHandler.iOS.cs iOS/MacCatalyst implementation: validates POST requests and reads body from NSData stream
HybridWebViewHandler.cs Shared handler: updated method signature to accept stream or string body instead of query string
HybridWebViewHandler.Windows.cs Windows implementation: validates POST requests and reads body from request content stream
HybridWebView.ts TypeScript client: changed from GET with query string to POST with JSON body and headers
HybridWebView.js JavaScript client: transpiled version of TypeScript changes
Core.csproj Build configuration: allows conditional override of TypeScript compilation blocking
invokedotnetfails.html Test HTML page: provides test functions for validation failure scenarios
HybridWebViewTests_InvokeDotNetFails.cs Unit tests: validates security checks and proper error handling

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

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

/Users/builder/azdo/_work/1/s/src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.cs(194,66): error CS8604: Possible null reference argument for parameter 'json' in 'JSInvokeMethodData? JsonSerializer.Deserialize<JSInvokeMethodData>(string json, JsonTypeInfo<JSInvokeMethodData> jsonTypeInfo)'. [/Users/builder/azdo/_work/1/s/src/Core/src/Core.csproj::TargetFramework=netstandard2.0]
    1 Error(s)

@github-project-automation github-project-automation bot moved this from Todo to Changes Requested in MAUI SDK Ongoing Oct 21, 2025
@rmarinho
Copy link
Member

/rebase

@github-actions github-actions bot force-pushed the dev/hybridwebview-post-invoke branch from b5f1c22 to 988aba9 Compare October 22, 2025 23:38
@rmarinho
Copy link
Member

/backport to release/10.0.1xx

@github-actions
Copy link
Contributor

Started backporting to release/10.0.1xx (link to workflow run)

Using GET was simpler, but required everything to be on the URL
and URL encoded. There is a limit to the number of chars.

Using a POST message allows for longer but also fits in with the
ability to extend later with different features.

Right now, Windows, iOS and Mac Catalyst use a POST with a body, but
Android is limited and has to use a header. In future, switching to
a [JavascriptInterface] may be worth investigation.

Fix the tests

whatever mac

no idea

some ws

Some versions of Android fail with missing headers
@mattleibow mattleibow force-pushed the dev/hybridwebview-post-invoke branch from 8dc3d45 to d536066 Compare October 28, 2025 16:24
@mattleibow
Copy link
Member Author

/backport to release/10.0.1xx

@github-actions
Copy link
Contributor

Started backporting to release/10.0.1xx (link to workflow run)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

internal const string InvokeDotNetTokenHeaderValue = "HybridWebView";
internal const string InvokeDotNetBodyHeaderName = "X-Maui-Request-Body";


Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

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

Multiple consecutive blank lines detected. Remove one of the blank lines to follow standard formatting practices.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines +146 to +149
// iframe.frameBorder = '0';
// iframe.loading = 'lazy';
// iframe.allowTransparency = 'true';
// iframe.allowFullscreen = 'true';
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

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

Commented-out code should be removed. If these iframe attributes are not needed for the test, delete them. If they may be needed in the future, document why they are preserved as comments.

Suggested change
// iframe.frameBorder = '0';
// iframe.loading = 'lazy';
// iframe.allowTransparency = 'true';
// iframe.allowFullscreen = 'true';

Copilot uses AI. Check for mistakes.
rmarinho
rmarinho previously approved these changes Oct 28, 2025
@rmarinho rmarinho changed the base branch from net10.0 to main October 29, 2025 10:36
@rmarinho rmarinho dismissed their stale review October 29, 2025 10:36

The base branch was changed.

@rmarinho rmarinho requested a review from jsuarezruiz October 29, 2025 10:36
@mattleibow mattleibow merged commit 9d861e9 into main Oct 29, 2025
158 of 163 checks passed
@mattleibow mattleibow deleted the dev/hybridwebview-post-invoke branch October 29, 2025 20:58
@github-project-automation github-project-automation bot moved this from Changes Requested to Done in MAUI SDK Ongoing Oct 29, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Nov 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

p/0 Current heighest priority issues that we are targeting for a release.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants