OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
limit upload file size and check for valid .ttf font
as-op13a4527054f0
diff --git a/app/controllers/custom_styles_controller.rb b/app/controllers/custom_styles_controller.rbindex 78ee35e5a30..ed20d5eaa3a 100644--- a/app/controllers/custom_styles_controller.rb+++ b/app/controllers/custom_styles_controller.rb@@ -73,6 +73,7 @@ class CustomStylesController < ApplicationControllerenddef update+ flash.clear@custom_style = get_or_create_custom_styleif @custom_style.update(custom_style_params)redirect_to custom_style_pathdiff --git a/app/models/custom_style.rb b/app/models/custom_style.rbindex b54b70b96d1..5463a7cdea4 100644--- a/app/models/custom_style.rb+++ b/app/models/custom_style.rb@@ -28,6 +28,8 @@# See COPYRIGHT and LICENSE files for more details.#+++require "ttfunk"+class CustomStyle < ApplicationRecordmount_uploader :logo, OpenProject::Configuration.file_uploadermount_uploader :export_logo, OpenProject::Configuration.file_uploader@@ -40,6 +42,9 @@ class CustomStyle < ApplicationRecordmount_uploader :export_font_italic, OpenProject::Configuration.file_uploadermount_uploader :export_font_bold_italic, OpenProject::Configuration.file_uploader+ MAX_FONT_UPLOAD_SIZE = 30.megabytes+ validate :validate_font_files+class << selfdef currentRequestStore.fetch(:current_custom_style) do@@ -78,4 +83,40 @@ class CustomStyle < ApplicationRecordendendend++ def validate_font_files+ %i(export_font_regular export_font_bold export_font_italic export_font_bold_italic).each do |name|+ attachment = send(name)+ validate_font_file(name, attachment)+ end+ end++ private++ def validate_font_file(name, attachment)+ validate_font_file_size(name, attachment) if attachment&.file+ validate_font_file_format(name, attachment) if attachment&.file+ end++ def validate_font_file_format(name, attachment)+ unless valid_ttf?(attachment.file.path)+ errors.add(name, I18n.t("admin.custom_styles.fonts.file_is_invalid"))+ attachment.remove!+ end+ end++ def validate_font_file_size(name, attachment)+ size = attachment.file.size.to_i+ if size >= MAX_FONT_UPLOAD_SIZE+ errors.add(name, I18n.t("admin.custom_styles.fonts.file_too_large", count: (MAX_FONT_UPLOAD_SIZE / 1.megabyte).to_i))+ attachment.remove!+ end+ end++ def valid_ttf?(filename)+ file = TTFunk::File.open(filename)+ file.name.font_name.present?+ rescue StandardError+ false+ endenddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex a9502ac965a..71d2ed54b4d 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -69,6 +69,9 @@ en:tab_branding: "Branding"tab_pdf_export_styles: "PDF export styles"tab_pdf_export_font: "PDF export font"+ fonts:+ file_too_large: "is too large (maximum size is %{count} MB)."+ file_is_invalid: "is not a valid TTF font file."colors:primary-button-color: "Primary button"accent-color: "Accent"diff --git a/spec/controllers/custom_styles_controller_spec.rb b/spec/controllers/custom_styles_controller_spec.rbindex 9525cab6071..dc298c853cf 100644--- a/spec/controllers/custom_styles_controller_spec.rb+++ b/spec/controllers/custom_styles_controller_spec.rb@@ -681,6 +681,29 @@ RSpec.describe CustomStylesController doexpect(custom_style.reload.export_font_bold_italic).to be_presentexpect(File.basename(custom_style.reload.export_font_bold_italic.file.path)).to eq("NotoEmoji.ttf")end++ describe "update with invalid file", with_ee: %i[define_custom_style] do+ let(:font_file) { Rack::Test::UploadedFile.new(Rails.public_path.join("favicon.ico"), "font/ttf") }++ it "does respect the file size limit" do+ # rubocop:disable RSpec/AnyInstance+ allow_any_instance_of(CarrierWave::SanitizedFile)+ .to receive(:size)+ .and_return(40.megabytes)+ # rubocop:enable RSpec/AnyInstance+ post :update, params: { custom_style: { export_font_regular: font_file } }+ expect(response).to have_http_status(:unprocessable_entity)+ expect(custom_style.reload.export_font_regular).not_to be_present+ expect(flash[:error].join).to include("is too large")+ end++ it "does not accept a non-font" do+ post :update, params: { custom_style: { export_font_regular: font_file } }+ expect(response).to have_http_status(:unprocessable_entity)+ expect(custom_style.reload.export_font_regular).not_to be_present+ expect(flash[:error].join).to include "not a valid TTF font file."+ end+ endenddescribe "export font deletions", with_ee: %i[define_custom_style] do