Skip to content

Commit 6ddba1a

Browse files
authored
Merge pull request #594 from imas/feature/deliverability-probe-rejection
API経由の配信性プローブを名乗るスパム登録にダミーのトークンを返す(imastodon)
2 parents 9838aea + 2151511 commit 6ddba1a

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎app/controllers/api/v1/accounts_controller.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class Api::V1::AccountsController < Api::BaseController
44
include DeprecationConcern
55
include RegistrationHelper
6+
include DeliverabilityProbeRejectionConcern
67

78
before_action -> { authorize_if_got_token! :read, :'read:accounts' }, except: [:create, :follow, :unfollow, :remove_from_followers, :block, :unblock, :mute, :unmute]
89
before_action -> { doorkeeper_authorize! :follow, :write, :'write:follows' }, only: [:follow, :unfollow, :remove_from_followers]
@@ -26,6 +27,9 @@ class Api::V1::AccountsController < Api::BaseController
2627

2728
deprecate_api '2026-10-01', only: :create
2829

30+
# 本物の登録と同じヘッダーにするため、deprecate_api の後で弾く
31+
before_action :reject_deliverability_probe, only: [:create]
32+
2933
def index
3034
render json: @accounts, each_serializer: REST::AccountSerializer
3135
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module DeliverabilityProbeRejectionConcern
4+
extend ActiveSupport::Concern
5+
6+
PROBE_USERNAME_REGEX = /\Abp[0-9a-f]{16}\z/
7+
PROBE_REASON = 'Automated protocol deliverability probe'
8+
9+
private
10+
11+
def reject_deliverability_probe
12+
return unless PROBE_USERNAME_REGEX.match?(params[:username].to_s) && params[:reason] == PROBE_REASON
13+
14+
Rails.logger.info("Rejected deliverability probe sign-up: username=#{params[:username]} ip=#{request.remote_ip} app=#{doorkeeper_token.application.name}")
15+
16+
response = Doorkeeper::OAuth::TokenResponse.new(dummy_access_token)
17+
18+
headers.merge!(response.headers)
19+
render json: response.body, status: response.status
20+
end
21+
22+
# AppSignUpService と同じ属性で組み立て、保存はしない。
23+
# validate を通すと、保存時と同じ手順でトークン文字列が生成される
24+
def dummy_access_token
25+
Doorkeeper::AccessToken.new(
26+
application: doorkeeper_token.application,
27+
scopes: doorkeeper_token.application.scopes,
28+
expires_in: Doorkeeper.configuration.access_token_expires_in,
29+
use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?,
30+
created_at: Time.now.utc
31+
).tap(&:validate)
32+
end
33+
end
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Deliverability probe sign-up (imastodon)', type: :request do
6+
let(:probe_username) { 'bp8d064b8ce361cf9f' }
7+
let(:probe_reason) { 'Automated protocol deliverability probe' }
8+
9+
before { Setting.registrations_mode = 'approved' }
10+
11+
describe 'POST /api/v1/accounts' do
12+
subject do
13+
post '/api/v1/accounts', headers: { 'Authorization' => "Bearer #{token.token}" }, params: { username: username, reason: reason, password: '12345678', email: 'probe@example.com', agreement: 'true' }
14+
end
15+
16+
let(:token) { Fabricate(:client_credentials_token, application: Fabricate(:application, scopes: 'read write'), scopes: 'read write') }
17+
18+
context 'when the username is bp + 16 hex digits and the reason is the probe text' do
19+
let(:username) { probe_username }
20+
let(:reason) { probe_reason }
21+
22+
it 'returns 200 without creating an account' do
23+
expect { subject }
24+
.to not_change(User, :count)
25+
.and not_change(Account, :count)
26+
27+
expect(response).to have_http_status(200)
28+
end
29+
30+
it 'returns a dummy token response shaped like a real sign-up' do
31+
post '/api/v1/accounts', headers: { 'Authorization' => "Bearer #{token.token}" }, params: { username: 'fuyuko', password: '12345678', email: 'fuyuko@example.com', agreement: 'true' }
32+
real_body = response.parsed_body
33+
real_headers = response.headers.slice('Cache-Control', 'Content-Type', 'Deprecation', 'Pragma')
34+
35+
expect { subject }
36+
.to not_change(Doorkeeper::AccessToken, :count)
37+
38+
expect(response.parsed_body.keys).to match_array(real_body.keys)
39+
expect(response.parsed_body)
40+
.to include(
41+
'access_token' => be_a(String).and(have_attributes(length: real_body['access_token'].length)),
42+
'token_type' => real_body['token_type'],
43+
'scope' => real_body['scope'],
44+
'created_at' => be_within(5).of(Time.now.to_i)
45+
)
46+
expect(response.parsed_body['access_token']).to_not eq(real_body['access_token'])
47+
expect(response.headers.slice('Cache-Control', 'Content-Type', 'Deprecation', 'Pragma')).to eq(real_headers)
48+
end
49+
50+
it 'logs the username, IP and application of the rejected probe' do
51+
allow(Rails.logger).to receive(:info)
52+
53+
subject
54+
55+
expect(Rails.logger)
56+
.to have_received(:info)
57+
.with("Rejected deliverability probe sign-up: username=#{probe_username} ip=127.0.0.1 app=#{token.application.name}")
58+
end
59+
end
60+
61+
context 'when only the username matches' do
62+
let(:username) { probe_username }
63+
let(:reason) { 'アイマスが好きです' }
64+
65+
it 'creates an account with the probe-like username' do
66+
expect { subject }
67+
.to change(User, :count).by(1)
68+
69+
expect(Account.find_local(probe_username)).to be_present
70+
end
71+
end
72+
73+
context 'when only the reason matches' do
74+
let(:username) { 'fuyuko' }
75+
let(:reason) { probe_reason }
76+
77+
it 'creates an account with the probe reason' do
78+
expect { subject }
79+
.to change(User, :count).by(1)
80+
81+
expect(Account.find_local('fuyuko')).to be_present
82+
end
83+
end
84+
85+
context 'when the hex part of the username is not 16 digits' do
86+
let(:username) { 'bp8d064b8ce361cf9f0' }
87+
let(:reason) { probe_reason }
88+
89+
it 'creates an account with the 17-digit username' do
90+
expect { subject }
91+
.to change(User, :count).by(1)
92+
93+
expect(Account.find_local('bp8d064b8ce361cf9f0')).to be_present
94+
end
95+
end
96+
end
97+
end

0 commit comments

Comments
 (0)