Skip to content

Commit

Permalink
Merge commit from fork
Browse files Browse the repository at this point in the history
* fix: avoid redos on host and protocol getter

Only effect on app.proxy enable

closes GHSA-593f-38f6-jp5m

* 3.0.0-alpha.3
  • Loading branch information
fengmk2 authored Feb 12, 2025
1 parent 71902b1 commit 5054af6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

3.0.0-alpha.3 / 2025-02-11
==================

**fixes**
- Avoid redos on host and protocol getter

3.0.0-alpha.2 / 2024-11-04
==================

Expand Down
18 changes: 15 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ module.exports = {
if (!host) host = this.get('Host')
}
if (!host) return ''
return host.split(/\s*,\s*/, 1)[0]
return splitCommaSeparatedValues(host, 1)[0]
},

/**
Expand Down Expand Up @@ -401,7 +401,7 @@ module.exports = {
if (this.socket.encrypted) return 'https'
if (!this.app.proxy) return 'http'
const proto = this.get('X-Forwarded-Proto')
return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http'
return proto ? splitCommaSeparatedValues(proto, 1)[0] : 'http'
},

/**
Expand Down Expand Up @@ -433,7 +433,7 @@ module.exports = {
const proxy = this.app.proxy
const val = this.get(this.app.proxyIpHeader)
let ips = proxy && val
? val.split(/\s*,\s*/)
? splitCommaSeparatedValues(val)
: []
if (this.app.maxIpsCount > 0) {
ips = ips.slice(-this.app.maxIpsCount)
Expand Down Expand Up @@ -723,3 +723,15 @@ module.exports = {
if (util.inspect.custom) {
module.exports[util.inspect.custom] = module.exports.inspect
}

/**
* Split a comma-separated value string into an array of values, with an optional limit.
* All the values are trimmed of whitespace.
*
* @param {string} value - The comma-separated value string to split.
* @param {number} [limit] - The maximum number of values to return.
* @returns {string[]} An array of values from the comma-separated string.
*/
function splitCommaSeparatedValues(value, limit) {
return value.split(',', limit).map(v => v.trim());
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa",
"version": "3.0.0-alpha.2",
"version": "3.0.0-alpha.3",
"publishConfig": {
"tag": "experimental"
},
Expand Down

0 comments on commit 5054af6

Please sign in to comment.