Skip to content
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

feat: add hostname for checkAddress #525

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/HttpAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
buildConnector,
} from 'undici';

export type CheckAddressFunction = (ip: string, family: number | string) => boolean;
export type CheckAddressFunction = (ip: string, family: number | string, hostname: string) => boolean;

export type HttpAgentOptions = {
lookup?: LookupFunction;
Expand Down Expand Up @@ -42,24 +42,25 @@
// address will be array on Node.js >= 20
const address = args[0];
const family = args[1];
console.log('args: ', args, address, family);
if (err) return (callback as any)(err, address, family);
if (options.checkAddress) {
// dnsOptions.all set to default on Node.js >= 20, dns.lookup will return address array object
if (typeof address === 'string') {
if (!options.checkAddress(address, family)) {
if (!options.checkAddress(address, family, hostname)) {
err = new IllegalAddressError(hostname, address, family);
}
} else if (Array.isArray(address)) {
const addresses = address as { address: string, family: number }[];
for (const addr of addresses) {
if (!options.checkAddress(addr.address, addr.family)) {
if (!options.checkAddress(addr.address, addr.family, hostname)) {

Check warning on line 56 in src/HttpAgent.ts

View check run for this annotation

Codecov / codecov/patch

src/HttpAgent.ts#L56

Added line #L56 was not covered by tests
Comment on lines +49 to +55
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Function call update required.

Some instances of the checkAddress function do not match the new signature with the hostname parameter. Please update the following locations:

  • test/HttpClient.test.ts: lines where checkAddress is called without the hostname parameter.
  • src/HttpClient.ts: lines where checkAddress is called without the hostname parameter.
Analysis chain

Function call update approved.

The checkAddress function is now called with the hostname parameter, which is correctly passed in both instances.

However, ensure that all function calls to checkAddress match the new signature.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `checkAddress` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type-add 'ts' --type-add 'tsx' --type ts,tsx 'checkAddress'

Length of output: 120


Script:

#!/bin/bash
# Description: Verify all function calls to `checkAddress` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type-add 'ts:*.ts' --type-add 'tsx:*.tsx' --type ts,tsx 'checkAddress'

Length of output: 106


Script:

#!/bin/bash
# Description: Verify all function calls to `checkAddress` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type-add 'ts:*.ts' --type-add 'tsx:*.tsx' --type ts --type tsx 'checkAddress'

Length of output: 1249

err = new IllegalAddressError(hostname, addr.address, addr.family);
break;
}
}
}
}
(callback as any)(err, address, family);

Check failure on line 63 in src/HttpAgent.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

test/HttpClient.test.ts > HttpClient.test.ts > clientOptions.checkAddress > should allow hostname check

TypeError: Invalid IP address: undefined ❯ emitLookup node:net:1464:17 ❯ src/HttpAgent.ts:63:10 ❯ lookup test/HttpClient.test.ts:346:18 ❯ lookup src/HttpAgent.ts:41:7 ❯ emitLookup node:net:1413:5 ❯ lookupAndConnectMultiple node:net:1412:3 ❯ node:net:1358:7 ❯ lookupAndConnect node:net:1357:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_IP_ADDRESS', opaque: undefined, status: -1, headers: {}, res: { status: -1, statusCode: -1, statusText: '', statusMessage: '', headers: {}, size: +0, aborted: false, rt: 3.095, keepAliveSocket: true, requestUrls: [ 'http://check-host-ssrf.com:49199/' ], timing: { queuing: 0.094, connected: +0, requestHeadersSent: +0, requestSent: +0, waiting: +0, contentDownload: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:21.511Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:49199', connectPort: '49199' }, retries: +0, socketErrorRetries: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:21.511Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:49199', connectPort: '49199' } }

Check failure on line 63 in src/HttpAgent.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

test/HttpClient.test.ts > HttpClient.test.ts > clientOptions.checkAddress > should allow hostname check

TypeError: Invalid IP address: undefined ❯ emitLookup node:net:1465:17 ❯ src/HttpAgent.ts:63:10 ❯ lookup test/HttpClient.test.ts:346:18 ❯ lookup src/HttpAgent.ts:41:7 ❯ emitLookup node:net:1414:5 ❯ lookupAndConnectMultiple node:net:1413:3 ❯ node:net:1359:7 ❯ lookupAndConnect node:net:1358:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_IP_ADDRESS', opaque: undefined, status: -1, headers: {}, res: { status: -1, statusCode: -1, statusText: '', statusMessage: '', headers: {}, size: +0, aborted: false, rt: 4.624, keepAliveSocket: true, requestUrls: [ 'http://check-host-ssrf.com:49201/' ], timing: { queuing: 0.182, connected: +0, requestHeadersSent: +0, requestSent: +0, waiting: +0, contentDownload: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:29.249Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:49201', connectPort: '49201' }, retries: +0, socketErrorRetries: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:29.249Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:49201', connectPort: '49201' } }

Check failure on line 63 in src/HttpAgent.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

test/HttpClient.test.ts > HttpClient.test.ts > clientOptions.checkAddress > should allow hostname check

TypeError: Invalid IP address: undefined ❯ emitLookup node:net:1464:17 ❯ src/HttpAgent.ts:63:10 ❯ lookup test/HttpClient.test.ts:346:18 ❯ lookup src/HttpAgent.ts:41:7 ❯ emitLookup node:net:1413:5 ❯ lookupAndConnectMultiple node:net:1412:3 ❯ node:net:1358:7 ❯ lookupAndConnect node:net:1357:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_IP_ADDRESS', opaque: undefined, status: -1, headers: {}, res: { status: -1, statusCode: -1, statusText: '', statusMessage: '', headers: {}, size: +0, aborted: false, rt: 4.97, keepAliveSocket: true, requestUrls: [ 'http://check-host-ssrf.com:41517/' ], timing: { queuing: 0.121, connected: +0, requestHeadersSent: +0, requestSent: +0, waiting: +0, contentDownload: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:00.292Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:41517', connectPort: '41517' }, retries: +0, socketErrorRetries: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:00.292Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:41517', connectPort: '41517' } }

Check failure on line 63 in src/HttpAgent.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

test/HttpClient.test.ts > HttpClient.test.ts > clientOptions.checkAddress > should allow hostname check

TypeError: Invalid IP address: undefined ❯ emitLookup node:net:1465:17 ❯ src/HttpAgent.ts:63:10 ❯ lookup test/HttpClient.test.ts:346:18 ❯ lookup src/HttpAgent.ts:41:7 ❯ emitLookup node:net:1414:5 ❯ lookupAndConnectMultiple node:net:1413:3 ❯ node:net:1359:7 ❯ lookupAndConnect node:net:1358:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_IP_ADDRESS', opaque: undefined, status: -1, headers: {}, res: { status: -1, statusCode: -1, statusText: '', statusMessage: '', headers: {}, size: +0, aborted: false, rt: 3.196, keepAliveSocket: true, requestUrls: [ 'http://check-host-ssrf.com:34891/' ], timing: { queuing: 0.156, connected: +0, requestHeadersSent: +0, requestSent: +0, waiting: +0, contentDownload: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:00.484Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:34891', connectPort: '34891' }, retries: +0, socketErrorRetries: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:00.484Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:34891', connectPort: '34891' } }

Check failure on line 63 in src/HttpAgent.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

test/HttpClient.test.ts > HttpClient.test.ts > clientOptions.checkAddress > should allow hostname check

TypeError: Invalid IP address: undefined ❯ emitLookup node:net:1464:17 ❯ src/HttpAgent.ts:63:10 ❯ lookup test/HttpClient.test.ts:346:18 ❯ lookup src/HttpAgent.ts:41:7 ❯ emitLookup node:net:1413:5 ❯ lookupAndConnectMultiple node:net:1412:3 ❯ node:net:1358:7 ❯ lookupAndConnect node:net:1357:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_IP_ADDRESS', opaque: undefined, status: -1, headers: {}, res: { status: -1, statusCode: -1, statusText: '', statusMessage: '', headers: {}, size: +0, aborted: false, rt: 4.893, keepAliveSocket: true, requestUrls: [ 'http://check-host-ssrf.com:53794/' ], timing: { queuing: 0.194, connected: +0, requestHeadersSent: +0, requestSent: +0, waiting: +0, contentDownload: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:21:06.119Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:53794', connectPort: '53794' }, retries: +0, socketErrorRetries: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:21:06.119Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:53794', connectPort: '53794' } }

Check failure on line 63 in src/HttpAgent.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

test/HttpClient.test.ts > HttpClient.test.ts > clientOptions.checkAddress > should allow hostname check

TypeError: Invalid IP address: undefined ❯ emitLookup node:net:1465:17 ❯ src/HttpAgent.ts:63:10 ❯ lookup test/HttpClient.test.ts:346:18 ❯ lookup src/HttpAgent.ts:41:7 ❯ emitLookup node:net:1414:5 ❯ lookupAndConnectMultiple node:net:1413:3 ❯ node:net:1359:7 ❯ lookupAndConnect node:net:1358:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_INVALID_IP_ADDRESS', opaque: undefined, status: -1, headers: {}, res: { status: -1, statusCode: -1, statusText: '', statusMessage: '', headers: {}, size: +0, aborted: false, rt: 4.195, keepAliveSocket: true, requestUrls: [ 'http://check-host-ssrf.com:58556/' ], timing: { queuing: 0.166, connected: +0, requestHeadersSent: +0, requestSent: +0, waiting: +0, contentDownload: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:31.583Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:58556', connectPort: '58556' }, retries: +0, socketErrorRetries: +0 }, socket: { id: 21, localAddress: '', localPort: +0, remoteAddress: '', remotePort: +0, remoteFamily: '', bytesWritten: +0, bytesRead: +0, handledRequests: +0, handledResponses: +0, connectErrorTime: '2024-07-08T07:20:31.583Z', connectProtocol: 'http:', connectHost: 'check-host-ssrf.com:58556', connectPort: '58556' } }
});
};
super({
Expand All @@ -79,7 +80,7 @@
const family = isIP(hostname);
if (family === 4 || family === 6) {
// if request hostname is ip, custom lookup won't execute
if (!this.#checkAddress(hostname, family)) {
if (!this.#checkAddress(hostname, family, hostname)) {
throw new IllegalAddressError(hostname, hostname, family);
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/HttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,24 @@ describe('HttpClient.test.ts', () => {
return true;
});
});

it('should allow hostname check', async () => {
let hostname: string;
const httpclient = new HttpClient({
checkAddress(ip, family, aHostname) {
console.log('ip: ', ip, family, aHostname);
hostname = aHostname;
return true;
},
lookup(hostname, options, callback) {
console.log('lookup: ', hostname, options);
return callback(null, '127.0.0.1', 4);
},
});

const response = await httpclient.request(_url.replace('localhost', 'check-host-ssrf.com'));
assert.equal(hostname, 'check-host-ssrf.com');
assert.equal(response.status, 200);
});
});
});
Loading