Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@glints/hapi-real-ip-plugin

Package Overview
Dependencies
Maintainers
4
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@glints/hapi-real-ip-plugin - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

5

dist/index.js

@@ -1,5 +0,2 @@

"use strict";
exports.__esModule = true;
var RealIPPlugin_1 = require("./RealIPPlugin");
exports.RealIPPlugin = RealIPPlugin_1["default"];
export { default as RealIPPlugin, } from './RealIPPlugin';
//# sourceMappingURL=index.js.map

79

dist/RealIPPlugin.js

@@ -1,65 +0,22 @@

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
const RealIPPlugin = {
name: 'RealIPPlugin',
register: async (server, options) => {
server.ext('onRequest', async (request, h) => {
const xffHeader = request.headers['x-forwarded-for'];
if (!xffHeader) {
return h.continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
exports.__esModule = true;
var RealIPPlugin = {
name: 'RealIPPlugin',
register: function (server, options) { return __awaiter(_this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
server.ext('onRequest', function (request, h) { return __awaiter(_this, void 0, void 0, function () {
var xffHeader, downstreamNodes;
return __generator(this, function (_a) {
xffHeader = request.headers['x-forwarded-for'];
if (!xffHeader) {
return [2 /*return*/, h["continue"]];
}
downstreamNodes = xffHeader.split(',').map(function (part) { return part.trim(); });
// X-Forwarded-For can be spoofed, so be sure to compare with the expected
// number of proxies.
if (downstreamNodes.length === options.numProxies) {
request.info.remoteAddress = downstreamNodes[0];
}
return [2 /*return*/, h["continue"]];
});
}); });
return [2 /*return*/];
// X-Forwarded-For can contain more than a single IP address.
// https://tools.ietf.org/html/rfc7239 - Section 7.4
const downstreamNodes = xffHeader.split(',').map(part => part.trim());
// X-Forwarded-For can be spoofed, so be sure to compare with the expected
// number of proxies.
if (downstreamNodes.length === options.numProxies) {
request.info.remoteAddress = downstreamNodes[0];
}
return h.continue;
});
}); }
},
};
exports["default"] = RealIPPlugin;
export default RealIPPlugin;
//# sourceMappingURL=RealIPPlugin.js.map

@@ -1,51 +0,13 @@

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
import 'mocha';
import { assert } from 'chai';
import * as Sinon from 'sinon';
import * as Hapi from 'hapi';
import RealIPPlugin from './RealIPPlugin';
describe('RealIPPlugin', () => {
let server;
let spy;
before(() => {
spy = Sinon.spy((request, h) => h.continue);
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
exports.__esModule = true;
require("mocha");
var chai_1 = require("chai");
var Sinon = require("sinon");
var Hapi = require("hapi");
var RealIPPlugin_1 = require("./RealIPPlugin");
describe('RealIPPlugin', function () {
var server;
var spy;
before(function () {
spy = Sinon.spy(function (request, h) { return h["continue"]; });
});
beforeEach(function () {
beforeEach(() => {
server = new Hapi.Server();

@@ -55,95 +17,58 @@ server.route({

path: '/{p*}',
handler: spy
handler: spy,
});
});
afterEach(function () {
afterEach(() => {
spy.resetHistory();
});
it('should be registered', function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, server.register({ plugin: RealIPPlugin_1["default"] })];
case 1:
_a.sent();
chai_1.assert.exists(server.registrations[RealIPPlugin_1["default"].name]);
return [2 /*return*/];
}
it('should be registered', async () => {
await server.register({ plugin: RealIPPlugin });
assert.exists(server.registrations[RealIPPlugin.name]);
});
describe('Plugin Functionality', () => {
it('should pass address as-is without X-Forwarded-For header', async () => {
await server.register({ plugin: RealIPPlugin });
await server.inject({
url: '/',
remoteAddress: '1.2.3.4',
});
const request = spy.args[0][0];
assert.strictEqual(request.info.remoteAddress, '1.2.3.4');
});
}); });
describe('Plugin Functionality', function () {
it('should pass address as-is without X-Forwarded-For header', function () { return __awaiter(_this, void 0, void 0, function () {
var request;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, server.register({ plugin: RealIPPlugin_1["default"] })];
case 1:
_a.sent();
return [4 /*yield*/, server.inject({
url: '/',
remoteAddress: '1.2.3.4'
})];
case 2:
_a.sent();
request = spy.args[0][0];
chai_1.assert.strictEqual(request.info.remoteAddress, '1.2.3.4');
return [2 /*return*/];
}
it('should set request.info.remoteAddress with X-Forwarded-For header', async () => {
await server.register({
plugin: RealIPPlugin,
options: {
numProxies: 1,
},
});
}); });
it('should set request.info.remoteAddress with X-Forwarded-For header', function () { return __awaiter(_this, void 0, void 0, function () {
var request;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, server.register({
plugin: RealIPPlugin_1["default"],
options: {
numProxies: 1
}
})];
case 1:
_a.sent();
return [4 /*yield*/, server.inject({
url: '/',
remoteAddress: '1.2.3.4',
headers: {
'X-Forwarded-For': '2.3.4.5'
}
})];
case 2:
_a.sent();
request = spy.args[0][0];
chai_1.assert.strictEqual(request.info.remoteAddress, '2.3.4.5');
return [2 /*return*/];
}
await server.inject({
url: '/',
remoteAddress: '1.2.3.4',
headers: {
'X-Forwarded-For': '2.3.4.5',
},
});
}); });
it('should handle multiple headers', function () { return __awaiter(_this, void 0, void 0, function () {
var request;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, server.register({
plugin: RealIPPlugin_1["default"],
options: {
numProxies: 2
}
})];
case 1:
_a.sent();
return [4 /*yield*/, server.inject({
url: '/',
remoteAddress: '1.2.3.4',
headers: {
'X-Forwarded-For': '3.4.5.6, 4.5.6.7'
}
})];
case 2:
_a.sent();
request = spy.args[0][0];
chai_1.assert.strictEqual(request.info.remoteAddress, '3.4.5.6');
return [2 /*return*/];
}
const request = spy.args[0][0];
assert.strictEqual(request.info.remoteAddress, '2.3.4.5');
});
it('should handle multiple headers', async () => {
await server.register({
plugin: RealIPPlugin,
options: {
numProxies: 2,
},
});
}); });
await server.inject({
url: '/',
remoteAddress: '1.2.3.4',
headers: {
'X-Forwarded-For': '3.4.5.6, 4.5.6.7',
},
});
const request = spy.args[0][0];
assert.strictEqual(request.info.remoteAddress, '3.4.5.6');
});
});
});
//# sourceMappingURL=RealIPPlugin.spec.js.map
{
"name": "@glints/hapi-real-ip-plugin",
"version": "1.0.0",
"version": "2.0.0",
"description": "A plugin that attempts to determine the real IP of the client.",

@@ -17,15 +17,18 @@ "main": "./dist/index.js",

},
"engines": {
"node": ">= 10"
},
"devDependencies": {
"@glints/tslint-rules": "^1.0.5",
"@types/chai": "^4.1.5",
"@types/hapi": "^17.0.19",
"@types/mocha": "^5.2.5",
"@types/sinon": "^5.0.2",
"chai": "^4.1.2",
"hapi": "^17.5.4",
"mocha": "^5.2.0",
"sinon": "^6.3.4",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"typescript": "^3.0.3"
"@glints/tslint-rules": "^1.0.6",
"@types/chai": "^4.2.4",
"@types/hapi": "^18.0.3",
"@types/mocha": "^5.2.7",
"@types/sinon": "^7.5.0",
"chai": "^4.2.0",
"hapi": "^18.1.0",
"mocha": "^6.2.2",
"sinon": "^7.5.0",
"ts-node": "^8.4.1",
"tslint": "^5.20.1",
"typescript": "^3.7.2"
},

@@ -32,0 +35,0 @@ "peerDependencies": {

@@ -1,2 +0,2 @@

# real-ip-plugin [![npm version](https://badge.fury.io/js/%40glints%2Fhapi-real-ip-plugin.svg)](https://badge.fury.io/js/%40glints%2Fhapi-real-ip-plugin)
# real-ip-plugin [![npm version](https://badge.fury.io/js/%40glints%2Fhapi-real-ip-plugin.svg)](https://badge.fury.io/js/%40glints%2Fhapi-real-ip-plugin) [![Greenkeeper badge](https://badges.greenkeeper.io/glints-dev/hapi-real-ip-plugin.svg)](https://greenkeeper.io/)

@@ -3,0 +3,0 @@ This plugin resolves client's IP address from the `X-Forwarded-For` header if

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc