From 81f7262a57b37a042dd10d79ebbe9a3ec0045967 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Fri, 20 Nov 2026 16:32:55 +0900 Subject: [PATCH] Allow mailto: in custom field URL patterns A URL pattern such as "mailto:%value%" was rejected as invalid although mailto is one of the allowed schemes. The pattern was validated with uri_with_safe_scheme? after removing the tokens, and URI.parse raises URI::InvalidComponentError for "mailto:" because the address part is missing. A URL pattern is not a complete URI until the tokens are substituted, so only its scheme is now checked against the allowed schemes. Co-Authored-By: Claude Opus 5.5 --- lib/redmine/field_format.rb | 12 +++++++++++- lib/redmine/helpers/url.rb | 4 +++- .../lib/redmine/field_format/field_format_test.rb | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/redmine/field_format.rb b/lib/redmine/field_format.rb index 8634f7ea6..2125e5937 100644 --- a/lib/redmine/field_format.rb +++ b/lib/redmine/field_format.rb @@ -219,7 +219,7 @@ module Redmine def validate_custom_field(custom_field) errors = [] pattern = custom_field.url_pattern - if pattern.present? && !uri_with_safe_scheme?(url_pattern_without_tokens(pattern)) + if pattern.present? && !url_pattern_with_safe_scheme?(pattern) errors << [:url_pattern, :invalid] end errors @@ -309,6 +309,16 @@ module Redmine end protected :url_pattern_without_tokens + # Checks only the scheme because the URL pattern is not a complete URI + # until the tokens are substituted (e.g. "mailto:%value%") + def url_pattern_with_safe_scheme?(url_pattern) + scheme = URI.split(url_pattern_without_tokens(url_pattern)).first + Redmine::Helpers::URL::SAFE_SCHEMES.include?(scheme) + rescue URI::InvalidURIError + false + end + protected :url_pattern_with_safe_scheme? + def edit_tag(view, tag_id, tag_name, custom_value, options={}) view.text_field_tag(tag_name, custom_value.value, options.merge(:id => tag_id)) end diff --git a/lib/redmine/helpers/url.rb b/lib/redmine/helpers/url.rb index af4854c8d..8ff5dbfb5 100644 --- a/lib/redmine/helpers/url.rb +++ b/lib/redmine/helpers/url.rb @@ -22,8 +22,10 @@ require 'uri' module Redmine module Helpers module URL + SAFE_SCHEMES = ['http', 'https', 'ftp', 'mailto', nil].freeze + # safe for resources fetched without user interaction? - def uri_with_safe_scheme?(uri, schemes = ['http', 'https', 'ftp', 'mailto', nil]) + def uri_with_safe_scheme?(uri, schemes = SAFE_SCHEMES) # URLs relative to the current document or document root (without a protocol # separator, should be harmless return true unless uri.to_s.include? ":" diff --git a/test/unit/lib/redmine/field_format/field_format_test.rb b/test/unit/lib/redmine/field_format/field_format_test.rb index 6418917f2..2fe1f99b7 100644 --- a/test/unit/lib/redmine/field_format/field_format_test.rb +++ b/test/unit/lib/redmine/field_format/field_format_test.rb @@ -66,6 +66,11 @@ class Redmine::FieldFormatTest < ActionView::TestCase assert_save field end + def test_should_validate_url_pattern_with_mailto_scheme + field = IssueCustomField.new(:field_format => 'string', :name => 'Email', :url_pattern => 'mailto:%value%') + assert_save field + end + def test_should_not_validate_url_pattern_with_unsafe_scheme field = IssueCustomField.new(:field_format => 'string', :name => 'URL', :url_pattern => 'foo://foo/%value%') assert !field.save -- 2.55.0