down 5.6.2 → 5.6.3

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: dafd791660ca13ac117b8db2da6a31a38b5c6008735039dcbf4263b60438cf6a
4
- data.tar.gz: 1ce7f027452f7c21421f169be6f967d3a180c3e34940d07c0d41be35fb1015cf
3
+ metadata.gz: 955f59acc793018ef1a03142df1cdd754d4431ab3bd35c7bb14dfa34a406c7c7
4
+ data.tar.gz: e693bea1311a3bb9d115b244b30076d486736b2914d44dc67a0badb1d04cacaf
5
5
  SHA512:
6
- metadata.gz: 32d796dfff756b332fbf688d2560a08820f3636b1e64b7e5545bb896f2955a5c5def44c9866207a7a9898eba0bbb45bee8d8d663e7c1060a43157556624cd176
7
- data.tar.gz: 13fedb0b5618d6c7ff9dda385835e6fb15a9a3d6bdad30db201c4ee216ddb6b508cbad374e7dd48d08df5ff26b39b2ca8e837789f44a6e59df2beab164c705c0
6
+ metadata.gz: 67958d2b712a6ae5932ffb656b6fe60466faa4a43eb12bc617d20801369823cf5d930fb6b21709bc665b22049fa5780b73ebbaff1a00d759e79bbffe1cf7d62c
7
+ data.tar.gz: 56160fe1dad7efe0e3803820afe2137632210b7f442afee5667ceb6753a1378636a917e56cdd745233e5a4eabcdbe512396455eac21416359b35fe3f522f3515
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 5.6.3 (2026-11-21)
2
+
3
+ * Prevent cookie injection via redirects in `Down::NetHttp`: only forward name/value pairs from `Set-Cookie` headers on same-origin redirects, and drop the `Cookie` header on cross-origin redirects unless `:auth_on_redirect` is set (reported by @JR0ch17) (@janko)
4
+
1
5
  ## 5.6.2 (2026-11-01)
2
6
 
3
7
  * Return `nil` `Down::ChunkedIO#size` from `Down::NetHttp.open` when net/http decompresses the body (@wtn)
data/lib/down/net_http.rb CHANGED
@@ -147,45 +147,101 @@ module Down
147
147
 
148
148
  # Calls open-uri's URI::HTTP#open method. Additionally handles redirects.
149
149
  def open_uri(uri, max_redirects:, auth_on_redirect: false, **options)
150
- uri.open(options)
151
- rescue OpenURI::HTTPRedirect => exception
152
- raise Down::TooManyRedirects, "too many redirects" if max_redirects == 0
150
+ # remember cookies provided by the caller, so that we can tell them apart
151
+ # from cookies collected from redirect responses
152
+ user_cookie = cookie_header(options)
153
153
 
154
- # fail if redirect URI scheme is not http or https
155
154
  begin
156
- uri = ensure_uri(exception.uri)
157
- rescue Down::InvalidUrl
155
+ uri.open(options)
156
+ rescue OpenURI::HTTPRedirect => exception
157
+ raise Down::TooManyRedirects, "too many redirects" if max_redirects == 0
158
+
159
+ # fail if redirect URI scheme is not http or https
160
+ begin
161
+ redirect_uri = ensure_uri(exception.uri)
162
+ rescue Down::InvalidUrl
163
+ response = rebuild_response_from_open_uri_exception(exception)
164
+
165
+ raise ResponseError.new("Invalid Redirect URI: #{exception.uri}", response)
166
+ end
167
+
168
+ # do not leak credentials on redirect
169
+ options.delete("Authorization") unless auth_on_redirect
170
+ options.delete(:http_basic_authentication) unless auth_on_redirect
171
+
172
+ if same_origin?(uri, redirect_uri)
173
+ # forward cookies set by the redirect response
174
+ forward_cookies(options, exception.io.metas["set-cookie"])
175
+ else
176
+ # cookies set by the redirect response are scoped to its origin, so
177
+ # we never send them to another origin, and caller's cookies are
178
+ # treated as credentials
179
+ set_cookie_header(options, auth_on_redirect ? user_cookie : nil)
180
+ end
181
+
182
+ uri = redirect_uri
183
+ max_redirects -= 1
184
+ retry
185
+ rescue OpenURI::HTTPError => exception
158
186
  response = rebuild_response_from_open_uri_exception(exception)
159
187
 
160
- raise ResponseError.new("Invalid Redirect URI: #{exception.uri}", response)
188
+ # open-uri attempts to parse the redirect URI, so we re-raise that exception
189
+ if exception.message.include?("(Invalid Location URI)")
190
+ raise ResponseError.new("Invalid Redirect URI: #{response["Location"]}", response)
191
+ end
192
+
193
+ response_error!(response)
194
+ rescue => exception
195
+ request_error!(exception)
161
196
  end
197
+ end
198
+
199
+ # Adds name/value pairs from the given Set-Cookie header values to the
200
+ # Cookie request header, ignoring any cookie attributes.
201
+ def forward_cookies(headers, set_cookies)
202
+ return if set_cookies.nil? || set_cookies.empty?
203
+
204
+ cookies = parse_cookie_header(cookie_header(headers))
205
+
206
+ set_cookies.each do |set_cookie|
207
+ name, value = set_cookie.split(";", 2).first.to_s.split("=", 2).map(&:strip)
208
+ next if value.nil? || name.empty?
162
209
 
163
- # do not leak credentials on redirect
164
- options.delete("Authorization") unless auth_on_redirect
165
- options.delete(:http_basic_authentication) unless auth_on_redirect
166
-
167
- # forward cookies on the redirect
168
- if !exception.io.meta["set-cookie"].to_s.empty?
169
- options["Cookie"] ||= ''
170
- # Add new cookies avoiding duplication
171
- new_cookies = exception.io.meta["set-cookie"].to_s.split(';').map(&:strip)
172
- old_cookies = options["Cookie"].split(';')
173
- options["Cookie"] = (old_cookies | new_cookies).join(';')
210
+ cookies[name] = value
174
211
  end
175
212
 
176
- max_redirects -= 1
177
- retry
178
- rescue OpenURI::HTTPError => exception
179
- response = rebuild_response_from_open_uri_exception(exception)
213
+ set_cookie_header(headers, cookies.map { |name, value| value ? "#{name}=#{value}" : name }.join("; "))
214
+ end
180
215
 
181
- # open-uri attempts to parse the redirect URI, so we re-raise that exception
182
- if exception.message.include?("(Invalid Location URI)")
183
- raise ResponseError.new("Invalid Redirect URI: #{response["Location"]}", response)
216
+ # Parses the Cookie request header value into a hash.
217
+ def parse_cookie_header(value)
218
+ value.to_s.split(";").each_with_object({}) do |pair, cookies|
219
+ name, value = pair.split("=", 2).map(&:strip)
220
+ cookies[name] = value unless name.nil? || name.empty?
184
221
  end
222
+ end
185
223
 
186
- response_error!(response)
187
- rescue => exception
188
- request_error!(exception)
224
+ # Returns the Cookie request header value from the given headers.
225
+ def cookie_header(headers)
226
+ headers.find { |name, _| cookie_header?(name) }&.last
227
+ end
228
+
229
+ # Sets the Cookie request header value in the given headers, removing it
230
+ # if the value is nil or empty.
231
+ def set_cookie_header(headers, value)
232
+ headers.delete_if { |name, _| cookie_header?(name) }
233
+ headers["Cookie"] = value unless value.nil? || value.empty?
234
+ end
235
+
236
+ def cookie_header?(name)
237
+ name.is_a?(String) && name.casecmp?("Cookie")
238
+ end
239
+
240
+ # Returns whether the given URIs have the same scheme, host and port.
241
+ def same_origin?(uri, other_uri)
242
+ uri.scheme.casecmp?(other_uri.scheme) &&
243
+ uri.host.to_s.casecmp?(other_uri.host.to_s) &&
244
+ uri.port == other_uri.port
189
245
  end
190
246
 
191
247
  # Converts the given IO into a Tempfile if it isn't one already (open-uri
@@ -253,6 +309,11 @@ module Down
253
309
  options[:http_basic_authentication] ||= [uri.user, uri.password]
254
310
  end
255
311
 
312
+ # do not leak cookies to other origins on redirect
313
+ unless auth_on_redirect || same_origin?(uri, location)
314
+ options[:headers] = options[:headers].reject { |name, _| cookie_header?(name) }
315
+ end
316
+
256
317
  net_http_request(location, **options, max_redirects: max_redirects - 1, auth_on_redirect:, &)
257
318
  end
258
319
  end
data/lib/down/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  module Down
4
- VERSION = "5.6.2"
4
+ VERSION = "5.6.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: down
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.6.2
4
+ version: 5.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
@@ -179,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  requirements: []
182
- rubygems_version: 4.0.20
182
+ rubygems_version: 4.0.13
183
183
  specification_version: 4
184
184
  summary: Robust streaming downloads using Net::HTTP, http.rb or HTTPX.
185
185
  test_files: []