Skip to content

Commit

Permalink
Merge pull request #258 from PerimeterX/release/v3.6.0
Browse files Browse the repository at this point in the history
[SDKNEW-2473] Release/v3.6.0 (to master)
  • Loading branch information
ori-gold-px authored Nov 16, 2022
2 parents 5213668 + d7442ff commit 79085ab
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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/).

## [3.6.0] - 2022-11-17

### Added

- Support for `px_modify_context`, a custom function that allows more flexibility for changes to the request context

## [3.5.0] - 2022-10-23

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[PerimeterX](http://www.perimeterx.com) Shared base for NodeJS enforcers
=============================================================

> Latest stable version: [v3.5.0](https://www.npmjs.com/package/perimeterx-node-core)
> Latest stable version: [v3.6.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 Down
12 changes: 8 additions & 4 deletions lib/pxconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class PxConfig {
['LOGIN_SUCCESSFUL_HEADER_VALUE', 'px_login_successful_header_value'],
['LOGIN_SUCCESSFUL_STATUS', 'px_login_successful_status'],
['LOGIN_SUCCESSFUL_BODY_REGEX', 'px_login_successful_body_regex'],
['LOGIN_SUCCESSFUL_CUSTOM_CALLBACK', 'px_login_successful_custom_callback']
['LOGIN_SUCCESSFUL_CUSTOM_CALLBACK', 'px_login_successful_custom_callback'],
['MODIFY_CONTEXT', 'px_modify_context'],
];

configKeyMapping.forEach(([targetKey, sourceKey]) => {
Expand Down Expand Up @@ -161,7 +162,8 @@ class PxConfig {
userInput === 'px_additional_activity_handler' ||
userInput === 'px_custom_request_handler' ||
userInput === 'px_enrich_custom_parameters' ||
userInput === 'px_login_successful_custom_callback'
userInput === 'px_login_successful_custom_callback' ||
userInput === 'px_modify_context'
) {
if (typeof params[userInput] === 'function') {
return params[userInput];
Expand Down Expand Up @@ -331,7 +333,8 @@ function pxDefaultConfig() {
LOGIN_SUCCESSFUL_HEADER_VALUE: '',
LOGIN_SUCCESSFUL_STATUS: 200,
LOGIN_SUCCESSFUL_BODY_REGEX: '',
LOGIN_SUCCESSFUL_CUSTOM_CALLBACK: null
LOGIN_SUCCESSFUL_CUSTOM_CALLBACK: null,
MODIFY_CONTEXT: null,
};
}

Expand Down Expand Up @@ -391,7 +394,8 @@ const allowedConfigKeys = [
'px_login_successful_header_value',
'px_login_successful_status',
'px_login_successful_body_regex',
'px_login_successful_custom_callback'
'px_login_successful_custom_callback',
'px_modify_context',
];

module.exports = PxConfig;
11 changes: 11 additions & 0 deletions lib/pxenforcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class PxEnforcer {
}

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

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

_tryModifyContext(ctx, req) {
if (this._config.MODIFY_CONTEXT && typeof this._config.MODIFY_CONTEXT === 'function') {
try {
this._config.MODIFY_CONTEXT(ctx, req);
} catch (e) {
this.logger.debug(`error modifying context: ${e}`);
}
}
}

_getAdditionalFields(req) {
const additionalFields = {};
if (this.loginCredentialsExtractor) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "perimeterx-node-core",
"version": "3.5.0",
"version": "3.6.0",
"description": "PerimeterX NodeJS shared core for various applications to monitor and block traffic according to PerimeterX risk score",
"main": "index.js",
"scripts": {
Expand Down
36 changes: 36 additions & 0 deletions test/pxenforcer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,42 @@ describe('PX Enforcer - pxenforcer.js', () => {
});
})

it('Should call px_modify_context if set', (done) => {
stub = sinon.stub(pxhttpc, 'callServer').callsFake((data, headers, uri, callType, config, callback) => {
return callback ? callback(null, data) : '';
});

const modifyCtx = sinon.stub().callsFake((ctx) => ctx.sensitiveRoute = true);
const curParams = {
...params,
px_modify_context: modifyCtx,
};

const pxenforcer = proxyquire('../lib/pxenforcer', { './pxlogger': logger });
enforcer = new pxenforcer(curParams, pxClient);
enforcer.enforce(req, null, () => {
(modifyCtx.calledOnce).should.equal(true);
(req.locals.pxCtx.sensitiveRoute).should.equal(true);
done();
});
});

it('should not throw exception if there is an error in px_modify_context', () => {
stub = sinon.stub(pxhttpc, 'callServer').callsFake((data, headers, uri, callType, config, callback) => {
return callback ? callback(null, data) : '';
});

const curParams = {
...params,
px_modify_context: sinon.stub().throws(),
};

const pxenforcer = proxyquire('../lib/pxenforcer', { './pxlogger': logger });
enforcer = new pxenforcer(curParams, pxClient);
const enforceFunc = enforcer.enforce.bind(enforcer, req, null, () => {});
(enforceFunc).should.not.throw();
});

it('Should add Nonce to CSP header (script-src directive exists)', (done) => {
const nonce = 'ImN0nc3Value';
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';
Expand Down

0 comments on commit 79085ab

Please sign in to comment.