-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathtwilio_webhook_authentication_spec.rb
284 lines (231 loc) · 10.2 KB
/
twilio_webhook_authentication_spec.rb
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require 'spec_helper'
require 'rack/mock'
describe Rack::TwilioWebhookAuthentication do
before do
@app = ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['Hello']] }
end
describe 'new' do
it 'should initialize with an app, auth token and a path' do
expect do
Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/)
end.not_to raise_error
end
it 'should initialize with an app, auth token and paths' do
expect do
Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/, /\/sms/)
end.not_to raise_error
end
it 'should initialize with an app, dynamic token and paths' do
expect do
Rack::TwilioWebhookAuthentication.new(@app, nil, /\/voice/, /\/sms/)
end.not_to raise_error
end
it 'should initialize with an app, auth token(s) and a path' do
expect do
Rack::TwilioWebhookAuthentication.new(@app, ['ABC', 'DEF'], /\/voice/, /\/sms/)
end.not_to raise_error
end
end
describe 'calling against one path with dynamic auth token' do
before do
allow_any_instance_of(Rack::Request).to receive(:post?).and_return(true)
allow_any_instance_of(Rack::Request).to receive(:media_type).and_return(Rack::MediaType.type('application/x-www-form-urlencoded'))
allow_any_instance_of(Rack::Request).to receive(:POST).and_return({ 'AccountSid' => 12_345 })
@middleware = Rack::TwilioWebhookAuthentication.new(@app, nil, /\/voice/) { |asid| 'qwerty' }
end
it 'should allow a request through if it validates' do
allow_any_instance_of(Twilio::Security::RequestValidator).to receive(:validate).and_return(true)
request = Rack::MockRequest.env_for('/voice')
status, headers, body = @middleware.call(request)
expect(status).to be(200)
end
end
describe 'calling against one path' do
before do
@middleware = Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/)
end
it 'should not intercept when the path doesn\'t match' do
expect(Twilio::Security::RequestValidator).to_not receive(:validate)
request = Rack::MockRequest.env_for('/sms')
status, headers, body = @middleware.call(request)
expect(status).to be(200)
end
it 'should allow a request through if it validates' do
expect_any_instance_of(Twilio::Security::RequestValidator).to(
receive(:validate).and_return(true)
)
request = Rack::MockRequest.env_for('/voice')
status, headers, body = @middleware.call(request)
expect(status).to be(200)
end
it 'should short circuit a request to 403 if it does not validate' do
expect_any_instance_of(Twilio::Security::RequestValidator).to(
receive(:validate).and_return(false)
)
request = Rack::MockRequest.env_for('/voice')
status, headers, body = @middleware.call(request)
expect(status).to be(403)
end
context 'with secondary auth_token' do
let(:auth_token) { ['ABC', 'DEF'] }
it 'should not intercept when the path doesn\'t match' do
expect(Twilio::Security::RequestValidator).to_not receive(:validate)
request = Rack::MockRequest.env_for('/sms')
status, headers, body = @middleware.call(request)
expect(status).to be(200)
end
it 'should allow a request through if it validates' do
expect_any_instance_of(Twilio::Security::RequestValidator).to(
receive(:validate).and_return(true)
)
request = Rack::MockRequest.env_for('/voice')
status, headers, body = @middleware.call(request)
expect(status).to be(200)
end
it 'should short circuit a request to 403 if it does not validate' do
expect_any_instance_of(Twilio::Security::RequestValidator).to(
receive(:validate).and_return(false)
)
request = Rack::MockRequest.env_for('/voice')
status, headers, body = @middleware.call(request)
expect(status).to be(403)
end
end
end
describe 'calling against many paths' do
before do
@middleware = Rack::TwilioWebhookAuthentication.new(@app, 'ABC', /\/voice/, /\/sms/)
end
it 'should not intercept when the path doesn\'t match' do
expect(Twilio::Security::RequestValidator).to_not receive(:validate)
request = Rack::MockRequest.env_for('icesms')
status, headers, body = @middleware.call(request)
expect(status).to be(200)
end
it 'shold allow a request through if it validates' do
expect_any_instance_of(Twilio::Security::RequestValidator).to(
receive(:validate).and_return(true)
)
request = Rack::MockRequest.env_for('/sms')
status, headers, body = @middleware.call(request)
expect(status).to be(200)
end
it 'should short circuit a request to 403 if it does not validate' do
expect_any_instance_of(Twilio::Security::RequestValidator).to(
receive(:validate).and_return(false)
)
request = Rack::MockRequest.env_for('/sms')
status, headers, body = @middleware.call(request)
expect(status).to be(403)
end
end
describe 'validating non-form-data POST payloads' do
it 'should fail if the body does not validate' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/)
input = StringIO.new('{"message": "a post body that does not match the bodySHA256"}')
request = Rack::MockRequest.env_for(
'https://example.com/test?bodySHA256=79bfb0acaf0045fd30f13d48d4fe296b393d85a3bfbee881a0172b2bd574b11e',
method: 'POST',
input: input
)
request['HTTP_X_TWILIO_SIGNATURE'] = '+LYlbGr/VmN84YPJQCuWs+9UA7E='
request['CONTENT_TYPE'] = 'application/json'
status, headers, body = middleware.call(request)
expect(status).not_to be(200)
end
it 'should validate if the body signature is correct' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/)
input = StringIO.new('{"message": "a post body"}')
request = Rack::MockRequest.env_for(
'https://example.com/test?bodySHA256=8d90d640c6ba47d595ac56203d7f5c6b511be80fdf44a2055acca75a119b9fd2',
method: 'POST',
input: input
)
request['HTTP_X_TWILIO_SIGNATURE'] = 'zR5Oq4f6cijN5oz5bisiVuxYnTU='
request['CONTENT_TYPE'] = 'application/json'
status, headers, body = middleware.call(request)
expect(status).to be(200)
end
it 'should validate if the body signature is correct for secondary token' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, ['invalid', 'qwerty'], /\/test/)
input = StringIO.new('{"message": "a post body"}')
request = Rack::MockRequest.env_for(
'https://example.com/test?bodySHA256=8d90d640c6ba47d595ac56203d7f5c6b511be80fdf44a2055acca75a119b9fd2',
method: 'POST',
input: input
)
request['HTTP_X_TWILIO_SIGNATURE'] = 'zR5Oq4f6cijN5oz5bisiVuxYnTU='
request['CONTENT_TYPE'] = 'application/json'
status, headers, body = middleware.call(request)
expect(status).to be(200)
end
it 'should fail if the body does not validate for any token' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, ['invalid', 'invalid2', 'invalid3'], /\/test/)
input = StringIO.new('{"message": "a post body that does not match the bodySHA256"}')
request = Rack::MockRequest.env_for(
'https://example.com/test?bodySHA256=79bfb0acaf0045fd30f13d48d4fe296b393d85a3bfbee881a0172b2bd574b11e',
method: 'POST',
input: input
)
request['HTTP_X_TWILIO_SIGNATURE'] = '+LYlbGr/VmN84YPJQCuWs+9UA7E='
request['CONTENT_TYPE'] = 'application/json'
status, headers, body = middleware.call(request)
expect(status).not_to be(200)
end
it 'should validate if the body signature is correct for dynamic secondary token' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, nil, /\/test/) do |account_sid|
return ['invalid', 'qwerty']
end
input = StringIO.new('{"message": "a post body"}')
request = Rack::MockRequest.env_for(
'https://example.com/test?bodySHA256=8d90d640c6ba47d595ac56203d7f5c6b511be80fdf44a2055acca75a119b9fd2',
method: 'POST',
input: input
)
request['HTTP_X_TWILIO_SIGNATURE'] = 'zR5Oq4f6cijN5oz5bisiVuxYnTU='
request['CONTENT_TYPE'] = 'application/json'
status, headers, body = middleware.call(request)
expect(status).to be(200)
end
it 'should validate even if a previous middleware read the body first' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/)
input = StringIO.new('{"message": "a post body"}')
request = Rack::MockRequest.env_for(
'https://example.com/test?bodySHA256=8d90d640c6ba47d595ac56203d7f5c6b511be80fdf44a2055acca75a119b9fd2',
method: 'POST',
input: input
)
request['HTTP_X_TWILIO_SIGNATURE'] = 'zR5Oq4f6cijN5oz5bisiVuxYnTU='
request['CONTENT_TYPE'] = 'application/json'
request['rack.input'].read
status, headers, body = middleware.call(request)
expect(status).to be(200)
end
end
describe 'validating application/x-www-form-urlencoded POST payloads' do
it 'should fail if the body does not validate' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/)
request = Rack::MockRequest.env_for(
'https://example.com/test',
method: 'POST',
params: { 'foo' => 'bar' }
)
request['HTTP_X_TWILIO_SIGNATURE'] = 'foobarbaz'
expect(request['CONTENT_TYPE']).to eq('application/x-www-form-urlencoded')
status, headers, body = middleware.call(request)
expect(status).not_to be(200)
end
it 'should validate if the body signature is correct' do
middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/)
request = Rack::MockRequest.env_for(
'https://example.com/test',
method: 'POST',
params: { 'foo' => 'bar' }
)
request['HTTP_X_TWILIO_SIGNATURE'] = 'TR9Skm9jiF4WVRJznU5glK5I83k='
expect(request['CONTENT_TYPE']).to eq('application/x-www-form-urlencoded')
status, headers, body = middleware.call(request)
expect(status).to be(200)
end
end
end