padrino-helpers 0.18.5 → 0.18.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c5c37d89bcdd281670618e9b8a73de547ac13eb5dfeddf244041ddd17e1313f
4
- data.tar.gz: 9366933626446a8da507877eacd11be68081346155be8fc3fd6228699ab82ba4
3
+ metadata.gz: e6ed027bb5dfe6ab9d40aff832528d42f74b8b8af6edd743088de049671e484d
4
+ data.tar.gz: e41ac27da5e82874d96604ff149e61f411106af71d2b38b5270b683ec68eeda9
5
5
  SHA512:
6
- metadata.gz: bb3b17cfc94e65eb11f9b264f805b56eb8226898ea89ae0394b42fb5afbcb12720c79cfefb42d1f3ee9c06ed96dd720fbad749e28ac181952230a6d058d7ba51
7
- data.tar.gz: efcb8495fd971b06303fcfa38c1bb6fd53385338fe1205384d35ab80501476075cf79bab7cd9e876685b67587ccd6181fcc0f7098f5562106daf506e86a873f5
6
+ metadata.gz: 810548985ca4feacab73f229e6de1b0fca30cb863e19756449ccdc27e4646837da5238ee35038da3edd45bfc3f43fdabd1b956ca015640692dc1861f1919bc1b
7
+ data.tar.gz: a8f269847b149528001c8e12674552eefa0737aa67df19767a193eea2586c8232c793c7fa1864e4c6b64e2f130143b92d0dcabb736556f68bf1d9754af223b75
@@ -6,8 +6,9 @@ module Padrino
6
6
  #
7
7
  class SafeBuffer < String
8
8
  UNSAFE_STRING_METHODS = %w[
9
- capitalize chomp chop delete downcase gsub lstrip next reverse rstrip
10
- slice squeeze strip sub succ swapcase tr tr_s upcase
9
+ capitalize chomp chop delete delete_prefix delete_suffix downcase encode
10
+ gsub lstrip next reverse rstrip scrub slice squeeze strip sub succ
11
+ swapcase tr tr_s unicode_normalize upcase
11
12
  ]
12
13
 
13
14
  alias original_concat concat
@@ -59,6 +60,38 @@ module Padrino
59
60
  super(html_escape_interpolated_argument(value))
60
61
  end
61
62
 
63
+ def insert(index, value)
64
+ super(index, html_escape_interpolated_argument(value))
65
+ end
66
+
67
+ def []=(*args)
68
+ args[-1] = html_escape_interpolated_argument(args[-1]) unless args.empty?
69
+ super(*args)
70
+ end
71
+
72
+ def replace(value)
73
+ super(html_escape_interpolated_argument(value))
74
+ end
75
+
76
+ def setbyte(index, integer)
77
+ @html_safe = false
78
+ super
79
+ end
80
+
81
+ if String.method_defined?(:bytesplice)
82
+ def bytesplice(*args)
83
+ @html_safe = false
84
+ super
85
+ end
86
+ end
87
+
88
+ if String.method_defined?(:append_as_bytes)
89
+ def append_as_bytes(*args)
90
+ @html_safe = false
91
+ super
92
+ end
93
+ end
94
+
62
95
  def +(other)
63
96
  dup.concat(other)
64
97
  end
@@ -95,14 +128,14 @@ module Padrino
95
128
  next unless unsafe_method.respond_to?(unsafe_method)
96
129
 
97
130
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
98
- def #{unsafe_method}(*args, &block) # def capitalize(*args, &block)
99
- to_str.#{unsafe_method}(*args, &block) # to_str.capitalize(*args, &block)
100
- end # end
101
-
102
- def #{unsafe_method}!(*args) # def capitalize!(*args)
103
- @html_safe = false # @html_safe = false
104
- super # super
105
- end # end
131
+ def #{unsafe_method}(...) # def capitalize(...)
132
+ to_str.#{unsafe_method}(...) # to_str.capitalize(...)
133
+ end # end
134
+
135
+ def #{unsafe_method}!(...) # def capitalize!(...)
136
+ @html_safe = false # @html_safe = false
137
+ super(...) # super(...)
138
+ end # end
106
139
  RUBY
107
140
  end
108
141
 
@@ -86,6 +86,7 @@ module Padrino
86
86
  name = block_given? ? '' : args.shift
87
87
  href = args.first
88
88
  options = { href: href ? escape_link(href) : '#' }.update(options)
89
+ options.keys.select { |key| key != :href && key.to_s.casecmp?('href') }.each { |key| options[:href] = options.delete(key) }
89
90
  options[:href] = escape_link(options[:href]) if options[:href]
90
91
  return name unless parse_conditions(href, options)
91
92
  block_given? ? content_tag(:a, options, &block) : content_tag(:a, name, options)
@@ -94,6 +94,10 @@ module Padrino
94
94
  protect_from_csrf: is_protected_from_csrf?,
95
95
  'accept-charset' => 'UTF-8'
96
96
  }.update(options)
97
+ %i[action method multipart].each do |opt|
98
+ name = opt.to_s
99
+ options.keys.select { |key| key != opt && key.to_s.casecmp?(name) }.each { |key| options[opt] = options.delete(key) }
100
+ end
97
101
  options[:action] = escape_link(options[:action]) if options[:action]
98
102
  options[:enctype] = 'multipart/form-data' if options.delete(:multipart)
99
103
 
@@ -349,6 +349,25 @@ module Padrino
349
349
  distance_of_time_in_words(from_time, Time.now, include_seconds)
350
350
  end
351
351
 
352
+ JS_ESCAPE_MAP = {
353
+ '\\' => '\\\\',
354
+ '</' => '<\/',
355
+ "\r\n" => '\n',
356
+ "\n" => '\n',
357
+ "\r" => '\n',
358
+ '"' => '\\"',
359
+ "'" => "\\'",
360
+ '`' => '\\`',
361
+ '$' => '\\$'
362
+ }.freeze
363
+ JS_ESCAPE_REGEXP = %r{(\\|</|\r\n|[\n\r"'`$])}.freeze
364
+
365
+ JS_UTF8_ESCAPE_MAP = {
366
+ "\u2028" => '\u2028',
367
+ "\u2029" => '\u2029'
368
+ }.freeze
369
+ JS_UTF8_ESCAPE_REGEXP = /[\u2028\u2029]/.freeze
370
+
352
371
  ##
353
372
  # Used in xxxx.js.erb files to escape html so that it can be passed to javascript from Padrino.
354
373
  #
@@ -362,8 +381,9 @@ module Padrino
362
381
  #
363
382
  def js_escape_html(html_content)
364
383
  return '' unless html_content
365
- javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
366
- escaped_content = html_content.gsub(%r{(\\|</|\r\n|[\n\r"'])}) { |m| javascript_mapping[m] }
384
+
385
+ escaped_content = html_content.gsub(JS_ESCAPE_REGEXP, JS_ESCAPE_MAP)
386
+ escaped_content = escaped_content.gsub(JS_UTF8_ESCAPE_REGEXP, JS_UTF8_ESCAPE_MAP) if escaped_content.encoding == Encoding::UTF_8
367
387
  escaped_content = escaped_content.html_safe if html_content.html_safe?
368
388
  escaped_content
369
389
  end
@@ -101,6 +101,21 @@ describe 'AssetTagHelpers' do
101
101
  assert_html_has_tag(actual_link, 'a', href: '#', content: 'click')
102
102
  end
103
103
 
104
+ it 'should normalize string and indifferent href option keys' do
105
+ [
106
+ { 'href' => 'javascript:alert(1)' },
107
+ { href: nil, 'href' => 'javascript:alert(1)' },
108
+ { 'HREF' => 'javascript:alert(1)' },
109
+ Sinatra::IndifferentHash['href' => 'javascript:alert(1)']
110
+ ].each do |opts|
111
+ actual_link = link_to('click', '/safe', opts)
112
+ assert_equal '<a href="#">click</a>', actual_link
113
+ end
114
+
115
+ frozen_opts = { 'href' => '/custom' }.freeze
116
+ assert_equal '<a href="/custom">click</a>', link_to('click', '/safe', frozen_opts)
117
+ end
118
+
104
119
  it 'should allow http and https URIs' do
105
120
  assert_match 'href="http://example.com"', link_to('click', 'http://example.com')
106
121
  assert_match 'href="https://example.com"', link_to('click', 'https://example.com')
@@ -24,6 +24,65 @@ describe 'FormHelpers' do
24
24
  assert_html_has_tag(actual_html, :form, action: '#')
25
25
  end
26
26
 
27
+ it 'should normalize string and indifferent action option keys' do
28
+ [
29
+ { 'action' => 'javascript:alert(1)' },
30
+ { action: nil, 'action' => 'javascript:alert(1)' },
31
+ { 'ACTION' => 'javascript:alert(1)' },
32
+ Sinatra::IndifferentHash['action' => 'javascript:alert(1)']
33
+ ].each do |opts|
34
+ @dom = nil
35
+ actual_html = form_tag('/safe', opts) { 'Demo' }
36
+ assert_html_has_tag(actual_html, :form, action: '#')
37
+ refute_match(/javascript:/i, actual_html)
38
+ assert_equal 1, actual_html.scan(/\baction=/i).size
39
+ end
40
+
41
+ frozen_opts = { 'action' => '/custom' }.freeze
42
+ custom_html = form_tag('/safe', frozen_opts) { 'Demo' }
43
+ assert_includes custom_html, 'action="/custom"'
44
+ assert_equal 1, custom_html.scan(/\baction=/i).size
45
+
46
+ @dom = nil
47
+ button_html = button_to('Go', '/safe', 'action' => 'javascript:alert(1)')
48
+ assert_html_has_tag(button_html, :form, action: '#')
49
+ refute_match(/javascript:/i, button_html)
50
+ assert_equal 1, button_html.scan(/\baction=/i).size
51
+ end
52
+
53
+ it 'should normalize string and indifferent method option keys' do
54
+ [
55
+ { 'method' => 'get' },
56
+ { 'METHOD' => 'get' },
57
+ Sinatra::IndifferentHash['method' => 'get']
58
+ ].each do |opts|
59
+ @dom = nil
60
+ get_html = form_tag('/search', opts) { 'Demo' }
61
+ assert_html_has_tag(get_html, :form, method: 'get')
62
+ assert_equal 1, get_html.scan(/\bmethod=/i).size
63
+ assert_html_has_no_tag(get_html, :input, name: 'authenticity_token')
64
+ end
65
+
66
+ @dom = nil
67
+ put_html = form_tag('/update', 'method' => 'put') { 'Demo' }
68
+ assert_html_has_tag(put_html, :form, method: 'post')
69
+ assert_equal 1, put_html.scan(/\bmethod=/i).size
70
+ assert_html_has_tag(put_html, 'form input', type: 'hidden', name: '_method', value: 'put')
71
+ end
72
+
73
+ it 'should normalize string and indifferent multipart option keys' do
74
+ [
75
+ { 'multipart' => true },
76
+ { 'MULTIPART' => true },
77
+ Sinatra::IndifferentHash['multipart' => true]
78
+ ].each do |opts|
79
+ @dom = nil
80
+ mp_html = form_tag('/upload', opts) { 'Demo' }
81
+ assert_html_has_tag(mp_html, :form, enctype: 'multipart/form-data')
82
+ refute_match(/\bmultipart=/i, mp_html)
83
+ end
84
+ end
85
+
27
86
  it 'should allow relative actions with colons in the query' do
28
87
  actual_html = form_tag('search?q=a:b') { 'Demo' }
29
88
  assert_html_has_tag(actual_html, :form, action: 'search?q=a:b')
@@ -306,5 +306,30 @@ describe 'FormatHelpers' do
306
306
  refute js_escape_html('"hello"').html_safe?
307
307
  assert js_escape_html(SafeBuffer.new('"hello"')).html_safe?
308
308
  end
309
+
310
+ it 'should escape backticks, dollar signs, and Unicode line separators' do
311
+ assert_equal '\`hello \${name}\`', js_escape_html('`hello ${name}`')
312
+ assert_equal 'a\u2028b\u2029c', js_escape_html("a\u2028b\u2029c")
313
+ escaped = js_escape_html(SafeBuffer.new("`a\u2028$b`"))
314
+ assert_equal '\`a\u2028\$b\`', escaped
315
+ assert escaped.html_safe?
316
+ end
317
+
318
+ it 'should escape ASCII characters in non-UTF-8 strings without raising' do
319
+ bin = "caf\xC3\xA9 'x' `y` $z".b
320
+ escaped_bin = js_escape_html(bin)
321
+ assert_equal Encoding::ASCII_8BIT, escaped_bin.encoding
322
+ assert_equal "caf\xC3\xA9 \\'x\\' \\`y\\` \\$z".b, escaped_bin
323
+
324
+ latin1 = "caf\xE9 'x' `y` $z".dup.force_encoding(Encoding::ISO_8859_1)
325
+ escaped_latin1 = js_escape_html(latin1)
326
+ assert_equal Encoding::ISO_8859_1, escaped_latin1.encoding
327
+ assert_equal "caf\xE9 \\'x\\' \\`y\\` \\$z".dup.force_encoding(Encoding::ISO_8859_1), escaped_latin1
328
+
329
+ sjis = "\x82\xA0 'x' `y`".dup.force_encoding(Encoding::Shift_JIS)
330
+ escaped_sjis = js_escape_html(sjis)
331
+ assert_equal Encoding::Shift_JIS, escaped_sjis.encoding
332
+ assert_equal "\x82\xA0 \\'x\\' \\`y\\`".dup.force_encoding(Encoding::Shift_JIS), escaped_sjis
333
+ end
309
334
  end
310
335
  end
@@ -0,0 +1,96 @@
1
+ require File.expand_path('helper', __dir__)
2
+
3
+ describe 'Padrino::SafeBuffer mutations' do
4
+ it 'should escape plain strings passed to insert' do
5
+ buffer = Padrino::SafeBuffer.new('<p></p>')
6
+ buffer.insert(3, '<b>x</b>')
7
+ assert_equal '<p>&lt;b&gt;x&lt;/b&gt;</p>', buffer
8
+ assert_predicate buffer, :html_safe?
9
+
10
+ safe = Padrino::SafeBuffer.new('<p></p>')
11
+ safe.insert(3, Padrino::SafeBuffer.new('<b>x</b>'))
12
+ assert_equal '<p><b>x</b></p>', safe
13
+ assert_predicate safe, :html_safe?
14
+ end
15
+
16
+ it 'should escape plain strings passed to []=' do
17
+ buffer = Padrino::SafeBuffer.new('<p>X</p>')
18
+ buffer['X'] = '<b>x</b>'
19
+ assert_equal '<p>&lt;b&gt;x&lt;/b&gt;</p>', buffer
20
+ assert_predicate buffer, :html_safe?
21
+
22
+ indexed = Padrino::SafeBuffer.new('<p>X</p>')
23
+ indexed[3, 1] = '<i>y</i>'
24
+ assert_equal '<p>&lt;i&gt;y&lt;/i&gt;</p>', indexed
25
+ assert_predicate indexed, :html_safe?
26
+
27
+ safe = Padrino::SafeBuffer.new('<p>X</p>')
28
+ safe['X'] = Padrino::SafeBuffer.new('<b>x</b>')
29
+ assert_equal '<p><b>x</b></p>', safe
30
+ assert_predicate safe, :html_safe?
31
+ end
32
+
33
+ it 'should escape plain strings passed to replace' do
34
+ buffer = Padrino::SafeBuffer.new('<p></p>')
35
+ buffer.replace('<b>x</b>')
36
+ assert_equal '&lt;b&gt;x&lt;/b&gt;', buffer
37
+ assert_predicate buffer, :html_safe?
38
+
39
+ safe = Padrino::SafeBuffer.new('<p></p>')
40
+ safe.replace(Padrino::SafeBuffer.new('<b>x</b>'))
41
+ assert_equal '<b>x</b>', safe
42
+ assert_predicate safe, :html_safe?
43
+ end
44
+
45
+ it 'should clear html_safe? on setbyte' do
46
+ buffer = Padrino::SafeBuffer.new('<p>X</p>')
47
+ buffer.setbyte(3, 60)
48
+ assert_equal '<p><</p>', buffer
49
+ refute_predicate buffer, :html_safe?
50
+ end
51
+
52
+ it 'should clear html_safe? on bytesplice' do
53
+ skip 'String#bytesplice not defined' unless String.method_defined?(:bytesplice)
54
+
55
+ buffer = Padrino::SafeBuffer.new('<p>X</p>')
56
+ buffer.bytesplice(3, 1, '<b>x</b>')
57
+ refute_predicate buffer, :html_safe?
58
+ end
59
+
60
+ it 'should clear html_safe? on append_as_bytes' do
61
+ skip 'String#append_as_bytes not defined' unless String.method_defined?(:append_as_bytes)
62
+
63
+ buffer = Padrino::SafeBuffer.new('<p>')
64
+ buffer.append_as_bytes('<b>x</b>', 33)
65
+ assert_equal '<p><b>x</b>!', buffer
66
+ refute_predicate buffer, :html_safe?
67
+ end
68
+
69
+ it 'should clear html_safe? for delete_prefix, delete_suffix, scrub, unicode_normalize and encode' do
70
+ refute_predicate Padrino::SafeBuffer.new('<a>').delete_prefix('<'), :html_safe?
71
+ refute_predicate Padrino::SafeBuffer.new('<a>').delete_suffix('>'), :html_safe?
72
+ refute_predicate Padrino::SafeBuffer.new('<a>').scrub, :html_safe?
73
+ refute_predicate Padrino::SafeBuffer.new('<a>').unicode_normalize, :html_safe?
74
+ refute_predicate Padrino::SafeBuffer.new('<a>').encode('UTF-8', invalid: :replace), :html_safe?
75
+
76
+ buf1 = Padrino::SafeBuffer.new('<a>')
77
+ buf1.delete_prefix!('<')
78
+ refute_predicate buf1, :html_safe?
79
+
80
+ buf2 = Padrino::SafeBuffer.new('<a>')
81
+ buf2.delete_suffix!('>')
82
+ refute_predicate buf2, :html_safe?
83
+
84
+ buf3 = Padrino::SafeBuffer.new('<a>')
85
+ buf3.scrub!
86
+ refute_predicate buf3, :html_safe?
87
+
88
+ buf4 = Padrino::SafeBuffer.new('<a>')
89
+ buf4.unicode_normalize!
90
+ refute_predicate buf4, :html_safe?
91
+
92
+ buf5 = Padrino::SafeBuffer.new('<a>')
93
+ buf5.encode!('UTF-8', invalid: :replace)
94
+ refute_predicate buf5, :html_safe?
95
+ end
96
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.5
4
+ version: 0.18.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2026-11-06 00:00:00.000000000 Z
14
+ date: 2026-11-24 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: padrino-support
@@ -19,14 +19,14 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 0.18.5
22
+ version: 0.18.6
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.18.5
29
+ version: 0.18.6
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: tilt
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -231,6 +231,7 @@ files:
231
231
  - test/test_mounted_asset_stamp.rb
232
232
  - test/test_number_helpers.rb
233
233
  - test/test_output_helpers.rb
234
+ - test/test_output_safety.rb
234
235
  - test/test_render_helpers.rb
235
236
  - test/test_rendering.rb
236
237
  - test/test_rendering_extensions.rb