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 all 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
8 changes: 4 additions & 4 deletions src/HttpAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
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 @@ -46,13 +46,13 @@ export class HttpAgent extends Agent {
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)) {
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;
}
Expand All @@ -79,7 +79,7 @@ export class HttpAgent extends Agent {
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
23 changes: 23 additions & 0 deletions test/HttpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,28 @@ describe('HttpClient.test.ts', () => {
return true;
});
});

it('should allow hostname check', async () => {
let hostname: string;
const httpclient = new HttpClient({
checkAddress(ip, family, aHostname) {
hostname = aHostname;
return true;
},
lookup(hostname, options, callback) {
if (process.version.startsWith('v18')) {
return callback(null, '127.0.0.1', 4);
}
return callback(null, [{
address: '127.0.0.1',
family: 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