-
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
- Loading branch information
Showing
12 changed files
with
106 additions
and
47 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