Skip to content

Restrict JavaScript root component operations - #69240

Merged
wtgodbe merged 3 commits into
mainfrom
wtgodbe/63482
Sep 11, 2026
Merged

wtgodbe merged 3 commits into
mainfrom
wtgodbe/63482

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Sep 11, 2026

Copy link
Copy Markdown
Member

Restricts parameter updates and removal to roots created by JavaScript.

javiercn and others added 3 commits September 10, 2026 08:50
Restricts parameter updates and removal to roots created by JavaScript.

----
#### AI description  (iteration 1)
#### PR Classification
Security enhancement to restrict JavaScript root component operations by validating component ownership.

#### PR Summary
This pull request adds validation to ensure that JavaScript can only modify or remove root components that it originally created, preventing unauthorized operations on server-rendered components.

- `JSComponentInterop.cs`: Added `_jsComponentIds` HashSet to track JS-added components and `EnsureValidJSComponentId()` validation method that throws `InvalidOperationException` for unauthorized operations in `SetRootComponentParameters()` and `RemoveRootComponent()`.
- `JSComponentInteropTest.cs`: Added comprehensive unit tests verifying that operations on server-rendered components are rejected while JS-added components remain accessible.
- `JSRootComponentsTest.cs` and test assets: Added E2E test case and supporting JavaScript helpers to verify parameters cannot be updated on non-JS root components, with UI elements to trigger and display validation errors.
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
@wtgodbe
wtgodbe requested a review from a team as a code owner September 11, 2026 16:11
Copilot AI lite review requested due to automatic review settings September 11, 2026 16:11
@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Sep 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The E2E coverage targets the wrong root, and its assertion does not verify the intended rejection.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Restricts parameter updates and removal to root components created through JavaScript interop.

Changes:

  • Tracks JavaScript-created root IDs and validates operations.
  • Adds unit and end-to-end coverage for unauthorized operations.
  • Adds renderer interop test plumbing and a non-JavaScript root scenario.
File summaries
File Summary
src/Components/Web/test/JSComponents/JSComponentInteropTest.cs Tests authorized and unauthorized operations.
src/Components/Web/src/JSComponents/JSComponentInterop.cs Tracks and validates JavaScript root IDs.
src/Components/test/testassets/Components.TestServer/Pages/_ServerHost.cshtml Captures renderer interop for tests.
src/Components/test/testassets/BasicTestApp/wwwroot/js/jsRootComponentInitializers.js Adds renderer interop helpers.
src/Components/test/testassets/BasicTestApp/wwwroot/index.html Enables WebAssembly interop capture.
src/Components/test/testassets/BasicTestApp/JavaScriptRootComponents.razor Adds the unauthorized-update scenario.
src/Components/test/testassets/BasicTestApp/Index.razor Adds a non-JavaScript root parameter.
src/Components/test/E2ETest/Tests/JSRootComponentsTest.cs Adds end-to-end rejection coverage.
Review details

Suppressed comments (1)

src/Components/test/E2ETest/Tests/JSRootComponentsTest.cs:117

  • This assertion only checks the locally generated prefix; any failure in the helper itself (for example, if rendererInterop was never captured) is caught and formatted with the same prefix, so the test can pass without reaching EnsureValidJSComponentId. Assert that the displayed error also contains was not added by JavaScript to verify the intended rejection.
        Browser.Contains("Error setting parameters on non-JS root", () => app.FindElement(By.Id("message")).Text);
  • Files reviewed: 8/8 changed files
  • Comments generated: 1
  • Review effort level: Lite


async function setNonJSRootParameters() {
const error = await myJsRootComponentInitializers.setRootComponentParameters(
0,
@wtgodbe
wtgodbe merged commit cd17eac into main Sep 11, 2026
33 checks passed
@wtgodbe
wtgodbe deleted the wtgodbe/63482 branch September 11, 2026 18:01
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants