Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/uri/mailto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def self.build(args)

case tmp[:to]
when Array
tmp[:opaque] = tmp[:to].join(',')
tmp[:opaque] = tmp[:to].map { |addr| encode_to_addrs(addr) }.join(',')
when String
tmp[:opaque] = tmp[:to].dup
tmp[:opaque] = encode_to_addrs(tmp[:to])
else
tmp[:opaque] = ''
end
Expand Down Expand Up @@ -120,6 +120,15 @@ def self.build(args)
super(tmp)
end

# Percent-encode octets that cannot appear raw in a mailto addr-spec.
# '+' is left as-is: RFC 6068 allows it for subaddresses.
def self.encode_to_addrs(str)
str.to_s.b.gsub(/%(?!\h\h)|[^!$&-.0-;=@-Z_a-z~%]/n) { |char|
'%%%02X' % char.ord
}
end
private_class_method :encode_to_addrs

#
# == Description
#
Expand Down
10 changes: 10 additions & 0 deletions test/uri/test_mailto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ def test_build
assert_equal(ok_all, ok.flatten.join("\0"))
end

def test_build_percent_encodes_unsafe_address_characters
address = "#!$%&'*+-/=?^_`{}|@example.org"
uri = URI::MailTo.build(:to => address)
assert_equal(
"mailto:%23!$%25&'*+-%2F=%3F%5E_%60%7B%7D%7C@example.org",
uri.to_s
)
assert_equal(address, uri.to.gsub(/%\h\h/) { |esc| [esc[1, 2]].pack('H2') })
end

def test_initializer
assert_raise(URI::InvalidComponentError) do
URI::MailTo.new('mailto', 'sdmitry:bla', 'localhost', '2000', nil,
Expand Down