From b0e85f254cb90fa759077fc55697c0fc9825eec4 Mon Sep 17 00:00:00 2001 From: Jack Li Date: Thu, 21 May 2020 16:12:27 -0700 Subject: [PATCH] Clear last_response on errors When an error occurs, the last_response does not get updated so it's possible to start inspecting the last last response. This change makes it so that if an error is raised, clear the last_response cache to prevent leaky reads --- lib/octokit/connection.rb | 5 ++++- spec/octokit/client_spec.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/octokit/connection.rb b/lib/octokit/connection.rb index 6877e848e..3d3e47781 100644 --- a/lib/octokit/connection.rb +++ b/lib/octokit/connection.rb @@ -155,6 +155,9 @@ def request(method, path, data, options = {}) @last_response = response = agent.call(method, Addressable::URI.parse(path.to_s).normalize.to_s, data, options) response.data + rescue Octokit::Error => error + @last_response = nil + raise error end # Executes the request, checking if it was successful @@ -177,7 +180,7 @@ def sawyer_options conn_opts[:proxy] = @proxy if @proxy if conn_opts[:ssl].nil? conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode - else + else if @connection_options[:ssl][:verify] == false conn_opts[:ssl] = { :verify_mode => 0} else diff --git a/spec/octokit/client_spec.rb b/spec/octokit/client_spec.rb index 3f3f08713..76027128d 100644 --- a/spec/octokit/client_spec.rb +++ b/spec/octokit/client_spec.rb @@ -932,6 +932,22 @@ expect { Octokit.get('/user') }.to raise_error Octokit::ServerError end + it "resets last_response on errors" do + stub_get('/booya').to_return(:status => 200) + stub_get('/user').to_return \ + :status => 509, + :headers => { + :content_type => "application/json", + }, + :body => {:message => "Bandwidth exceeded"}.to_json + + client = Octokit.client + client.get('/booya') + expect(client.last_response).to_not be_nil + expect { client.get('/user') }.to raise_error Octokit::ServerError + expect(client.last_response).to be_nil + end + it "handles documentation URLs in error messages" do stub_get('/user').to_return \ :status => 415,