-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add end2end test for apps bundled with esbuild #459
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
345bf7d
Add end2end test for apps bundled with esbuild
hansott 0e29d6a
Simplify docs
hansott c2fb2e1
Improve docs
hansott 947b06d
Replace with Zen
hansott 947b541
Deduplicate
hansott 40261b7
Add notes to esbuild docs
hansott 7766003
Update docs/esbuild.md
willem-delbare 04d411a
Update docs/esbuild.md
willem-delbare f05b5f0
Add instructions to top
hansott 7f719c9
Merge branch 'esbuild' of github.com:AikidoSec/node-RASP into esbuild
hansott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Existing tests are run twice, for the normal app.js and once for compiled.js