Détail du package

lavamoat-browserify

LavaMoat71.4kMIT18.1.8

browserify plugin for sandboxing dependencies with LavaMoat

readme

LavaMoat Browserify - a Browserify Plugin for creating LavaMoat-protected builds

Warning experimental, under development, has not been audited, etc**

lavamoat-browserify is a browserify plugin for generating app bundles protected by LavaMoat, where modules are defined in SES containers. It aims to reduce the risk of malicious code in the app dependency graph, known as "software supplychain attacks".

For an overview of LavaMoat tools see the main README.

Anatomy of a LavaMoat bundle

The lavamoat-browserify plugin replaces the last internal build step of the browserify compiler pipeline. This step takes all the modules and their metadata and outputs the final bundle content, including the LavaMoat kernel. It also generates the LavaMoat policy file.

LavaMoat builds differ from standard browserify builds in that they:

  1. Uses lockdown() from SES to prevent tampering with the execution environment. Thanks to lockdown, prototype-pollution attacks are neutralized. It's also a prerequisite to code isolation.
  2. Uses SES Compartments to isolate each package's execution. Packages don't share references to anything unless explicitly passed in or allowed by policy. Custom LavaMoat kernel handles the require() calls in the resulting bundle. When required, a module is initialized, usually by evaluation inside a SES container.
  3. Enforces the app-specified LavaMoat policy. The policy specifies what execution environment each package should run with, which means: what global/builtin APIs should it be exposed to, and what other packages can it require/import.

The result is a bundle that should work just as before, but provides some protection against supplychain attacks.

Example

Create a file, index.js with some requires.

const foo = require('./foo.js');
const gamma = require('gamma');

const elem = document.getElementById('result');
const x = foo(100);
elem.textContent = x;

Now use the browserify command with lavamoat as a plugin to build a lavamoat-protected bundle starting at index.js:

$ browserify index.js --plugin [ lavamoat-browserify --autopolicy ]

All of the modules that index.js needs are included in the bundle.js as strings to be evaluated inside SES containers. A lavamoat policy object is generated from a recursive walk of the require() graph and injected into the bundle (via --autopolicy), which is also written to disk at ./lavamoat/browserify/policy.json. Commit this policy file and regenerate it when your dependencies change and you agree with them.

Note You should review the diff in regenerated policy for suspicious changes, e.g. a simple maths package getting access to fetch or document

Warning Do not edit the autogenerated policy.json directly. It will be overwritten if a new bundle is created using LavaMoat. Instead, edit the policy-override.json.

See Policy file explained for details on the policy file definition.

To use this bundle, just toss a <script src="bundle.js"></script> into your html, as per the official browserify documentation.

Be sure to use the same Browserify configuration (eg. plugins and transforms like babelify) that you normally use, so that it can parse the code as it will appear in your final bundle.

Install

Before you use lavamoat runtime protections, make sure you've set up allow-scripts and install dependencies using that setup.

Use one of:

yarn add -D browserify lavamoat-browserify
npm i -D browserify lavamoat-browserify
npm i --ignore-scripts -g browserify lavamoat-browserify

Usage

Usage: browserify [entry files] {BROWSERIFY OPTIONS} --plugin [ lavamoat-browserify {OPTIONS} ]

Options:

 --autopolicy, -a  Generate a `policy.json` and `policy-override.json` in the current
                   working directory. Overwrites any existing policy files. The override policy is for making manual policy changes and always takes precedence over the automatically generated policy.

     --policy, -p  Pass in policy. Accepts a policy object {} or a filepath string to the existing
                   policy. When used in conjunction with --autopolicy, specifies where to write the policy. Default: ./lavamoat/browserify/policy.json

   --override, -o  Pass in override policy. Accepts a policy object {} or a filepath string to the
                   existing override policy. Default: ./lavamoat/browserify/policy-override.json

Advanced Options:

    --prelude, -pr  Omit the lavamoat prelude from the bundle.

--prunepolicy, -pp Remove redundant package entries from the policy.

--debugpolicy, -dp Generate a `policy-debug.json` in the current working directory. Used for the
                   lavamoat visualisation tool.

      --debug, -d  Turn on extra logging for debugging.

       --help, -h  Show this message

More Examples

Run with Policy

This uses the existing policy files to generate a bundle.

$ browserify index.js --plugin [ lavamoat-browserify ]

Automatically searches for policy files inside ./lavamoat/browserify/.

Policy Override with Relative Path

This uses the override policy specified at ./policies/policy-override.json to generate a new bundle.

$ browserify index.js --plugin [ lavamoat-browserify --override './policies/policy-override.json' ]

browserify API

Create a browserify bundle with LavaMoat directly from the API and write it to bundle.js.

const browserify = require('browserify')
const fs = require('fs')

const lavamoatOpts = {
  policy: '../../policy.json',
  override: '../../policy-override.json',
  writeAutoPolicyDebug: true,
  prunePolicy: true,
}

const bundler = browserify(['./index.js'], {
  plugin: [['lavamoat-browserify', lavamoatOpts]],
})

bundler.bundle().pipe(fs.createWriteStream('./bundle.js'))

Policy Formats

Policy as an object

const lavamoatOpts = {
  policy: {
    resources: {
      'dependency-name': {
        packages: {
          react: true,
        },
      },
    },
  },
}

Policy as a function, must return a file path or an object:

const lavamoatOpts = {
  policy: () => './lavamoat/policy.json',
}

OR

const policyObject = {
  resources: {
    'dependency-name': {
      packages: {
        react: true,
      },
    },
  },
}
const lavamoatOpts = {
  policy: () => policyObject,
}

See lavamoat-browserify examples for more usage examples.

See Policy file explained for details on the policy file definition.

changelog

Changelog

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^5.2.2 to ^5.2.3
      • lavamoat-core bumped from ^14.2.2 to ^14.2.3

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^5.2.3 to ^5.2.4

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^6.0.1 to ^6.0.2
      • lavamoat-core bumped from ^15.1.0 to ^15.1.1

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^6.0.3 to ^6.1.0

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^6.1.1 to ^6.1.2
      • lavamoat-core bumped from ^15.2.0 to ^15.2.1

18.1.8 (2025-08-25)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.3.3 to ^4.3.4
      • @lavamoat/lavapack bumped from ^7.0.10 to ^7.0.11
      • lavamoat-core bumped from ^16.5.0 to ^16.5.1
    • devDependencies
      • lavamoat bumped from 9.0.10 to 9.0.11

18.1.7 (2025-06-24)

Bug Fixes

  • add Node.js v24.0.0 to supported engines (ad9cdcd)
  • deps: update babel monorepo (afc9fe5)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.3.2 to ^4.3.3
      • @lavamoat/lavapack bumped from ^7.0.9 to ^7.0.10
      • lavamoat-core bumped from ^16.4.0 to ^16.5.0
    • devDependencies
      • lavamoat bumped from 9.0.9 to 9.0.10

18.1.6 (2025-05-01)

Bug Fixes

  • core: block circular global object endowments (#1505) (6745a0e)
  • deps: update dependency readable-stream to v4.7.0 (#1617) (9cb1e4b)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^7.0.8 to ^7.0.9
      • lavamoat-core bumped from ^16.3.2 to ^16.4.0
    • devDependencies
      • lavamoat bumped from 9.0.8 to 9.0.9

18.1.5 (2025-03-11)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.3.1 to ^4.3.2
      • @lavamoat/lavapack bumped from ^7.0.7 to ^7.0.8
      • lavamoat-core bumped from ^16.3.1 to ^16.3.2
    • devDependencies
      • lavamoat bumped from 9.0.7 to 9.0.8

18.1.4 (2025-01-28)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^7.0.6 to ^7.0.7
      • lavamoat-core bumped from ^16.3.0 to ^16.3.1
    • devDependencies
      • lavamoat bumped from 9.0.6 to 9.0.7

18.1.3 (2025-01-15)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^7.0.5 to ^7.0.6
      • lavamoat-core bumped from ^16.2.2 to ^16.3.0
    • devDependencies
      • lavamoat bumped from 9.0.5 to 9.0.6

18.1.2 (2024-12-09)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^7.0.4 to ^7.0.5
      • lavamoat-core bumped from ^16.2.1 to ^16.2.2
    • devDependencies
      • lavamoat bumped from 9.0.4 to 9.0.5

18.1.1 (2024-12-05)

Bug Fixes

  • make policy ordering consistntly manifest itself in json files produced (a149a7d)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^7.0.3 to ^7.0.4
      • lavamoat-core bumped from ^16.2.0 to ^16.2.1
    • devDependencies
      • lavamoat bumped from 9.0.3 to 9.0.4

18.1.0 (2024-12-04)

Features

  • browserify: allow overriding resolve implementation (7ad954f)

Bug Fixes

  • browserify: use @lavamoat/sourcemap-validator (9ad3373)
  • deps: update babel monorepo (e6f4e70)
  • deps: update babel monorepo to v7.25.9 (6d9d5a3)
  • deps: update babel monorepo to v7.26.2 (9bac12a)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.3.0 to ^4.3.1
      • @lavamoat/lavapack bumped from ^7.0.2 to ^7.0.3
      • lavamoat-core bumped from ^16.1.0 to ^16.2.0
    • devDependencies
      • lavamoat bumped from 9.0.2 to 9.0.3

18.0.2 (2024-10-11)

Bug Fixes

  • deps: update babel monorepo to v7.25.7 (1ee84bf)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^7.0.1 to ^7.0.2
      • lavamoat-core bumped from ^16.0.1 to ^16.1.0

18.0.1 (2024-09-11)

Bug Fixes

  • browserify: support Node.js ^20.17.0 (a5a3949)
  • node: support Node.js ^20.17.0 (a5a3949)
  • perf: support Node.js ^20.17.0 (a5a3949)
  • support Node.js ^20.17.0 (a5a3949)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^7.0.0 to ^7.0.1
      • lavamoat-core bumped from ^16.0.0 to ^16.0.1

18.0.0 (2024-08-15)

⚠ BREAKING CHANGES

  • node: remove support for Node.js v16
  • browserify: remove support for Node.js v16
  • perf: remove support for Node.js v16
  • tofu: remove support for Node.js v16
  • webpack: remove support for Node.js v16
  • core: remove support for Node.js v16

Bug Fixes

  • deps: update babel monorepo (b6aa714)
  • node: support Node.js ^22.5.1 (f631ae2)
  • perf: support Node.js ^22.5.1 (f631ae2)

Miscellaneous Chores

  • browserify: remove support for Node.js v16 (6ca05ba)
  • core: remove support for Node.js v16 (6ca05ba)
  • node: remove support for Node.js v16 (6ca05ba)
  • perf: remove support for Node.js v16 (6ca05ba)
  • tofu: remove support for Node.js v16 (6ca05ba)
  • webpack: remove support for Node.js v16 (6ca05ba)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^6.1.4 to ^7.0.0
      • lavamoat-core bumped from ^15.4.0 to ^16.0.0

17.0.8 (2024-07-11)

Bug Fixes

  • browserify,perf: update node.engines to match lavamoat-node (5946024)
  • deps: update babel monorepo (246ec25)
  • deps: update babel monorepo to v7.24.6 (f6d450f)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.2.0 to ^4.3.0
      • @lavamoat/lavapack bumped from ^6.1.3 to ^6.1.4
      • lavamoat-core bumped from ^15.3.0 to ^15.4.0

17.0.7 (2024-03-26)

Bug Fixes

  • deps: update dependency duplexify to v4.1.3 (c7afaf4)
  • deps: update dependency readable-stream to v4 (2a2c624)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.1.0 to ^4.2.0
      • @lavamoat/lavapack bumped from ^6.1.2 to ^6.1.3
      • lavamoat-core bumped from ^15.2.1 to ^15.3.0

17.0.5 (2024-02-29)

Bug Fixes

  • webpack: prevent lockdown from being minified (aa195e5)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.0.1 to ^4.1.0
      • @lavamoat/lavapack bumped from ^6.1.0 to ^6.1.1
      • lavamoat-core bumped from ^15.1.2 to ^15.2.0

17.0.3 (2024-02-07)

Bug Fixes

  • browserify/test: result output references in error handling (791a6af)
  • deps/browserify,viz: pify@4.0.1->5.0.0 (0feeeb2)
  • deps: update babel monorepo (e30facc)
  • deps: update dependency concat-stream to v2 (a7d9279)
  • deps: update dependency json-stable-stringify to v1.1.1 (7d28e79)
  • deps: update dependency source-map to v0.7.4 (1274a56)
  • deps: update dependency through2 to v4 (f847a36)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^6.0.2 to ^6.0.3
      • lavamoat-core bumped from ^15.1.1 to ^15.1.2

17.0.1 (2024-01-18)

Bug Fixes

  • deps: pin dependencies (4006c1f)
  • deps: update dependency convert-source-map to v2.0.0 (f91b369)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^4.0.0 to ^4.0.1
      • @lavamoat/lavapack bumped from ^6.0.0 to ^6.0.1
      • lavamoat-core bumped from ^15.0.0 to ^15.1.0

17.0.0 (2023-10-18)

⚠ BREAKING CHANGES

  • The minimum supported Node.js version is now v16.20.0.

Features

  • core: add overrideTaming: 'severe' for improved developer experience under lavamoat (#730) (20e4f76)
  • node20 (ef3a0da)

Bug Fixes

  • browserify,node,tofu,viz: bump babel packages (4e12040)
  • browserify/examples: Use workspace version of lavamoat-browserify (06de8c8)
  • drop Node.js v14 (#729) (10c667b)
  • lavapack: run build (generate runtimes) (d2749e9)
  • normalize all package versions (0800c11)
  • run test:prep (2cb45f0)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^3.1.5 to ^4.0.0
      • @lavamoat/lavapack bumped from ^5.4.1 to ^6.0.0
      • lavamoat-core bumped from ^14.4.1 to ^15.0.0

15.9.1 (2023-09-14)

Bug Fixes

  • browserify/examples: Use workspace version of lavamoat-browserify (06de8c8)
  • lavapack: run build (generate runtimes) (d2749e9)
  • normalize all package versions (0800c11)
  • run test:prep (2cb45f0)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/aa bumped from ^3.1.0 to ^3.1.5
      • @lavamoat/lavapack bumped from ^5.2.4 to ^5.4.1
      • lavamoat-core bumped from ^14.2.3 to ^14.4.1

15.7.2 (2023-09-08)

Bug Fixes

  • browserify/examples: Use workspace version of lavamoat-browserify (06de8c8)
  • lavapack: run build (generate runtimes) (d2749e9)
  • run test:prep (2cb45f0)

Dependencies

  • The following workspace dependencies were updated
    • dependencies
      • @lavamoat/lavapack bumped from ^5.2.1 to ^5.2.2
      • lavamoat-core bumped from ^14.2.1 to ^14.2.2

3.0.0

  • Plugin option writeAutoConfig now requires config to be set to a string, and uses that as the path to write the config to.