From 480f3a129fd222ab7a2b00145d6f0b101f570c1b Mon Sep 17 00:00:00 2001 From: "s.sisti@elogic.it" Date: Tue, 10 Nov 2026 08:33:08 +0100 Subject: [PATCH] FIX: Accept upload extensions in any case The upload field compared the file extension with the allowed file types case-sensitively, so FOTO.JPG was rejected when the field allowed .jpg. Phones and cameras often name files in uppercase. Compare extensions case-insensitively and ignore spaces around the allowed file types (".jpg, .png"). Co-Authored-By: Claude Opus 5.5 --- lib/custom_wizard/validators/update.rb | 8 ++-- plugin.rb | 2 +- .../custom_wizard/update_validator_spec.rb | 43 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/custom_wizard/validators/update.rb b/lib/custom_wizard/validators/update.rb index 64f440043..0f012c7bc 100644 --- a/lib/custom_wizard/validators/update.rb +++ b/lib/custom_wizard/validators/update.rb @@ -169,10 +169,10 @@ def validate_answer(field, field_id, value, label) end def validate_file_type(value, file_types) - file_types - .split(",") - .map { |t| t.gsub(".", "") } - .include?(File.extname(value["original_filename"])[1..-1]) + extension = File.extname(value["original_filename"].to_s).delete_prefix(".").downcase + return false if extension.blank? + + file_types.split(",").map { |t| t.strip.delete_prefix(".").downcase }.include?(extension) end def validate_date(value, format) diff --git a/plugin.rb b/plugin.rb index 81b200bfe..3fac8b9f0 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.18.16 +# version: 2.18.17 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech diff --git a/spec/components/custom_wizard/update_validator_spec.rb b/spec/components/custom_wizard/update_validator_spec.rb index 10233c354..93cb295e8 100644 --- a/spec/components/custom_wizard/update_validator_spec.rb +++ b/spec/components/custom_wizard/update_validator_spec.rb @@ -129,6 +129,49 @@ def perform_validation(step_id, submission) end end + context "upload fields" do + let(:upload_field) { get_wizard_fixture("field/upload") } + + def add_upload_field(file_types) + template[:steps][1][:fields] << upload_field.merge(file_types: file_types) + CustomWizard::Template.save(template) + end + + def upload_errors(filename) + updater = perform_validation("step_2", step_2_field_7: { "original_filename" => filename }) + updater.errors.messages[:step_2_field_7] + end + + it "accepts files with an allowed extension" do + add_upload_field(".jpg,.jpeg,.png") + expect(upload_errors("photo.jpg")).to be_blank + end + + it "rejects files with an extension that isn't allowed" do + add_upload_field(".jpg,.jpeg,.png") + expect(upload_errors("document.pdf").first).to eq( + I18n.t("wizard.field.invalid_file", label: "Upload", types: ".jpg,.jpeg,.png"), + ) + expect(upload_errors("photo")).to be_present + end + + it "ignores the case of the extension" do + add_upload_field(".jpg,.jpeg,.png") + expect(upload_errors("PHOTO.JPG")).to be_blank + end + + it "ignores the case of the allowed file types" do + add_upload_field(".JPG,.PNG") + expect(upload_errors("photo.Png")).to be_blank + end + + it "accepts file types written without a dot or with spaces" do + add_upload_field(".jpg, .png,webp") + expect(upload_errors("image.webp")).to be_blank + expect(upload_errors("photo.png")).to be_blank + end + end + context "answer validation" do def set_answer_validation(opts) @template[:steps][0][:fields][0][:validations] = { answer: { status: true }.merge(opts) }