Browse Source

Fixes #4320 - Zammad-scheduler constantly crashes if user have unknown timezone

Mantas Masalskis 2 years ago
parent
commit
2dcc0e3900

+ 31 - 0
.rubocop/cop/zammad/timezone_default.rb

@@ -0,0 +1,31 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+module RuboCop
+  module Cop
+    module Zammad
+      class TimezoneDefault < Base
+        def on_str(node)
+          return if !matching_string?(node)
+          return if !matching_node?(node)
+
+          add_offense(node.parent, message: <<~TEXT.chomp)
+            Please refrain from using Setting.get('timezone_default').
+            You're probably looking for Setting.get('timezone_default_sanitized').
+          TEXT
+        end
+
+        def matching_string?(node)
+          node.source == "'timezone_default'"
+        end
+
+        def matching_node?(node)
+          parent = node.parent
+
+          return false if !parent
+
+          parent.receiver&.const_name == 'Setting' && parent.method_name == :get
+        end
+      end
+    end
+  end
+end

+ 4 - 0
.rubocop/default.yml

@@ -440,3 +440,7 @@ Zammad/ExistsDbStrategy:
     - "spec/**/*.rb"
   Exclude:
     - "spec/support/capybara/*.rb"
+
+Zammad/TimezoneDefault:
+  Exclude:
+    - "spec/**/*.rb"

+ 1 - 0
.rubocop/rubocop_zammad.rb

@@ -13,3 +13,4 @@ require_relative 'cop/zammad/update_copyright'
 require_relative 'cop/zammad/forbid_rand'
 require_relative 'cop/zammad/forbid_translatable_marker'
 require_relative 'cop/zammad/to_forbid_over_not_to_permit'
+require_relative 'cop/zammad/timezone_default'

+ 3 - 0
Gemfile

@@ -53,6 +53,9 @@ gem 'graphql-batch', require: 'graphql/batch'
 # core - image processing
 gem 'rszr'
 
+# core - use same timezone data on any host
+gem 'tzinfo-data'
+
 # performance - Memcached
 gem 'dalli', require: false
 

+ 3 - 0
Gemfile.lock

@@ -559,6 +559,8 @@ GEM
       simple_oauth (~> 0.3.0)
     tzinfo (2.0.5)
       concurrent-ruby (~> 1.0)
+    tzinfo-data (1.2022.6)
+      tzinfo (>= 1.0.0)
     unf (0.1.4)
       unf_ext
     unf_ext (0.0.8.2)
@@ -709,6 +711,7 @@ DEPENDENCIES
   thor
   twilio-ruby
   twitter
+  tzinfo-data
   unicorn
   vcr
   viewpoint

+ 1 - 1
app/assets/javascripts/app/controllers/_ui_element/timer.coffee

@@ -114,7 +114,7 @@ class App.UiElement.timer
 
     days = @joinItems days
     hours = @joinItems hours
-    timezone = App.Config.get('timezone_default') || 'UTC'
+    timezone = App.Config.get('timezone_default_sanitized')
 
     formGroup.find('.js-timerResult').text(App.i18n.translateInline('Run every %s at %s in %s time', days, hours, timezone))
 

+ 2 - 2
app/assets/javascripts/app/index.coffee

@@ -157,8 +157,8 @@ class App extends Spine.Controller
         title = timestamp
         timezone = ''
         if attributeConfig.include_timezone
-          timezone = " timezone=\"#{App.Config.get('timezone_default')}\""
-          title += ' ' + App.Config.get('timezone_default')
+          timezone = " timezone=\"#{App.Config.get('timezone_default_sanitized')}\""
+          title += ' ' + App.Config.get('timezone_default_sanitized')
 
         resultLocal = "<time class=\"humanTimeFromNow #{cssClass}\" datetime=\"#{resultLocal}\" title=\"#{title}\"#{timezone}>#{humanTime}</time>"
 

+ 2 - 2
app/controllers/reports_controller.rb

@@ -174,8 +174,8 @@ class ReportsController < ApplicationController
       stop_at = Time.zone.parse("#{params[:year]}-12-31T23:59:59Z")
       range = 'month'
     end
-    params[:timezone] ||= Setting.get('timezone_default')
-    if params[:timezone].present? && params[:timeRange] != 'realtime'
+    params[:timezone] ||= Setting.get('timezone_default_sanitized')
+    if params[:timeRange] != 'realtime'
       offset = stop_at.in_time_zone(params[:timezone]).utc_offset
       start_at -= offset
       stop_at -= offset

+ 2 - 0
app/controllers/sessions_controller.rb

@@ -252,6 +252,8 @@ class SessionsController < ApplicationController
       config[setting.name] = value
     end
 
+    Setting::Processed.process_frontend_settings! config
+
     # NB: Explicitly include SAML display name config
     #   This is needed because the setting is not frontend related,
     #   but we still to display one of the options

+ 1 - 1
app/helpers/knowledge_base_human_date_helper.rb

@@ -2,7 +2,7 @@
 
 module KnowledgeBaseHumanDateHelper
   def human_time_tag(time, locale: system_locale_via_uri)
-    timezone     = Setting.get('timezone_default').presence || 'UTC'
+    timezone     = Setting.get('timezone_default_sanitized')
     time_in_zone = time.in_time_zone(timezone)
     locale_name  = locale.locale
 

Some files were not shown because too many files changed in this diff