-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Switch to using POST for .NET method invoke #32104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 |
jsuarezruiz
left a comment
There was a problem hiding this 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)
|
/rebase |
b5f1c22 to
988aba9
Compare
|
/backport to release/10.0.1xx |
|
Started backporting to |
988aba9 to
bd78699
Compare
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
8dc3d45 to
d536066
Compare
|
/backport to release/10.0.1xx |
|
Started backporting to |
There was a problem hiding this 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"; | ||
|
|
||
|
|
Copilot
AI
Oct 28, 2025
There was a problem hiding this comment.
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.
| // iframe.frameBorder = '0'; | ||
| // iframe.loading = 'lazy'; | ||
| // iframe.allowTransparency = 'true'; | ||
| // iframe.allowFullscreen = 'true'; |
Copilot
AI
Oct 28, 2025
There was a problem hiding this comment.
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.
| // iframe.frameBorder = '0'; | |
| // iframe.loading = 'lazy'; | |
| // iframe.allowTransparency = 'true'; | |
| // iframe.allowFullscreen = 'true'; |
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
__hwvInvokeDotNetendpoint, 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
__hwvInvokeDotNetendpoint: only POST requests with the correctX-Maui-Invoke-TokenandX-Maui-Request-Bodyheaders 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]JavaScript Interop and API Updates
__hwvInvokeDotNetendpoint, 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
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
HeaderPairTypefor header handling, improving maintainability and cross-platform support. [1] [2]TypeScriptCompileBlockedproperty.Future Improvements