|
| 1 | +--- |
| 2 | +title: "User's Guide: lavamoat node runtime" |
| 3 | +description: 'A user guide for running LavaMoat-protected NodeJS applications' |
| 4 | +--- |
| 5 | + |
| 6 | +`lavamoat` is a Node.js runtime where modules are defined in [SES][ses-github-ext] Compartments. It aims to reduce the risk of malicious code in the app dependency graph, known as "software supply chain attacks". |
| 7 | + |
| 8 | +:::caution |
| 9 | + |
| 10 | +This runtime does not support ESM modules. A new runtime that's ESM-first is a work in progress. |
| 11 | + |
| 12 | +::: |
| 13 | + |
| 14 | +## LavaMoat Runtime |
| 15 | + |
| 16 | +LavaMoat differs from the standard node runtime in that it: |
| 17 | + |
| 18 | +1. Uses `lockdown()` from [SES][ses-github-ext] to prevent tampering with the execution environment. |
| 19 | + Thanks to lockdown, prototype-pollution attacks are neutralized. It's also a prerequisite to code isolation. |
| 20 | +2. Uses SES Compartments to isolate each package's execution. |
| 21 | + Packages don't share references to anything unless explicitly passed in or allowed by policy. Custom `require` and linking implementation is provided for the purpose of loading allowed dependencies. |
| 22 | +3. Enforces the app-specified LavaMoat policy. |
| 23 | + The policy specifies what execution environment each package should run with, which means: what global/built-in APIs should it be exposed to, and what other packages can it require. |
| 24 | + |
| 25 | +The result is a runtime that should work just as before, but provides some protection against supply chain attacks. |
| 26 | + |
| 27 | +## Install |
| 28 | + |
| 29 | +:::tip[Protect your installation] |
| 30 | +Before you use lavamoat runtime protections, make sure you've set up allow-scripts and install dependencies using that setup. |
| 31 | +::: |
| 32 | + |
| 33 | +Use one of: |
| 34 | + |
| 35 | +``` |
| 36 | +npm i lavamoat |
| 37 | +yarn add lavamoat |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +### Recommended usage |
| 43 | + |
| 44 | +1. Install |
| 45 | +2. Run your application once with `lavamoat app.js --autopolicy` |
| 46 | +3. Inspect the `./lavamoat/node/policy.json` file it generated |
| 47 | +4. Run your application with `lavamoat app.js` |
| 48 | +5. If you find you need to change the policy in step 2 or 3 create a `./lavamoat/node/policy-override.json` file and introduce changes there. You can both expand and trim the permissions. |
| 49 | + |
| 50 | +For more information on the lavamoat policy file, check [Policy file explained][policy-file] in documentation. |
| 51 | + |
| 52 | +:::tip[Policy maintenance] |
| 53 | + |
| 54 | +You can regenerate the main policy file on updates (and review for unexpected new permissions) while the modifications you needed to make remain in a separate overrides file. It makes reviewing and maintaining both files easier. |
| 55 | + |
| 56 | +::: |
| 57 | + |
| 58 | +### All options |
| 59 | + |
| 60 | +``` |
| 61 | +lavamoat <entryPath> [Options] |
| 62 | +
|
| 63 | +Positionals: |
| 64 | + entryPath the path to the entry file for your application. same as node.js |
| 65 | + [string] |
| 66 | +
|
| 67 | +Options: |
| 68 | + --version Show version number [boolean] |
| 69 | + --help Show help [boolean] |
| 70 | + -p, --policy, --policyPath Pass in policy. Accepts a filepath |
| 71 | + string to the existing policy. When |
| 72 | + used in conjunction with |
| 73 | + --autopolicy, specifies where to |
| 74 | + write the policy. Default: |
| 75 | + ./lavamoat/node/policy.json |
| 76 | + [string] [default: "lavamoat/node/policy.json"] |
| 77 | + -o, --policyOverride, --override, Pass in override policy. Accepts a |
| 78 | + --policyOverridePath filepath string to the existing |
| 79 | + override policy. Default: |
| 80 | + ./lavamoat/node/policy-override.json |
| 81 | + [string] [default: "lavamoat/node/policy-override.json"] |
| 82 | + --policyDebug, --pd, --policydebug, Pass in debug policy. Accepts a |
| 83 | + --policyDebugPath filepath string to the existing |
| 84 | + debug policy. Default: |
| 85 | + ./lavamoat/node/policy-debug.json |
| 86 | + [string] [default: "lavamoat/node/policy-debug.json"] |
| 87 | + -a, --writeAutoPolicy, --autopolicy Generate a "policy.json" and |
| 88 | + "policy-override.json" in the |
| 89 | + current working directory. |
| 90 | + Overwrites any existing policy |
| 91 | + files. The override policy is for |
| 92 | + making manual policy changes and |
| 93 | + always takes precedence over the |
| 94 | + automatically generated policy. |
| 95 | + [boolean] [default: false] |
| 96 | + --writeAutoPolicyAndRun, --ar, parse + generate a LavaMoat policy |
| 97 | + --autorun file then execute with the new |
| 98 | + policy. [boolean] [default: false] |
| 99 | + --writeAutoPolicyDebug, --dp, when writeAutoPolicy is enabled, |
| 100 | + --debugpolicy write policy debug info to specified |
| 101 | + or default path |
| 102 | + [boolean] [default: false] |
| 103 | + --projectRoot specify the director from where |
| 104 | + packages should be resolved |
| 105 | + [string] [default: "/home/naugtur/work/metamask/metamask-extension"] |
| 106 | + -d, --debugMode, --debug Disable some protections and extra |
| 107 | + logging for easier debugging. |
| 108 | + [boolean] [default: false] |
| 109 | + --statsMode, --stats enable writing and logging of stats |
| 110 | + [boolean] [default: false] |
| 111 | +
|
| 112 | +``` |
| 113 | + |
| 114 | +## Examples |
| 115 | + |
| 116 | +### Run with Policy in default location |
| 117 | + |
| 118 | +This uses the existing policy and policy-override files to run your app. |
| 119 | + |
| 120 | +```bash |
| 121 | +lavamoat index.js |
| 122 | +``` |
| 123 | + |
| 124 | +Automatically searches for policy files inside `./lavamoat/node/`. |
| 125 | + |
| 126 | +### Policy Override with Relative Path |
| 127 | + |
| 128 | +This uses the override policy specified at `./policies/policy-override.json`. |
| 129 | + |
| 130 | +``` |
| 131 | +$ lavamoat index.js --override './policies/policy-override.json' |
| 132 | +``` |
| 133 | + |
| 134 | +## Troubleshooting |
| 135 | + |
| 136 | +- Having trouble reading thrown Errors? try running with the `--debugMode` flag. **Warning:** not safe for production runs. |
| 137 | + |
| 138 | +- Got a dependency that wont quite work under LavaMoat? try [patch-package](https://www.npmjs.com/package/patch-package) |
| 139 | + |
| 140 | +For more details go to the [troubleshooting section][troubleshooting] |
| 141 | + |
| 142 | +## Programmatic usage |
| 143 | + |
| 144 | +Programmatic usage is almost identical to the commandline and its arguments. |
| 145 | + |
| 146 | +```js |
| 147 | +const { runLava } = require('lavamoat') |
| 148 | + |
| 149 | +runLava({ |
| 150 | + entryPath: './app.js', |
| 151 | + // Optional: |
| 152 | + writeAutoPolicy: false, |
| 153 | + writeAutoPolicyDebug: false, |
| 154 | + writeAutoPolicyAndRun: false, |
| 155 | + policyPath: 'path to file', |
| 156 | + policyDebugPath: 'path to file', |
| 157 | + policyOverridePath: 'path to file', |
| 158 | + projectRoot: process.cwd(), |
| 159 | + debugMode: false, |
| 160 | + statsMode: false, |
| 161 | +}) |
| 162 | +``` |
| 163 | + |
| 164 | +[ses-github-ext]: https://github.com/endojs/endo/tree/master/packages/ses |
| 165 | +[policy-file]: ./policy |
| 166 | +[troubleshooting]: ./troubleshooting |
0 commit comments