@@ -4,6 +4,7 @@ const { expect } = require('chai')
4
4
const { channel } = require ( 'dc-polyfill' )
5
5
const proxyquire = require ( 'proxyquire' )
6
6
const { getExecutedMetric, getInstrumentedMetric, TagKey } = require ( '../../../src/appsec/iast/telemetry/iast-metric' )
7
+ const { IastPlugin } = require ( '../../../src/appsec/iast/iast-plugin' )
7
8
8
9
const VULNERABILITY_TYPE = TagKey . VULNERABILITY_TYPE
9
10
const SOURCE_TYPE = TagKey . SOURCE_TYPE
@@ -71,33 +72,23 @@ describe('IAST Plugin', () => {
71
72
} )
72
73
73
74
describe ( 'addSub' , ( ) => {
74
- it ( 'should call Plugin.addSub with channelName and wrapped handler' , ( ) => {
75
+ it ( 'should call Plugin.addSub with channelName and handler' , ( ) => {
75
76
iastPlugin . addSub ( 'test' , handler )
76
77
77
78
expect ( addSubMock ) . to . be . calledOnce
78
79
const args = addSubMock . getCall ( 0 ) . args
79
80
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 )
86
82
} )
87
83
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' , ( ) => {
89
85
const iastPluginSub = { channelName : 'test' }
90
86
iastPlugin . addSub ( iastPluginSub , handler )
91
87
92
88
expect ( addSubMock ) . to . be . calledOnce
93
89
const args = addSubMock . getCall ( 0 ) . args
94
90
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 )
101
92
} )
102
93
103
94
it ( 'should infer moduleName from channelName after registering iastPluginSub' , ( ) => {
@@ -117,20 +108,15 @@ describe('IAST Plugin', () => {
117
108
} )
118
109
119
110
it ( 'should not call _getTelemetryHandler' , ( ) => {
120
- const wrapHandler = sinon . stub ( )
121
- iastPlugin . _wrapHandler = wrapHandler
122
111
const getTelemetryHandler = sinon . stub ( )
123
112
iastPlugin . _getTelemetryHandler = getTelemetryHandler
124
113
iastPlugin . addSub ( { channelName, tagKey : VULNERABILITY_TYPE } , handler )
125
114
126
- expect ( wrapHandler ) . to . be . calledOnceWith ( handler )
127
115
expect ( getTelemetryHandler ) . to . be . not . called
128
116
129
- wrapHandler . reset ( )
130
117
getTelemetryHandler . reset ( )
131
118
132
119
iastPlugin . addSub ( { channelName, tagKey : SOURCE_TYPE , tag : 'test-tag' } , handler )
133
- expect ( wrapHandler ) . to . be . calledOnceWith ( handler )
134
120
expect ( getTelemetryHandler ) . to . be . not . called
135
121
} )
136
122
} )
@@ -235,20 +221,15 @@ describe('IAST Plugin', () => {
235
221
236
222
describe ( 'addSub' , ( ) => {
237
223
it ( 'should call _getTelemetryHandler with correct metrics' , ( ) => {
238
- const wrapHandler = sinon . stub ( )
239
- iastPlugin . _wrapHandler = wrapHandler
240
224
const getTelemetryHandler = sinon . stub ( )
241
225
iastPlugin . _getTelemetryHandler = getTelemetryHandler
242
226
iastPlugin . addSub ( { channelName, tagKey : VULNERABILITY_TYPE } , handler )
243
227
244
- expect ( wrapHandler ) . to . be . calledOnceWith ( handler )
245
228
expect ( getTelemetryHandler ) . to . be . calledOnceWith ( iastPlugin . pluginSubs [ 0 ] )
246
229
247
- wrapHandler . reset ( )
248
230
getTelemetryHandler . reset ( )
249
231
250
232
iastPlugin . addSub ( { channelName, tagKey : SOURCE_TYPE , tag : 'test-tag' } , handler )
251
- expect ( wrapHandler ) . to . be . calledOnceWith ( handler )
252
233
expect ( getTelemetryHandler ) . to . be . calledOnceWith ( iastPlugin . pluginSubs [ 1 ] )
253
234
} )
254
235
@@ -399,4 +380,50 @@ describe('IAST Plugin', () => {
399
380
} )
400
381
} )
401
382
} )
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
+ } )
402
429
} )
0 commit comments