Skip to content

Conversation

gwleuverink
Copy link
Contributor

@gwleuverink gwleuverink commented Sep 9, 2025

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 NativePHP/laravel#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
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
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
Contributor Author

Closes NativePHP/laravel#688

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants