-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support SSRF check on useHttpClientNext = true (#96)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new workflow for publishing any commit. - Added SSRF protection configurations and a new HTTP client instance in `config.default.js`. - **Bug Fixes** - Updated Node.js version requirements to `>=14.20.0` in workflow and `package.json`. - **Documentation** - Updated contributors and license sections in `README.md` and `README.zh-CN.md`. - **Refactor** - Renamed `safeCurl` function to `safeCurlForApplication` and `safeCurlForContext`. - **Tests** - Added new test cases for SSRF protection configurations in `ssrf.test.js`. - **Chores** - Updated `egg` dependency to version `^3.26.0`. - Removed `git-contributor` dependency. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
12 changed files
with
109 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Publish Any Commit | ||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- run: corepack enable | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
|
||
- name: Build | ||
run: npm run prepublishOnly --if-present | ||
|
||
- run: npx pkg-pr-new publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
'use strict'; | ||
|
||
const safeCurl = require('../../lib/extend/safe_curl'); | ||
const { safeCurlForApplication } = require('../../lib/extend/safe_curl'); | ||
|
||
module.exports = { | ||
safeCurl, | ||
safeCurl: safeCurlForApplication, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
'use strict'; | ||
const SSRF_HTTPCLIENT = Symbol('SSRF_HTTPCLIENT'); | ||
|
||
/** | ||
* safe curl with ssrf protect | ||
* @param {String} url request url | ||
* @param {Object} options request options | ||
* @return {Promise} response | ||
*/ | ||
module.exports = function safeCurl(url, options = {}) { | ||
const config = this.config || this.app.config; | ||
if (config.security.ssrf && config.security.ssrf.checkAddress) { | ||
options.checkAddress = config.security.ssrf.checkAddress; | ||
exports.safeCurlForApplication = function safeCurlForApplication(url, options = {}) { | ||
const app = this; | ||
const ssrfConfig = app.config.security.ssrf; | ||
if (ssrfConfig?.checkAddress) { | ||
options.checkAddress = ssrfConfig.checkAddress; | ||
} else { | ||
this.logger.warn('[egg-security] please configure `config.security.ssrf` first'); | ||
app.logger.warn('[egg-security] please configure `config.security.ssrf` first'); | ||
} | ||
|
||
return this.curl(url, options); | ||
if (app.config.httpclient.useHttpClientNext && ssrfConfig?.checkAddress) { | ||
// use the new httpClient init with checkAddress | ||
if (!app[SSRF_HTTPCLIENT]) { | ||
app[SSRF_HTTPCLIENT] = app.createHttpClient({ | ||
checkAddress: ssrfConfig.checkAddress, | ||
}); | ||
} | ||
return app[SSRF_HTTPCLIENT].request(url, options); | ||
} | ||
|
||
return app.curl(url, options); | ||
}; | ||
|
||
exports.safeCurlForContext = function safeCurlForContext(url, options = {}) { | ||
return this.app.safeCurl(url, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
test/fixtures/apps/ssrf-check-address-useHttpClientNext/config/config.default.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
exports.security = { | ||
ssrf: { | ||
ipBlackList: [ | ||
'10.0.0.0/8', | ||
'127.0.0.1', | ||
'0.0.0.0/32', | ||
], | ||
checkAddress(ip) { | ||
return ip !== '127.0.0.2'; | ||
}, | ||
}, | ||
}; | ||
|
||
exports.httpclient = { | ||
useHttpClientNext: true, | ||
}; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/apps/ssrf-check-address-useHttpClientNext/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ssrf-ip-check-address-useHttpClientNext" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters