-
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 #337 from AikidoSec/fix-needle
Fix SSRF protection using needle
- Loading branch information
Showing
5 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,90 @@ | ||
import * as t from "tap"; | ||
import { Agent } from "../agent/Agent"; | ||
import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting"; | ||
import { Token } from "../agent/api/Token"; | ||
import { Context, runWithContext } from "../agent/Context"; | ||
import { LoggerNoop } from "../agent/logger/LoggerNoop"; | ||
import { HTTPRequest } from "./HTTPRequest"; | ||
|
||
const context: Context = { | ||
remoteAddress: "::1", | ||
method: "POST", | ||
url: "http://localhost:4000", | ||
query: {}, | ||
headers: {}, | ||
body: { | ||
image: "http://localhost:5000/api/internal", | ||
}, | ||
cookies: {}, | ||
routeParams: {}, | ||
source: "express", | ||
route: "/posts/:id", | ||
}; | ||
|
||
const redirectTestUrl = | ||
"http://firewallssrfredirects-env-2.eba-7ifve22q.eu-north-1.elasticbeanstalk.com"; | ||
|
||
t.test("it works", async (t) => { | ||
const agent = new Agent( | ||
true, | ||
new LoggerNoop(), | ||
new ReportingAPIForTesting(), | ||
new Token("123"), | ||
undefined | ||
); | ||
agent.start([new HTTPRequest()]); | ||
|
||
t.same(agent.getHostnames().asArray(), []); | ||
|
||
const needle = require("needle"); | ||
|
||
await runWithContext(context, async () => { | ||
await needle("get", "https://www.aikido.dev"); | ||
}); | ||
|
||
t.same(agent.getHostnames().asArray(), [ | ||
{ hostname: "www.aikido.dev", port: 443 }, | ||
]); | ||
agent.getHostnames().clear(); | ||
|
||
const error = await t.rejects( | ||
async () => | ||
await runWithContext(context, async () => { | ||
await needle("get", "http://localhost:5000/api/internal"); | ||
}) | ||
); | ||
if (error instanceof Error) { | ||
t.same( | ||
error.message, | ||
"Aikido firewall has blocked a server-side request forgery: http.request(...) originating from body.image" | ||
); | ||
} | ||
|
||
await runWithContext( | ||
{ | ||
...context, | ||
...{ body: { image: `${redirectTestUrl}/ssrf-test-domain` } }, | ||
}, | ||
async () => { | ||
await new Promise<void>((resolve) => { | ||
needle.request( | ||
"get", | ||
`${redirectTestUrl}/ssrf-test-domain`, | ||
{}, | ||
{ | ||
// eslint-disable-next-line camelcase | ||
follow_max: 1, | ||
}, | ||
(error, response) => { | ||
t.ok(error instanceof Error); | ||
t.match( | ||
error?.message, | ||
/Aikido firewall has blocked a server-side request forgery/ | ||
); | ||
resolve(); | ||
} | ||
); | ||
}); | ||
} | ||
); | ||
}); |
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