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 hostnameExceptionList for ssrf #100

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ In a [Server-Side Request Forgery (SSRF)](https://www.owasp.org/index.php/Server

- ipBlackList(Array) - specific which IP addresses are illegal when requested with `safeCurl`.
- ipExceptionList(Array) - specific which IP addresses are legal within ipBlackList.
- hostnameExceptionList(Array) - specific which hostname are legal within ipBlackList.
killagu marked this conversation as resolved.
Show resolved Hide resolved
- checkAddress(Function) - determine the ip by the function's return value, `false` means illegal ip.

```js
Expand All @@ -540,6 +541,10 @@ exports.security = {
'10.1.1.1',
'10.10.0.1/24',
],
// legal hostname
hostnameExceptionList: [
'example.com',
],
// checkAddress has higher priority than ipBlackList
checkAddress(ip) {
return ip !== '127.0.0.1';
Expand Down
2 changes: 1 addition & 1 deletion app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ module.exports = {
},

[CSRF_REFERER_CHECK]() {
const { refererWhiteList } = this.app.config.security.csrf;
const { refererWhiteList } = this.app.config.security.csrf.refererWhiteList;
killagu marked this conversation as resolved.
Show resolved Hide resolved
const referer = (this.headers.referer || '').toLowerCase();
if (!referer) {
debug('missing csrf referer');
Expand Down
1 change: 1 addition & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module.exports = () => {
ssrf: {
ipBlackList: null,
ipExceptionList: null,
hostnameExceptionList: null,
checkAddress: null,
},
};
Expand Down
9 changes: 8 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ exports.preprocessConfig = function(config) {
if (ssrf && ssrf.ipBlackList && !ssrf.checkAddress) {
const containsList = ssrf.ipBlackList.map(getContains);
const exceptionList = (ssrf.ipExceptionList || []).map(getContains);
ssrf.checkAddress = ipAddresses => {
const hostnameExceptionList = ssrf.hostnameExceptionList;
ssrf.checkAddress = (ipAddresses, family, hostname) => {
// Check hostname first
if (hostname && hostnameExceptionList) {
if (hostnameExceptionList.includes(hostname)) {
return true;
}
}
// ipAddresses will be array address on Node.js >= 20
// [
// { address: '220.181.125.241', family: 4 },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

exports.security = {
ssrf: {
ipBlackList: [
'10.0.0.0/8',
'127.0.0.1',
'0.0.0.0/32',
],
hostnameExceptionList: [
'registry.npmjs.org',
'registry.npmmirror.com',
],
},
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/ssrf-hostname-exception-list/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ssrf-ip-black-list"
}
31 changes: 31 additions & 0 deletions test/ssrf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,37 @@ describe('test/ssrf.test.js', () => {
assert(count === 3);
});
});

describe('hostnameExceptionList', () => {
before(() => {
app = mm.app({ baseDir: 'apps/ssrf-hostname-exception-list' });
return app.ready();
});

it('should safeCurl work', async () => {
const ctx = app.createAnonymousContext();
const host = process.env.CI ? 'registry.npmjs.org' : 'registry.npmmirror.com';
const url = `https://${host}`;
let count = 0;

mm(app, 'curl', async (url, options) => {
options.checkAddress('10.0.0.1', 4, host) && count++;
return 'response';
});
mm(app.agent, 'curl', async (url, options) => {
options.checkAddress('10.0.0.1', 4, host) && count++;
return 'response';
});
mm(ctx, 'curl', async (url, options) => {
options.checkAddress('10.0.0.1', 4, host) && count++;
return 'response';
});

await app.safeCurl(url, { dataType: 'json' });
await app.agent.safeCurl(url, { dataType: 'json' });
await ctx.safeCurl(url, { dataType: 'json' });
});
});
});

async function checkIllegalAddressError(instance, url) {
Expand Down
Loading