Fix min_id and max_id causing error in search API (#32857)

This commit is contained in:
Eugen Rochko 2024-11-18 10:37:01 +01:00 committed by GitHub
parent 9a46329fbd
commit 911d7dbbf6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 14 deletions

View file

@ -144,6 +144,8 @@ class SearchQueryTransformer < Parslet::Transform
end
class PrefixClause
EPOCH_RE = /\A\d+\z/
attr_reader :operator, :prefix, :term
def initialize(prefix, operator, term, options = {})
@ -168,15 +170,15 @@ class SearchQueryTransformer < Parslet::Transform
when 'before'
@filter = :created_at
@type = :range
@term = { lt: TermValidator.validate_date!(term), time_zone: @options[:current_account]&.user_time_zone.presence || 'UTC' }
@term = { lt: date_from_term(term), time_zone: @options[:current_account]&.user_time_zone.presence || 'UTC' }
when 'after'
@filter = :created_at
@type = :range
@term = { gt: TermValidator.validate_date!(term), time_zone: @options[:current_account]&.user_time_zone.presence || 'UTC' }
@term = { gt: date_from_term(term), time_zone: @options[:current_account]&.user_time_zone.presence || 'UTC' }
when 'during'
@filter = :created_at
@type = :range
@term = { gte: TermValidator.validate_date!(term), lte: TermValidator.validate_date!(term), time_zone: @options[:current_account]&.user_time_zone.presence || 'UTC' }
@term = { gte: date_from_term(term), lte: date_from_term(term), time_zone: @options[:current_account]&.user_time_zone.presence || 'UTC' }
when 'in'
@operator = :flag
@term = term
@ -222,16 +224,10 @@ class SearchQueryTransformer < Parslet::Transform
term
end
end
class TermValidator
STRICT_DATE_REGEX = /\A\d{4}-\d{2}-\d{2}\z/ # yyyy-MM-dd
EPOCH_MILLIS_REGEX = /\A\d{1,19}\z/
def self.validate_date!(value)
return value if value.match?(STRICT_DATE_REGEX) || value.match?(EPOCH_MILLIS_REGEX)
raise Mastodon::FilterValidationError, "Invalid date #{value}"
def date_from_term(term)
DateTime.iso8601(term) unless term.match?(EPOCH_RE) # This will raise Date::Error if the date is invalid
term
end
end

View file

@ -8,7 +8,6 @@ module Mastodon
class LengthValidationError < ValidationError; end
class DimensionsValidationError < ValidationError; end
class StreamValidationError < ValidationError; end
class FilterValidationError < ValidationError; end
class RaceConditionError < Error; end
class RateLimitExceededError < Error; end
class SyntaxError < Error; end

View file

@ -34,7 +34,7 @@ RSpec.describe SearchQueryTransformer do
let(:query) { "#{operator}:\"abc\"" }
it 'raises an exception' do
expect { subject }.to raise_error(Mastodon::FilterValidationError, 'Invalid date abc')
expect { subject }.to raise_error(Date::Error)
end
end
end