-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathlfi.spec.js
142 lines (106 loc) · 3.37 KB
/
lfi.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict'
const proxyquire = require('proxyquire')
const { assert } = require('chai')
const { fsOperationStart, incomingHttpRequestStart } = require('../../../src/appsec/channels')
const { FS_OPERATION_PATH } = require('../../../src/appsec/addresses')
const { RASP_MODULE } = require('../../../src/appsec/rasp/fs-plugin')
describe('RASP - lfi.js', () => {
let waf, legacyStorage, lfi, web, blocking, appsecFsPlugin, config
beforeEach(() => {
legacyStorage = {
getStore: sinon.stub()
}
waf = {
run: sinon.stub()
}
web = {
root: sinon.stub()
}
blocking = {
block: sinon.stub()
}
appsecFsPlugin = {
enable: sinon.stub(),
disable: sinon.stub()
}
lfi = proxyquire('../../../src/appsec/rasp/lfi', {
'../../../../datadog-core': { storage: () => legacyStorage },
'../waf': waf,
'../../plugins/util/web': web,
'../blocking': blocking,
'./fs-plugin': appsecFsPlugin
})
config = {
appsec: {
stackTrace: {
enabled: true,
maxStackTraces: 2,
maxDepth: 42
}
}
}
})
afterEach(() => {
sinon.restore()
lfi.disable()
})
describe('enable', () => {
it('should subscribe to first http req', () => {
const subscribe = sinon.stub(incomingHttpRequestStart, 'subscribe')
lfi.enable(config)
sinon.assert.calledOnce(subscribe)
})
it('should enable AppsecFsPlugin after the first request', () => {
const unsubscribe = sinon.stub(incomingHttpRequestStart, 'unsubscribe')
const fsOpSubscribe = sinon.stub(fsOperationStart, 'subscribe')
lfi.enable(config)
incomingHttpRequestStart.publish({})
sinon.assert.calledOnceWithExactly(appsecFsPlugin.enable, RASP_MODULE)
assert(fsOpSubscribe.calledAfter(appsecFsPlugin.enable))
process.nextTick(() => {
sinon.assert.calledOnce(unsubscribe)
})
})
})
describe('disable', () => {
it('should disable AppsecFsPlugin', () => {
lfi.enable(config)
lfi.disable()
sinon.assert.calledOnceWithExactly(appsecFsPlugin.disable, RASP_MODULE)
})
})
describe('analyzeLfi', () => {
const path = '/etc/passwd'
const ctx = { path }
const req = {}
beforeEach(() => {
lfi.enable(config)
incomingHttpRequestStart.publish({})
})
it('should analyze lfi for root fs operations', () => {
const fs = { root: true }
legacyStorage.getStore.returns({ req, fs })
fsOperationStart.publish(ctx)
const ephemeral = { [FS_OPERATION_PATH]: path }
sinon.assert.calledOnceWithExactly(waf.run, { ephemeral }, req, { type: 'lfi' })
})
it('should NOT analyze lfi for child fs operations', () => {
const fs = {}
legacyStorage.getStore.returns({ req, fs })
fsOperationStart.publish(ctx)
sinon.assert.notCalled(waf.run)
})
it('should NOT analyze lfi for undefined fs (AppsecFsPlugin disabled)', () => {
const fs = undefined
legacyStorage.getStore.returns({ req, fs })
fsOperationStart.publish(ctx)
sinon.assert.notCalled(waf.run)
})
it('should NOT analyze lfi for excluded operations', () => {
const fs = { opExcluded: true, root: true }
legacyStorage.getStore.returns({ req, fs })
fsOperationStart.publish(ctx)
sinon.assert.notCalled(waf.run)
})
})
})