about summary refs log tree commit diff
path: root/spec/controllers/concerns/signature_verification_spec.rb
blob: 13655f3133482923f81a842b507480f2990c7140 (plain) (blame)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# frozen_string_literal: true

require 'rails_helper'

describe ApplicationController, type: :controller do
  class WrappedActor
    attr_reader :wrapped_account

    def initialize(wrapped_account)
      @wrapped_account = wrapped_account
    end

    delegate :uri, :keypair, to: :wrapped_account
  end

  controller do
    include SignatureVerification

    before_action :require_actor_signature!, only: [:signature_required]

    def success
      head 200
    end

    def alternative_success
      head 200
    end

    def signature_required
      head 200
    end
  end

  before do
    routes.draw do
      match via: [:get, :post], 'success' => 'anonymous#success'
      match via: [:get, :post], 'signature_required' => 'anonymous#signature_required'
    end
  end

  context 'without signature header' do
    before do
      get :success
    end

    describe '#signed_request?' do
      it 'returns false' do
        expect(controller.signed_request?).to be false
      end
    end

    describe '#signed_request_account' do
      it 'returns nil' do
        expect(controller.signed_request_account).to be_nil
      end
    end
  end

  context 'with signature header' do
    let!(:author) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/actor') }

    context 'without body' do
      before do
        get :success

        fake_request = Request.new(:get, request.url)
        fake_request.on_behalf_of(author)

        request.headers.merge!(fake_request.headers)
      end

      describe '#signed_request?' do
        it 'returns true' do
          expect(controller.signed_request?).to be true
        end
      end

      describe '#signed_request_account' do
        it 'returns an account' do
          expect(controller.signed_request_account).to eq author
        end

        it 'returns nil when path does not match' do
          request.path = '/alternative-path'
          expect(controller.signed_request_account).to be_nil
        end

        it 'returns nil when method does not match' do
          post :success
          expect(controller.signed_request_account).to be_nil
        end
      end
    end

    context 'with a valid actor that is not an Account' do
      let(:actor) { WrappedActor.new(author) }

      before do
        get :success

        fake_request = Request.new(:get, request.url)
        fake_request.on_behalf_of(author)

        request.headers.merge!(fake_request.headers)

        allow(ActivityPub::TagManager.instance).to receive(:uri_to_actor).with(anything) do
          actor
        end
      end

      describe '#signed_request?' do
        it 'returns true' do
          expect(controller.signed_request?).to be true
        end
      end

      describe '#signed_request_account' do
        it 'returns nil' do
          expect(controller.signed_request_account).to be_nil
        end
      end

      describe '#signed_request_actor' do
        it 'returns the expected actor' do
          expect(controller.signed_request_actor).to eq actor
        end
      end
    end

    context 'with request with unparseable Date header' do
      before do
        get :success

        fake_request = Request.new(:get, request.url)
        fake_request.add_headers({ 'Date' => 'wrong date' })
        fake_request.on_behalf_of(author)

        request.headers.merge!(fake_request.headers)
      end

      describe '#signed_request?' do
        it 'returns true' do
          expect(controller.signed_request?).to be true
        end
      end

      describe '#signed_request_account' do
        it 'returns nil' do
          expect(controller.signed_request_account).to be_nil
        end
      end

      describe '#signature_verification_failure_reason' do
        it 'contains an error description' do
          controller.signed_request_account
          expect(controller.signature_verification_failure_reason[:error]).to eq 'Invalid Date header: not RFC 2616 compliant date: "wrong date"'
        end
      end
    end

    context 'with request older than a day' do
      before do
        get :success

        fake_request = Request.new(:get, request.url)
        fake_request.add_headers({ 'Date' => 2.days.ago.utc.httpdate })
        fake_request.on_behalf_of(author)

        request.headers.merge!(fake_request.headers)
      end

      describe '#signed_request?' do
        it 'returns true' do
          expect(controller.signed_request?).to be true
        end
      end

      describe '#signed_request_account' do
        it 'returns nil' do
          expect(controller.signed_request_account).to be_nil
        end
      end

      describe '#signature_verification_failure_reason' do
        it 'contains an error description' do
          controller.signed_request_account
          expect(controller.signature_verification_failure_reason[:error]).to eq 'Signed request date outside acceptable time window'
        end
      end
    end

    context 'with inaccessible key' do
      before do
        get :success

        author = Fabricate(:account, domain: 'localhost:5000', uri: 'http://localhost:5000/actor')
        fake_request = Request.new(:get, request.url)
        fake_request.on_behalf_of(author)
        author.destroy

        request.headers.merge!(fake_request.headers)

        stub_request(:get, 'http://localhost:5000/actor#main-key').to_raise(Mastodon::HostValidationError)
      end

      describe '#signed_request?' do
        it 'returns true' do
          expect(controller.signed_request?).to be true
        end
      end

      describe '#signed_request_account' do
        it 'returns nil' do
          expect(controller.signed_request_account).to be_nil
        end
      end
    end

    context 'with body' do
      before do
        allow(controller).to receive(:actor_refresh_key!).and_return(author)
        post :success, body: 'Hello world'

        fake_request = Request.new(:post, request.url, body: 'Hello world')
        fake_request.on_behalf_of(author)

        request.headers.merge!(fake_request.headers)
      end

      describe '#signed_request?' do
        it 'returns true' do
          expect(controller.signed_request?).to be true
        end
      end

      describe '#signed_request_account' do
        it 'returns an account' do
          expect(controller.signed_request_account).to eq author
        end
      end

      context 'when path does not match' do
        before do
          request.path = '/alternative-path'
        end

        describe '#signed_request_account' do
          it 'returns nil' do
            expect(controller.signed_request_account).to be_nil
          end
        end

        describe '#signature_verification_failure_reason' do
          it 'contains an error description' do
            controller.signed_request_account
            expect(controller.signature_verification_failure_reason[:error]).to include('using rsa-sha256 (RSASSA-PKCS1-v1_5 with SHA-256)')
            expect(controller.signature_verification_failure_reason[:signed_string]).to include("(request-target): post /alternative-path\n")
          end
        end
      end

      context 'when method does not match' do
        before do
          get :success
        end

        describe '#signed_request_account' do
          it 'returns nil' do
            expect(controller.signed_request_account).to be_nil
          end
        end
      end

      context 'when body has been tampered' do
        before do
          post :success, body: 'doo doo doo'
        end

        describe '#signed_request_account' do
          it 'returns nil when body has been tampered' do
            expect(controller.signed_request_account).to be_nil
          end
        end
      end
    end
  end

  context 'when a signature is required' do
    before do
      get :signature_required
    end

    context 'without signature header' do
      it 'returns HTTP 401' do
        expect(response).to have_http_status(401)
      end

      it 'returns an error' do
        expect(Oj.load(response.body)['error']).to eq 'Request not signed'
      end
    end
  end
end