-
Notifications
You must be signed in to change notification settings - Fork 22
Block outbound HTTP(s) requests #646
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
Open
timokoessler
wants to merge
19
commits into
main
Choose a base branch
from
block-outbound
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
48776d1
Add types for outbound blocking
timokoessler d05edaf
Prepare ServiceConfig and stats
timokoessler 662bc44
Fix linting
timokoessler 7c3beb4
Add blocking to Fetch, http and Undici
timokoessler b79cc2c
Extend test coverage
timokoessler 094577a
Rename, fix tests
timokoessler f641243
Merge branch 'main' into block-outbound
timokoessler 0313529
Merge branch 'main' into block-outbound
timokoessler 19f00e9
Fix merge
timokoessler 7a3d47c
Merge branch 'main' of github.com:AikidoSec/node-RASP into block-outb…
hansott bb887f4
Fix ESM tests (port as string vs number)
hansott 83b2169
Merge branch 'main' of github.com:AikidoSec/node-RASP into block-outb…
hansott 8a7fbc1
Merge branch 'main' of github.com:AikidoSec/node-RASP into block-outb…
hansott d02cb1f
Remove blockedHits
hansott 4f8609d
Simplify InspectionResult and move domains to normal config
hansott 45d0ced
Align message with expected test message
hansott 2a4b71c
Fix tests
hansott 075ab8e
Fix test
hansott 0d9707c
Add end2end tests for blocking outbound connections
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 hidden or 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 hidden or 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 hidden or 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,207 @@ | ||
| import { spawn } from "child_process"; | ||
| import { resolve } from "path"; | ||
| import { test } from "node:test"; | ||
| import { equal, match, fail } from "node:assert"; | ||
| import { getRandomPort } from "./utils/get-port.mjs"; | ||
| import { timeout } from "./utils/timeout.mjs"; | ||
|
|
||
| const pathToAppDir = resolve( | ||
| import.meta.dirname, | ||
| "../../sample-apps/hono-pg-esm" | ||
| ); | ||
| const testServerUrl = "http://localhost:5874"; | ||
|
|
||
| const blockedUrl = "https://ssrf-redirects.testssandbox.com/"; | ||
| const allowedUrl = "https://aikido.dev/"; | ||
| const unknownUrl = "https://google.com/"; | ||
|
|
||
| test("blockNewOutgoingRequests is false", async () => { | ||
| const port = await getRandomPort(); | ||
|
|
||
| const response = await fetch(`${testServerUrl}/api/runtime/apps`, { | ||
| method: "POST", | ||
| }); | ||
| const body = await response.json(); | ||
| const token = body.token; | ||
|
|
||
| const config = await fetch(`${testServerUrl}/api/runtime/config`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: token, | ||
| }, | ||
| body: JSON.stringify({ | ||
| allowedIPAddresses: ["1.2.3.4"], | ||
| blockNewOutgoingRequests: false, | ||
| domains: [ | ||
| { hostname: "ssrf-redirects.testssandbox.com", mode: "block" }, | ||
| { hostname: "aikido.dev", mode: "allow" }, | ||
| // Otherwise we cannot communicate with the mock server | ||
| { hostname: "localhost", mode: "allow" }, | ||
| ], | ||
| }), | ||
| }); | ||
| equal(config.status, 200); | ||
|
|
||
| const server = spawn( | ||
| `node`, | ||
| ["--require", "@aikidosec/firewall/instrument", "./app.js", port], | ||
| { | ||
| cwd: pathToAppDir, | ||
| env: { | ||
| ...process.env, | ||
| AIKIDO_DEBUG: "true", | ||
| AIKIDO_BLOCK: "true", | ||
| AIKIDO_TOKEN: token, | ||
| AIKIDO_ENDPOINT: testServerUrl, | ||
| AIKIDO_REALTIME_ENDPOINT: testServerUrl, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| server.on("error", (err) => { | ||
| fail(err); | ||
| }); | ||
|
|
||
| let stderr = ""; | ||
| server.stderr.on("data", (data) => { | ||
| stderr += data.toString(); | ||
| }); | ||
|
|
||
| try { | ||
| await timeout(2000); | ||
|
|
||
| // Blocks request to blocked domain | ||
| const blocked = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(blockedUrl)}`, | ||
| { signal: AbortSignal.timeout(5000) } | ||
| ); | ||
| equal(blocked.status, 500); | ||
| match(stderr, /Zen has blocked an outbound connection/); | ||
|
|
||
| // Allows request to allowed domain | ||
| const allowed = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(allowedUrl)}`, | ||
| { signal: AbortSignal.timeout(5000) } | ||
| ); | ||
| equal(allowed.status, 200); | ||
| equal((await allowed.json()).success, true); | ||
|
|
||
| // Allows request to unknown domain | ||
| const unknown = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(unknownUrl)}`, | ||
| { signal: AbortSignal.timeout(5000) } | ||
| ); | ||
| equal(unknown.status, 200); | ||
| equal((await unknown.json()).success, true); | ||
|
|
||
| // Allows blocked domain from bypass IP | ||
| stderr = ""; | ||
| const bypass = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(blockedUrl)}`, | ||
| { | ||
| headers: { "X-Forwarded-For": "1.2.3.4" }, | ||
| signal: AbortSignal.timeout(5000), | ||
| } | ||
| ); | ||
| equal(bypass.status, 200); | ||
| equal((await bypass.json()).success, true); | ||
| } finally { | ||
| server.kill(); | ||
| } | ||
| }); | ||
|
|
||
| test("blockNewOutgoingRequests is true", async () => { | ||
| const port = await getRandomPort(); | ||
|
|
||
| const response = await fetch(`${testServerUrl}/api/runtime/apps`, { | ||
| method: "POST", | ||
| }); | ||
| const body = await response.json(); | ||
| const token = body.token; | ||
|
|
||
| const config = await fetch(`${testServerUrl}/api/runtime/config`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: token, | ||
| }, | ||
| body: JSON.stringify({ | ||
| allowedIPAddresses: ["1.2.3.4"], | ||
| blockNewOutgoingRequests: true, | ||
| domains: [ | ||
| { hostname: "ssrf-redirects.testssandbox.com", mode: "block" }, | ||
| { hostname: "aikido.dev", mode: "allow" }, | ||
| ], | ||
| }), | ||
| }); | ||
| equal(config.status, 200); | ||
|
|
||
| const server = spawn( | ||
| `node`, | ||
| ["--require", "@aikidosec/firewall/instrument", "./app.js", port], | ||
| { | ||
| cwd: pathToAppDir, | ||
| env: { | ||
| ...process.env, | ||
| AIKIDO_DEBUG: "true", | ||
| AIKIDO_BLOCK: "true", | ||
| AIKIDO_TOKEN: token, | ||
| AIKIDO_ENDPOINT: testServerUrl, | ||
| AIKIDO_REALTIME_ENDPOINT: testServerUrl, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| server.on("error", (err) => { | ||
| fail(err); | ||
| }); | ||
|
|
||
| let stderr = ""; | ||
| server.stderr.on("data", (data) => { | ||
| stderr += data.toString(); | ||
| }); | ||
|
|
||
| try { | ||
| await timeout(2000); | ||
|
|
||
| // Blocks request to blocked domain | ||
| const blocked = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(blockedUrl)}`, | ||
| { signal: AbortSignal.timeout(5000) } | ||
| ); | ||
| equal(blocked.status, 500); | ||
| match(stderr, /Zen has blocked an outbound connection/); | ||
|
|
||
| // Allows request to allowed domain | ||
| const allowed = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(allowedUrl)}`, | ||
| { signal: AbortSignal.timeout(5000) } | ||
| ); | ||
| equal(allowed.status, 200); | ||
| equal((await allowed.json()).success, true); | ||
|
|
||
| // Blocks request to unknown domain | ||
| stderr = ""; | ||
| const unknown = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(unknownUrl)}`, | ||
| { signal: AbortSignal.timeout(5000) } | ||
| ); | ||
| equal(unknown.status, 500); | ||
| match(stderr, /Zen has blocked an outbound connection/); | ||
|
|
||
| // Allows unknown domain from bypass IP | ||
| stderr = ""; | ||
| const bypass = await fetch( | ||
| `http://127.0.0.1:${port}/fetch?url=${encodeURIComponent(unknownUrl)}`, | ||
| { | ||
| headers: { "X-Forwarded-For": "1.2.3.4" }, | ||
| signal: AbortSignal.timeout(5000), | ||
| } | ||
| ); | ||
| equal(bypass.status, 200); | ||
| equal((await bypass.json()).success, true); | ||
| } finally { | ||
| server.kill(); | ||
| } | ||
| }); |
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.
why is that directory called tests-new btw?
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.
We use two separate test runners for the old and new e2e tests :). The reason was a bug preventing ESM tests using the old one and that we would like to switch.