|
31 | 31 | auth_token = 'qwerty'
|
32 | 32 | account_sid = 12_345
|
33 | 33 | expect_any_instance_of(Rack::Request).to receive(:post?).and_return(true)
|
| 34 | + expect_any_instance_of(Rack::Request).to receive(:media_type).and_return(Rack::MediaType.type('application/x-www-form-urlencoded')) |
34 | 35 | expect_any_instance_of(Rack::Request).to receive(:POST).and_return({ 'AccountSid' => account_sid })
|
35 | 36 | @middleware = Rack::TwilioWebhookAuthentication.new(@app, nil, /\/voice/) { |asid| auth_token }
|
36 | 37 | request_validator = double('RequestValidator')
|
|
103 | 104 | expect(status).to be(403)
|
104 | 105 | end
|
105 | 106 | end
|
| 107 | + |
| 108 | + describe 'validating non-form-data POST payloads' do |
| 109 | + it 'should fail if the body does not validate' do |
| 110 | + middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/) |
| 111 | + input = StringIO.new('{"message": "a post body that does not match the bodySHA256"}') |
| 112 | + |
| 113 | + request = Rack::MockRequest.env_for( |
| 114 | + 'https://example.com/test?bodySHA256=79bfb0acaf0045fd30f13d48d4fe296b393d85a3bfbee881a0172b2bd574b11e', |
| 115 | + method: 'POST', |
| 116 | + input: input |
| 117 | + ) |
| 118 | + request['HTTP_X_TWILIO_SIGNATURE'] = '+LYlbGr/VmN84YPJQCuWs+9UA7E=' |
| 119 | + request['CONTENT_TYPE'] = 'application/json' |
| 120 | + |
| 121 | + status, headers, body = middleware.call(request) |
| 122 | + |
| 123 | + expect(status).not_to be(200) |
| 124 | + end |
| 125 | + |
| 126 | + it 'should validate if the body signature is correct' do |
| 127 | + middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/) |
| 128 | + input = StringIO.new('{"message": "a post body"}') |
| 129 | + |
| 130 | + request = Rack::MockRequest.env_for( |
| 131 | + 'https://example.com/test?bodySHA256=8d90d640c6ba47d595ac56203d7f5c6b511be80fdf44a2055acca75a119b9fd2', |
| 132 | + method: 'POST', |
| 133 | + input: input |
| 134 | + ) |
| 135 | + request['HTTP_X_TWILIO_SIGNATURE'] = 'zR5Oq4f6cijN5oz5bisiVuxYnTU=' |
| 136 | + request['CONTENT_TYPE'] = 'application/json' |
| 137 | + |
| 138 | + status, headers, body = middleware.call(request) |
| 139 | + |
| 140 | + expect(status).to be(200) |
| 141 | + end |
| 142 | + |
| 143 | + it 'should validate even if a previous middleware read the body first' do |
| 144 | + middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/) |
| 145 | + input = StringIO.new('{"message": "a post body"}') |
| 146 | + |
| 147 | + request = Rack::MockRequest.env_for( |
| 148 | + 'https://example.com/test?bodySHA256=8d90d640c6ba47d595ac56203d7f5c6b511be80fdf44a2055acca75a119b9fd2', |
| 149 | + method: 'POST', |
| 150 | + input: input |
| 151 | + ) |
| 152 | + request['HTTP_X_TWILIO_SIGNATURE'] = 'zR5Oq4f6cijN5oz5bisiVuxYnTU=' |
| 153 | + request['CONTENT_TYPE'] = 'application/json' |
| 154 | + request['rack.input'].read |
| 155 | + |
| 156 | + status, headers, body = middleware.call(request) |
| 157 | + |
| 158 | + expect(status).to be(200) |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + describe 'validating application/x-www-form-urlencoded POST payloads' do |
| 163 | + it 'should fail if the body does not validate' do |
| 164 | + middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/) |
| 165 | + |
| 166 | + request = Rack::MockRequest.env_for( |
| 167 | + 'https://example.com/test', |
| 168 | + method: 'POST', |
| 169 | + params: { 'foo' => 'bar' } |
| 170 | + ) |
| 171 | + request['HTTP_X_TWILIO_SIGNATURE'] = 'foobarbaz' |
| 172 | + expect(request['CONTENT_TYPE']).to eq('application/x-www-form-urlencoded') |
| 173 | + |
| 174 | + status, headers, body = middleware.call(request) |
| 175 | + |
| 176 | + expect(status).not_to be(200) |
| 177 | + end |
| 178 | + |
| 179 | + it 'should validate if the body signature is correct' do |
| 180 | + middleware = Rack::TwilioWebhookAuthentication.new(@app, 'qwerty', /\/test/) |
| 181 | + |
| 182 | + request = Rack::MockRequest.env_for( |
| 183 | + 'https://example.com/test', |
| 184 | + method: 'POST', |
| 185 | + params: { 'foo' => 'bar' } |
| 186 | + ) |
| 187 | + request['HTTP_X_TWILIO_SIGNATURE'] = 'TR9Skm9jiF4WVRJznU5glK5I83k=' |
| 188 | + expect(request['CONTENT_TYPE']).to eq('application/x-www-form-urlencoded') |
| 189 | + |
| 190 | + status, headers, body = middleware.call(request) |
| 191 | + |
| 192 | + expect(status).to be(200) |
| 193 | + end |
| 194 | + end |
106 | 195 | end
|
0 commit comments