A platform for community discussion. Free, open, simple.
DEV: Fix rubocop issues (#14715)
Dan Ungureanu69f0f48dc084
diff --git a/app/models/locale_site_setting.rb b/app/models/locale_site_setting.rbindex f541a27f488..68431871648 100644--- a/app/models/locale_site_setting.rb+++ b/app/models/locale_site_setting.rb@@ -23,7 +23,7 @@ class LocaleSiteSetting < EnumSiteSetting@lock.synchronize do@language_names ||= begin- names = YAML.load(File.read(File.join(Rails.root, 'config', 'locales', 'names.yml')))+ names = YAML.safe_load(File.read(File.join(Rails.root, 'config', 'locales', 'names.yml')))DiscoursePluginRegistry.locales.each do |locale, options|if !names.key?(locale) && options[:name] && options[:nativeName]diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex 502409d3c19..22739134d1e 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -117,8 +117,8 @@ class TopicEmbed < ActiveRecord::Basefollow_canonical: true,)- url = fd.resolve- return if url.blank?+ uri = fd.resolve+ return if uri.blank?opts = {tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote],@@ -132,7 +132,7 @@ class TopicEmbed < ActiveRecord::Baseresponse = FetchResponse.newbegin- html = open(url, allow_redirections: :safe).read+ html = uri.read(allow_redirections: :safe)rescue OpenURI::HTTPError, Net::OpenTimeoutreturnend@@ -256,10 +256,6 @@ class TopicEmbed < ActiveRecord::Basebodyendend-- def self.open(uri, **kwargs)- URI.open(uri, **kwargs)- endend# == Schema Informationdiff --git a/app/services/site_settings_task.rb b/app/services/site_settings_task.rbindex 64c80486ffe..356d8eccf14 100644--- a/app/services/site_settings_task.rb+++ b/app/services/site_settings_task.rb@@ -16,7 +16,7 @@ class SiteSettingsTaskcounts = { updated: 0, not_found: 0, errors: 0 }log = []- site_settings = YAML::load(yml)+ site_settings = YAML::safe_load(yml)site_settings.each do |site_setting|key = site_setting[0]val = site_setting[1]diff --git a/lib/cache.rb b/lib/cache.rbindex 7d69361915b..ca814c9e2bb 100644--- a/lib/cache.rb+++ b/lib/cache.rb@@ -86,7 +86,7 @@ class Cacheif rawbegin- Marshal.load(raw)+ Marshal.load(raw) # rubocop:disable Security/MarshalLoadrescue => elog_first_exception(e)end@@ -113,7 +113,7 @@ class Cachedef read_entry(key)if data = redis.get(key)- Marshal.load(data)+ Marshal.load(data) # rubocop:disable Security/MarshalLoadendrescue => e# corrupt cache, this can happen if Marshal versiondiff --git a/lib/discourse_updates.rb b/lib/discourse_updates.rbindex fbc81aac9f7..32afc2f6d2d 100644--- a/lib/discourse_updates.rb+++ b/lib/discourse_updates.rb@@ -61,18 +61,34 @@ module DiscourseUpdatesDiscourse.redis.get last_installed_version_keyend+ def last_installed_version=(arg)+ Discourse.redis.set(last_installed_version, arg)+ end+def latest_versionDiscourse.redis.get latest_version_keyend+ def latest_version=(arg)+ Discourse.redis.set(latest_version, arg)+ end+def missing_versions_countDiscourse.redis.get(missing_versions_count_key).try(:to_i)end+ def missing_versions_count=(arg)+ Discourse.redis.set(missing_versions_count, arg)+ end+def critical_updates_available?(Discourse.redis.get(critical_updates_available_key) || false) == 'true'end+ def critical_updates_available=(arg)+ Discourse.redis.set(critical_updates_available, arg)+ end+def updated_att = Discourse.redis.get(updated_at_key)t ? Time.zone.parse(t) : nil@@ -82,12 +98,6 @@ module DiscourseUpdatesDiscourse.redis.set updated_at_key, time_with_zone.as_jsonend- ['last_installed_version', 'latest_version', 'missing_versions_count', 'critical_updates_available'].each do |name|- eval "define_method :#{name}= do |arg|- Discourse.redis.set #{name}_key, arg- end"- end-def missing_versions=(versions)# delete previous list from redisprev_keys = Discourse.redis.lrange(missing_versions_list_key, 0, 4)diff --git a/lib/i18n/locale_file_checker.rb b/lib/i18n/locale_file_checker.rbindex e818823fd8d..757e9b969a2 100644--- a/lib/i18n/locale_file_checker.rb+++ b/lib/i18n/locale_file_checker.rb@@ -165,7 +165,7 @@ class LocaleFileCheckerdef plural_keys@plural_keys ||= begin- eval(File.read("#{Rails.root}/#{PLURALS_FILE}")).map do |locale, value|+ eval(File.read("#{Rails.root}/#{PLURALS_FILE}")).map do |locale, value| # rubocop:disable Security/Eval[locale.to_s, value[:i18n][:plural][:keys].map(&:to_s)]end.to_henddiff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rbindex a62fb01836c..8d605ae537c 100644--- a/lib/middleware/anonymous_cache.rb+++ b/lib/middleware/anonymous_cache.rb@@ -29,7 +29,7 @@ module Middlewaremethod << "|#{k}=#\{h.#{v}}"endmethod << "\"\nend"- eval(method)+ eval(method) # rubocop:disable Security/Eval@@compiled = trueenddiff --git a/lib/onebox/engine/github_actions_onebox.rb b/lib/onebox/engine/github_actions_onebox.rbindex cdb7e16378f..182fff49d58 100644--- a/lib/onebox/engine/github_actions_onebox.rb+++ b/lib/onebox/engine/github_actions_onebox.rb@@ -71,7 +71,7 @@ module Oneboxraw["head_commit"]["message"].lines.firstelsif type == :pr_runpr_url = "https://api.github.com/repos/#{match[:org]}/#{match[:repo]}/pulls/#{match[:pr_id]}"- ::MultiJson.load(URI.open(pr_url, read_timeout: timeout))["title"]+ ::MultiJson.load(URI.parse(pr_url).open(read_timeout: timeout))["title"]end{diff --git a/lib/onebox/engine/json.rb b/lib/onebox/engine/json.rbindex 261dc0309cf..204b09c720e 100644--- a/lib/onebox/engine/json.rb+++ b/lib/onebox/engine/json.rb@@ -6,7 +6,7 @@ module Oneboxprivatedef raw- @raw ||= ::MultiJson.load(URI.open(url, read_timeout: timeout))+ @raw ||= ::MultiJson.load(URI.parse(url).open(read_timeout: timeout))endendenddiff --git a/lib/onebox/engine/pubmed_onebox.rb b/lib/onebox/engine/pubmed_onebox.rbindex 1cf8a0ac9bb..366d5d50299 100644--- a/lib/onebox/engine/pubmed_onebox.rb+++ b/lib/onebox/engine/pubmed_onebox.rb@@ -12,7 +12,7 @@ module Oneboxdef xmlreturn @xml if defined?(@xml)- doc = Nokogiri::XML(URI.open(URI.join(@url, "?report=xml&format=text")))+ doc = Nokogiri::XML(URI.join(@url, "?report=xml&format=text").open)pre = doc.xpath("//pre")@xml = Nokogiri::XML("<root>" + pre.text + "</root>")enddiff --git a/lib/onebox/mixins/git_blob_onebox.rb b/lib/onebox/mixins/git_blob_onebox.rbindex cac7b2a7644..0c9b2bda128 100644--- a/lib/onebox/mixins/git_blob_onebox.rb+++ b/lib/onebox/mixins/git_blob_onebox.rb@@ -167,7 +167,7 @@ module Onebox@model_file = @lang.dup@raw = "https://render.githubusercontent.com/view/solid?url=" + self.raw_template(m)else- contents = URI.open(self.raw_template(m), read_timeout: timeout).read+ contents = URI.parse(self.raw_template(m)).open(read_timeout: timeout).readcontents_lines = contents.lines #get contents linescontents_lines_size = contents_lines.size #get number of linesdiff --git a/lib/onebox/status_check.rb b/lib/onebox/status_check.rbindex 73b3bca24e6..0980b8d1355 100644--- a/lib/onebox/status_check.rb+++ b/lib/onebox/status_check.rb@@ -35,7 +35,7 @@ module Oneboxprivatedef check- res = URI.open(@url, read_timeout: (@options.timeout || Onebox.options.timeout))+ res = URI.parse(@url).open(read_timeout: (@options.timeout || Onebox.options.timeout))@status = res.status.first.to_irescue OpenURI::HTTPError => e@status = e.io.status.first.to_idiff --git a/lib/site_settings/type_supervisor.rb b/lib/site_settings/type_supervisor.rbindex 99170c3dc0d..428aadfe4c3 100644--- a/lib/site_settings/type_supervisor.rb+++ b/lib/site_settings/type_supervisor.rb@@ -92,7 +92,7 @@ class SiteSettings::TypeSupervisorendif (new_choices = opts[:choices])- new_choices = eval(new_choices) if new_choices.is_a?(String)+ new_choices = eval(new_choices) if new_choices.is_a?(String) # rubocop:disable Security/Evalif @choices.has_key?(name)@choices[name].concat(new_choices)diff --git a/lib/tasks/cdn.rake b/lib/tasks/cdn.rakeindex 438e1020fce..b9c6c27501e 100644--- a/lib/tasks/cdn.rake+++ b/lib/tasks/cdn.rake@@ -20,7 +20,7 @@ task 'assets:prestage' => :environment do |t|puts "pre staging: #{assets.join(' ')}"# makes testing simpler leaving this here- config = YAML::load(File.open("#{Rails.root}/config/cdn.yml"))+ config = YAML::safe_load(File.open("#{Rails.root}/config/cdn.yml"))start = Time.nowdiff --git a/lib/tasks/emoji.rake b/lib/tasks/emoji.rakeindex 18ec5fc034a..3cb7f5ff3ad 100644--- a/lib/tasks/emoji.rake+++ b/lib/tasks/emoji.rake@@ -284,7 +284,7 @@ desc "update emoji images"task "emoji:update" docopy_emoji_db- json_db = open(File.join(GENERATED_PATH, "db.json")).read+ json_db = File.read(File.join(GENERATED_PATH, "db.json"))db = JSON.parse(json_db)write_db_json(db["emojis"], db["translations"])@@ -352,7 +352,7 @@ enddef generate_emoji_groups(keywords, sections)puts "Generating groups..."- list = open(EMOJI_ORDERING_URL).read+ list = URI.parse(EMOJI_ORDERING_URL).readdoc = Nokogiri::HTML5(list)table = doc.css("table")[0]diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rakeindex 1e2fdfacc03..cd9eda143f1 100644--- a/lib/tasks/themes.rake+++ b/lib/tasks/themes.rake@@ -30,7 +30,7 @@ task "themes:install" => :environment do |task, args|use_json = theme_args == ''theme_args = begin- use_json ? JSON.parse(ARGV.last.gsub('--', '')) : YAML::load(theme_args)+ use_json ? JSON.parse(ARGV.last.gsub('--', '')) : YAML::safe_load(theme_args)rescueputs use_json ? "Invalid JSON input. \n#{ARGV.last}" : "Invalid YML: \n#{theme_args}"exit 1diff --git a/script/bench.rb b/script/bench.rbindex ce88f408b4b..d1ab78d791a 100644--- a/script/bench.rb+++ b/script/bench.rb@@ -297,7 +297,7 @@ beginrun("RAILS_ENV=profile bundle exec rake assets:clean")def get_mem(pid)- YAML.load `ruby script/memstats.rb #{pid} --yaml`+ YAML.safe_load `ruby script/memstats.rb #{pid} --yaml`endmem = get_mem(pid)diff --git a/script/benchmarks/cache/bench.rb b/script/benchmarks/cache/bench.rbindex ff6092e8595..fad1b73607a 100644--- a/script/benchmarks/cache/bench.rb+++ b/script/benchmarks/cache/bench.rb@@ -46,7 +46,7 @@ Benchmark.ips do |x|x.report("redis get string marshal") do |times|while times > 0- Marshal.load(Discourse.redis.get("test_keym"))+ Marshal.load(Discourse.redis.get("test_keym")) # rubocop:disable Security/MarshalLoadtimes -= 1endenddiff --git a/script/import_scripts/jforum.rb b/script/import_scripts/jforum.rbindex e5d56718b62..1c1136f2084 100644--- a/script/import_scripts/jforum.rb+++ b/script/import_scripts/jforum.rb@@ -10,7 +10,7 @@ class ImportScripts::JForum < ImportScripts::Basedef initializesuper- @settings = YAML.load(File.read(ARGV.first), symbolize_names: true)+ @settings = YAML.safe_load(File.read(ARGV.first), symbolize_names: true)@database_client = Mysql2::Client.new(host: @settings[:database][:host],diff --git a/script/import_scripts/mbox/support/indexer.rb b/script/import_scripts/mbox/support/indexer.rbindex 47ce3ed1334..c8164534d77 100644--- a/script/import_scripts/mbox/support/indexer.rb+++ b/script/import_scripts/mbox/support/indexer.rb@@ -48,7 +48,7 @@ module ImportScripts::Mboxif File.exist?(metadata_file)# workaround for YML files that contain classname in file headeryaml = File.read(metadata_file).sub(/^--- !.*$/, '---')- metadata = YAML.load(yaml)+ metadata = YAML.safe_load(yaml)elsemetadata = {}enddiff --git a/script/import_scripts/nodebb/nodebb.rb b/script/import_scripts/nodebb/nodebb.rbindex 45fe69c7c8c..5fed857301d 100644--- a/script/import_scripts/nodebb/nodebb.rb+++ b/script/import_scripts/nodebb/nodebb.rb@@ -180,7 +180,7 @@ class ImportScripts::NodeBB < ImportScripts::Baseif is_external# download external imagebegin- string_io = open(picture, read_timeout: 5)+ string_io = uri.open(read_timeout: 5)rescue Net::ReadTimeoutputs "timeout downloading avatar for user #{imported_user.id}"return nil@@ -246,7 +246,7 @@ class ImportScripts::NodeBB < ImportScripts::Baseif is_externalbegin- string_io = open(picture, read_timeout: 5)+ string_io = uri.open(read_timeout: 5)rescue Net::ReadTimeoutreturn nilenddiff --git a/script/import_scripts/smf2.rb b/script/import_scripts/smf2.rbindex 74b63189cd8..9885f1edc32 100644--- a/script/import_scripts/smf2.rb+++ b/script/import_scripts/smf2.rb@@ -558,7 +558,7 @@ class ImportScripts::Smf2 < ImportScripts::Basedef read_smf_settingssettings = File.join(self.smfroot, 'Settings.php')- IO.readlines(settings).each do |line|+ File.readlines(settings).each do |line|next unless m = /\$([a-z_]+)\s*=\s*['"](.+?)['"]\s*;\s*((#|\/\/).*)?$/.match(line)case m[1]when 'db_server' then self.host ||= m[2]diff --git a/script/import_scripts/zendesk_api.rb b/script/import_scripts/zendesk_api.rbindex 4b5a2b56801..9237a764423 100644--- a/script/import_scripts/zendesk_api.rb+++ b/script/import_scripts/zendesk_api.rb@@ -333,7 +333,7 @@ class ImportScripts::ZendeskApi < ImportScripts::Baseattempts = 0begin- open("#{$1}") do |image|+ URI.parse(image_url).open do |image|# IMAGE_DOWNLOAD_PATH is whatever image, it will be replaced with the downloaded imageFile.open(IMAGE_DOWNLOAD_PATH, "wb") do |file|file.write(image.read)diff --git a/script/memstats.rb b/script/memstats.rbindex f387a883370..998d8d525f7 100755--- a/script/memstats.rb+++ b/script/memstats.rb@@ -131,7 +131,7 @@ def format_number(n)enddef get_commandline(pid)- commandline = IO.read("/proc/#{pid}/cmdline").split("\0")+ commandline = File.read("/proc/#{pid}/cmdline").split("\0")if commandline.first =~ /java$/ thenloop { break if commandline.shift == "-jar" }return "[java] #{commandline.shift}"diff --git a/spec/components/discourse_updates_spec.rb b/spec/components/discourse_updates_spec.rbindex 6bb8fd27400..8cb95dd5fdf 100644--- a/spec/components/discourse_updates_spec.rb+++ b/spec/components/discourse_updates_spec.rb@@ -3,12 +3,16 @@require 'rails_helper'describe DiscourseUpdates do+ let(:latest_version_key) { DiscourseUpdates.send(:latest_version_key) }+ let(:missing_versions_count_key) { DiscourseUpdates.send(:missing_versions_count_key) }+ let(:critical_updates_available_key) { DiscourseUpdates.send(:critical_updates_available_key) }+ let(:updated_at_key) { DiscourseUpdates.send(:updated_at_key) }def stub_data(latest, missing, critical, updated_at)- DiscourseUpdates.stubs(:latest_version).returns(latest)- DiscourseUpdates.stubs(:missing_versions_count).returns(missing)- DiscourseUpdates.stubs(:critical_updates_available?).returns(critical)- DiscourseUpdates.stubs(:updated_at).returns(updated_at)+ Discourse.redis.set(latest_version_key, latest)+ Discourse.redis.set(missing_versions_count_key, missing)+ Discourse.redis.set(critical_updates_available_key, critical)+ Discourse.redis.set(updated_at_key, updated_at)endbefore do@@ -36,7 +40,7 @@ describe DiscourseUpdates doendit 'returns the timestamp of the last version check' do- expect(subject.updated_at).to eq_time(time)+ expect(subject.updated_at).to be_within_one_second_of(time)endend@@ -52,7 +56,7 @@ describe DiscourseUpdates doendit 'returns the timestamp of the last version check' do- expect(subject.updated_at).to eq_time(time)+ expect(subject.updated_at).to be_within_one_second_of(time)endendenddiff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rbindex 8659eb7d569..4ce37dc37a1 100644--- a/spec/models/topic_embed_spec.rb+++ b/spec/models/topic_embed_spec.rb@@ -191,19 +191,13 @@ describe TopicEmbed dodescribe '.find_remote' dofab!(:embeddable_host) { Fabricate(:embeddable_host) }- let(:file) { StringIO.new }-- before do- TopicEmbed.stubs(:open).returns file- endcontext ".title_scrub" dolet(:url) { 'http://eviltrout.com/123' }let(:contents) { "<title>Through the Looking Glass - Classic Books</title><body>some content here</body>" }before do- file.stubs(:read).returns contents- stub_request(:get, url)+ stub_request(:get, url).to_return(status: 200, body: contents)endit "doesn't scrub the title by default" do@@ -225,8 +219,7 @@ describe TopicEmbed dobefore doSiteSetting.allowed_embed_classnames = 'emoji, foo'- file.stubs(:read).returns contents- stub_request(:get, url)+ stub_request(:get, url).to_return(status: 200, body: contents)@response = TopicEmbed.find_remote(url)end@@ -257,9 +250,7 @@ describe TopicEmbed dolet(:contents) { '<html><head><meta name="author" content="eviltrout"></head><body>rich and morty</body></html>' }before(:each) do- file.stubs(:read).returns contents- TopicEmbed.stubs(:open).returns file- stub_request(:get, url)+ stub_request(:get, url).to_return(status: 200, body: contents)endit "has no author tag" do@@ -276,8 +267,7 @@ describe TopicEmbed dobefore(:each) doSiteSetting.allowed_embed_classnames = ''- file.stubs(:read).returns contents- stub_request(:get, url)+ stub_request(:get, url).to_return(status: 200, body: contents)@response = TopicEmbed.find_remote(url)end@@ -303,8 +293,7 @@ describe TopicEmbed dolet(:contents) { "<title>سلام</title><body>این یک پاراگراف آزمون است.</body>" }before do- stub_request(:get, url)- file.stubs(:read).returns contents+ stub_request(:get, url).to_return(status: 200, body: contents)endit "doesn't throw an error" do@@ -318,8 +307,7 @@ describe TopicEmbed dolet(:contents) { "<title>Hello World!</title><body></body>" }before do- stub_request(:get, url)- file.stubs(:read).returns contents+ stub_request(:get, url).to_return(status: 200, body: contents)endit "doesn't throw an error" do@@ -341,8 +329,7 @@ describe TopicEmbed dolet(:contents) { '<p><a href="mailto:foo%40example.com">URL encoded @ symbol</a></p><p><a href="mailto:[email protected]">normal mailto link</a></p>' }before do- file.stubs(:read).returns contents- stub_request(:get, url)+ stub_request(:get, url).to_return(status: 200, body: contents)endit "handles mailto links" do@@ -358,8 +345,7 @@ describe TopicEmbed dolet(:contents) { '<p><a href="(http://foo.bar)">Baz</a></p>' }before do- file.stubs(:read).returns contents- stub_request(:get, url)+ stub_request(:get, url).to_return(status: 200, body: contents)endit "doesn’t raise an exception" do@@ -374,9 +360,9 @@ describe TopicEmbed dolet(:canonical_content) { "<title>Canonical</title><body></body>" }before do- file.stubs(:read).returns canonical_content- stub_request(:get, url)+ stub_request(:get, url).to_return(status: 200, body: content)stub_request(:head, canonical_url)+ stub_request(:get, canonical_url).to_return(status: 200, body: canonical_content)endit 'a' dodiff --git a/spec/serializers/upload_serializer_spec.rb b/spec/serializers/upload_serializer_spec.rbindex c2a1cf6e048..01be44b5521 100644--- a/spec/serializers/upload_serializer_spec.rb+++ b/spec/serializers/upload_serializer_spec.rb@@ -7,7 +7,7 @@ RSpec.describe UploadSerializer dolet(:subject) { UploadSerializer.new(upload, root: false) }it 'should render without errors' do- json_data = JSON.load(subject.to_json)+ json_data = JSON.parse(subject.to_json)expect(json_data['id']).to eql upload.idexpect(json_data['width']).to eql upload.width