Skip to content

Commit ea9e7a4

Browse files
authored
Merge pull request #214 from rails/add-allowed-uri
Add Rails::HTML::Sanitizer.allowed_uri? delegating to Loofah
2 parents cc83f51 + f26dc35 commit ea9e7a4

6 files changed

Lines changed: 30 additions & 12 deletions

File tree

.rubocop.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ Performance:
2323
Exclude:
2424
- '**/test/**/*'
2525

26-
# Prefer assert_not over assert !
27-
Rails/AssertNot:
28-
Include:
29-
- '**/test/**/*'
30-
31-
# Prefer assert_not_x over refute_x
32-
Rails/RefuteMethods:
33-
Include:
34-
- '**/test/**/*'
35-
3626
Rails/IndexBy:
3727
Enabled: true
3828

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## unreleased
2+
3+
* Add `Rails::HTML::Sanitizer.allowed_uri?` which delegates to `Loofah::HTML5::Scrub.allowed_uri?`,
4+
allowing the Rails framework to check URI safety without a direct dependency on Loofah.
5+
6+
The minimum Loofah dependency is now `~> 2.25`.
7+
8+
*Mike Dalessio*
9+
10+
111
## v1.6.2 / 2024-12-12
212

313
* `PermitScrubber` fully supports frozen "allowed tags".

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ PATH
22
remote: .
33
specs:
44
rails-html-sanitizer (1.7.0.dev)
5-
loofah (~> 2.21)
5+
loofah (~> 2.25)
66
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
77

88
GEM

lib/rails/html/sanitizer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def html5_support?
1313
def best_supported_vendor
1414
html5_support? ? Rails::HTML5::Sanitizer : Rails::HTML4::Sanitizer
1515
end
16+
17+
def allowed_uri?(uri_string)
18+
Loofah::HTML5::Scrub.allowed_uri?(uri_string)
19+
end
1620
end
1721

1822
def sanitize(html, options = {})

rails-html-sanitizer.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
2626
spec.test_files = Dir["test/**/*"]
2727
spec.require_paths = ["lib"]
2828

29-
spec.add_dependency "loofah", "~> 2.21"
29+
spec.add_dependency "loofah", "~> 2.25"
3030

3131
# A fix was shipped in nokogiri v1.15.7 and v1.16.8 without which there is a vulnerability in this gem.
3232
spec.add_dependency "nokogiri", [">=1.15.7",

test/rails_api_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,18 @@ def test_html5_white_list_sanitizer
8484
skip("no HTML5 support on this platform") unless Rails::HTML::Sanitizer.html5_support?
8585
assert_equal(Rails::HTML5::SafeListSanitizer, Rails::HTML5::Sanitizer.white_list_sanitizer)
8686
end
87+
88+
def test_allowed_uri_returns_true_for_allowed_protocols
89+
assert(Rails::HTML::Sanitizer.allowed_uri?("https://example.com"))
90+
assert(Rails::HTML::Sanitizer.allowed_uri?("http://example.com"))
91+
assert(Rails::HTML::Sanitizer.allowed_uri?("mailto:user@example.com"))
92+
end
93+
94+
def test_allowed_uri_returns_false_for_disallowed_protocols
95+
refute(Rails::HTML::Sanitizer.allowed_uri?("javascript:alert(1)"))
96+
end
97+
98+
def test_allowed_uri_returns_true_for_relative_uris
99+
assert(Rails::HTML::Sanitizer.allowed_uri?("/relative/path"))
100+
end
87101
end

0 commit comments

Comments
 (0)