Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@zcbenz
Copy link
Contributor

@zcbenz zcbenz commented Jan 30, 2024

Description of Change

For certain apps they want stricter security checks than the default settings provided by Electron. For example in VS Code, they restrict module search paths in certain locations, and forbids file operations into UNC locations.

The app.setNodePreload API provides them a way to apply the restrictions reliably without patching Electron.

Checklist

Release Notes

Notes: Add app.setNodePreload API.

@zcbenz zcbenz added semver/minor backwards-compatible functionality target/27-x-y PR should also be added to the "27-x-y" branch. target/28-x-y PR should also be added to the "28-x-y" branch. target/29-x-y PR should also be added to the "29-x-y" branch. labels Jan 30, 2024
@zcbenz zcbenz requested review from deepak1556 and miniak January 30, 2024 05:03
@zcbenz
Copy link
Contributor Author

zcbenz commented Jan 30, 2024

/cc @bpasero

@zcbenz zcbenz requested a review from a team as a code owner January 30, 2024 23:44
@zcbenz
Copy link
Contributor Author

zcbenz commented Jan 30, 2024

Note that this PR includes an update to the patches/node/src_preload_function_for_environment.patch, which I will separate into another PR once nodejs/node#51539 gets approved/merged.

@zcbenz zcbenz force-pushed the app-node-preload branch 3 times, most recently from e200621 to 19b618f Compare January 31, 2024 01:48
Copy link
Member

@deepak1556 deepak1556 left a comment

Choose a reason for hiding this comment

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

LGTM on the implementation. Thank you!

Still needs API review.

@deepak1556
Copy link
Member

The failing WoA test is from newly added one

not ok 156 default behavior app.setNodePreload throws when path outside of resources path is passed
  expected [Function] to throw error matching /The preload script must reside in resourcesPath/ but got 'The file path of preload script must be absolute.'
  AssertionError: expected [Function] to throw error matching /The preload script must reside in resourcesPath/ but got 'The file path of preload script must be absolute.'
      at Context.<anonymous> (electron\spec\api-app-spec.ts:2068:18)

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened recently label Feb 6, 2024
@github-actions github-actions bot added the target/30-x-y PR should also be added to the "30-x-y" branch. label Feb 21, 2024
@zcbenz zcbenz force-pushed the app-node-preload branch from 2495207 to 4c43d9b Compare March 6, 2024 01:06
@miniak
Copy link
Contributor

miniak commented Mar 6, 2024

API LGTM

@miniak miniak removed the target/27-x-y PR should also be added to the "27-x-y" branch. label Mar 6, 2024
Copy link
Contributor

@nornagon nornagon left a comment

Choose a reason for hiding this comment

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

It's not clear to me how this approach protects against e.g. prototype pollution. What is this trying to prevent?

Comment on lines +708 to +714
const Module = require('node:module')
const originalMethod = Module._nodeModulePaths
exports.onBuiltinModulesPatched = () => {
Module._nodeModulePaths = (...args) => {
originalMethod(...args)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be ESM?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the lib/node/init.ts is loaded by Electron synchronously, supporting ESM implies supporting async/await in exports, which is not how Electron's initialization works.

@zcbenz zcbenz force-pushed the app-node-preload branch from 4c43d9b to b9377a4 Compare March 13, 2024 00:34
@zcbenz
Copy link
Contributor Author

zcbenz commented Mar 13, 2024

It's not clear to me how this approach protects against e.g. prototype pollution. What is this trying to prevent?

In VS Code this will be used for 2 things:

  1. Reset the module search paths to certain places for extensions.
  2. Monkey-patch fs module to disallow access to UNC paths (UNC host '...' access is not allowed microsoft/vscode#182070).

Without this feature they have to patch Electron to implement the behaviors, because there is no way to inject scripts to workers or child processes.

I don't think they mean to prevent malicious extensions code, this is more about setting up an environment for extensions. @deepak1556 should know more about the security part.

@deepak1556
Copy link
Member

Yes both the mentioned features are not protection against untrusted code executed in the process but rather against external actors, both the described features in VS Code are specific to Windows

  1. We want to control module search paths in child process forked from our application, to remove world writable locations like C:/ from the search paths so that extensions don't unintentionally load a malicious module that can end up causing RCE for users sharing the same system. This does not fall under the threat model of Electron or Node.js, so the fix is specific to our application.

  2. NTLM hash leaks is a known issue on Windows and the problem lies at the system read/write calls. There is not much a runtime can do here but the best course is to prompt the user before making any sort of I/O with a UNC path. Since there are numerous places this can happen from our application, we wanted to gate the checks at the Node.js fs layer to ensure any I/O from the application will be checked against an allowed set of UNC hosts explicitly consented by the user before proceeding. We are hoping this change can be removed once NTLM auth is removed on Windows, refs https://techcommunity.microsoft.com/t5/windows-it-pro-blog/the-evolution-of-windows-authentication/ba-p/3926848

Both the changes are additional checks on top of what Electron/Node.js provides, so the preload API seems a better alternative to maintain the changes at the application layer rather than maintaining them as patch in the custom fork of the runtime.

Copy link
Member

@jkleinsc jkleinsc left a comment

Choose a reason for hiding this comment

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

API LGTM

@github-actions github-actions bot added the target/31-x-y PR should also be added to the "31-x-y" branch. label Apr 17, 2024
@github-actions github-actions bot added the target/32-x-y PR should also be added to the "32-x-y" branch. label Jun 12, 2024
@zcbenz zcbenz closed this Aug 5, 2024
@zcbenz zcbenz deleted the app-node-preload branch August 5, 2024 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-review/approved ✅ semver/minor backwards-compatible functionality target/28-x-y PR should also be added to the "28-x-y" branch. target/29-x-y PR should also be added to the "29-x-y" branch. target/30-x-y PR should also be added to the "30-x-y" branch. target/31-x-y PR should also be added to the "31-x-y" branch. target/32-x-y PR should also be added to the "32-x-y" branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants