Skip to content
Browse files

Allow our govspeak helper to deal with partial URLs.

We were seeing an exception in production because someone had added a
policy containing a partial URL in markdown, something like `[link
text](http://)`. This was causing Addressable to fail with a "Absolute
URI missing hierarchical segment" exception when it tried to parse the
link. Given that our parsing only cares about finding valid admin links,
I think we can safely ignore any invalid URLs.
  • Loading branch information...
1 parent 4fef4b1 commit f323367969a312dd60ec6a2f3f50a5a2c149f518 @chrisroos chrisroos committed Jan 31, 2012
Showing with 16 additions and 1 deletion.
  1. +6 −1 app/helpers/govspeak_helper.rb
  2. +10 −0 test/unit/helpers/govspeak_helper_test.rb
View
7 app/helpers/govspeak_helper.rb
@@ -67,7 +67,12 @@ def markup_to_nokogiri_doc(text)
end
def is_internal_admin_link?(href)
- uri = Addressable::URI.parse(href)
+ begin
+ uri = Addressable::URI.parse(href)
+ rescue Addressable::URI::InvalidURIError
+ return false
+ end
+
return false unless %w(http https).include?(uri.scheme)
truncated_link_uri = [uri.host, uri.path.split("/")[1,2]].join("/")
truncated_host_uri = [request.host + Whitehall.router_prefix, "admin"].join("/")
View
10 test/unit/helpers/govspeak_helper_test.rb
@@ -46,6 +46,16 @@ class GovspeakHelperTest < ActionView::TestCase
assert_govspeak %{<p>no <a href="not%20a%20valid%20url">change</a></p>}, html
end
+ test "should not alter partial urls in the admin preview" do
+ html = govspeak_to_admin_html("no [change](http://)")
+ assert_govspeak %{<p>no <a href="http://">change</a></p>}, html
+ end
+
+ test "should not alter partial urls" do
+ html = govspeak_to_html("no [change](http://)")
+ assert_govspeak %{<p>no <a href="http://">change</a></p>}, html
+ end
+
test "should rewrite link to draft document in admin preview" do
publication = create(:draft_publication)
html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")

0 comments on commit f323367

Please sign in to comment.
Something went wrong with that request. Please try again.