-
Notifications
You must be signed in to change notification settings - Fork 1
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
BREAKING CHANGE: Support trust proxy and frameworks other than Express #2
Open
paulmelnikow
wants to merge
1
commit into
master
Choose a base branch
from
trust-proxy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package.json | ||
package-lock.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 |
---|---|---|
@@ -1,23 +1,47 @@ | ||
# cloudflare-middleware | ||
|
||
Restores request origin ip to `req.origin_ip`. Denies other requests. | ||
Allows requests which originate from Cloudflare. Restores request origin IP | ||
to `req.origin_ip`. Denies other requests. | ||
|
||
## Usage | ||
|
||
```nodejs | ||
let app = express(); | ||
app.use(require("cloudflare-middleware")()); | ||
Using [Express trust proxy][trust proxy] (preferred): | ||
|
||
```js | ||
const { trustProxy } = require('cloudflare-middleware') | ||
|
||
const app = express() | ||
app.set('trust proxy', trustProxy) | ||
``` | ||
|
||
Using Express middleware: | ||
|
||
```js | ||
const { middleware } = require('cloudflare-middleware') | ||
|
||
const app = express() | ||
app.use(middleware()) | ||
``` | ||
|
||
This is a safer alternative to naively using `app.set("trust proxy");`, as this checks CloudFlare IP address ranges. | ||
Using with another framework: | ||
|
||
```js | ||
const http = require('http') | ||
const { isCloudflare } = require('cloudflare-middleware') | ||
|
||
http.createServer((req, res) => { | ||
if (!isCloudflare(req.socket.remoteAddress)) { | ||
response.end() | ||
} | ||
|
||
// Proceed with the response. | ||
response.write('oh, you good') | ||
response.end() | ||
}) | ||
``` | ||
|
||
This, however, has been deprecated in favor of the following: | ||
[trust proxy]: https://expressjs.com/en/guide/behind-proxies.html | ||
|
||
```nodejs | ||
const cloudflareIp = require('cloudflare-ip'); | ||
## License | ||
|
||
app.set('trust proxy', ip => { | ||
if(ip.startsWith('::ffff:')) | ||
ip = ip.substr(7); | ||
return ip == '127.0.0.1' || cloudflareIp(ip); | ||
}); | ||
This project is licensed under the ISC license. |
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,16 +1,32 @@ | ||
const rangeCheck = require("range_check"); | ||
const ranges = require("./ranges"); | ||
const rangeCheck = require('range_check') | ||
const ranges = require('./ranges') | ||
|
||
module.exports = function(options){ | ||
return function(req, res, next) { | ||
ip = req.ip; | ||
if(ip.startsWith("::ffff:")) | ||
ip = ip.substr(7); | ||
if(rangeCheck.inRange(ip, ranges)){ | ||
req.origin_ip = req.headers['cf-connecting-ip']; | ||
next(); | ||
}else{ | ||
res.end(); | ||
function trimIp(ip) { | ||
return ip.startsWith('::ffff:') ? ip.substr(7) : ip | ||
} | ||
|
||
function isCloudflare(ip) { | ||
return rangeCheck.inRange(ip, ranges) | ||
} | ||
|
||
function trustProxy(ip) { | ||
const trimmed = trimIp(ip) | ||
return trimmed == '127.0.0.1' || isCloudflare(trimmed) | ||
} | ||
|
||
function middleware(options) { | ||
return function (req, res, next) { | ||
if (isCloudflare(trimIp(req.ip))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No 127.0.0.1 here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops! Good catch. |
||
req.origin_ip = req.headers['cf-connecting-ip'] | ||
next() | ||
} else { | ||
res.end() | ||
} | ||
}; | ||
}; | ||
} | ||
} | ||
|
||
module.exports = { | ||
isCloudflare, | ||
middleware, | ||
trustProxy, | ||
} |
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,25 +1,25 @@ | ||
const got = require("got"); | ||
const { expect } = require("chai"); | ||
const ranges = require("./ranges"); | ||
const got = require('got') | ||
const { expect } = require('chai') | ||
const ranges = require('./ranges') | ||
|
||
async function collectAdvertisedRanges() { | ||
let result = []; | ||
let result = [] | ||
for (const url of [ | ||
"https://www.cloudflare.com/ips-v4", | ||
"https://www.cloudflare.com/ips-v6", | ||
'https://www.cloudflare.com/ips-v4', | ||
'https://www.cloudflare.com/ips-v6', | ||
]) { | ||
const { body } = await got(url); | ||
const theseRanges = body.split("\n").filter(Boolean); | ||
result = result.concat(theseRanges); | ||
const { body } = await got(url) | ||
const theseRanges = body.split('\n').filter(Boolean) | ||
result = result.concat(theseRanges) | ||
} | ||
return result; | ||
return result | ||
} | ||
|
||
describe("Ranges", function () { | ||
describe('Ranges', function () { | ||
it("match Cloudflare's advertised ranges", async function () { | ||
const advertisedRanges = await collectAdvertisedRanges(); | ||
const advertisedRanges = await collectAdvertisedRanges() | ||
|
||
expect(ranges).to.have.members(advertisedRanges); | ||
expect(advertisedRanges).to.have.members(ranges); | ||
}); | ||
}); | ||
expect(ranges).to.have.members(advertisedRanges) | ||
expect(advertisedRanges).to.have.members(ranges) | ||
}) | ||
}) |
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,25 +1,25 @@ | ||
module.exports = [ | ||
// From https://www.cloudflare.com/ips-v4 | ||
"103.21.244.0/22", | ||
"103.22.200.0/22", | ||
"103.31.4.0/22", | ||
"104.16.0.0/12", | ||
"108.162.192.0/18", | ||
"131.0.72.0/22", | ||
"141.101.64.0/18", | ||
"162.158.0.0/15", | ||
"172.64.0.0/13", | ||
"173.245.48.0/20", | ||
"188.114.96.0/20", | ||
"190.93.240.0/20", | ||
"197.234.240.0/22", | ||
"198.41.128.0/17", | ||
// From https://www.cloudflare.com/ips-v6 | ||
"2400:cb00::/32", | ||
"2405:8100::/32", | ||
"2405:b500::/32", | ||
"2606:4700::/32", | ||
"2803:f800::/32", | ||
"2c0f:f248::/32", | ||
"2a06:98c0::/29", | ||
]; | ||
// From https://www.cloudflare.com/ips-v4 | ||
'103.21.244.0/22', | ||
'103.22.200.0/22', | ||
'103.31.4.0/22', | ||
'104.16.0.0/12', | ||
'108.162.192.0/18', | ||
'131.0.72.0/22', | ||
'141.101.64.0/18', | ||
'162.158.0.0/15', | ||
'172.64.0.0/13', | ||
'173.245.48.0/20', | ||
'188.114.96.0/20', | ||
'190.93.240.0/20', | ||
'197.234.240.0/22', | ||
'198.41.128.0/17', | ||
// From https://www.cloudflare.com/ips-v6 | ||
'2400:cb00::/32', | ||
'2405:8100::/32', | ||
'2405:b500::/32', | ||
'2606:4700::/32', | ||
'2803:f800::/32', | ||
'2c0f:f248::/32', | ||
'2a06:98c0::/29', | ||
] |
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 |
---|---|---|
|
@@ -851,6 +851,11 @@ picomatch@^2.0.4, picomatch@^2.2.1: | |
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad" | ||
integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg== | ||
|
||
prettier@^2.1.2: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5" | ||
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg== | ||
|
||
[email protected]: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/promise.allsettled/-/promise.allsettled-1.0.2.tgz#d66f78fbb600e83e863d893e98b3d4376a9c47c9" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe trim4Mapped6?
Future would be nice to have proper ipv6 support.