Skip to content
This repository was archived by the owner on Jun 2, 2022. It is now read-only.

Commit fdff85c

Browse files
committed
feat: add an error event when fail to add bp
1 parent ecf65b7 commit fdff85c

File tree

8 files changed

+123
-10
lines changed

8 files changed

+123
-10
lines changed

lib/debugger.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,17 @@ function start() {
7070
};
7171
console.log(JSON.stringify(message));
7272
} else {
73-
console.log(JSON.stringify(error));
73+
var errorMessage = {
74+
methodName,
75+
moduleInfo,
76+
error,
77+
message: 'Failed to set breakpoint',
78+
};
79+
console.log(JSON.stringify(errorMessage));
80+
transmitter.addEvent( {
81+
error: errorMessage,
82+
timestamp: (new Date()).toISOString(),
83+
});
7484
}
7585
});
7686
}
@@ -80,6 +90,7 @@ function handleDebuggerPausedEvent(message) {
8090
var breakpointId = message.hitBreakpoints[0];
8191
transmitter.addEvent( {
8292
bp: breakpointId,
93+
message: 'Method was called',
8394
info: breakpointsMap[breakpointId],
8495
timestamp: (new Date()).toISOString(),
8596
});

lib/transmitter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function addEvent(event) {
2424
eventsToSend.push(event);
2525
var message = {
2626
event,
27-
message: 'Method was called',
27+
message: 'Event was added',
2828
};
2929
console.log(JSON.stringify(message));
3030
}

lib/vuln-mgmt.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function loadVulnerabiltiesMetadata() {
1616
console.log(JSON.stringify(
1717
{message: 'Using app defined method.json to load vulnerabilties'})
1818
);
19-
console.log('Before require', appMetadataFilePath);
2019
vulnerabiltiesMetadata = require(appMetadataFilePath);
2120
}
2221
return vulnerabiltiesMetadata;
@@ -45,11 +44,15 @@ function isVulnerableModulePath(moduleInfo) {
4544
return foundVulnerableMethod;
4645
}
4746

47+
//TODO: fix it hack for tests
48+
function setVulnerabiltiesMetadata(vulnMetadata) {
49+
vulnerabiltiesMetadata = vulnMetadata;
50+
}
51+
4852
function getVulnerableMethodsLocations(moduleInfo, scriptPath) {
4953
var name = moduleInfo.name;
5054
var version = moduleInfo.version;
5155
var scriptRelativePath = moduleInfo.scriptRelativePath;
52-
5356
var vulnerableFunctionNames = [];
5457
var scriptPathMethods =
5558
vulnerabiltiesMetadata[name][scriptRelativePath];
@@ -72,6 +75,7 @@ function getVulnerableMethodsLocations(moduleInfo, scriptPath) {
7275
}
7376

7477
module.exports = {
78+
setVulnerabiltiesMetadata,
7579
loadVulnerabiltiesMetadata,
7680
isVulnerableModulePath,
7781
getVulnerableMethodsLocations,

package-lock.json

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

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
"devDependencies": {
2020
"eslint": "^4.19.1",
2121
"tap": "^12.0.1",
22-
"tap-only": "0.0.5"
22+
"tap-only": "0.0.5",
23+
"proxyquire": "^2.1.0",
24+
"sinon": "^6.1.5"
2325
},
2426
"dependencies": {
2527
"acorn": "^5.7.1",
26-
"needle": "^2.2.1",
27-
"sinon": "^6.1.5"
28+
"needle": "^2.2.1"
2829
},
2930
"publishConfig": {
3031
"access": "restricted"

test/debugger.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const test = require('tap').test;
2+
const sinon = require('sinon');
3+
var inspector = require('inspector');
4+
const EventEmitter = require('events');
5+
var dbg = require('../lib/debugger');
6+
var vulnMgmt = require('../lib/vuln-mgmt');
7+
var moduleUtils = require('../lib/moduleUtils');
8+
var transmitter = require('../lib/transmitter');
9+
10+
class MockSession extends EventEmitter {
11+
constructor() {
12+
super();
13+
};
14+
15+
connect() {
16+
};
17+
18+
post(method, params, cb) {
19+
if ((method === 'Debugger.setBreakpointByUrl') && (params.lineNumber === 158)) {
20+
cb(undefined, {breakpointId: 'MY_BP_IDDD'});
21+
} else if ((method === 'Debugger.setBreakpointByUrl') && (params.lineNumber !== 158)) {
22+
cb({error: 'MY_ERROR_MESSAGE'}, undefined);
23+
};
24+
}
25+
}
26+
27+
test('test setting a breakpoint', function (t) {
28+
var mock = new MockSession();
29+
sinon.stub(inspector, 'Session').returns(mock);
30+
sinon.stub(moduleUtils, 'getModuleInfo').returns(
31+
{'version': '0.2.1','name': 'st', 'scriptRelativePath': 'st.js'}
32+
);
33+
dbg.start();
34+
vulnMgmt.setVulnerabiltiesMetadata(require('./fixtures/st/vulnerable_methods.json'));
35+
var stScriptInfo = require('./fixtures/st/script.json');
36+
var transmitterSpy = sinon.spy(transmitter, 'addEvent');
37+
stScriptInfo.params.url = __dirname + '/' + stScriptInfo.params.url;
38+
mock.emit('Debugger.scriptParsed', stScriptInfo);
39+
t.assert('error' in transmitterSpy.args[0][0], 'Error event was added to transmitter');
40+
t.equal(1, transmitterSpy.callCount, 'Add event was call once because of set bp error');
41+
t.equal(true, true, 'Mount.prototype.getPath found');
42+
t.end();
43+
});

test/fixtures/st/script.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"params": {
3+
"scriptId": 1111,
4+
"url": "./fixtures/st/node_modules/st.js"
5+
}
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"st": {
3+
"st.js" : [
4+
{
5+
"name" : ["Mount.prototype.getPath"],
6+
"semver": ["<0.2.5"],
7+
"id": "npm:st:20140206"
8+
},
9+
{
10+
"name" : ["Mount.prototype.getCacheOptions"],
11+
"semver": ["<0.2.5"],
12+
"id": "npm:st:20140207"
13+
}
14+
]
15+
}
16+
}

0 commit comments

Comments
 (0)