Skip to content

Commit

Permalink
Relaxed validation for width and height options - #546
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 5, 2020
1 parent 3de1865 commit 1092181
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.4.1 (unreleased)

- Relaxed validation for `width` and `height` options

## 3.4.0 (2020-08-04)

- Fixed CSS injection with `width` and `height` options - [more info](https://github.com/ankane/chartkick/issues/546)
Expand Down
3 changes: 2 additions & 1 deletion lib/chartkick/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def chartkick_chart(klass, data_source, **options)
css_vars.each_key do |k|
# limit to alphanumeric and % for simplicity
# this prevents things like calc() but safety is the priority
raise ArgumentError, "Invalid #{k}" unless css_vars[k] =~ /\A[a-zA-Z0-9%]*\z/
# dot does not need escaped in square brackets
raise ArgumentError, "Invalid #{k}" unless css_vars[k] =~ /\A[a-zA-Z0-9%.]*\z/
# we limit above, but escape for safety as fail-safe
# to prevent XSS injection in worse-case scenario
css_vars[k] = ERB::Util.html_escape(css_vars[k])
Expand Down
8 changes: 8 additions & 0 deletions test/chartkick_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def test_height_percent
assert_match "height: 100%;", line_chart(@data, height: "100%")
end

def test_height_dot
assert_match "height: 2.5rem;", line_chart(@data, height: "2.5rem")
end

def test_height_quote
error = assert_raises(ArgumentError) do
line_chart(@data, height: "150px\"")
Expand All @@ -110,6 +114,10 @@ def test_width_percent
assert_match "width: 100%;", line_chart(@data, width: "100%")
end

def test_width_dot
assert_match "width: 2.5rem;", line_chart(@data, width: "2.5rem")
end

def test_width_quote
error = assert_raises(ArgumentError) do
line_chart(@data, width: "80%\"")
Expand Down

0 comments on commit 1092181

Please # to comment.