Fix error when passing unknown filter param in REST API (#20626)

Fix #19156
This commit is contained in:
Eugen Rochko 2022-11-14 08:06:06 +01:00 committed by GitHub
parent 5c826c408d
commit b31afc6294
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 31 additions and 28 deletions

View file

@ -57,7 +57,7 @@ class Api::BaseController < ApplicationController
render json: { error: I18n.t('errors.429') }, status: 429 render json: { error: I18n.t('errors.429') }, status: 429
end end
rescue_from ActionController::ParameterMissing do |e| rescue_from ActionController::ParameterMissing, Mastodon::InvalidParameterError do |e|
render json: { error: e.to_s }, status: 400 render json: { error: e.to_s }, status: 400
end end

View file

@ -57,7 +57,7 @@ class AccountFilter
when 'order' when 'order'
order_scope(value) order_scope(value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
@ -68,7 +68,7 @@ class AccountFilter
when 'remote' when 'remote'
Account.remote Account.remote
else else
raise "Unknown origin: #{value}" raise Mastodon::InvalidParameterError, "Unknown origin: #{value}"
end end
end end
@ -84,8 +84,10 @@ class AccountFilter
accounts_with_users.merge(User.disabled) accounts_with_users.merge(User.disabled)
when 'silenced' when 'silenced'
Account.silenced Account.silenced
when 'sensitized'
Account.sensitized
else else
raise "Unknown status: #{value}" raise Mastodon::InvalidParameterError, "Unknown status: #{value}"
end end
end end
@ -96,7 +98,7 @@ class AccountFilter
when 'recent' when 'recent'
Account.recent Account.recent
else else
raise "Unknown order: #{value}" raise Mastodon::InvalidParameterError, "Unknown order: #{value}"
end end
end end

View file

@ -95,7 +95,7 @@ class Admin::ActionLogFilter
account = Account.find_or_initialize_by(id: value) account = Account.find_or_initialize_by(id: value)
Admin::ActionLog.where(target: [account, account.user].compact) Admin::ActionLog.where(target: [account, account.user].compact)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
end end

View file

@ -30,7 +30,7 @@ class Admin::AppealFilter
when 'status' when 'status'
status_scope(value) status_scope(value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
@ -43,7 +43,7 @@ class Admin::AppealFilter
when 'pending' when 'pending'
Appeal.pending Appeal.pending
else else
raise "Unknown status: #{value}" raise Mastodon::InvalidParameterError, "Unknown status: #{value}"
end end
end end
end end

View file

@ -32,7 +32,7 @@ class Admin::StatusFilter
when 'media' when 'media'
Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id).reorder('statuses.id desc') Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id).reorder('statuses.id desc')
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
end end

View file

@ -33,7 +33,7 @@ class AnnouncementFilter
when 'unpublished' when 'unpublished'
Announcement.unpublished Announcement.unpublished
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
end end

View file

@ -39,7 +39,7 @@ class CustomEmojiFilter
when 'shortcode' when 'shortcode'
CustomEmoji.search(value.strip) CustomEmoji.search(value.strip)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
end end

View file

@ -36,7 +36,7 @@ class InstanceFilter
when 'availability' when 'availability'
availability_scope(value) availability_scope(value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
@ -47,7 +47,7 @@ class InstanceFilter
when 'unavailable' when 'unavailable'
Instance.joins(:unavailable_domain) Instance.joins(:unavailable_domain)
else else
raise "Unknown availability: #{value}" raise Mastodon::InvalidParameterError, "Unknown availability: #{value}"
end end
end end
end end

View file

@ -31,7 +31,7 @@ class InviteFilter
when 'expired' when 'expired'
Invite.expired Invite.expired
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
end end

View file

@ -53,7 +53,7 @@ class RelationshipFilter
when 'activity' when 'activity'
activity_scope(value) activity_scope(value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
@ -68,7 +68,7 @@ class RelationshipFilter
when 'invited' when 'invited'
Account.joins(user: :invite).merge(Invite.where(user: account.user)).eager_load(:account_stat).reorder(nil) Account.joins(user: :invite).merge(Invite.where(user: account.user)).eager_load(:account_stat).reorder(nil)
else else
raise "Unknown relationship: #{value}" raise Mastodon::InvalidParameterError, "Unknown relationship: #{value}"
end end
end end
@ -83,7 +83,7 @@ class RelationshipFilter
when 'remote' when 'remote'
Account.remote Account.remote
else else
raise "Unknown location: #{value}" raise Mastodon::InvalidParameterError, "Unknown location: #{value}"
end end
end end
@ -94,7 +94,7 @@ class RelationshipFilter
when 'primary' when 'primary'
Account.where(moved_to_account_id: nil) Account.where(moved_to_account_id: nil)
else else
raise "Unknown status: #{value}" raise Mastodon::InvalidParameterError, "Unknown status: #{value}"
end end
end end
@ -105,7 +105,7 @@ class RelationshipFilter
when 'recent' when 'recent'
params[:relationship] == 'invited' ? Account.recent : Follow.recent params[:relationship] == 'invited' ? Account.recent : Follow.recent
else else
raise "Unknown order: #{value}" raise Mastodon::InvalidParameterError, "Unknown order: #{value}"
end end
end end
@ -114,7 +114,7 @@ class RelationshipFilter
when 'dormant' when 'dormant'
AccountStat.where(last_status_at: nil).or(AccountStat.where(AccountStat.arel_table[:last_status_at].lt(1.month.ago))) AccountStat.where(last_status_at: nil).or(AccountStat.where(AccountStat.arel_table[:last_status_at].lt(1.month.ago)))
else else
raise "Unknown activity: #{value}" raise Mastodon::InvalidParameterError, "Unknown activity: #{value}"
end end
end end
end end

View file

@ -38,7 +38,7 @@ class ReportFilter
when :target_origin when :target_origin
target_origin_scope(value) target_origin_scope(value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
@ -49,7 +49,7 @@ class ReportFilter
when :remote when :remote
Report.where(target_account: Account.remote) Report.where(target_account: Account.remote)
else else
raise "Unknown value: #{value}" raise Mastodon::InvalidParameterError, "Unknown value: #{value}"
end end
end end
end end

View file

@ -40,7 +40,7 @@ class Trends::PreviewCardFilter
when 'locale' when 'locale'
PreviewCardTrend.where(language: value) PreviewCardTrend.where(language: value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end

View file

@ -30,7 +30,7 @@ class Trends::PreviewCardProviderFilter
when 'status' when 'status'
status_scope(value) status_scope(value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end
@ -43,7 +43,7 @@ class Trends::PreviewCardProviderFilter
when 'pending_review' when 'pending_review'
PreviewCardProvider.pending_review PreviewCardProvider.pending_review
else else
raise "Unknown status: #{value}" raise Mastodon::InvalidParameterError, "Unknown status: #{value}"
end end
end end
end end

View file

@ -40,7 +40,7 @@ class Trends::StatusFilter
when 'locale' when 'locale'
StatusTrend.where(language: value) StatusTrend.where(language: value)
else else
raise "Unknown filter: #{key}" raise Mastodon::InvalidParameterError, "Unknown filter: #{key}"
end end
end end

View file

@ -11,6 +11,7 @@ module Mastodon
class RaceConditionError < Error; end class RaceConditionError < Error; end
class RateLimitExceededError < Error; end class RateLimitExceededError < Error; end
class SyntaxError < Error; end class SyntaxError < Error; end
class InvalidParameterError < Error; end
class UnexpectedResponseError < Error class UnexpectedResponseError < Error
attr_reader :response attr_reader :response

View file

@ -50,10 +50,10 @@ RSpec.describe CustomEmojiFilter do
context 'else' do context 'else' do
let(:params) { { else: 'else' } } let(:params) { { else: 'else' } }
it 'raises RuntimeError' do it 'raises Mastodon::InvalidParameterError' do
expect do expect do
subject subject
end.to raise_error(RuntimeError, /Unknown filter: else/) end.to raise_error(Mastodon::InvalidParameterError, /Unknown filter: else/)
end end
end end
end end