Skip to content

Commit

Permalink
feat: add hostname for checkAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Jul 8, 2024
1 parent 3ae0296 commit f5390e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface RequestOptions {
writeStream?: Writable;
/** consume the writeStream, invoke the callback after writeStream close. */
consumeWriteStream?: boolean;
/**
/**
* The files will send with multipart/form-data format, base on formstream.
* If method not set, will use POST method by default.
*/
Expand Down Expand Up @@ -130,7 +130,7 @@ export interface RequestOptions {
* It receive two arguments(ip and family) and should return true or false to identified the address is legal or not.
* It rely on lookup and have the same version requirement.
*/
checkAddress?: (ip: string, family: number | string) => boolean;
checkAddress?: (ip: string, family: number | string, hostname: string) => boolean;
/**
* UNIX domain socket path. (Windows is not supported)
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function requestWithCallback(url, args, callback) {
lookup = function(host, dnsopts, callback) {
_lookup(host, dnsopts, function emitLookup(err, ip, family) {
// add check address logic in custom dns lookup
if (!err && !args.checkAddress(ip, family)) {
if (!err && !args.checkAddress(ip, family, host)) {
err = new Error('illegal address');
err.name = 'IllegalAddressError';
err.hostname = host;
Expand Down Expand Up @@ -1017,7 +1017,7 @@ function requestWithCallback(url, args, callback) {
family = 6;
}
if (family) {
if (!args.checkAddress(hostname, family)) {
if (!args.checkAddress(hostname, family, hostname)) {
var err = new Error('illegal address');
err.name = 'IllegalAddressError';
err.hostname = hostname;
Expand Down
16 changes: 16 additions & 0 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2076,6 +2076,22 @@ describe('test/urllib.test.js', function () {
done();
});
});

it('should work with hostname', function(done) {
let hostname;
urllib.request('http://check-ssrf-host.com/redirect_to_domain', {
checkAddress: function(address, family, aHostname) {
hostname = aHostname;
return true;
},
lookup: function(host, options, callback) {
callback(null, '10.10.10.10');
},
}, function (err) {
assert.equal(hostname, 'check-ssrf-host.com');
done();
});
});
});
}
});

0 comments on commit f5390e4

Please sign in to comment.