Skip to content
This repository was archived by the owner on Oct 29, 2025. It is now read-only.
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
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.8.0] - 2018-02-25
## [2.0.0] - 2019-03-14
### Added
- Support for multiple instances of PxEnforcer (for multi px-app in same web app)

### Refactored
- Major parts of the code to inject an instance of PxLogger and PX config.

### Changed
- Changed PxClient.submitActivities() signature to receive a config object.

## [1.8.0] - 2019-02-25
### Added
- Support for testing blocking flow in monitor mode

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[PerimeterX](http://www.perimeterx.com) Shared base for NodeJS enforcers
=============================================================

> Latest stable version: [v1.8.0](https://www.npmjs.com/package/perimeterx-node-core)
> Latest stable version: [v2.0.0](https://www.npmjs.com/package/perimeterx-node-core)

This is a shared base implementation for PerimeterX Express enforcer and future NodeJS enforcers. For a fully functioning implementation example, see the [Node-Express enforcer](https://github.com/PerimeterX/perimeterx-node-express/) implementation.

Expand All @@ -28,9 +28,9 @@ Table of Contents
### <a name="basic-usage"></a> Basic Usage Example
To integrate this module into an enforcer, users should initialize the enforcer.
```javascript
function initPXModule(params) {
function initPXModule(params, client) {
params.moduleVersion = '<your module version>';
enforcer = new PxEnforcer(params);
enforcer = new PxEnforcer(params, client);
//if dynamic configurations is configured
if (enforcer.config.conf.DYNAMIC_CONFIGURATIONS) {
setInterval(enforcer.config.confManager.loadData.bind(enforcer.config.confManager), enforcer.config.conf.CONFIGURATION_LOAD_INTERVAL);
Expand Down Expand Up @@ -64,9 +64,9 @@ Extend the `PxClient` class to send activities to PerimeterX.
```javascript
const PxClient = require('perimeterx-node-core').PxClient;
class MyClient extends PxClient {
init() {
init(config) {
setInterval(() => {
this.submitActivities();
this.submitActivities(config);
}, 1000);
}
}
Expand All @@ -77,7 +77,7 @@ Make sure to pass the client instance when initializing the enforcer.
```javascript
function initPXModule(params) {
params.moduleVersion = '<your module version>';
let pxClient = new MyClient();
const pxClient = new MyClient();
enforcer = new PxEnforcer(params, pxClient);
//if dynamic configurations is configured
if (enforcer.config.conf.DYNAMIC_CONFIGURATIONS) {
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@
module.exports = {
PxEnforcer: require('./lib/pxenforcer'),
PxClient: require('./lib/pxclient'),
request: require('./lib/request')
};
19 changes: 9 additions & 10 deletions lib/configloader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

const logger = require('./pxlogger');
const request = require('./request');

class ConfigLoader {

constructor(config, pxClient) {
this.config = config;
constructor(pxConfig, pxClient) {
this.pxConfig = pxConfig;
this.config = pxConfig.conf;
this.pxClient = pxClient;
}

Expand All @@ -13,18 +13,17 @@ class ConfigLoader {
}

loadData() {
const request = require('./request');
const checksum = this.config.checksum;
const callData = {
'url': `https://${this.config.CONFIGURATIONS_HOST}${this.config.CONFIGURATIONS_URI + (checksum ? `?checksum=${checksum}` : '')}`,
'headers': {Authorization: 'Bearer ' + this.config.AUTH_TOKEN},
'timeout': this.config.API_TIMEOUT_MS
};
request.get(callData, (error, response) => {
request.get(callData, this.config, (error, response) => {
if (error || !response || !(response.statusCode === 200 || response.statusCode === 204)) {
logger.error(`Failed to get configurations: ${error}`);
this.config.logger.error(`Failed to get configurations: ${error}`);
if (!checksum) { //no configuration loaded and we can't get configuration - disable module
logger.debug('Failed to pull initial config, switching module to disable until remote configuration found');
this.config.logger.debug('Failed to pull initial config, switching module to disable until remote configuration found');
this.config.ENABLE_MODULE = false;
}
return;
Expand All @@ -33,7 +32,7 @@ class ConfigLoader {
// new configuration available
if (response.statusCode === 200) {
const body = JSON.parse(response.body.toString());
logger.debug(`Found new configuration - checksum: ${body.checksum}, new configuration: ${JSON.stringify(body)}`);
this.config.logger.debug(`Found new configuration - checksum: ${body.checksum}, new configuration: ${JSON.stringify(body)}`);
this.config.checksum = body.checksum;
this.config.COOKIE_SECRET_KEY = body.cookieKey;
this.config.PX_APP_ID = body.appId;
Expand All @@ -47,7 +46,7 @@ class ConfigLoader {
this.config.MODULE_MODE = body.moduleMode === 'blocking' ? this.config.MONITOR_MODE.BLOCK : this.config.MONITOR_MODE.MONITOR;
this.config.FIRST_PARTY_ENABLED = body.firstPartyEnabled;
this.config.FIRST_PARTY_XHR_ENABLED = body.firstPartyXhrEnabled;
this.pxClient.sendEnforcerTelemetry('remote_config');
this.pxClient.sendEnforcerTelemetry('remote_config', this.config);
}
});
}
Expand Down
9 changes: 4 additions & 5 deletions lib/cookie/cookieV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ const Payload = require('../pxpayload');
class CookieV1 extends Payload {

constructor(ctx, config) {
super();
super(config);
this.pxCookie = ctx.cookies['_px'];
this.pxConfig = config;
this.pxContext = ctx;
this.ctx = ctx;
this.cookieSecret = config.COOKIE_SECRET_KEY;
}

Expand All @@ -33,10 +32,10 @@ class CookieV1 extends Payload {
const baseHmacStr = '' + this.getTime() + this.decodedCookie.s.a + this.getScore() + this.getUuid() + this.getVid();

// hmac string with IP - for backward support
const hmacWithIp = baseHmacStr + this.pxContext.ip + this.pxContext.userAgent;
const hmacWithIp = baseHmacStr + this.ctx.ip + this.ctx.userAgent;

// hmac string without IP
const hmacWithoutIp = baseHmacStr + this.pxContext.userAgent;
const hmacWithoutIp = baseHmacStr + this.ctx.userAgent;

return this.isHmacValid(hmacWithoutIp, this.getHmac()) || this.isHmacValid(hmacWithIp, this.getHmac());
}
Expand Down
7 changes: 3 additions & 4 deletions lib/cookie/cookieV3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ const Payload = require('../pxpayload');

class CookieV3 extends Payload {
constructor(ctx, config) {
super();
super(config);
let [hash, ...cookie] = ctx.cookies['_px3'].split(':');
cookie = cookie.join(':');
this.pxCookie = cookie;
this.cookieHash = hash;
this.pxConfig = config;
this.pxContext = ctx;
this.ctx = ctx;
this.cookieSecret = config.COOKIE_SECRET_KEY;
}

Expand All @@ -31,7 +30,7 @@ class CookieV3 extends Payload {
}

isSecure() {
const hmacStr = this.pxCookie + this.pxContext.userAgent;
const hmacStr = this.pxCookie + this.ctx.userAgent;
return this.isHmacValid(hmacStr, this.getHmac());
}
}
Expand Down
7 changes: 3 additions & 4 deletions lib/cookie/tokenV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ const Payload = require('../pxpayload');
class TokenV1 extends Payload {

constructor(ctx, config, token) {
super();
super(config);
this.pxCookie = token;
this.pxConfig = config;
this.pxContext = ctx;
this.ctx = ctx;
this.cookieSecret = config.COOKIE_SECRET_KEY;
}

Expand All @@ -33,7 +32,7 @@ class TokenV1 extends Payload {
const baseHmacStr = '' + this.getTime() + this.decodedCookie.s.a + this.getScore() + this.getUuid() + this.getVid();

// hmac string with IP - for backward support
const hmacWithIp = baseHmacStr + this.pxContext.ip;
const hmacWithIp = baseHmacStr + this.ctx.ip;

// hmac string without IP
const hmacWithoutIp = baseHmacStr;
Expand Down
5 changes: 2 additions & 3 deletions lib/cookie/tokenV3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ const Payload = require('../pxpayload');

class TokenV3 extends Payload {
constructor(ctx, config, token) {
super();
super(config);
let [hash, ...cookie] = token.split(':');
cookie = cookie.join(':');
this.pxCookie = cookie;
this.cookieHash = hash;
this.pxConfig = config;
this.pxContext = ctx;
this.ctx = ctx;
this.cookieSecret = config.COOKIE_SECRET_KEY;
}

Expand Down
Loading