Skip to content

Commit

Permalink
feat: Add support for using existing http(s).Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
y-mehta committed Aug 17, 2023
1 parent 16139b7 commit 286e29d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
18 changes: 14 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const checkIp = (ip) => {
return true;
};

const manageConnection = (url) => {
const httpAgent = new http.Agent();
const httpsAgent = new https.Agent();
const agent = url.startsWith('https') ? httpsAgent : httpAgent;
// prevent memory leak
const ACTIVE = Symbol('active');

const requestFilterHandler = (agent)=>{
if (agent[ACTIVE]) return agent;
agent[ACTIVE] = true;
const {createConnection} = agent;
agent.createConnection = function(options, func) {
const {host: address} = options;
Expand All @@ -40,4 +42,12 @@ const manageConnection = (url) => {
return agent;
};

const manageConnection = (url) => {
const httpAgent = new http.Agent();
const httpsAgent = new https.Agent();
const agent = url.startsWith('https') ? httpsAgent : httpAgent;
return requestFilterHandler(agent);
};

module.exports = (url) => manageConnection(url);
module.exports.requestFilterHandler = (agent) => requestFilterHandler(agent);
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 63 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const ssrfFilter = require('../lib/index.js');
const {requestFilterHandler} = require('../lib/index.js');
const http = require('http');
const https = require('https');
const axios = require('axios');
const fs = require('fs');
const expect = require('chai').expect;
Expand All @@ -7,7 +10,11 @@ const allowedUrlsFile = `${__dirname}/allowedUrls.txt`;
let blockUrls;
let allowedUrls;

// Test: Blocked URLs

const httpAgent = new http.Agent();
const httpsAgent = new https.Agent();

// ssrfFilter: Test: Blocked URLs
try {
blockUrls = JSON.parse(fs.readFileSync(blockUrlsFile));
} catch (err) {
Expand All @@ -17,11 +24,11 @@ try {
blockUrls.forEach((url)=>{
it(`${url} is Blocked`, async () => {
let check = 0;
// eslint-disable-next-line max-len
const response = await axios.get(url, {httpAgent: ssrfFilter(url),
httpsAgent: ssrfFilter(url)})
.then((response) => {
check = 1;
console.log(response);
})
.catch((error) => {
check = 0;
Expand All @@ -33,7 +40,7 @@ blockUrls.forEach((url)=>{
});
});

// Test: Allowed URLs
// ssrfFilter: Test: Allowed URLs
try {
allowedUrls = JSON.parse(fs.readFileSync(allowedUrlsFile));
} catch (err) {
Expand All @@ -58,7 +65,7 @@ allowedUrls.forEach((url)=>{
});
});

// Test: DNS Rebind
// ssrfFilter: Test: DNS Rebind
it(`Test DNS Rebind`, async () => {
let check = 0;
const url = 'http://s-35.185.206.165-127.0.0.1-'+ new Date().valueOf() +'-rr-e.d.rebind.it';
Expand All @@ -80,3 +87,55 @@ it(`Test DNS Rebind`, async () => {
});
expect(response).to.equal(1);
});

// requestFilterHandler: Test: Blocked URLs
try {
blockUrls = JSON.parse(fs.readFileSync(blockUrlsFile));
} catch (err) {
console.log(err);
}

blockUrls.forEach((url)=>{
it(`${url} is Blocked`, async () => {
let check = 0;
// eslint-disable-next-line max-len
const response = await axios.get(url, {httpAgent: requestFilterHandler(httpAgent),
httpsAgent: requestFilterHandler(httpsAgent)})
.then((response) => {
check = 1;
})
.catch((error) => {
check = 0;
})
.then(() => {
return check;
});
expect(response).to.equal(0);
});
});

// requestFilterHandler: Test: Allowed URLs
try {
allowedUrls = JSON.parse(fs.readFileSync(allowedUrlsFile));
} catch (err) {
console.log(err);
}

allowedUrls.forEach((url)=>{
it(`${url} is Allowed`, async () => {
let check = 0;
// eslint-disable-next-line max-len
const response = await axios.get(url, {httpAgent: requestFilterHandler(httpAgent),
httpsAgent: requestFilterHandler(httpsAgent)})
.then((response) => {
check = 1;
})
.catch((error) => {
check = 0;
})
.then(() => {
return check;
});
expect(response).to.equal(1);
});
});

0 comments on commit 286e29d

Please sign in to comment.