http.rb 1.3.5 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b19f251bfdf7beb262c7f6fe8c00950fb580937b1679cef33059346e4695d8a4
4
- data.tar.gz: 6dd0dfa38e25d6805552778398c36df2bf8e42d6f507f23fe8bde2aeb229945c
3
+ metadata.gz: 198e221dc9a1ced43833cfa01886896755615ccc22d9773d0798627d0af1bd45
4
+ data.tar.gz: 53d2e227daa4558d0f810980e2b86ec60fa17c4222c2e61e507c164097c25071
5
5
  SHA512:
6
- metadata.gz: 923a61906a76fd676e1f47808ccc3d295cc9d7c6b5b977d5ec1ff9a496daf8488dfd5f49f1169b650343d66aba9d9994cc166d9caf9d2a64a9e6440c5ac3fb64
7
- data.tar.gz: 672784fee166a077e19e8b79866c24b84793c9aa82c7e4bbe217cbca60565fa0ad241c3d739bb3808108a4caa9ac2f036428943c7d1337327f35b8083ddc737b
6
+ metadata.gz: 2ef160053a5a5069e6be6714daf9229358ece7af8899ec0f5fcc8b98f9b5b4cb05c434d0c3a5a946f7e95a66cf55e19feb918f8096f8793b0bbc25780bd8545a
7
+ data.tar.gz: 0a9721b58212b93670c368496c50bc19f7ea4d53d1b860728b6a3fd5ff8b2d7f81d388cee42a6f969bd65c5ca5584d126d75e1fb5092e35372312e32593c6cd7
data/CHANGELOG CHANGED
@@ -1,5 +1,17 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 20261011
4
+
5
+ 1.3.6: Authorization and Cookie headers go no further than the origin they were given to.
6
+
7
+ A 307 or 308 was followed with every header it was given, so an Authorization or Cookie header meant for one host was sent to whichever host the Location named. A 301, 302, or 303 sent no headers on at all, so it did not leak, though only because it lost everything else too. Following curl and browsers, credentials now stay with their origin: the same scheme, host, and port.
8
+
9
+ 1. + lib/HTTP/REDIRECT.rb: HTTP::REDIRECT::CREDENTIAL_HEADERS (Authorization, Cookie); HTTP.same_origin?, and HTTP.redirect_headers, which drops the credential headers, whatever their case, where a redirect goes to another origin.
10
+ 2. ~ HTTP.request: a 307 or 308 is followed with redirect_headers rather than the headers as given.
11
+ 3. + test/HTTP/REDIRECT_test.rb: credentials kept for the same origin, and dropped for another host by 307 and by 308, for another scheme, and for another port, with other headers carried on. All but the first fail against 1.3.5.
12
+ 4. ~ HTTP::VERSION: /1.3.5/1.3.6/
13
+
14
+
3
15
  ## 20261011
4
16
 
5
17
  1.3.5: Non-ASCII is URL-encoded in full, and without a warning.
@@ -0,0 +1,20 @@
1
+ # HTTP/REDIRECT.rb
2
+ # HTTP::REDIRECT (redirect helpers)
3
+
4
+ module HTTP
5
+ module REDIRECT
6
+ CREDENTIAL_HEADERS = %w{authorization cookie}.freeze
7
+ end
8
+
9
+ def same_origin?(a, b)
10
+ [a.scheme, a.host, a.port] == [b.scheme, b.host, b.port]
11
+ end
12
+ module_function :same_origin?
13
+
14
+ # Credentials are for the origin they were given to, so they go no further than it.
15
+ def redirect_headers(headers, from_uri, to_uri)
16
+ return headers if same_origin?(from_uri, to_uri)
17
+ headers.reject{|k, v| REDIRECT::CREDENTIAL_HEADERS.include?(k.downcase)}
18
+ end
19
+ module_function :redirect_headers
20
+ end
data/lib/HTTP/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # HTTP::VERSION
3
3
 
4
4
  module HTTP
5
- VERSION = '1.3.5'
5
+ VERSION = '1.3.6'
6
6
  end
data/lib/HTTP/request.rb CHANGED
@@ -9,6 +9,7 @@ require_relative '../Net/HTTP/set_options'
9
9
  require_relative '../Net/HTTPRequest/set_headers'
10
10
  require_relative '../Net/HTTPResponse/StatusPredicates'
11
11
  require_relative '../URI/Generic/use_sslQ'
12
+ require_relative './REDIRECT'
12
13
  require_relative './RETRY'
13
14
 
14
15
  module HTTP
@@ -46,7 +47,7 @@ module HTTP
46
47
  redirect_uri = uri.merge(response['location'])
47
48
  if response.code =~ /^30[78]$/
48
49
  data = METHODS::WITH_BODY.include?(request_method) ? request_object.body : {}
49
- response = send(request_method, redirect_uri.to_s, data, headers, options, &block)
50
+ response = send(request_method, redirect_uri.to_s, data, redirect_headers(headers, uri, redirect_uri), options, &block)
50
51
  else
51
52
  response = get(redirect_uri.to_s, {}, {}, options, &block)
52
53
  end
@@ -0,0 +1,40 @@
1
+ # test/HTTP/REDIRECT_test.rb
2
+
3
+ require_relative '../helper'
4
+
5
+ describe "redirect behaviour" do
6
+ let(:uri){'http://example.com/path'}
7
+ let(:same_origin_uri){'http://example.com/elsewhere'}
8
+ let(:other_origin_uri){'http://redirected.com/path'}
9
+
10
+ describe "credentials" do
11
+ it "are kept for the same origin" do
12
+ stub_request(:get, uri).to_return(status: 307, headers: {'location' => same_origin_uri})
13
+ stub_request(:get, same_origin_uri).with(headers: {'Authorization' => 'Bearer x', 'Cookie' => 'session=1'}).to_return(status: 200)
14
+ _(HTTP.get(uri, {}, {'Authorization' => 'Bearer x', 'Cookie' => 'session=1'}).success?).must_equal(true)
15
+ end
16
+
17
+ %w{307 308}.each do |code|
18
+ it "are dropped for another origin via #{code}" do
19
+ stub_request(:get, uri).to_return(status: code.to_i, headers: {'location' => other_origin_uri})
20
+ stub_request(:get, other_origin_uri).to_return(status: 200)
21
+ HTTP.get(uri, {}, {'Authorization' => 'Bearer x', 'cookie' => 'session=1', 'User-Agent' => 'Redback'})
22
+ assert_requested(:get, other_origin_uri){|request| !request.headers.key?('Authorization') && !request.headers.key?('Cookie') && request.headers['User-Agent'] == 'Redback'}
23
+ end
24
+ end
25
+
26
+ it "are dropped for another scheme" do
27
+ stub_request(:get, uri).to_return(status: 307, headers: {'location' => 'https://example.com/path'})
28
+ stub_request(:get, 'https://example.com/path').to_return(status: 200)
29
+ HTTP.get(uri, {}, {'Cookie' => 'session=1'})
30
+ assert_requested(:get, 'https://example.com/path'){|request| !request.headers.key?('Cookie')}
31
+ end
32
+
33
+ it "are dropped for another port" do
34
+ stub_request(:get, uri).to_return(status: 307, headers: {'location' => 'http://example.com:8080/path'})
35
+ stub_request(:get, 'http://example.com:8080/path').to_return(status: 200)
36
+ HTTP.get(uri, {}, {'Cookie' => 'session=1'})
37
+ assert_requested(:get, 'http://example.com:8080/path'){|request| !request.headers.key?('Cookie')}
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.5
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -95,6 +95,7 @@ files:
95
95
  - http.rb.gemspec
96
96
  - lib/HTTP.rb
97
97
  - lib/HTTP/METHODS.rb
98
+ - lib/HTTP/REDIRECT.rb
98
99
  - lib/HTTP/RETRY.rb
99
100
  - lib/HTTP/VERSION.rb
100
101
  - lib/HTTP/request.rb
@@ -108,6 +109,7 @@ files:
108
109
  - lib/Thoran/String/Urlencode/urlencode.rb
109
110
  - lib/URI/Generic/use_sslQ.rb
110
111
  - test/HTTP/METHODS_test.rb
112
+ - test/HTTP/REDIRECT_test.rb
111
113
  - test/HTTP/RETRY_test.rb
112
114
  - test/HTTP/VERSION_test.rb
113
115
  - test/HTTP/basic_auth_test.rb