Skip to content

Commit

Permalink
Merge pull request #544 from alphagov/fix-encoding-exceptions
Browse files Browse the repository at this point in the history
Fix encoding exceptions
  • Loading branch information
alanth committed Sep 29, 2016
2 parents e6fa54d + dd5c6ca commit 38e8e40
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Expand Up @@ -47,6 +47,10 @@ def noindex_page?
!params.values_at(:controller, :action).in?(INDEXED_PAGES)
end

def original_url
request.original_url.force_encoding('utf-8')
end

private

def referer_url
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/cache_helper.rb
Expand Up @@ -77,7 +77,7 @@ def site_updated_at
end

def url
request.original_url
request.original_url.force_encoding('utf-8')
end

def for(keys)
Expand Down
2 changes: 1 addition & 1 deletion app/views/application/_local_search.html.erb
@@ -1,4 +1,4 @@
<%= form_tag local_petitions_path, class: 'search-inline', method: 'get' do %>
<%= form_tag local_petitions_path, class: 'search-inline', method: 'get', enforce_utf8: false do %>
<%= label_tag 'postcode', 'UK postcode', class: 'form-label' %>
<%= search_field_tag 'postcode', @postcode, class: 'form-control' %>
<%= submit_tag 'Search', class: 'inline-submit', name: nil %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/application/_social_meta.html.erb
Expand Up @@ -15,7 +15,7 @@
<%= open_graph_tag 'title', :title, petition: @petition.action %>
<%= open_graph_tag 'description', @petition.background %>
<% else %>
<%= open_graph_tag 'url', request.original_url %>
<%= open_graph_tag 'url', original_url %>
<%= open_graph_tag 'type', 'website' %>
<%= open_graph_tag 'title', page_title %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/archived/petitions/index.html.erb
Expand Up @@ -3,7 +3,7 @@
<h1 class="page-title">Archived petitions</h1>
<p class="text-secondary">Petitions from the 2010–2015 Conservative – Liberal Democrat coalition government</p>

<%= form_tag archived_petitions_path, class: 'search-inline', method: 'get' do %>
<%= form_tag archived_petitions_path, class: 'search-inline', method: 'get', enforce_utf8: false do %>
<%= search_field_tag 'q', @petitions.query, class: 'form-control', id: 'search' %>
<%= submit_tag 'Search', name: nil, class: 'inline-submit' %>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/petitions/index.html.erb
Expand Up @@ -19,10 +19,10 @@
<p class="text-secondary"><%= petition_list_header %></p>
<% end %>
<%= form_tag petitions_path, class: 'search-inline', method: 'get' do %>
<%= form_tag petitions_path, class: 'search-inline', method: 'get', enforce_utf8: false do %>
<%= search_field_tag 'q', @petitions.query, class: 'form-control', id: 'search' %>
<%= hidden_field_tag 'state', @petitions.scope %>
<%= submit_tag 'Search', class: 'inline-submit' %>
<%= submit_tag 'Search', name: nil, class: 'inline-submit' %>
<% end %>

<p class="filtered-petition-count"><%= filtered_petition_count(@petitions) %></p>
Expand Down
19 changes: 19 additions & 0 deletions spec/helpers/application_helper_spec.rb
Expand Up @@ -178,4 +178,23 @@
end
end
end

describe "#original_url" do
let(:headers) { helper.request.env }

before do
headers["HTTP_HOST"] = "petition.parliament.uk"
headers["HTTP_X_FORWARDED_PROTO"] = "https"
headers["PATH_INFO"] = "/petitions"
headers["QUERY_STRING"] = "utf8=✓&q=foo".force_encoding('binary')
end

it "returns the original request url" do
expect(helper.original_url).to eq("https://petition.parliament.uk/petitions?utf8=✓&q=foo")
end

it "is encoded as UTF-8" do
expect(helper.original_url.encoding).to eq(Encoding::UTF_8)
end
end
end
10 changes: 10 additions & 0 deletions spec/helpers/cache_helper_spec.rb
Expand Up @@ -181,6 +181,16 @@
expect(helper).to receive(:request).and_return(request)
expect(keys.url).to eq("/petitions/123")
end

context "when the URL isn't encoded properly" do
let(:original_url) { "/petitions?utf=✓&q=foo".force_encoding('binary') }
let(:request) { double(:request, original_url: original_url) }

it "forces the encoding to UTF-8" do
expect(helper).to receive(:request).and_return(request)
expect(keys.url.encoding).to eq(Encoding::UTF_8)
end
end
end

describe "#method_missing" do
Expand Down

0 comments on commit 38e8e40

Please sign in to comment.