Fix bug where it was possible to submit blank text in various text fields. Caused by `String#blank?` not considering certain Unicode characters as blank. `blank?` is defined as `match?(/\A[[:space:]]*\z/)`, where `[[:space:]]` matches ASCII spaces (space, tab, newline, etc) and Unicode characters in the Space category ([1]). However, there are other space-like characters not in the Space category. This includes U+200B (Zero-Width Space), and many more. It turns out the "Default ignorable code points" [2][3] are what we're after. These are the set of 400 or so formatting and control characters that are invisible when displayed. Note that there are other control characters that aren't invisible when rendered, instead they're shown with a placeholder glyph. These include the ASCII C0 and C1 control codes [4], certain Unicode control characters [5], and unassigned, reserved, and private use codepoints. There is one outlier: the Braille pattern blank (U+2800) [6]. This character is visually blank, but is not considered to be a space or an ignorable code point. [1]: https://codepoints.net/search?gc[]=Z [2]: https://codepoints.net/search?DI=1 [3]: https://www.unicode.org/review/pr-5.html [4]: https://codepoints.net/search?gc[]=Cc [5]: https://codepoints.net/search?gc[]=Cf [6]: https://codepoints.net/U+2800 [7]: https://en.wikipedia.org/wiki/Whitespace_character [8]: https://character.construction/blanks [9]: https://invisible-characters.com
102 lines
2.8 KiB
Ruby
102 lines
2.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Ban < ApplicationRecord
|
|
attribute :duration, :interval
|
|
|
|
after_create :create_feedback
|
|
after_create :create_dmail
|
|
after_create :update_user_on_create
|
|
after_create :create_ban_mod_action
|
|
after_destroy :update_user_on_destroy
|
|
after_destroy :create_unban_mod_action
|
|
belongs_to :user
|
|
belongs_to :banner, :class_name => "User"
|
|
|
|
validates :duration, presence: true
|
|
validates :duration, inclusion: { in: [1.day, 3.days, 1.week, 1.month, 3.months, 6.months, 1.year, 100.years], message: "%{value} is not a valid ban duration" }, if: :duration_changed?
|
|
validates :reason, visible_string: true
|
|
validate :user, :validate_user_is_bannable, on: :create
|
|
|
|
scope :unexpired, -> { where("bans.created_at + bans.duration > ?", Time.zone.now) }
|
|
scope :expired, -> { where("bans.created_at + bans.duration <= ?", Time.zone.now) }
|
|
scope :active, -> { unexpired }
|
|
|
|
def self.search(params, current_user)
|
|
q = search_attributes(params, [:id, :created_at, :updated_at, :duration, :reason, :user, :banner], current_user: current_user)
|
|
|
|
q = q.expired if params[:expired].to_s.truthy?
|
|
q = q.unexpired if params[:expired].to_s.falsy?
|
|
|
|
case params[:order]
|
|
when "expires_at_desc"
|
|
q = q.order(Arel.sql("bans.created_at + bans.duration DESC"))
|
|
else
|
|
q = q.apply_default_order(params)
|
|
end
|
|
|
|
q
|
|
end
|
|
|
|
def self.prune!
|
|
expired.includes(:user).find_each do |ban|
|
|
ban.user.unban! if ban.user.ban_expired?
|
|
end
|
|
end
|
|
|
|
def validate_user_is_bannable
|
|
errors.add(:user, "is already banned") if user&.is_banned?
|
|
end
|
|
|
|
def update_user_on_create
|
|
user.update!(is_banned: true)
|
|
end
|
|
|
|
def update_user_on_destroy
|
|
user.update!(is_banned: false)
|
|
end
|
|
|
|
def user_name
|
|
user ? user.name : nil
|
|
end
|
|
|
|
def user_name=(username)
|
|
self.user = User.find_by_name(username)
|
|
end
|
|
|
|
def expires_at
|
|
created_at + duration
|
|
end
|
|
|
|
def humanized_duration
|
|
ApplicationController.helpers.humanized_duration(duration)
|
|
end
|
|
|
|
def forever?
|
|
duration.present? && duration >= 100.years
|
|
end
|
|
|
|
def expired?
|
|
persisted? && expires_at < Time.zone.now
|
|
end
|
|
|
|
def create_feedback
|
|
user.feedback.create!(creator: banner, category: "negative", body: "Banned #{humanized_duration}: #{reason}", disable_dmail_notification: true)
|
|
end
|
|
|
|
def create_dmail
|
|
Dmail.create_automated(to: user, title: "You have been banned", body: "You have been banned #{forever? ? "forever" : "for #{humanized_duration}"}: #{reason}")
|
|
end
|
|
|
|
def create_ban_mod_action
|
|
ModAction.log(%{banned <@#{user_name}> #{humanized_duration}: #{reason}}, :user_ban, subject: user, user: banner)
|
|
end
|
|
|
|
def create_unban_mod_action
|
|
ModAction.log(%{unbanned <@#{user_name}>}, :user_unban, subject: user, user: CurrentUser.user)
|
|
end
|
|
|
|
def self.available_includes
|
|
[:user, :banner]
|
|
end
|
|
end
|