|
| 1 | +# Code Integrity |
| 2 | + |
| 3 | +<!--introduced_in=REPLACEME--> |
| 4 | + |
| 5 | +<!-- type=misc --> |
| 6 | + |
| 7 | +> Stability: 1.1 - Active development |
| 8 | +
|
| 9 | +Code integrity refers to the assurance that software code has not been |
| 10 | +altered or tampered with in any unauthorized way. It ensures that |
| 11 | +the code running on a system is exactly what was intended by the developers. |
| 12 | + |
| 13 | +Code integrity in Node.js integrates with platform features for code integrity |
| 14 | +policy enforcement. See platform speficic sections below for more information. |
| 15 | + |
| 16 | +The Node.js threat model considers the code that the runtime executes to be |
| 17 | +trusted. As such, this feature is an additional safety belt, not a strict |
| 18 | +security boundary. |
| 19 | + |
| 20 | +If you find a potential security vulnerability, please refer to our |
| 21 | +[Security Policy][]. |
| 22 | + |
| 23 | +## Code Integrity on Windows |
| 24 | + |
| 25 | +Code integrity is an opt-in feature that leverages Window Defender Application Control |
| 26 | +to verify the code executing conforms to system policy and has not been modified since |
| 27 | +signing time. |
| 28 | + |
| 29 | +There are three audiences that are involved when using Node.js in an |
| 30 | +environment enforcing code integrity: the application developers, |
| 31 | +those administrating the system enforcing code integrity, and |
| 32 | +the end user. The following sections describe how each audience |
| 33 | +can interact with code integrity enforcement. |
| 34 | + |
| 35 | +### Windows Code Integrity and Application Developers |
| 36 | + |
| 37 | +Windows Defender Application Control uses digital signatures to verify |
| 38 | +a file's integrity. Application developers are responsible for generating and |
| 39 | +distributing the signature information for their Node.js application. |
| 40 | +Application developers are also expected to design their application |
| 41 | +in robust ways to avoid unintended code execution. This includes |
| 42 | +use of `eval` and loading modules outside of standard methods. |
| 43 | + |
| 44 | +Signature information for files which Node.js is intended to execute |
| 45 | +can be stored in a catalog file. Application developers can generate |
| 46 | +a Windows catalog file to store the hash of all files Node.js |
| 47 | +is expected to execute. |
| 48 | + |
| 49 | +A catalog can be generated using the `New-FileCatalog` Powershell |
| 50 | +cmdlet. For example |
| 51 | + |
| 52 | +```powershell |
| 53 | +New-FileCatalog -Version 2 -CatalogFilePath MyApplicationCatalog.cat -Path \my\application\path\ |
| 54 | +``` |
| 55 | + |
| 56 | +The `Path` argument should point to the root folder containing your application's code. If |
| 57 | +your application's code is fully contained in one file, `Path` can point to that single file. |
| 58 | + |
| 59 | +Be sure that the catalog is generated using the final version of the files that you intend to ship |
| 60 | +(i.e. after minifying). |
| 61 | + |
| 62 | +The application developer should then sign the generated catalog with their Code Signing certificate |
| 63 | +to ensure the catalog is not tampered with between distribution and execution. |
| 64 | + |
| 65 | +This can be done with the [Set-AuthenticodeSignature commandlet](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-authenticodesignature). |
| 66 | + |
| 67 | +### Windows Code Integrity and System Administrators |
| 68 | + |
| 69 | +This section is intended for system administrators who want to enable Node.js |
| 70 | +code integrity features in their environments. |
| 71 | + |
| 72 | +This section assumes familiarity with managing WDAC polcies. |
| 73 | +Official documentation for WDAC can be found [here](https://learn.microsoft.com/en-us/windows/security/application-security/application-control/windows-defender-application-control/). |
| 74 | + |
| 75 | +Code integrity enforcement on Windows has two toggleable settings: |
| 76 | +`EnforceCodeIntegrity` and `DisableInteractiveMode`. These settings are configured |
| 77 | +by WDAC policy. |
| 78 | + |
| 79 | +`EnforceCodeIntegrity` causes Node.js to call WldpCanExecuteFile whenever a module is loaded using `require`. |
| 80 | +WldpCanExecuteFile verifies that the file's integrity has not been tampered with from signing time. |
| 81 | +The system administrator should sign and install the application's file catalog where the application |
| 82 | +is running, per WDAC guidance. |
| 83 | + |
| 84 | +`DisableInteractiveMode` prevents Node.js from being run in interactive mode, and also disables the `-e` and `--eval` |
| 85 | +command line options. |
| 86 | + |
| 87 | +#### Enabling Code Integrity Enforcement |
| 88 | + |
| 89 | +On newer Windows versions (22H2+), the preferred method of configuring application settings is done using |
| 90 | +`AppSettings` in your WDAC Policy. |
| 91 | + |
| 92 | +```text |
| 93 | +<AppSettings> |
| 94 | + <App Manifest="wdac-manifest.xml"> |
| 95 | + <Setting Name="EnforceCodeIntegrity" > |
| 96 | + <Value>True</Value> |
| 97 | + </Setting> |
| 98 | + <Setting Name="DisableInteractiveMode" > |
| 99 | + <Value>True</Value> |
| 100 | + </Setting> |
| 101 | + </App> |
| 102 | +</AppSettings> |
| 103 | +``` |
| 104 | + |
| 105 | +On older Windows versions, use the `Settings` section of your WDAC Policy. |
| 106 | + |
| 107 | +```text |
| 108 | +<Settings> |
| 109 | + <Setting Provider="Node.js" Key="Settings" ValueName="EnforceCodeIntegrity"> |
| 110 | + <Value> |
| 111 | + <Boolean>true</Boolean> |
| 112 | + </Value> |
| 113 | + </Setting> |
| 114 | + <Setting Provider="Node.js" Key="Settings" ValueName="DisableInteractiveMode"> |
| 115 | + <Value> |
| 116 | + <Boolean>true</Boolean> |
| 117 | + </Value> |
| 118 | + </Setting> |
| 119 | +</Settings> |
| 120 | +``` |
| 121 | + |
| 122 | +## Code Integrity on Linux |
| 123 | + |
| 124 | +Code integrity on Linux is not yet implemented. Plans for implementation will |
| 125 | +be made once the necessary APIs on Linux have been upstreamed. More information |
| 126 | +can be found here: <https://github.com/nodejs/security-wg/issues/1388> |
| 127 | + |
| 128 | +## Code Integrity on MacOS |
| 129 | + |
| 130 | +Code integrity on MacOS is not yet implemented. Currently, there is no |
| 131 | +timeline for implementation. |
| 132 | + |
| 133 | +[Security Policy]: https://github.com/nodejs/node/blob/main/SECURITY.md |
0 commit comments