Skip to content

Commit 0022641

Browse files
authored
feat: add hostname for checkAddress (#525) (#528)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced hostname validation in address checking for enhanced security. - **Tests** - Added a new test case to verify hostname validation logic. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 97a25c5 commit 0022641

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/HttpAgent.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
buildConnector,
77
} from 'undici';
88

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

1111
export type HttpAgentOptions = {
1212
lookup?: LookupFunction;
@@ -46,13 +46,13 @@ export class HttpAgent extends Agent {
4646
if (options.checkAddress) {
4747
// dnsOptions.all set to default on Node.js >= 20, dns.lookup will return address array object
4848
if (typeof address === 'string') {
49-
if (!options.checkAddress(address, family)) {
49+
if (!options.checkAddress(address, family, hostname)) {
5050
err = new IllegalAddressError(hostname, address, family);
5151
}
5252
} else if (Array.isArray(address)) {
5353
const addresses = address as { address: string, family: number }[];
5454
for (const addr of addresses) {
55-
if (!options.checkAddress(addr.address, addr.family)) {
55+
if (!options.checkAddress(addr.address, addr.family, hostname)) {
5656
err = new IllegalAddressError(hostname, addr.address, addr.family);
5757
break;
5858
}
@@ -79,7 +79,7 @@ export class HttpAgent extends Agent {
7979
const family = isIP(hostname);
8080
if (family === 4 || family === 6) {
8181
// if request hostname is ip, custom lookup won't execute
82-
if (!this.#checkAddress(hostname, family)) {
82+
if (!this.#checkAddress(hostname, family, hostname)) {
8383
throw new IllegalAddressError(hostname, hostname, family);
8484
}
8585
}

test/HttpClient.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,32 @@ describe('HttpClient.test.ts', () => {
313313
return true;
314314
});
315315
});
316+
317+
it('should allow hostname check', async () => {
318+
let hostname: string;
319+
const httpclient = new HttpClient({
320+
checkAddress(ip, family, aHostname) {
321+
hostname = aHostname;
322+
return true;
323+
},
324+
lookup(hostname, options, callback) {
325+
if (
326+
process.version.startsWith('v18')
327+
|| process.version.startsWith('v16')
328+
|| process.version.startsWith('v14')
329+
) {
330+
return callback(null, '127.0.0.1', 4);
331+
}
332+
return callback(null, [{
333+
address: '127.0.0.1',
334+
family: 4,
335+
}]);
336+
},
337+
});
338+
339+
const response = await httpclient.request(_url.replace('localhost', 'check-host-ssrf.com'));
340+
assert.equal(hostname, 'check-host-ssrf.com');
341+
assert.equal(response.status, 200);
342+
});
316343
});
317344
});

0 commit comments

Comments
 (0)