-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #459 from AikidoSec/esbuild
Add end2end test for apps bundled with esbuild
- Loading branch information
Showing
10 changed files
with
617 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Installing Zen in a Node.js Application Bundled with esbuild | ||
|
||
Note: Zen runs only on the server side, it does not run in the browser. | ||
|
||
Note: If `bundle` is set to `false` in the esbuild configuration, Zen will work without any additional configuration. | ||
|
||
Modify your esbuild configuration to include the external option using this utility: | ||
|
||
```js | ||
const { build } = require("esbuild"); | ||
const { externals } = require("@aikidosec/firewall/bundler"); // <-- Add this line | ||
|
||
build({ | ||
entryPoints: ["./app.js"], | ||
bundle: true, | ||
platform: "node", | ||
target: "node18", | ||
outfile: "./dist/app.js", | ||
external: externals(), // <-- Add this line | ||
}); | ||
``` | ||
|
||
This tells esbuild to exclude @aikidosec/firewall and any packages that Zen hooks into from the bundle. | ||
|
||
⚠️ Don't forget to copy the node_modules directory to the output directory. | ||
|
||
## Why do I need to do this? | ||
|
||
Zen works by intercepting `require()` calls that a Node.js application makes when loading modules. This includes modules that are built-in to Node.js, like the `fs` module for accessing the filesystem, as well as modules installed from the NPM registry, like the `pg` database module. | ||
|
||
Bundlers like esbuild crawl all of the `require()` calls that an application makes to files on disk. It replaces the `require()` calls with custom code and combines all the resulting JavaScript into one "bundled" file. When a built-in module is loaded, such as `require('fs')`, that call can then remain the same in the resulting bundle. | ||
|
||
Zen can continue to intercept the calls for built-in modules but cannot intercept calls to third party libraries under those conditions. This means that when you bundle a Zen app with a bundler Zen is likely to capture information about disk access (through `fs`) and outbound HTTP requests (through `http`), but omit calls to third party libraries. | ||
|
||
The solution is to treat all third party modules that Zen needs to instrument as being "external" to the bundler. With this setting the instrumented modules remain on disk and continue to be loaded with `require()` while the non-instrumented modules are bundled. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as t from "tap"; | ||
import { externals } from "./externals"; | ||
|
||
t.test("it returns externals", async (t) => { | ||
t.ok(externals().includes("@aikidosec/firewall")); | ||
t.ok(externals().includes("pg")); | ||
t.ok(externals().includes("mysql")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Hooks } from "../agent/hooks/Hooks"; | ||
import { getWrappers } from "../agent/protect"; | ||
|
||
export function externals() { | ||
const wrappers = getWrappers(); | ||
const hooks = new Hooks(); | ||
|
||
wrappers.forEach((wrapper) => { | ||
wrapper.wrap(hooks); | ||
}); | ||
|
||
const packages = ["@aikidosec/firewall"].concat( | ||
hooks.getPackages().map((pkg) => pkg.getName()) | ||
); | ||
|
||
// Remove duplicates | ||
return Array.from(new Set(packages)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { externals } from "./externals"; | ||
|
||
// eslint-disable-next-line import/no-unused-modules | ||
export { externals }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/compiled.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { build } = require("esbuild"); | ||
const { externals } = require("@aikidosec/firewall/bundler"); | ||
|
||
build({ | ||
entryPoints: ["./app.js"], | ||
bundle: true, | ||
platform: "node", | ||
target: "node18", | ||
outfile: "./compiled.js", | ||
external: externals(), | ||
}); |
Oops, something went wrong.