2017-07-14 21:41:49 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe ApplicationController, type: :controller do
|
2022-09-21 23:45:57 +03:00
|
|
|
class WrappedActor
|
|
|
|
attr_reader :wrapped_account
|
|
|
|
|
|
|
|
def initialize(wrapped_account)
|
|
|
|
@wrapped_account = wrapped_account
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :uri, :keypair, to: :wrapped_account
|
|
|
|
end
|
|
|
|
|
2017-07-14 21:41:49 +03:00
|
|
|
controller do
|
|
|
|
include SignatureVerification
|
|
|
|
|
|
|
|
def success
|
|
|
|
head 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def alternative_success
|
|
|
|
head 200
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2017-08-10 00:54:14 +03:00
|
|
|
routes.draw { match via: [:get, :post], 'success' => 'anonymous#success' }
|
2017-07-14 21:41:49 +03:00
|
|
|
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
|
2019-07-11 15:49:55 +03:00
|
|
|
let!(:author) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/actor') }
|
2017-07-14 21:41:49 +03:00
|
|
|
|
2017-08-10 00:54:14 +03:00
|
|
|
context 'without body' do
|
|
|
|
before do
|
|
|
|
get :success
|
2017-07-14 21:41:49 +03:00
|
|
|
|
2017-08-10 00:54:14 +03:00
|
|
|
fake_request = Request.new(:get, request.url)
|
|
|
|
fake_request.on_behalf_of(author)
|
2017-07-14 21:41:49 +03:00
|
|
|
|
2017-08-10 00:54:14 +03:00
|
|
|
request.headers.merge!(fake_request.headers)
|
|
|
|
end
|
2017-07-14 21:41:49 +03:00
|
|
|
|
2017-08-10 00:54:14 +03:00
|
|
|
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
|
2017-07-14 21:41:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-21 23:45:57 +03:00
|
|
|
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
|
|
|
|
|
2018-10-12 01:15:55 +03:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2020-02-03 18:48:23 +02:00
|
|
|
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
|
|
|
|
|
2017-08-10 00:54:14 +03:00
|
|
|
context 'with body' do
|
|
|
|
before do
|
|
|
|
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)
|
2017-07-14 21:41:49 +03:00
|
|
|
end
|
|
|
|
|
2017-08-10 00:54:14 +03:00
|
|
|
describe '#signed_request?' do
|
|
|
|
it 'returns true' do
|
|
|
|
expect(controller.signed_request?).to be true
|
|
|
|
end
|
2017-07-14 21:41:49 +03:00
|
|
|
end
|
|
|
|
|
2017-08-10 00:54:14 +03:00
|
|
|
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
|
|
|
|
get :success
|
|
|
|
expect(controller.signed_request_account).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil when body has been tampered' do
|
2018-08-12 13:25:23 +03:00
|
|
|
post :success, body: 'doo doo doo'
|
2017-08-10 00:54:14 +03:00
|
|
|
expect(controller.signed_request_account).to be_nil
|
|
|
|
end
|
2017-07-14 21:41:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|