From c66250abf17b4aad1abc1500c14bf9862c078226 Mon Sep 17 00:00:00 2001 From: Nick Schonning <nschonni@gmail.com> Date: Tue, 6 Jun 2023 08:50:51 -0400 Subject: [PATCH] Autofix Rubocop Regex Style rules (#23690) Co-authored-by: Claire <claire.github-309c@sitedethib.com> --- .rubocop_todo.yml | 46 ---------------------- app/lib/link_details_extractor.rb | 14 +++---- app/lib/plain_text_formatter.rb | 2 +- app/lib/tag_manager.rb | 6 +-- app/lib/text_formatter.rb | 2 +- app/lib/webfinger_resource.rb | 2 +- app/models/account.rb | 6 +-- app/models/domain_allow.rb | 2 +- app/models/domain_block.rb | 2 +- app/models/site_upload.rb | 2 +- app/models/tag.rb | 2 +- app/services/backup_service.rb | 4 +- app/services/fetch_link_card_service.rb | 2 +- app/services/fetch_oembed_service.rb | 2 +- app/services/search_service.rb | 2 +- config/initializers/rack_attack.rb | 6 +-- config/initializers/twitter_regex.rb | 16 ++++---- config/routes.rb | 6 +-- lib/mastodon/premailer_webpack_strategy.rb | 2 +- lib/paperclip/color_extractor.rb | 2 +- lib/tasks/emojis.rake | 2 +- lib/tasks/mastodon.rake | 8 ++-- 22 files changed, 46 insertions(+), 92 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1a16472bdb..0d0fbfac91 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1412,52 +1412,6 @@ Style/RedundantFetchBlock: - 'config/initializers/paperclip.rb' - 'config/puma.rb' -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantRegexpCharacterClass: - Exclude: - - 'app/lib/link_details_extractor.rb' - - 'app/lib/tag_manager.rb' - - 'app/models/domain_allow.rb' - - 'app/models/domain_block.rb' - - 'app/services/fetch_oembed_service.rb' - - 'config/initializers/rack_attack.rb' - - 'lib/tasks/emojis.rake' - - 'lib/tasks/mastodon.rake' - -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantRegexpEscape: - Exclude: - - 'app/lib/webfinger_resource.rb' - - 'app/models/account.rb' - - 'app/models/tag.rb' - - 'app/services/fetch_link_card_service.rb' - - 'config/initializers/twitter_regex.rb' - - 'lib/paperclip/color_extractor.rb' - - 'lib/tasks/mastodon.rake' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, AllowInnerSlashes. -# SupportedStyles: slashes, percent_r, mixed -Style/RegexpLiteral: - Exclude: - - 'app/lib/link_details_extractor.rb' - - 'app/lib/plain_text_formatter.rb' - - 'app/lib/tag_manager.rb' - - 'app/lib/text_formatter.rb' - - 'app/models/account.rb' - - 'app/models/domain_allow.rb' - - 'app/models/domain_block.rb' - - 'app/models/site_upload.rb' - - 'app/models/tag.rb' - - 'app/services/backup_service.rb' - - 'app/services/fetch_oembed_service.rb' - - 'app/services/search_service.rb' - - 'config/initializers/rack_attack.rb' - - 'config/initializers/twitter_regex.rb' - - 'config/routes.rb' - - 'lib/mastodon/premailer_webpack_strategy.rb' - - 'lib/tasks/mastodon.rake' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength. # AllowedMethods: present?, blank?, presence, try, try! diff --git a/app/lib/link_details_extractor.rb b/app/lib/link_details_extractor.rb index dfed69285f..f0aeec0b3e 100644 --- a/app/lib/link_details_extractor.rb +++ b/app/lib/link_details_extractor.rb @@ -7,15 +7,15 @@ class LinkDetailsExtractor # Some publications wrap their JSON-LD data in their <script> tags # in commented-out CDATA blocks, they need to be removed before # attempting to parse JSON - CDATA_JUNK_PATTERN = %r{^[\s]*( - (/\*[\s]*<!\[CDATA\[[\s]*\*/) # Block comment style opening + CDATA_JUNK_PATTERN = %r{^\s*( + (/\*\s*<!\[CDATA\[\s*\*/) # Block comment style opening | - (//[\s]*<!\[CDATA\[) # Single-line comment style opening + (//\s*<!\[CDATA\[) # Single-line comment style opening | - (/\*[\s]*\]\]>[\s]*\*/) # Block comment style closing + (/\*\s*\]\]>\s*\*/) # Block comment style closing | - (//[\s]*\]\]>) # Single-line comment style closing - )[\s]*$}x + (//\s*\]\]>) # Single-line comment style closing + )\s*$}x class StructuredData SUPPORTED_TYPES = %w( @@ -204,7 +204,7 @@ class LinkDetailsExtractor def host_to_url(str) return if str.blank? - str.start_with?(/https?:\/\//) ? str : "http://#{str}" + str.start_with?(%r{https?://}) ? str : "http://#{str}" end def valid_url_or_nil(str, same_origin_only: false) diff --git a/app/lib/plain_text_formatter.rb b/app/lib/plain_text_formatter.rb index 6121660180..8eac730bec 100644 --- a/app/lib/plain_text_formatter.rb +++ b/app/lib/plain_text_formatter.rb @@ -3,7 +3,7 @@ class PlainTextFormatter include ActionView::Helpers::TextHelper - NEWLINE_TAGS_RE = /(<br \/>|<br>|<\/p>)+/ + NEWLINE_TAGS_RE = %r{(<br />|<br>|</p>)+} attr_reader :text, :local diff --git a/app/lib/tag_manager.rb b/app/lib/tag_manager.rb index 7fbf4437da..ea096855f0 100644 --- a/app/lib/tag_manager.rb +++ b/app/lib/tag_manager.rb @@ -7,18 +7,18 @@ class TagManager include RoutingHelper def web_domain?(domain) - domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.web_domain).zero? + domain.nil? || domain.delete('/').casecmp(Rails.configuration.x.web_domain).zero? end def local_domain?(domain) - domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.local_domain).zero? + domain.nil? || domain.delete('/').casecmp(Rails.configuration.x.local_domain).zero? end def normalize_domain(domain) return if domain.nil? uri = Addressable::URI.new - uri.host = domain.gsub(/[\/]/, '') + uri.host = domain.delete('/') uri.normalized_host end diff --git a/app/lib/text_formatter.rb b/app/lib/text_formatter.rb index b7bc8320ae..243e892891 100644 --- a/app/lib/text_formatter.rb +++ b/app/lib/text_formatter.rb @@ -5,7 +5,7 @@ class TextFormatter include ERB::Util include RoutingHelper - URL_PREFIX_REGEX = /\A(https?:\/\/(www\.)?|xmpp:)/ + URL_PREFIX_REGEX = %r{\A(https?://(www\.)?|xmpp:)} DEFAULT_REL = %w(nofollow noopener noreferrer).freeze diff --git a/app/lib/webfinger_resource.rb b/app/lib/webfinger_resource.rb index 7e1a7196d7..e28723ff82 100644 --- a/app/lib/webfinger_resource.rb +++ b/app/lib/webfinger_resource.rb @@ -13,7 +13,7 @@ class WebfingerResource case resource when /\Ahttps?/i username_from_url - when /\@/ + when /@/ username_from_acct else raise InvalidRequest diff --git a/app/models/account.rb b/app/models/account.rb index 062beb98da..1d24a7ec81 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -62,9 +62,9 @@ class Account < ApplicationRecord trust_level ) - USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.-]+[a-z0-9_]+)?/i - MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[[:word:]\.\-]+[[:word:]]+)?)/i - URL_PREFIX_RE = /\Ahttp(s?):\/\/[^\/]+/ + USERNAME_RE = /[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?/i + MENTION_RE = %r{(?<=^|[^/[:word:]])@((#{USERNAME_RE})(?:@[[:word:].-]+[[:word:]]+)?)}i + URL_PREFIX_RE = %r{\Ahttp(s?)://[^/]+} USERNAME_ONLY_RE = /\A#{USERNAME_RE}\z/i include Attachmentable diff --git a/app/models/domain_allow.rb b/app/models/domain_allow.rb index 9e746b9157..ce9597b4d1 100644 --- a/app/models/domain_allow.rb +++ b/app/models/domain_allow.rb @@ -35,7 +35,7 @@ class DomainAllow < ApplicationRecord def rule_for(domain) return if domain.blank? - uri = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') } + uri = Addressable::URI.new.tap { |u| u.host = domain.delete('/') } find_by(domain: uri.normalized_host) end diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 06d35d8bee..108dfe3d45 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -67,7 +67,7 @@ class DomainBlock < ApplicationRecord def rule_for(domain) return if domain.blank? - uri = Addressable::URI.new.tap { |u| u.host = domain.strip.gsub(/[\/]/, '') } + uri = Addressable::URI.new.tap { |u| u.host = domain.strip.delete('/') } segments = uri.normalized_host.split('.') variants = segments.map.with_index { |_, i| segments[i..-1].join('.') } diff --git a/app/models/site_upload.rb b/app/models/site_upload.rb index e17668110c..2335ffac53 100644 --- a/app/models/site_upload.rb +++ b/app/models/site_upload.rb @@ -43,7 +43,7 @@ class SiteUpload < ApplicationRecord has_attached_file :file, styles: ->(file) { STYLES[file.instance.var.to_sym] }, convert_options: { all: '-coalesce +profile "!icc,*" +set modify-date +set create-date' }, processors: [:lazy_thumbnail, :blurhash_transcoder, :type_corrector] - validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/ + validates_attachment_content_type :file, content_type: %r{\Aimage/.*\z} validates :file, presence: true validates :var, presence: true, uniqueness: true diff --git a/app/models/tag.rb b/app/models/tag.rb index 9fb32cd06e..7668f16cee 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -34,7 +34,7 @@ class Tag < ApplicationRecord HASHTAG_LAST_SEQUENCE = '([[:word:]_]*[[:alpha:]][[:word:]_]*)' HASHTAG_NAME_PAT = "#{HASHTAG_FIRST_SEQUENCE}|#{HASHTAG_LAST_SEQUENCE}" - HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_PAT})/i + HASHTAG_RE = %r{(?:^|[^/)\w])#(#{HASHTAG_NAME_PAT})}i HASHTAG_NAME_RE = /\A(#{HASHTAG_NAME_PAT})\z/i HASHTAG_INVALID_CHARS_RE = /[^[:alnum:]#{HASHTAG_SEPARATORS}]/ diff --git a/app/services/backup_service.rb b/app/services/backup_service.rb index 4cad11fde9..3ef0366c3c 100644 --- a/app/services/backup_service.rb +++ b/app/services/backup_service.rb @@ -77,8 +77,8 @@ class BackupService < BaseService path = m.file&.path next unless path - path = path.gsub(/\A.*\/system\//, '') - path = path.gsub(/\A\/+/, '') + path = path.gsub(%r{\A.*/system/}, '') + path = path.gsub(%r{\A/+}, '') download_to_zip(zipfile, m.file, path) end diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 9c56c862ec..dccd86c1e6 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -7,7 +7,7 @@ class FetchLinkCardService < BaseService URL_PATTERN = %r{ (#{Twitter::TwitterText::Regex[:valid_url_preceding_chars]}) # $1 preceding chars ( # $2 URL - (https?:\/\/) # $3 Protocol (required) + (https?://) # $3 Protocol (required) (#{Twitter::TwitterText::Regex[:valid_domain]}) # $4 Domain(s) (?::(#{Twitter::TwitterText::Regex[:valid_port_number]}))? # $5 Port number (optional) (/#{Twitter::TwitterText::Regex[:valid_url_path]}*)? # $6 URL Path and anchor diff --git a/app/services/fetch_oembed_service.rb b/app/services/fetch_oembed_service.rb index f922df64c4..1ae592238e 100644 --- a/app/services/fetch_oembed_service.rb +++ b/app/services/fetch_oembed_service.rb @@ -2,7 +2,7 @@ class FetchOEmbedService ENDPOINT_CACHE_EXPIRES_IN = 24.hours.freeze - URL_REGEX = /(=(http[s]?(%3A|:)(\/\/|%2F%2F)))([^&]*)/i + URL_REGEX = %r{(=(https?(%3A|:)(//|%2F%2F)))([^&]*)}i attr_reader :url, :options, :format, :endpoint_url diff --git a/app/services/search_service.rb b/app/services/search_service.rb index b1ce5453fb..f475f81536 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -70,7 +70,7 @@ class SearchService < BaseService end def url_query? - @resolve && /\Ahttps?:\/\//.match?(@query) + @resolve && %r{\Ahttps?://}.match?(@query) end def url_resource_results diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index 5cc4e9380a..da0b04af11 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -79,7 +79,7 @@ class Rack::Attack end throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req| - req.authenticated_user_id if req.post? && req.path.match?(/\A\/api\/v\d+\/media\z/i) + req.authenticated_user_id if req.post? && req.path.match?(%r{\A/api/v\d+/media\z}i) end throttle('throttle_media_proxy', limit: 30, period: 10.minutes) do |req| @@ -98,8 +98,8 @@ class Rack::Attack req.throttleable_remote_ip if req.paging_request? && req.unauthenticated? end - API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog\z/ - API_DELETE_STATUS_REGEX = /\A\/api\/v1\/statuses\/[\d]+\z/ + API_DELETE_REBLOG_REGEX = %r{\A/api/v1/statuses/\d+/unreblog\z} + API_DELETE_STATUS_REGEX = %r{\A/api/v1/statuses/\d+\z} throttle('throttle_api_delete', limit: 30, period: 30.minutes) do |req| req.authenticated_user_id if (req.post? && req.path.match?(API_DELETE_REBLOG_REGEX)) || (req.delete? && req.path.match?(API_DELETE_STATUS_REGEX)) diff --git a/config/initializers/twitter_regex.rb b/config/initializers/twitter_regex.rb index e65b05dfde..2e80cabcdb 100644 --- a/config/initializers/twitter_regex.rb +++ b/config/initializers/twitter_regex.rb @@ -6,8 +6,8 @@ module Twitter::TwitterText end class Regex - REGEXEN[:valid_general_url_path_chars] = /[^\p{White_Space}<>\(\)\?]/iou - REGEXEN[:valid_url_path_ending_chars] = /[^\p{White_Space}\(\)\?!\*"'「」<>;:=\,\.\$%\[\]~&\|@]|(?:#{REGEXEN[:valid_url_balanced_parens]})/iou + REGEXEN[:valid_general_url_path_chars] = /[^\p{White_Space}<>()?]/iou + REGEXEN[:valid_url_path_ending_chars] = /[^\p{White_Space}()?!*"'「」<>;:=,.$%\[\]~&|@]|(?:#{REGEXEN[:valid_url_balanced_parens]})/iou REGEXEN[:valid_url_balanced_parens] = / \( (?: @@ -25,20 +25,20 @@ module Twitter::TwitterText \) /iox UCHARS = '\u{A0}-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFEF}\u{10000}-\u{1FFFD}\u{20000}-\u{2FFFD}\u{30000}-\u{3FFFD}\u{40000}-\u{4FFFD}\u{50000}-\u{5FFFD}\u{60000}-\u{6FFFD}\u{70000}-\u{7FFFD}\u{80000}-\u{8FFFD}\u{90000}-\u{9FFFD}\u{A0000}-\u{AFFFD}\u{B0000}-\u{BFFFD}\u{C0000}-\u{CFFFD}\u{D0000}-\u{DFFFD}\u{E1000}-\u{EFFFD}\u{E000}-\u{F8FF}\u{F0000}-\u{FFFFD}\u{100000}-\u{10FFFD}' - REGEXEN[:valid_url_query_chars] = /[a-z0-9!?\*'\(\);:&=\+\$\/%#\[\]\-_\.,~|@\^#{UCHARS}]/iou - REGEXEN[:valid_url_query_ending_chars] = /[a-z0-9_&=#\/\-#{UCHARS}]/iou - REGEXEN[:valid_url_path] = /(?: + REGEXEN[:valid_url_query_chars] = %r{[a-z0-9!?*'();:&=+$/%#\[\]\-_.,~|@\^#{UCHARS}]}iou + REGEXEN[:valid_url_query_ending_chars] = %r{[a-z0-9_&=#/\-#{UCHARS}]}iou + REGEXEN[:valid_url_path] = %r{(?: (?: #{REGEXEN[:valid_general_url_path_chars]}* (?:#{REGEXEN[:valid_url_balanced_parens]} #{REGEXEN[:valid_general_url_path_chars]}*)* #{REGEXEN[:valid_url_path_ending_chars]} - )|(?:#{REGEXEN[:valid_general_url_path_chars]}+\/) - )/iox + )|(?:#{REGEXEN[:valid_general_url_path_chars]}+/) + )}iox REGEXEN[:valid_url] = %r{ ( # $1 total match (#{REGEXEN[:valid_url_preceding_chars]}) # $2 Preceding character ( # $3 URL - ((?:https?|dat|dweb|ipfs|ipns|ssb|gopher|gemini):\/\/)? # $4 Protocol (optional) + ((?:https?|dat|dweb|ipfs|ipns|ssb|gopher|gemini)://)? # $4 Protocol (optional) (#{REGEXEN[:valid_domain]}) # $5 Domain(s) (?::(#{REGEXEN[:valid_port_number]}))? # $6 Port number (optional) (/#{REGEXEN[:valid_url_path]}*)? # $7 URL Path and anchor diff --git a/config/routes.rb b/config/routes.rb index 45b3d90e0b..55e5cf36a0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -115,21 +115,21 @@ Rails.application.routes.draw do get '/:encoded_at(*path)', to: redirect("/@%{path}"), constraints: { encoded_at: /%40/ } - constraints(username: /[^@\/.]+/) do + constraints(username: %r{[^@/.]+}) do get '/@:username', to: 'accounts#show', as: :short_account get '/@:username/with_replies', to: 'accounts#show', as: :short_account_with_replies get '/@:username/media', to: 'accounts#show', as: :short_account_media get '/@:username/tagged/:tag', to: 'accounts#show', as: :short_account_tag end - constraints(account_username: /[^@\/.]+/) do + constraints(account_username: %r{[^@/.]+}) do get '/@:account_username/following', to: 'following_accounts#index' get '/@:account_username/followers', to: 'follower_accounts#index' get '/@:account_username/:id', to: 'statuses#show', as: :short_account_status get '/@:account_username/:id/embed', to: 'statuses#embed', as: :embed_short_account_status end - get '/@:username_with_domain/(*any)', to: 'home#index', constraints: { username_with_domain: /([^\/])+?/ }, format: false + get '/@:username_with_domain/(*any)', to: 'home#index', constraints: { username_with_domain: %r{([^/])+?} }, format: false get '/settings', to: redirect('/settings/profile') draw(:settings) diff --git a/lib/mastodon/premailer_webpack_strategy.rb b/lib/mastodon/premailer_webpack_strategy.rb index 5c297d4d05..45e87603f0 100644 --- a/lib/mastodon/premailer_webpack_strategy.rb +++ b/lib/mastodon/premailer_webpack_strategy.rb @@ -16,7 +16,7 @@ module PremailerWebpackStrategy Rails.public_path.join(url).read end - css.gsub(/url\(\//, "url(#{asset_host}/") + css.gsub(%r{url\(/}, "url(#{asset_host}/") end module_function :load diff --git a/lib/paperclip/color_extractor.rb b/lib/paperclip/color_extractor.rb index 19529fb8a1..d2f7e7c602 100644 --- a/lib/paperclip/color_extractor.rb +++ b/lib/paperclip/color_extractor.rb @@ -171,7 +171,7 @@ module Paperclip end def palette_from_histogram(result, quantity) - frequencies = result.scan(/([0-9]+)\:/).flatten.map(&:to_f) + frequencies = result.scan(/([0-9]+):/).flatten.map(&:to_f) hex_values = result.scan(/\#([0-9A-Fa-f]{6,8})/).flatten total_frequencies = frequencies.sum.to_f diff --git a/lib/tasks/emojis.rake b/lib/tasks/emojis.rake index fbb2e8d4f3..c24ea42e7d 100644 --- a/lib/tasks/emojis.rake +++ b/lib/tasks/emojis.rake @@ -31,7 +31,7 @@ def gen_border(codepoint, color) end def codepoints_to_filename(codepoints) - codepoints.downcase.gsub(/\A[0]+/, '').tr(' ', '-') + codepoints.downcase.gsub(/\A0+/, '').tr(' ', '-') end def codepoints_to_unicode(codepoints) diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake index 6c0e669227..19a369079e 100644 --- a/lib/tasks/mastodon.rake +++ b/lib/tasks/mastodon.rake @@ -21,7 +21,7 @@ namespace :mastodon do env['LOCAL_DOMAIN'] = prompt.ask('Domain name:') do |q| q.required true q.modify :strip - q.validate(/\A[a-z0-9\.\-]+\z/i) + q.validate(/\A[a-z0-9.-]+\z/i) q.messages[:valid?] = 'Invalid domain. If you intend to use unicode characters, enter punycode here' end @@ -240,7 +240,7 @@ namespace :mastodon do end env['S3_PROTOCOL'] = env['S3_ENDPOINT'].start_with?('https') ? 'https' : 'http' - env['S3_HOSTNAME'] = env['S3_ENDPOINT'].gsub(/\Ahttps?:\/\//, '') + env['S3_HOSTNAME'] = env['S3_ENDPOINT'].gsub(%r{\Ahttps?://}, '') env['S3_BUCKET'] = prompt.ask('Minio bucket name:') do |q| q.required true @@ -269,7 +269,7 @@ namespace :mastodon do end env['S3_PROTOCOL'] = env['S3_ENDPOINT'].start_with?('https') ? 'https' : 'http' - env['S3_HOSTNAME'] = env['S3_ENDPOINT'].gsub(/\Ahttps?:\/\//, '') + env['S3_HOSTNAME'] = env['S3_ENDPOINT'].gsub(%r{\Ahttps?://}, '') env['S3_BUCKET'] = prompt.ask('Storj DCS bucket name:') do |q| q.required true @@ -573,7 +573,7 @@ def dotenv_escape(value) # As long as the value doesn't include single quotes, we can safely # rely on single quotes - return "'#{value}'" unless /[']/.match?(value) + return "'#{value}'" unless value.include?("'") # If the value contains the string '\n' or '\r' we simply can't use # a double-quoted string, because Dotenv will expand \n or \r no -- GitLab