|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'spec_helper' |
| 3 | +require 'rack' |
| 4 | +require 'rack/lint' |
| 5 | +require 'rack/rewindable_input' |
| 6 | +require 'rackup/handler/webrick' |
| 7 | + |
| 8 | +RSpec.describe Slack::Events::Request do |
| 9 | + let(:signing_secret) { 'test-signing-secret' } |
| 10 | + let(:timestamp) { Time.now.to_i.to_s } |
| 11 | + let(:body) { '{"type":"url_verification","challenge":"hello"}' } |
| 12 | + let(:signature) do |
| 13 | + "v0=#{OpenSSL::HMAC.hexdigest('SHA256', signing_secret, "v0:#{timestamp}:#{body}")}" |
| 14 | + end |
| 15 | + let(:consume_body) { false } |
| 16 | + let(:buffer_body) { false } |
| 17 | + let(:observed_inputs) { [] } |
| 18 | + let(:app) do |
| 19 | + Rack::Lint.new(lambda do |env| |
| 20 | + observed_inputs << env.fetch('rack.input') |
| 21 | + env['rack.input'] = Rack::RewindableInput.new(env['rack.input']) if buffer_body |
| 22 | + env['rack.input'].read if consume_body |
| 23 | + slack_request = described_class.new(Rack::Request.new(env), signing_secret: signing_secret) |
| 24 | + begin |
| 25 | + slack_request.verify! |
| 26 | + [200, { 'content-type' => 'application/json' }, [slack_request.body, slack_request.body]] |
| 27 | + rescue Slack::Events::Request::InvalidSignature |
| 28 | + [401, { 'content-type' => 'text/plain' }, ['invalid signature']] |
| 29 | + ensure |
| 30 | + env['rack.input'].close if buffer_body |
| 31 | + end |
| 32 | + end) |
| 33 | + end |
| 34 | + let(:server) { WEBrick::HTTPServer.new(DoNotListen: true, Logger: WEBrick::Log.new(File::NULL), AccessLog: []) } |
| 35 | + |
| 36 | + def http_request |
| 37 | + raw_request = [ |
| 38 | + 'POST /events HTTP/1.1', |
| 39 | + 'Host: localhost', |
| 40 | + 'Content-Type: application/json', |
| 41 | + "Content-Length: #{body.bytesize}", |
| 42 | + "X-Slack-Request-Timestamp: #{timestamp}", |
| 43 | + "X-Slack-Signature: #{signature}", |
| 44 | + '', body |
| 45 | + ].join("\r\n") |
| 46 | + WEBrick::HTTPRequest.new(server.config).tap { |request| request.parse(StringIO.new(raw_request)) } |
| 47 | + end |
| 48 | + |
| 49 | + after do |
| 50 | + server.shutdown |
| 51 | + end |
| 52 | + |
| 53 | + def dispatch |
| 54 | + response = WEBrick::HTTPResponse.new(server.config) |
| 55 | + Rackup::Handler::WEBrick.new(server, app).service(http_request, response) |
| 56 | + response |
| 57 | + end |
| 58 | + |
| 59 | + it 'verifies a signed WEBrick request and caches its complete body' do |
| 60 | + http_response = dispatch |
| 61 | + expect(http_response.status).to eq 200 |
| 62 | + expect(http_response.body).to eq body * 2 |
| 63 | + expect(observed_inputs.first).not_to respond_to(:rewind) |
| 64 | + end |
| 65 | + |
| 66 | + context 'with an invalid signature' do |
| 67 | + let(:signature) { 'v0=invalid' } |
| 68 | + |
| 69 | + it 'rejects the request' do |
| 70 | + http_response = dispatch |
| 71 | + expect(http_response.status).to eq 401 |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context 'with input already read by another consumer' do |
| 76 | + let(:consume_body) { true } |
| 77 | + |
| 78 | + it 'rejects the request because the full body cannot be recovered' do |
| 79 | + http_response = dispatch |
| 80 | + expect(http_response.status).to eq 401 |
| 81 | + end |
| 82 | + |
| 83 | + context 'with input buffering before the first consumer' do |
| 84 | + let(:buffer_body) { true } |
| 85 | + |
| 86 | + it 'verifies the request and preserves the full body' do |
| 87 | + http_response = dispatch |
| 88 | + expect(http_response.status).to eq 200 |
| 89 | + expect(http_response.body).to eq body * 2 |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments