Skip to content

Commit 02cad0e

Browse files
authored
feat: add hostname for checkAddress (#526)
1 parent 3ae0296 commit 02cad0e

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface RequestOptions {
3232
writeStream?: Writable;
3333
/** consume the writeStream, invoke the callback after writeStream close. */
3434
consumeWriteStream?: boolean;
35-
/**
35+
/**
3636
* The files will send with multipart/form-data format, base on formstream.
3737
* If method not set, will use POST method by default.
3838
*/
@@ -130,7 +130,7 @@ export interface RequestOptions {
130130
* It receive two arguments(ip and family) and should return true or false to identified the address is legal or not.
131131
* It rely on lookup and have the same version requirement.
132132
*/
133-
checkAddress?: (ip: string, family: number | string) => boolean;
133+
checkAddress?: (ip: string, family: number | string, hostname: string) => boolean;
134134
/**
135135
* UNIX domain socket path. (Windows is not supported)
136136
*/

lib/urllib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function requestWithCallback(url, args, callback) {
276276
lookup = function(host, dnsopts, callback) {
277277
_lookup(host, dnsopts, function emitLookup(err, ip, family) {
278278
// add check address logic in custom dns lookup
279-
if (!err && !args.checkAddress(ip, family)) {
279+
if (!err && !args.checkAddress(ip, family, host)) {
280280
err = new Error('illegal address');
281281
err.name = 'IllegalAddressError';
282282
err.hostname = host;
@@ -1017,7 +1017,7 @@ function requestWithCallback(url, args, callback) {
10171017
family = 6;
10181018
}
10191019
if (family) {
1020-
if (!args.checkAddress(hostname, family)) {
1020+
if (!args.checkAddress(hostname, family, hostname)) {
10211021
var err = new Error('illegal address');
10221022
err.name = 'IllegalAddressError';
10231023
err.hostname = hostname;

test/urllib.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,22 @@ describe('test/urllib.test.js', function () {
20762076
done();
20772077
});
20782078
});
2079+
2080+
it('should work with hostname', function(done) {
2081+
let hostname;
2082+
urllib.request('http://check-ssrf-host.com/redirect_to_domain', {
2083+
checkAddress: function(address, family, aHostname) {
2084+
hostname = aHostname;
2085+
return true;
2086+
},
2087+
lookup: function(host, options, callback) {
2088+
callback(null, '10.10.10.10');
2089+
},
2090+
}, function () {
2091+
assert.equal(hostname, 'check-ssrf-host.com');
2092+
done();
2093+
});
2094+
});
20792095
});
20802096
}
20812097
});

0 commit comments

Comments
 (0)