Skip to content

Commit

Permalink
added functionality to stop request even if socket is closed to preve…
Browse files Browse the repository at this point in the history
…nt url redirection attack
  • Loading branch information
jamesbyrne113 committed Mar 29, 2022
1 parent 37513d7 commit dd1f4fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/request-filtering-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface RequestFilteringAgentOptions {
// Deny address list
// Default: []
denyIPAddressList?: string[];

stopUrlRedirectionAttack?: boolean;
}

/**
Expand Down Expand Up @@ -108,7 +110,9 @@ export function applyRequestFilter<T extends http.Agent | https.Agent>(
options && options.allowPrivateIPAddress !== undefined ? options.allowPrivateIPAddress : false,
allowMetaIPAddress: options && options.allowMetaIPAddress !== undefined ? options.allowMetaIPAddress : false,
allowIPAddressList: options && options.allowIPAddressList ? options.allowIPAddressList : [],
denyIPAddressList: options && options.denyIPAddressList ? options.denyIPAddressList : []
denyIPAddressList: options && options.denyIPAddressList ? options.denyIPAddressList : [],
stopUrlRedirectionAttack:
options && options.stopUrlRedirectionAttack !== undefined ? options.stopUrlRedirectionAttack : false
};
// override http.Agent#createConnection
// https://nodejs.org/api/http.html#http_agent_createconnection_options_callback
Expand All @@ -117,6 +121,23 @@ export function applyRequestFilter<T extends http.Agent | https.Agent>(
const createConnection = agent.createConnection;
// @ts-expect-error - @types/node does not defined createConnection
agent.createConnection = (options: TcpNetConnectOpts, connectionListener?: (error?: Error) => void) => {
if (requestFilterOptions.stopUrlRedirectionAttack) {
// Prevents malicious user from identifying which ports are open
const { host, family } = options;
if (host) {
if (net.isIP(host)) {
const addr = ipaddr.parse(host);
const range = addr.range();

if (range !== "unicast") {
throw new Error(
`DNS lookup ${host}(family:${family}, host:${host}) is not allowed. Because, It is private IP address.`
);
}
}
}
}

const socket = createConnection.call(agent, options, () => {
// https://nodejs.org/api/net.html#net_socket_connect_options_connectlistener
const { host } = options;
Expand Down
1 change: 1 addition & 0 deletions test/request-filtering-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ describe("request-filtering-agent", function () {
if (error instanceof ReferenceError) {
assert.fail(error);
}
assert.match(error.message, /failed, reason: DNS lookup/);
assert.strictEqual(error.type, "system", `Failed at ${ipAddress}, error: ${error}`);
}
}
Expand Down

0 comments on commit dd1f4fe

Please sign in to comment.