Fix uncaught query param encoding errors (#12741)
parent
9edab7afaf
commit
09d54d1f62
@ -0,0 +1,18 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
# See: https://jamescrisp.org/2018/05/28/fixing-invalid-query-parameters-invalid-encoding-in-a-rails-app/ |
||||||
|
|
||||||
|
class HandleBadEncodingMiddleware |
||||||
|
def initialize(app) |
||||||
|
@app = app |
||||||
|
end |
||||||
|
|
||||||
|
def call(env) |
||||||
|
begin |
||||||
|
Rack::Utils.parse_nested_query(env['QUERY_STRING'].to_s) |
||||||
|
rescue Rack::Utils::InvalidParameterError |
||||||
|
env['QUERY_STRING'] = '' |
||||||
|
end |
||||||
|
|
||||||
|
@app.call(env) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,21 @@ |
|||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
RSpec.describe HandleBadEncodingMiddleware do |
||||||
|
let(:app) { double() } |
||||||
|
let(:middleware) { HandleBadEncodingMiddleware.new(app) } |
||||||
|
|
||||||
|
it "request with query string is unchanged" do |
||||||
|
expect(app).to receive(:call).with("PATH" => "/some/path", "QUERY_STRING" => "name=fred") |
||||||
|
middleware.call("PATH" => "/some/path", "QUERY_STRING" => "name=fred") |
||||||
|
end |
||||||
|
|
||||||
|
it "request with no query string is unchanged" do |
||||||
|
expect(app).to receive(:call).with("PATH" => "/some/path") |
||||||
|
middleware.call("PATH" => "/some/path") |
||||||
|
end |
||||||
|
|
||||||
|
it "request with invalid encoding in query string drops query string" do |
||||||
|
expect(app).to receive(:call).with("QUERY_STRING" => "", "PATH" => "/some/path") |
||||||
|
middleware.call("QUERY_STRING" => "q=%2Fsearch%2Fall%Forder%3Ddescending%26page%3D5%26sort%3Dcreated_at", "PATH" => "/some/path") |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue