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

Commit

Permalink
feat: add an error event when fail to add bp
Browse files Browse the repository at this point in the history
  • Loading branch information
AH7 committed Sep 26, 2018
1 parent ecf65b7 commit fdff85c
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 10 deletions.
13 changes: 12 additions & 1 deletion lib/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ function start() {
};
console.log(JSON.stringify(message));
} else {
console.log(JSON.stringify(error));
var errorMessage = {
methodName,
moduleInfo,
error,
message: 'Failed to set breakpoint',
};
console.log(JSON.stringify(errorMessage));
transmitter.addEvent( {
error: errorMessage,
timestamp: (new Date()).toISOString(),
});
}
});
}
Expand All @@ -80,6 +90,7 @@ function handleDebuggerPausedEvent(message) {
var breakpointId = message.hitBreakpoints[0];
transmitter.addEvent( {
bp: breakpointId,
message: 'Method was called',
info: breakpointsMap[breakpointId],
timestamp: (new Date()).toISOString(),
});
Expand Down
2 changes: 1 addition & 1 deletion lib/transmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function addEvent(event) {
eventsToSend.push(event);
var message = {
event,
message: 'Method was called',
message: 'Event was added',
};
console.log(JSON.stringify(message));
}
Expand Down
8 changes: 6 additions & 2 deletions lib/vuln-mgmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function loadVulnerabiltiesMetadata() {
console.log(JSON.stringify(
{message: 'Using app defined method.json to load vulnerabilties'})
);
console.log('Before require', appMetadataFilePath);
vulnerabiltiesMetadata = require(appMetadataFilePath);
}
return vulnerabiltiesMetadata;
Expand Down Expand Up @@ -45,11 +44,15 @@ function isVulnerableModulePath(moduleInfo) {
return foundVulnerableMethod;
}

//TODO: fix it hack for tests
function setVulnerabiltiesMetadata(vulnMetadata) {
vulnerabiltiesMetadata = vulnMetadata;
}

function getVulnerableMethodsLocations(moduleInfo, scriptPath) {
var name = moduleInfo.name;
var version = moduleInfo.version;
var scriptRelativePath = moduleInfo.scriptRelativePath;

var vulnerableFunctionNames = [];
var scriptPathMethods =
vulnerabiltiesMetadata[name][scriptRelativePath];
Expand All @@ -72,6 +75,7 @@ function getVulnerableMethodsLocations(moduleInfo, scriptPath) {
}

module.exports = {
setVulnerabiltiesMetadata,
loadVulnerabiltiesMetadata,
isVulnerableModulePath,
getVulnerableMethodsLocations,
Expand Down
38 changes: 35 additions & 3 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
"devDependencies": {
"eslint": "^4.19.1",
"tap": "^12.0.1",
"tap-only": "0.0.5"
"tap-only": "0.0.5",
"proxyquire": "^2.1.0",
"sinon": "^6.1.5"
},
"dependencies": {
"acorn": "^5.7.1",
"needle": "^2.2.1",
"sinon": "^6.1.5"
"needle": "^2.2.1"
},
"publishConfig": {
"access": "restricted"
Expand Down
43 changes: 43 additions & 0 deletions test/debugger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const test = require('tap').test;
const sinon = require('sinon');
var inspector = require('inspector');
const EventEmitter = require('events');
var dbg = require('../lib/debugger');
var vulnMgmt = require('../lib/vuln-mgmt');
var moduleUtils = require('../lib/moduleUtils');
var transmitter = require('../lib/transmitter');

class MockSession extends EventEmitter {
constructor() {
super();
};

connect() {
};

post(method, params, cb) {
if ((method === 'Debugger.setBreakpointByUrl') && (params.lineNumber === 158)) {
cb(undefined, {breakpointId: 'MY_BP_IDDD'});
} else if ((method === 'Debugger.setBreakpointByUrl') && (params.lineNumber !== 158)) {
cb({error: 'MY_ERROR_MESSAGE'}, undefined);
};
}
}

test('test setting a breakpoint', function (t) {
var mock = new MockSession();
sinon.stub(inspector, 'Session').returns(mock);
sinon.stub(moduleUtils, 'getModuleInfo').returns(
{'version': '0.2.1','name': 'st', 'scriptRelativePath': 'st.js'}
);
dbg.start();
vulnMgmt.setVulnerabiltiesMetadata(require('./fixtures/st/vulnerable_methods.json'));
var stScriptInfo = require('./fixtures/st/script.json');
var transmitterSpy = sinon.spy(transmitter, 'addEvent');
stScriptInfo.params.url = __dirname + '/' + stScriptInfo.params.url;
mock.emit('Debugger.scriptParsed', stScriptInfo);
t.assert('error' in transmitterSpy.args[0][0], 'Error event was added to transmitter');
t.equal(1, transmitterSpy.callCount, 'Add event was call once because of set bp error');
t.equal(true, true, 'Mount.prototype.getPath found');
t.end();
});
6 changes: 6 additions & 0 deletions test/fixtures/st/script.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"params": {
"scriptId": 1111,
"url": "./fixtures/st/node_modules/st.js"
}
}
16 changes: 16 additions & 0 deletions test/fixtures/st/vulnerable_methods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"st": {
"st.js" : [
{
"name" : ["Mount.prototype.getPath"],
"semver": ["<0.2.5"],
"id": "npm:st:20140206"
},
{
"name" : ["Mount.prototype.getCacheOptions"],
"semver": ["<0.2.5"],
"id": "npm:st:20140207"
}
]
}
}

0 comments on commit fdff85c

Please sign in to comment.