Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to whitelist particular functions #122 #123

Merged
merged 1 commit into from Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/loofah/html5/scrub.rb
Expand Up @@ -71,7 +71,7 @@ def scrub_css style
style_tree.each do |node|
next unless node[:node] == :property
next if node[:children].any? do |child|
[:url, :bad_url, :function].include? child[:node]
[:url, :bad_url].include?(child[:node]) || (child[:node] == :function && !WhiteList::ALLOWED_CSS_FUNCTIONS.include?(child[:name].downcase))
end
name = node[:name].downcase
if WhiteList::ALLOWED_CSS_PROPERTIES.include?(name) || WhiteList::ALLOWED_SVG_PROPERTIES.include?(name)
Expand Down
3 changes: 3 additions & 0 deletions lib/loofah/html5/whitelist.rb
Expand Up @@ -137,6 +137,8 @@ module WhiteList
purple red right solid silver teal top transparent underline white
yellow]

ACCEPTABLE_CSS_FUNCTIONS = Set.new %w[calc]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the light of this comment: #123 (comment) I think this should be extended to cover rgb, rgba, hls, hlsa and probably a bunch more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but I'd suggest merging in as it is and raising a separate issue for assessing other functions as defaults.
Until this is merged in, users of loofah can't whitelist any themselves.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the two weeks this PR has been open, I think it wouldn't hurt to make it completely functional. Whitelisting just calc is very very limited. However, it's your PR, so it's yours and maintainers choice :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitelisting many functions by default, opens up the gem as being the root cause of sanitization an XSS attack vector which, unless I'm mistaken, could potentially undermine many Rails projects.
The primary concern of this PR is to allow individuals to whitelist functions themselves, that they can assess in their own time.
I'd be quite happy to even remove calc as a default as long as the gem allowed defining ACCEPTABLE_CSS_FUNCTIONS it would fulfil the functionality aim I had.

Copy link

@pjg pjg Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, for your use-case this is enough. I'm rather thinking of the bigger picture. Gems like this one only make sense if they whitelist all allowed (according to W3C) tags, attributes, css functions, etc. etc. If you had to define all such lists on your own, this gem would be a major PITA to use. Not to mention it would lose all of its security features as a lot of ppl would whitelist stuff which shouldn't be allowed in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling ppl to whitelist to their own demise is exactly what the gem should enable.
Rushing into having a large whitelist could potentially mean undermining the safety of sanitizing strings.

For example if the wrong function is accidently whitelisted, it could mean sanitize(unsafe_html) in all apps running a particular version of Loofah could be targets of a XSS attack.
If each individual is responsible for whitelisting their own functions, it is unlikely that malicious attackers will have a way of identifying the version of Loofah being used through automated testing.

I'm not saying more shouldn't be added to the default function whitelist like the color functions. But before they are, there should be some research done into ways that each function may be used maliciously.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference: https://www.quackit.com/css/functions/ -- all those functions should be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick note: at least url() and image() can be used for arbitrary JS code execution and parameter snooping.
They can also be used to snoop URL params by logging all requests via a remote call.


SHORTHAND_CSS_PROPERTIES = Set.new %w[background border margin padding]

ACCEPTABLE_SVG_PROPERTIES = Set.new %w[fill fill-opacity fill-rule stroke
Expand All @@ -152,6 +154,7 @@ module WhiteList
ALLOWED_ATTRIBUTES = ACCEPTABLE_ATTRIBUTES + MATHML_ATTRIBUTES + SVG_ATTRIBUTES
ALLOWED_CSS_PROPERTIES = ACCEPTABLE_CSS_PROPERTIES
ALLOWED_CSS_KEYWORDS = ACCEPTABLE_CSS_KEYWORDS
ALLOWED_CSS_FUNCTIONS = ACCEPTABLE_CSS_FUNCTIONS
ALLOWED_SVG_PROPERTIES = ACCEPTABLE_SVG_PROPERTIES
ALLOWED_PROTOCOLS = ACCEPTABLE_PROTOCOLS

Expand Down
12 changes: 12 additions & 0 deletions test/html5/test_sanitizer.rb
Expand Up @@ -240,6 +240,18 @@ def test_css_negative_value_sanitization_shorthand_css_properties
assert_match %r/-0.05em/, sane.inner_html
end

def test_css_function_sanitization_leaves_whitelisted_functions
html = "<span style=\"width:calc(5%)\">"
sane = Nokogiri::HTML(Loofah.scrub_fragment(html, :strip).to_html)
assert_match %r/calc\(5%\)/, sane.inner_html
end

def test_css_function_sanitization_strips_style_attributes_with_unsafe_functions
html = "<span style=\"width: attr(data-evil-attr)\">"
sane = Nokogiri::HTML(Loofah.scrub_fragment(html, :strip).to_html)
assert_match %r/<span><\/span>/, sane.inner_html
end

def test_issue_90_slow_regex
skip("timing tests are hard to make pass and have little regression-testing value")

Expand Down