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

fix: remove ip dependency and use node blocklist to check for 10.0.0.0/8 range #32

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"env": {
"es6": true,
"mocha": true
"mocha": true,
"node": true
},
"extends": "airbnb-base",
"rules": {
Expand Down
123 changes: 70 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"publishConfig": {
"registry": "https://artifactory.brandwatch.com/artifactory/api/npm/npm"
},
"dependencies": {
"ip": "^2.0.1"
"engines": {
"node": ">=14.18.0"
}
}
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Configure a ExpressJS middleware to expose useful health/metrics/checks endpoints.
*/
const ip = require('ip');
const net = require('net');

// Default empty configuration
let metrics;
Expand All @@ -10,6 +10,11 @@ let healthChecks = [];

const prometheusContentType = 'text/plain; version=0.0.4';

// Expect only requests in this range to get access to
// the health/metrics/checks endpoints
const blocklist = new net.BlockList();
blocklist.addSubnet('10.0.0.0', 8);

const doCheck = ({ name, check }) => new Promise((resolve) => {
// Pass in the [ok, warning, critical, unknown] callbacks. i.e. just resolve the promise.
check(
Expand All @@ -33,7 +38,9 @@ const addMetrics = (m) => { metrics = m; };
const getMiddleware = () => (req, res, next) => {
const requestingIP = req.ip || req.connection.remoteAddress
|| req.socket.remoteAddress || req.connection.socket.remoteAddress;
if (!ip.isPrivate(requestingIP)) {

// Only expect private IPs from this range
if (!blocklist.check(requestingIP)) {
return next();
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('bw-monitoring', () => {
beforeEach(() => {
req = {
connection: {
remoteAddress: '127.0.0.1'
remoteAddress: '10.0.0.1'
}
};
res = {
Expand Down
Loading