Skip to content

Commit 4218e95

Browse files
committed
Fix interception and bind issue
1 parent c857eae commit 4218e95

7 files changed

Lines changed: 92 additions & 94 deletions

File tree

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@securenative/sdk",
3-
"version": "1.9.3",
3+
"version": "1.9.4",
44
"description": "",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -53,7 +53,7 @@
5353
"chai-as-promised": "^7.1.1",
5454
"codecov": "^3.6.5",
5555
"fetch-mock": "^9.3.1",
56-
"mocha": "^7.1.0",
56+
"mocha": "^7.1.1",
5757
"mocked-env": "^1.3.2",
5858
"nyc": "^15.0.1",
5959
"semantic-release": "^17.0.4",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ if (compareVersions(process.version, config.minSupportedVersion) < 0) {
4848
secureNative.agent.startAgent().catch(() => { });
4949
}
5050

51-
const SDK = secureNative.SDK;
51+
const SDK = secureNative.sdk;
5252

5353
export {
5454
SDK as secureNative,

src/interceptors/https-server-interceptor.ts

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import SessionManager from './../session-manager';
55
import { wrapListener } from '../utils/shimer';
66
import { Logger } from '../logger';
77
import Hook from 'require-in-the-middle';
8-
import { wrap } from 'shimmer';
98
import ActionsList from './../actions-list';
109
import { clientIpFromRequest } from './../utils/utils';
1110
import { SecureNativeOptions } from '../types/securenative-options';
@@ -14,9 +13,9 @@ import SetType from '../enums/set-type';
1413
import { v4 } from 'uuid';
1514
import ApiManager from '../api-manager';
1615

17-
export default class HttpsServerInterceptor extends Interceptor implements IInterceptor {
16+
export default class HttpServerInterceptor extends Interceptor implements IInterceptor {
1817
private name = 'https-server';
19-
18+
2019
constructor(private moduleManger: ModuleManager, private apiManager: ApiManager, private options: SecureNativeOptions) {
2120
super();
2221
}
@@ -39,62 +38,25 @@ export default class HttpsServerInterceptor extends Interceptor implements IInte
3938
wrapListener(exports.Server.prototype, 'emit', 'request', (event, req, res) => {
4039
const snuid = v4();
4140
SessionManager.setSession(snuid, { req, res });
42-
41+
if (req.method === 'OPTIONS') {
42+
return true;
43+
}
4344
const url = req.url;
4445
const clientIp = clientIpFromRequest(req);
4546
const deviceFP = getDeviceFp(req, this.options);
4647

47-
if (ActionsList.whitelist.has(SetType.IP, clientIp) || ActionsList.whitelist.has(SetType.USER, deviceFP) || ActionsList.whitelist.has(SetType.PATH, url)) {
48+
if (
49+
ActionsList.whitelist.has(SetType.IP, clientIp) ||
50+
ActionsList.whitelist.has(SetType.USER, deviceFP) ||
51+
ActionsList.whitelist.has(SetType.PATH, url)
52+
) {
4853
req.sn_whitelisted = true;
4954
} else if (ActionsList.blackList.has(SetType.IP, clientIp) || ActionsList.blackList.has(SetType.USER, deviceFP)) {
50-
req.sn_finished = true;
51-
super.intercept(snuid, 'blockRequest');
52-
return false;
55+
super.intercept(snuid, 'block');
5356
}
54-
5557
return true;
5658
});
5759

58-
wrap(exports && exports.ServerResponse && exports.ServerResponse.prototype, 'setHeader', function (original) {
59-
return function () {
60-
if (this.sn_finished) {
61-
return;
62-
}
63-
return original.apply(this, arguments);
64-
};
65-
});
66-
67-
68-
wrap(exports && exports.ServerResponse && exports.ServerResponse.prototype, 'writeHead', function (original) {
69-
return function () {
70-
if (this.sn_finished) {
71-
return;
72-
}
73-
return original.apply(this, arguments);
74-
};
75-
});
76-
77-
wrap(exports && exports.ServerResponse && exports.ServerResponse.prototype, 'write', (original) => {
78-
const intercept = super.intercept.bind(this);
79-
return function () {
80-
if (this.sn_finished) {
81-
return;
82-
}
83-
intercept(this.sn_uid, 'write');
84-
return original.apply(this, arguments);
85-
};
86-
});
87-
88-
wrap(exports && exports.ServerResponse && exports.ServerResponse.prototype, 'end', function (original) {
89-
return function () {
90-
SessionManager.cleanSession(this.req.sn_uid);
91-
if (this.sn_finished) {
92-
return;
93-
}
94-
return original.apply(this, arguments);
95-
}
96-
});
97-
9860
return exports;
9961
});
10062
}

src/sdk-manager.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import ApiManager from "./api-manager";
2+
import { EventOptions } from "./types/event-options";
3+
import VerifyResult from "./types/verify-result";
4+
5+
export class SDKManager {
6+
constructor(private apiManager: ApiManager) {}
7+
8+
public track(opts: EventOptions) {
9+
return this.apiManager.track(opts);
10+
}
11+
12+
public async verify(opts: EventOptions): Promise<VerifyResult> {
13+
return await this.apiManager.verify(opts);
14+
}
15+
}

src/securenative.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ describe('SecureNative', () => {
2626
expect(secureNative.agent).to.have.property('startAgent');
2727
expect(secureNative.agent).to.have.property('stopAgent');
2828

29-
expect(secureNative).to.have.property('SDK');
30-
expect(secureNative.SDK).to.have.property('track');
31-
expect(secureNative.SDK).to.have.property('verify');
29+
expect(secureNative).to.have.property('sdk');
30+
expect(secureNative.sdk).to.have.property('track');
31+
expect(secureNative.sdk).to.have.property('verify');
3232
});
3333
});

src/securenative.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@ import EventManager from './event-manager';
33
import ModuleManager from './module-manager';
44
import AgentManager from './agent-manager';
55
import ApiManager from './api-manager';
6+
import { SDKManager } from './sdk-manager';
67

78
export default class SecureNative {
9+
private sdkManager: SDKManager;
810
private agentManager: AgentManager;
911
public apiManager: ApiManager;
1012

11-
constructor(public moduleManager: ModuleManager, eventManager: EventManager, private options: SecureNativeOptions) {
13+
constructor(moduleManager: ModuleManager, eventManager: EventManager, options: SecureNativeOptions) {
1214
if (!moduleManager || !eventManager || !options) {
1315
throw new Error('Unable to create SecureNative instance, invalid config provided');
1416
}
15-
1617
this.apiManager = new ApiManager(eventManager, options);
18+
this.sdkManager = new SDKManager(this.apiManager);
1719
this.agentManager = new AgentManager(moduleManager, this.apiManager, eventManager, options);
1820
}
1921

2022
public get agent(): AgentManager {
2123
return this.agentManager;
2224
}
2325

24-
public get SDK() {
25-
return {
26-
track: this.apiManager.track,
27-
verify: this.apiManager.verify,
28-
};
26+
public get sdk() {
27+
return this.sdkManager;
2928
}
3029
}

0 commit comments

Comments
 (0)