2017-07-14 18:41:49 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-07-27 12:27:21 +00:00
|
|
|
describe SignatureVerification do
|
2023-05-03 08:32:30 +00:00
|
|
|
let(:wrapped_actor_class) do
|
|
|
|
Class.new do
|
|
|
|
attr_reader :wrapped_account
|
2022-09-21 20:45:57 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
def initialize(wrapped_account)
|
|
|
|
@wrapped_account = wrapped_account
|
|
|
|
end
|
2022-09-21 20:45:57 +00:00
|
|
|
|
2023-05-03 08:32:30 +00:00
|
|
|
delegate :uri, :keypair, to: :wrapped_account
|
|
|
|
end
|
2022-09-21 20:45:57 +00:00
|
|
|
end
|
|
|
|
|
2023-07-27 12:27:21 +00:00
|
|
|
controller(ApplicationController) do
|
2017-07-14 18:41:49 +00:00
|
|
|
include SignatureVerification
|
|
|
|
|
2023-01-18 15:47:56 +00:00
|
|
|
before_action :require_actor_signature!, only: [:signature_required]
|
|
|
|
|
2017-07-14 18:41:49 +00:00
|
|
|
def success
|
|
|
|
head 200
|
|
|
|
end
|
|
|
|
|
|
|
|
def alternative_success
|
|
|
|
head 200
|
|
|
|
end
|
2023-01-18 15:47:56 +00:00
|
|
|
|
|
|
|
def signature_required
|
|
|
|
head 200
|
|
|
|
end
|
2017-07-14 18:41:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2023-01-18 15:47:56 +00:00
|
|
|
routes.draw do
|
2023-05-04 03:54:26 +00:00
|
|
|
match :via => [:get, :post], 'success' => 'anonymous#success'
|
|
|
|
match :via => [:get, :post], 'signature_required' => 'anonymous#signature_required'
|
2023-01-18 15:47:56 +00:00
|
|
|
end
|
2017-07-14 18:41:49 +00: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 12:49:55 +00:00
|
|
|
let!(:author) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/actor') }
|
2017-07-14 18:41:49 +00:00
|
|
|
|
2017-08-09 21:54:14 +00:00
|
|
|
context 'without body' do
|
|
|
|
before do
|
|
|
|
get :success
|
2017-07-14 18:41:49 +00:00
|
|
|
|
2017-08-09 21:54:14 +00:00
|
|
|
fake_request = Request.new(:get, request.url)
|
|
|
|
fake_request.on_behalf_of(author)
|
2017-07-14 18:41:49 +00:00
|
|
|
|
2017-08-09 21:54:14 +00:00
|
|
|
request.headers.merge!(fake_request.headers)
|
|
|
|
end
|
2017-07-14 18:41:49 +00:00
|
|
|
|
2017-08-09 21:54:14 +00: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 18:41:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-21 20:45:57 +00:00
|
|
|
context 'with a valid actor that is not an Account' do
|
2023-05-03 08:32:30 +00:00
|
|
|
let(:actor) { wrapped_actor_class.new(author) }
|
2022-09-21 20:45:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-19 15:13:29 +00:00
|
|
|
context 'with request with unparsable Date header' do
|
2023-01-18 15:47:56 +00:00
|
|
|
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
|
|
|
|
|
2018-10-11 22:15:55 +00: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
|
2023-01-18 15:47:56 +00:00
|
|
|
|
|
|
|
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
|
2018-10-11 22:15:55 +00:00
|
|
|
end
|
|
|
|
|
2020-02-03 16:48:23 +00: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-09 21:54:14 +00:00
|
|
|
context 'with body' do
|
|
|
|
before do
|
2023-01-18 15:47:56 +00:00
|
|
|
allow(controller).to receive(:actor_refresh_key!).and_return(author)
|
2017-08-09 21:54:14 +00:00
|
|
|
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 18:41:49 +00:00
|
|
|
end
|
|
|
|
|
2017-08-09 21:54:14 +00:00
|
|
|
describe '#signed_request?' do
|
|
|
|
it 'returns true' do
|
|
|
|
expect(controller.signed_request?).to be true
|
|
|
|
end
|
2017-07-14 18:41:49 +00:00
|
|
|
end
|
|
|
|
|
2017-08-09 21:54:14 +00:00
|
|
|
describe '#signed_request_account' do
|
|
|
|
it 'returns an account' do
|
|
|
|
expect(controller.signed_request_account).to eq author
|
|
|
|
end
|
2023-01-18 15:47:56 +00:00
|
|
|
end
|
2017-08-09 21:54:14 +00:00
|
|
|
|
2023-01-18 15:47:56 +00:00
|
|
|
context 'when path does not match' do
|
|
|
|
before do
|
2017-08-09 21:54:14 +00:00
|
|
|
request.path = '/alternative-path'
|
|
|
|
end
|
|
|
|
|
2023-01-18 15:47:56 +00:00
|
|
|
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
|
2017-08-09 21:54:14 +00:00
|
|
|
get :success
|
|
|
|
end
|
|
|
|
|
2023-01-18 15:47:56 +00:00
|
|
|
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
|
2018-08-12 10:25:23 +00:00
|
|
|
post :success, body: 'doo doo doo'
|
2017-08-09 21:54:14 +00:00
|
|
|
end
|
2023-01-18 15:47:56 +00:00
|
|
|
|
|
|
|
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'
|
2017-07-14 18:41:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|