Skip to content

Commit 79085ab

Browse files
authored
Merge pull request #258 from PerimeterX/release/v3.6.0
[SDKNEW-2473] Release/v3.6.0 (to master)
2 parents 5213668 + d7442ff commit 79085ab

File tree

7 files changed

+65
-8
lines changed

7 files changed

+65
-8
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+
## [3.6.0] - 2022-11-17
9+
10+
### Added
11+
12+
- Support for `px_modify_context`, a custom function that allows more flexibility for changes to the request context
13+
814
## [3.5.0] - 2022-10-23
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: [v3.5.0](https://www.npmjs.com/package/perimeterx-node-core)
9+
> Latest stable version: [v3.6.0](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/pxconfig.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class PxConfig {
8989
['LOGIN_SUCCESSFUL_HEADER_VALUE', 'px_login_successful_header_value'],
9090
['LOGIN_SUCCESSFUL_STATUS', 'px_login_successful_status'],
9191
['LOGIN_SUCCESSFUL_BODY_REGEX', 'px_login_successful_body_regex'],
92-
['LOGIN_SUCCESSFUL_CUSTOM_CALLBACK', 'px_login_successful_custom_callback']
92+
['LOGIN_SUCCESSFUL_CUSTOM_CALLBACK', 'px_login_successful_custom_callback'],
93+
['MODIFY_CONTEXT', 'px_modify_context'],
9394
];
9495

9596
configKeyMapping.forEach(([targetKey, sourceKey]) => {
@@ -161,7 +162,8 @@ class PxConfig {
161162
userInput === 'px_additional_activity_handler' ||
162163
userInput === 'px_custom_request_handler' ||
163164
userInput === 'px_enrich_custom_parameters' ||
164-
userInput === 'px_login_successful_custom_callback'
165+
userInput === 'px_login_successful_custom_callback' ||
166+
userInput === 'px_modify_context'
165167
) {
166168
if (typeof params[userInput] === 'function') {
167169
return params[userInput];
@@ -331,7 +333,8 @@ function pxDefaultConfig() {
331333
LOGIN_SUCCESSFUL_HEADER_VALUE: '',
332334
LOGIN_SUCCESSFUL_STATUS: 200,
333335
LOGIN_SUCCESSFUL_BODY_REGEX: '',
334-
LOGIN_SUCCESSFUL_CUSTOM_CALLBACK: null
336+
LOGIN_SUCCESSFUL_CUSTOM_CALLBACK: null,
337+
MODIFY_CONTEXT: null,
335338
};
336339
}
337340

@@ -391,7 +394,8 @@ const allowedConfigKeys = [
391394
'px_login_successful_header_value',
392395
'px_login_successful_status',
393396
'px_login_successful_body_regex',
394-
'px_login_successful_custom_callback'
397+
'px_login_successful_custom_callback',
398+
'px_modify_context',
395399
];
396400

397401
module.exports = PxConfig;

lib/pxenforcer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class PxEnforcer {
112112
}
113113

114114
const ctx = new PxContext(this._config, req, this._getAdditionalFields(req));
115+
this._tryModifyContext(ctx, req);
115116
req.locals = { ...req.locals, pxCtx: ctx };
116117

117118
this.logger.debug('Request context created successfully');
@@ -127,6 +128,16 @@ class PxEnforcer {
127128
}
128129
}
129130

131+
_tryModifyContext(ctx, req) {
132+
if (this._config.MODIFY_CONTEXT && typeof this._config.MODIFY_CONTEXT === 'function') {
133+
try {
134+
this._config.MODIFY_CONTEXT(ctx, req);
135+
} catch (e) {
136+
this.logger.debug(`error modifying context: ${e}`);
137+
}
138+
}
139+
}
140+
130141
_getAdditionalFields(req) {
131142
const additionalFields = {};
132143
if (this.loginCredentialsExtractor) {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "3.5.0",
3+
"version": "3.6.0",
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": {

test/pxenforcer.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,42 @@ describe('PX Enforcer - pxenforcer.js', () => {
842842
});
843843
})
844844

845+
it('Should call px_modify_context if set', (done) => {
846+
stub = sinon.stub(pxhttpc, 'callServer').callsFake((data, headers, uri, callType, config, callback) => {
847+
return callback ? callback(null, data) : '';
848+
});
849+
850+
const modifyCtx = sinon.stub().callsFake((ctx) => ctx.sensitiveRoute = true);
851+
const curParams = {
852+
...params,
853+
px_modify_context: modifyCtx,
854+
};
855+
856+
const pxenforcer = proxyquire('../lib/pxenforcer', { './pxlogger': logger });
857+
enforcer = new pxenforcer(curParams, pxClient);
858+
enforcer.enforce(req, null, () => {
859+
(modifyCtx.calledOnce).should.equal(true);
860+
(req.locals.pxCtx.sensitiveRoute).should.equal(true);
861+
done();
862+
});
863+
});
864+
865+
it('should not throw exception if there is an error in px_modify_context', () => {
866+
stub = sinon.stub(pxhttpc, 'callServer').callsFake((data, headers, uri, callType, config, callback) => {
867+
return callback ? callback(null, data) : '';
868+
});
869+
870+
const curParams = {
871+
...params,
872+
px_modify_context: sinon.stub().throws(),
873+
};
874+
875+
const pxenforcer = proxyquire('../lib/pxenforcer', { './pxlogger': logger });
876+
enforcer = new pxenforcer(curParams, pxClient);
877+
const enforceFunc = enforcer.enforce.bind(enforcer, req, null, () => {});
878+
(enforceFunc).should.not.throw();
879+
});
880+
845881
it('Should add Nonce to CSP header (script-src directive exists)', (done) => {
846882
const nonce = 'ImN0nc3Value';
847883
const headerWithoutNonce = 'connect-src \'self\' *.bazaarvoice.com *.google.com *.googleapis.com *.perimeterx.net *.px-cdn.net *.px-client.net; script-src \'self\' \'unsafe-eval\' \'unsafe-inline\' *.bazaarvoice.com *.forter.com *.google-analytics.com report-uri https://csp.px-cloud.net/report?report=1&id=8a3a7c5242c0e7646bd7d86284f408f6&app_id=PXFF0j69T5&p=d767ae06-b964-4b42-96a2-6d4089aab525';

0 commit comments

Comments
 (0)