Skip to content
This repository was archived by the owner on May 27, 2026. It is now read-only.

Better webPreferences defaults#255

Closed
gwleuverink wants to merge 11 commits into
mainfrom
enhancement/disable-node-integration
Closed

Better webPreferences defaults#255
gwleuverink wants to merge 11 commits into
mainfrom
enhancement/disable-node-integration

Conversation

@gwleuverink

@gwleuverink gwleuverink commented Sep 9, 2025

Copy link
Copy Markdown
Contributor

This PR improves the default security preferences & changes preload.js to support those changes:

  • disabled node integration
  • enabled context isolation (required - can't be overwritten)
  • refactored preload script to use the contextBridge API
  • added native:init event for registering Native listeners (prevents race conditions where preload hasn't evaluated yet)
  • updated which of these defaults may be overwritten (preload, contextIsolation and sandbox cannot be changed)
  • make sure Window & MenuBar use the same preferences
  • added the ability to pass custom webPreferences to MenuBar windows

See https://github.com/NativePHP/laravel/issues/688 for more details


This PR also introduces a event that's called whenever the preload script is fully evaluated. In the past some people have reported race conditions where they register a listener with Native.on() but the Native object is not available yet.

This event adresses that:

document.addEventListener('native:init' function() {

    Native.on("Native\\Laravel\\Events\\Windows\\WindowBlurred", (payload, event) => {
        //
    });
})

@gwleuverink

Copy link
Copy Markdown
Contributor Author

Additionally I've deleted the preload file at src/preload/index.js (also from vite builder config)

We're only using the one from the electron plugin, so the one I removed was probably an artifact. Everything is still working as expected in my testing environment.

Can someone confirm this is okay? I've checked high and low but that second preload wasn't doing anything at all.

@gwleuverink

Copy link
Copy Markdown
Contributor Author

I've locked sandbox, preload & contextIsolation prefs so they can't change. Other defaults may be overwritten now.

While working on this I noticed we couldn't pass custom webPreferences to MenuBar windows. I've added this in NativePHP/laravel#694

@gwleuverink gwleuverink marked this pull request as ready for review September 10, 2025 08:30
@gwleuverink gwleuverink requested a review from a team September 10, 2025 08:31
@gwleuverink gwleuverink self-assigned this Sep 10, 2025
@gwleuverink gwleuverink added the v2 label Sep 10, 2025
@gwleuverink gwleuverink changed the title disabled nodeIntegration & enabled contextIsolation Better webPreferences defaults Sep 10, 2025
@gwleuverink

Copy link
Copy Markdown
Contributor Author

@gwleuverink gwleuverink mentioned this pull request Sep 22, 2025
@SRWieZ

SRWieZ commented Oct 18, 2025

Copy link
Copy Markdown
Member

Already merged in https://github.com/NativePHP/desktop

@SRWieZ SRWieZ closed this Oct 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants