Skip to content

Commit d6fd88c

Browse files
authored
remove try catch from iast plugin (#4804)
* remove try catch from iast plugin * fix linter
1 parent 048736e commit d6fd88c

File tree

3 files changed

+55
-63
lines changed

3 files changed

+55
-63
lines changed

packages/dd-trace/src/appsec/iast/iast-plugin.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,10 @@ class IastPlugin extends Plugin {
6060
this.pluginSubs = []
6161
}
6262

63-
_wrapHandler (handler) {
64-
return (message, name) => {
65-
try {
66-
handler(message, name)
67-
} catch (e) {
68-
log.error('[ASM] Error executing IAST plugin handler', e)
69-
}
70-
}
71-
}
72-
7363
_getTelemetryHandler (iastSub) {
7464
return () => {
75-
try {
76-
const iastContext = getIastContext(storage.getStore())
77-
iastSub.increaseExecuted(iastContext)
78-
} catch (e) {
79-
log.error('[ASM] Error increasing handler executed metrics', e)
80-
}
65+
const iastContext = getIastContext(storage.getStore())
66+
iastSub.increaseExecuted(iastContext)
8167
}
8268
}
8369

@@ -99,11 +85,11 @@ class IastPlugin extends Plugin {
9985

10086
addSub (iastSub, handler) {
10187
if (typeof iastSub === 'string') {
102-
super.addSub(iastSub, this._wrapHandler(handler))
88+
super.addSub(iastSub, handler)
10389
} else {
10490
iastSub = this._getAndRegisterSubscription(iastSub)
10591
if (iastSub) {
106-
super.addSub(iastSub.channelName, this._wrapHandler(handler))
92+
super.addSub(iastSub.channelName, handler)
10793

10894
if (iastTelemetry.isEnabled()) {
10995
super.addSub(iastSub.channelName, this._getTelemetryHandler(iastSub))

packages/dd-trace/test/appsec/iast/analyzers/vulnerability-analyzer.spec.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,6 @@ describe('vulnerability-analyzer', () => {
133133
)
134134
})
135135

136-
it('should wrap subscription handler and catch thrown Errors', () => {
137-
const vulnerabilityAnalyzer = new VulnerabilityAnalyzer(ANALYZER_TYPE)
138-
const handler = sinon.spy(() => {
139-
throw new Error('handler Error')
140-
})
141-
const wrapped = vulnerabilityAnalyzer._wrapHandler(handler)
142-
143-
const iastContext = {
144-
name: 'test'
145-
}
146-
iastContextHandler.getIastContext.returns(iastContext)
147-
148-
expect(typeof wrapped).to.be.equal('function')
149-
const message = {}
150-
const name = 'test'
151-
expect(() => wrapped(message, name)).to.not.throw()
152-
const args = handler.firstCall.args
153-
expect(args[0]).to.be.equal(message)
154-
expect(args[1]).to.be.equal(name)
155-
})
156-
157136
it('should catch thrown Errors inside subscription handlers', () => {
158137
const vulnerabilityAnalyzer = new VulnerabilityAnalyzer(ANALYZER_TYPE)
159138
vulnerabilityAnalyzer.addSub({ channelName: 'dd-trace:test:error:sub' }, () => {

packages/dd-trace/test/appsec/iast/iast-plugin.spec.js

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { expect } = require('chai')
44
const { channel } = require('dc-polyfill')
55
const proxyquire = require('proxyquire')
66
const { getExecutedMetric, getInstrumentedMetric, TagKey } = require('../../../src/appsec/iast/telemetry/iast-metric')
7+
const { IastPlugin } = require('../../../src/appsec/iast/iast-plugin')
78

89
const VULNERABILITY_TYPE = TagKey.VULNERABILITY_TYPE
910
const SOURCE_TYPE = TagKey.SOURCE_TYPE
@@ -71,33 +72,23 @@ describe('IAST Plugin', () => {
7172
})
7273

7374
describe('addSub', () => {
74-
it('should call Plugin.addSub with channelName and wrapped handler', () => {
75+
it('should call Plugin.addSub with channelName and handler', () => {
7576
iastPlugin.addSub('test', handler)
7677

7778
expect(addSubMock).to.be.calledOnce
7879
const args = addSubMock.getCall(0).args
7980
expect(args[0]).equal('test')
80-
81-
const wrapped = args[1]
82-
expect(wrapped).to.be.a('function')
83-
expect(wrapped).to.not.be.equal(handler)
84-
expect(wrapped()).to.not.throw
85-
expect(logError).to.be.calledOnce
81+
expect(args[1]).to.equal(handler)
8682
})
8783

88-
it('should call Plugin.addSub with channelName and wrapped handler after registering iastPluginSub', () => {
84+
it('should call Plugin.addSub with channelName and handler after registering iastPluginSub', () => {
8985
const iastPluginSub = { channelName: 'test' }
9086
iastPlugin.addSub(iastPluginSub, handler)
9187

9288
expect(addSubMock).to.be.calledOnce
9389
const args = addSubMock.getCall(0).args
9490
expect(args[0]).equal('test')
95-
96-
const wrapped = args[1]
97-
expect(wrapped).to.be.a('function')
98-
expect(wrapped).to.not.be.equal(handler)
99-
expect(wrapped()).to.not.throw
100-
expect(logError).to.be.calledOnce
91+
expect(args[1]).to.equal(handler)
10192
})
10293

10394
it('should infer moduleName from channelName after registering iastPluginSub', () => {
@@ -117,20 +108,15 @@ describe('IAST Plugin', () => {
117108
})
118109

119110
it('should not call _getTelemetryHandler', () => {
120-
const wrapHandler = sinon.stub()
121-
iastPlugin._wrapHandler = wrapHandler
122111
const getTelemetryHandler = sinon.stub()
123112
iastPlugin._getTelemetryHandler = getTelemetryHandler
124113
iastPlugin.addSub({ channelName, tagKey: VULNERABILITY_TYPE }, handler)
125114

126-
expect(wrapHandler).to.be.calledOnceWith(handler)
127115
expect(getTelemetryHandler).to.be.not.called
128116

129-
wrapHandler.reset()
130117
getTelemetryHandler.reset()
131118

132119
iastPlugin.addSub({ channelName, tagKey: SOURCE_TYPE, tag: 'test-tag' }, handler)
133-
expect(wrapHandler).to.be.calledOnceWith(handler)
134120
expect(getTelemetryHandler).to.be.not.called
135121
})
136122
})
@@ -235,20 +221,15 @@ describe('IAST Plugin', () => {
235221

236222
describe('addSub', () => {
237223
it('should call _getTelemetryHandler with correct metrics', () => {
238-
const wrapHandler = sinon.stub()
239-
iastPlugin._wrapHandler = wrapHandler
240224
const getTelemetryHandler = sinon.stub()
241225
iastPlugin._getTelemetryHandler = getTelemetryHandler
242226
iastPlugin.addSub({ channelName, tagKey: VULNERABILITY_TYPE }, handler)
243227

244-
expect(wrapHandler).to.be.calledOnceWith(handler)
245228
expect(getTelemetryHandler).to.be.calledOnceWith(iastPlugin.pluginSubs[0])
246229

247-
wrapHandler.reset()
248230
getTelemetryHandler.reset()
249231

250232
iastPlugin.addSub({ channelName, tagKey: SOURCE_TYPE, tag: 'test-tag' }, handler)
251-
expect(wrapHandler).to.be.calledOnceWith(handler)
252233
expect(getTelemetryHandler).to.be.calledOnceWith(iastPlugin.pluginSubs[1])
253234
})
254235

@@ -399,4 +380,50 @@ describe('IAST Plugin', () => {
399380
})
400381
})
401382
})
383+
384+
describe('Add sub to iast plugin', () => {
385+
class BadPlugin extends IastPlugin {
386+
static get id () { return 'badPlugin' }
387+
388+
constructor () {
389+
super()
390+
this.addSub('appsec:badPlugin:start', this.start)
391+
}
392+
393+
start () {
394+
throw new Error('this is one bad plugin')
395+
}
396+
}
397+
class GoodPlugin extends IastPlugin {
398+
static get id () { return 'goodPlugin' }
399+
400+
constructor () {
401+
super()
402+
this.addSub('appsec:goodPlugin:start', this.start)
403+
}
404+
405+
start () {}
406+
}
407+
408+
const badPlugin = new BadPlugin()
409+
const goodPlugin = new GoodPlugin()
410+
411+
it('should disable bad plugin', () => {
412+
badPlugin.configure({ enabled: true })
413+
expect(badPlugin._enabled).to.be.true
414+
415+
channel('appsec:badPlugin:start').publish({ foo: 'bar' })
416+
417+
expect(badPlugin._enabled).to.be.false
418+
})
419+
420+
it('should not disable good plugin', () => {
421+
goodPlugin.configure({ enabled: true })
422+
expect(goodPlugin._enabled).to.be.true
423+
424+
channel('appsec:goodPlugin:start').publish({ foo: 'bar' })
425+
426+
expect(goodPlugin._enabled).to.be.true
427+
})
428+
})
402429
})

0 commit comments

Comments
 (0)