-
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 #458 from AikidoSec/custom-dispatcher
Immediately patch global dispatcher undici
- Loading branch information
Showing
3 changed files
with
119 additions
and
12 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,100 @@ | ||
/* eslint-disable prefer-rest-params */ | ||
import * as dns from "dns"; | ||
import * as t from "tap"; | ||
import { Token } from "../agent/api/Token"; | ||
import { Context, runWithContext } from "../agent/Context"; | ||
import { wrap } from "../helpers/wrap"; | ||
import { getMajorNodeVersion } from "../helpers/getNodeVersion"; | ||
import { Undici } from "./Undici"; | ||
import { createTestAgent } from "../helpers/createTestAgent"; | ||
|
||
function createContext(): Context { | ||
return { | ||
remoteAddress: "::1", | ||
method: "POST", | ||
url: "http://localhost:4003", | ||
query: {}, | ||
headers: {}, | ||
body: { | ||
image: "http://thisdomainpointstointernalip.com/path", | ||
}, | ||
cookies: {}, | ||
routeParams: {}, | ||
source: "express", | ||
route: "/posts/:id", | ||
}; | ||
} | ||
|
||
wrap(dns, "lookup", function lookup(original) { | ||
return function lookup() { | ||
original.apply( | ||
// @ts-expect-error We don't know the type of `this` | ||
this, | ||
["localhost", ...Array.from(arguments).slice(1)] | ||
); | ||
}; | ||
}); | ||
|
||
t.test( | ||
"it works", | ||
{ | ||
skip: | ||
getMajorNodeVersion() <= 16 ? "ReadableStream is not available" : false, | ||
}, | ||
async (t) => { | ||
const agent = createTestAgent({ | ||
token: new Token("123"), | ||
}); | ||
agent.start([new Undici()]); | ||
|
||
const { request, Dispatcher, setGlobalDispatcher, getGlobalDispatcher } = | ||
require("undici") as typeof import("undici"); | ||
|
||
// See https://www.npmjs.com/package/@n8n_io/license-sdk | ||
// They set a custom dispatcher to proxy certain requests | ||
const originalDispatcher = getGlobalDispatcher(); | ||
|
||
const kOptions = Object.getOwnPropertySymbols(originalDispatcher).find( | ||
(symbol) => { | ||
return symbol.description === "options"; | ||
} | ||
); | ||
|
||
if (!kOptions) { | ||
throw new Error("Could not find the options symbol on the dispatcher"); | ||
} | ||
|
||
// @ts-expect-error kOptions is a symbol | ||
const originalOptions = originalDispatcher[kOptions]; | ||
|
||
t.ok( | ||
"connect" in originalOptions && | ||
originalOptions.connect && | ||
"lookup" in originalOptions.connect | ||
); | ||
|
||
setGlobalDispatcher( | ||
new (class CustomDispatcher extends Dispatcher { | ||
// @ts-expect-error The types of options and handler are unknown | ||
dispatch(options, handler) { | ||
// Custom logic comes here | ||
|
||
// Fallback to the original dispatcher | ||
return originalDispatcher.dispatch(options, handler); | ||
} | ||
})() | ||
); | ||
|
||
await runWithContext(createContext(), async () => { | ||
const error = await t.rejects(() => | ||
request("http://thisdomainpointstointernalip.com") | ||
); | ||
if (error instanceof Error) { | ||
t.same( | ||
error.message, | ||
"Zen has blocked a server-side request forgery: undici.[method](...) originating from body.image" | ||
); | ||
} | ||
}); | ||
} | ||
); |
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