Skip to content
This repository was archived by the owner on Oct 29, 2025. It is now read-only.

Commit 63de221

Browse files
author
Johnny Tordgeman
authored
Merge pull request #162 from PerimeterX/dev
Version 2.12.1
2 parents 2a12151 + 2da3069 commit 63de221

File tree

8 files changed

+53
-21
lines changed

8 files changed

+53
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [2.12.1] - 2021-05-25
9+
10+
### Fixed
11+
12+
- Wrong reporting for bypass monitor header.
13+
814
## [2.12.0] - 2021-04-08
915

1016
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[PerimeterX](http://www.perimeterx.com) Shared base for NodeJS enforcers
77
=============================================================
88

9-
> Latest stable version: [v2.12.0](https://www.npmjs.com/package/perimeterx-node-core)
9+
> Latest stable version: [v2.12.1](https://www.npmjs.com/package/perimeterx-node-core)
1010
1111
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.
1212

lib/pxapi.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ function buildRequestData(ctx, config) {
3737
const uuid = ctx.uuid || '';
3838
const headers = pxUtil.formatHeaders(ctx.headers, config.SENSITIVE_HEADERS);
3939
const httpVersion = ctx.httpVersion;
40-
const riskMode = config.MODULE_MODE === config.MONITOR_MODE.MONITOR || ctx.monitoredRoute ? 'monitor' : 'active_blocking';
40+
const riskMode =
41+
(config.MODULE_MODE === config.MONITOR_MODE.MONITOR && !ctx.shouldBypassMonitor) || ctx.monitoredRoute
42+
? 'monitor'
43+
: 'active_blocking';
4144

4245
const data = {
4346
request: {
@@ -122,7 +125,9 @@ function evalByServerCall(ctx, config, callback) {
122125
ctx.passReason = PassReason.S2S_TIMEOUT;
123126
return callback(ScoreEvaluateAction.S2S_TIMEOUT_PASS);
124127
}
125-
config.logger.error(`Unexpected exception while evaluating Risk API. ${err.errorReason}:${err.errorMessage}`);
128+
config.logger.error(
129+
`Unexpected exception while evaluating Risk API. ${err.errorReason}:${err.errorMessage}`,
130+
);
126131
ctx.passReason = PassReason.S2S_ERROR;
127132
ctx.s2sErrorInfo = err;
128133
return callback(ScoreEvaluateAction.UNEXPECTED_RESULT);
@@ -172,7 +177,10 @@ function isBadRiskScore(riskResponse, ctx, config) {
172177
if (!riskResponse || !pxUtil.verifyDefined(riskResponse.score) || !riskResponse.action) {
173178
ctx.passReason = PassReason.S2S_ERROR;
174179
if (!ctx.s2sErrorInfo) {
175-
ctx.s2sErrorInfo = new S2SErrorInfo(S2SErrorReason.INVALID_RESPONSE, `Response is ${JSON.stringify(riskResponse)}`);
180+
ctx.s2sErrorInfo = new S2SErrorInfo(
181+
S2SErrorReason.INVALID_RESPONSE,
182+
`Response is ${JSON.stringify(riskResponse)}`,
183+
);
176184
}
177185
return -1;
178186
}

lib/pxconfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class PxConfig {
7979
this.PX_DEFAULT,
8080
params,
8181
targetKey,
82-
sourceKey
82+
sourceKey,
8383
);
8484
});
8585

@@ -101,7 +101,7 @@ class PxConfig {
101101
this.PX_INTERNAL,
102102
params,
103103
'MODULE_VERSION',
104-
'moduleVersion'
104+
'moduleVersion',
105105
);
106106

107107
//update config

lib/pxcontext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class PxContext {
2323
this.enforcedRoute = this.isSpecialRoute(config.ENFORCED_ROUTES, this.uri);
2424
this.whitelistRoute = this.isSpecialRoute(config.WHITELIST_ROUTES, this.uri);
2525
this.monitoredRoute = !this.enforcedRoute && this.isSpecialRoute(config.MONITORED_ROUTES, this.uri);
26+
this.shouldBypassMonitor = config.BYPASS_MONITOR_HEADER && req.headers[config.BYPASS_MONITOR_HEADER] === '1';
2627
this.cookieOrigin = 'cookie';
2728
this.additionalFields = additionalFields || {};
2829
const mobileHeader = this.headers[mobileSdkHeader];

lib/pxenforcer.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,14 @@ class PxEnforcer {
210210
return callback(ScoreEvaluateAction.S2S_PASS_TRAFFIC);
211211
}
212212

213-
this.logger.debug(`Risk API response returned successfully, risk score: ${ctx.score}, round_trip_time: ${ctx.riskRtt}ms`);
213+
this.logger.debug(
214+
`Risk API response returned successfully, risk score: ${ctx.score}, round_trip_time: ${ctx.riskRtt}ms`,
215+
);
214216

215217
if (action === ScoreEvaluateAction.GOOD_SCORE) {
216-
this.logger.debug(`Risk score is lower than blocking score. score: ${ctx.score} blocking score: ${this._config.BLOCKING_SCORE}`);
218+
this.logger.debug(
219+
`Risk score is lower than blocking score. score: ${ctx.score} blocking score: ${this._config.BLOCKING_SCORE}`,
220+
);
217221
return callback(ScoreEvaluateAction.S2S_PASS_TRAFFIC);
218222
}
219223

@@ -248,13 +252,16 @@ class PxEnforcer {
248252

249253
handleVerification(ctx, req, res, cb) {
250254
const verified = ctx.score < this._config.BLOCKING_SCORE;
255+
251256
if (res) {
252257
const setCookie = res.getHeader('Set-Cookie') ? res.getHeader('Set-Cookie') : '';
253258
const secure = this._config.PXHD_SECURE ? '; Secure' : '';
254259
const pxhdCookie = ctx.pxhdServer ? `_pxhd=${ctx.pxhdServer} ${secure}` : '';
255260
const setCookieModified = [setCookie, pxhdCookie].filter(Boolean);
256261
if (setCookieModified.length > 0) {
257-
const expires = `expires=${new Date(new Date().getTime() + Constants.MILLISECONDS_IN_YEAR).toUTCString()}`;
262+
const expires = `expires=${new Date(
263+
new Date().getTime() + Constants.MILLISECONDS_IN_YEAR,
264+
).toUTCString()}`;
258265
res.setHeader('Set-Cookie', `${setCookieModified}; ${expires}`);
259266
}
260267
}
@@ -285,8 +292,7 @@ class PxEnforcer {
285292
}
286293

287294
// If verified, pass the request here
288-
const shouldBypassMonitor = this._config.BYPASS_MONITOR_HEADER && req.headers[this._config.BYPASS_MONITOR_HEADER] === '1';
289-
if (verified || ctx.monitoredRoute || (this._config.MODULE_MODE === this._config.MONITOR_MODE.MONITOR && !shouldBypassMonitor)) {
295+
if (verified || pxUtil.isReqInMonitorMode(this._config, ctx)) {
290296
return cb();
291297
}
292298

@@ -299,7 +305,9 @@ class PxEnforcer {
299305
ctx.blockAction !== 'r';
300306

301307
this.logger.debug(
302-
`Enforcing action: ${pxUtil.parseAction(ctx.blockAction)} page is served ${isJsonResponse ? 'using advanced protection mode' : ''}`,
308+
`Enforcing action: ${pxUtil.parseAction(ctx.blockAction)} page is served ${
309+
isJsonResponse ? 'using advanced protection mode' : ''
310+
}`,
303311
);
304312
const config = this._config;
305313
this.generateResponse(ctx, isJsonResponse, function (responseObject) {
@@ -369,7 +377,9 @@ class PxEnforcer {
369377
}
370378

371379
if (this._config.EXTERNAL_ACTIVITIES && req) {
372-
req.headers['x-px-pagerequested'] = JSON.stringify(this.pxClient.generateActivity('page_requested', details, ctx, this._config));
380+
req.headers['x-px-pagerequested'] = JSON.stringify(
381+
this.pxClient.generateActivity('page_requested', details, ctx, this._config),
382+
);
373383
} else {
374384
this.logger.debug('Sending page requested activity');
375385
this.pxClient.sendToPerimeterX('page_requested', details, ctx, this._config);
@@ -396,7 +406,7 @@ class PxEnforcer {
396406
block_module: 'px-node-express',
397407
block_score: ctx.score,
398408
module_version: this.pxConfig.conf.MODULE_VERSION,
399-
simulated_block: this._config.MODULE_MODE === this._config.MONITOR_MODE.MONITOR || ctx.monitoredRoute,
409+
simulated_block: pxUtil.isReqInMonitorMode(this._config, ctx),
400410
...ctx.additionalFields,
401411
};
402412

@@ -431,17 +441,17 @@ class PxEnforcer {
431441

432442
getProps(ctx) {
433443
let jsClientSrc = `//${this._config.CLIENT_HOST}/${this._config.PX_APP_ID}/main.min.js`;
434-
let captchaSrc = `//${this._config.CAPTCHA_HOST}/${this._config.PX_APP_ID}/captcha.js?a=${ctx.blockAction}&u=${ctx.uuid}&v=${
435-
ctx.vid || ''
436-
}&m=${ctx.isMobile() ? '1' : '0'}`;
444+
let captchaSrc = `//${this._config.CAPTCHA_HOST}/${this._config.PX_APP_ID}/captcha.js?a=${ctx.blockAction}&u=${
445+
ctx.uuid
446+
}&v=${ctx.vid || ''}&m=${ctx.isMobile() ? '1' : '0'}`;
437447
let hostUrl = ctx.collectorUrl;
438448

439449
if (this._config.FIRST_PARTY_ENABLED && !ctx.isMobile()) {
440450
const prefix = this._config.PX_APP_ID.substring(2);
441451
jsClientSrc = `/${prefix}${this._config.FIRST_PARTY_VENDOR_PATH}`;
442-
captchaSrc = `/${prefix}${this._config.FIRST_PARTY_CAPTCHA_PATH}/captcha.js?a=${ctx.blockAction}&u=${ctx.uuid}&v=${ctx.vid || ''}&m=${
443-
ctx.isMobile() ? '1' : '0'
444-
}`;
452+
captchaSrc = `/${prefix}${this._config.FIRST_PARTY_CAPTCHA_PATH}/captcha.js?a=${ctx.blockAction}&u=${
453+
ctx.uuid
454+
}&v=${ctx.vid || ''}&m=${ctx.isMobile() ? '1' : '0'}`;
445455
hostUrl = `/${prefix}${this._config.FIRST_PARTY_XHR_PATH}`;
446456
}
447457

lib/pxutil.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ function generateHMAC(cookieSecret, payload) {
243243
return hmacCreator.read();
244244
}
245245

246+
function isReqInMonitorMode(pxConfig, pxCtx) {
247+
return (
248+
(pxConfig.MODULE_MODE === pxConfig.MONITOR_MODE.MONITOR && !pxCtx.shouldBypassMonitor) || pxCtx.monitoredRoute
249+
);
250+
}
251+
246252
module.exports = {
247253
formatHeaders,
248254
filterSensitiveHeaders,
@@ -259,4 +265,5 @@ module.exports = {
259265
sha256,
260266
isStringMatchWith,
261267
generateHMAC,
268+
isReqInMonitorMode,
262269
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "perimeterx-node-core",
3-
"version": "2.12.0",
3+
"version": "2.12.1",
44
"description": "PerimeterX NodeJS shared core for various applications to monitor and block traffic according to PerimeterX risk score",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)