httparty 0.25.0 → 0.25.1

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: f086f07dd66b6d801b3a103e90f2c53d42549081324c495e840a118dd6227e49
4
- data.tar.gz: 460a21e7486089bad58092ac13db538b0b6eabd019ecab8c1f4e5272ba0e700a
3
+ metadata.gz: 6fcd7a36016d83ccd756fbb81ee2614828d7764255e0e51ee9fe6a19fb9fe400
4
+ data.tar.gz: 3c7132cec52abc3f9909269787ab37a358ed960aecb2f79fa4ef013614399759
5
5
  SHA512:
6
- metadata.gz: 80f68016d35bab11bb2c218021463a7bd3c6224e6e11fbc2ad703b6f32801f992da79497607a162863b7d0fd25717a88eb25b0248afb673330a72b187b1633f0
7
- data.tar.gz: a7422e258662e81f4869a21f7c235db0216dbdc41e93ac719bbc512296f46951517be887d8aec84f077dd308bbc46a5262f4341f5dbfe390f64c5f401287768e
6
+ metadata.gz: 6ebb92691c60a4c2799080667be437ce3b298fda670ff65fa8b27d20488b8747a8dc37f44d5f58b65e4d59324a41fa44dfa35b54f05fd26e9b474fc12b150a0f
7
+ data.tar.gz: 0d760a50dce828591aa5c49fbcbc99c56730bcbae07caa5e1ce818a6fd3b1c5fc36c815115bb5774e3355bf2924b5d357525bb55fd4c6cfcdd9e0abf34a088a8
data/docs/README.md CHANGED
@@ -147,7 +147,7 @@ class Client
147
147
  def self.fetch
148
148
  get("/resources", verify: false)
149
149
  # You can also use something like:
150
- # get("resources", verify_peer: false)
150
+ # get("/resources", verify_peer: false)
151
151
  end
152
152
  end
153
153
  ```
data/httparty.gemspec CHANGED
@@ -24,7 +24,9 @@ Gem::Specification.new do |s|
24
24
  s.post_install_message = "When you HTTParty, you must party hard!"
25
25
 
26
26
  all_files = `git ls-files`.split("\n")
27
- test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
27
+ # Separate pathspecs, not {test,spec,features}/*: backticks run /bin/sh,
28
+ # and dash (the /bin/sh on Debian and Ubuntu) doesn't expand braces.
29
+ test_files = `git ls-files -- test spec features`.split("\n")
28
30
  dev_files = `git ls-files -- .github Gemfile Guardfile '.rubocop*' script`.split("\n")
29
31
 
30
32
  s.files = all_files - test_files - dev_files
@@ -4,6 +4,7 @@ module HTTParty
4
4
  module Logger
5
5
  class ApacheFormatter #:nodoc:
6
6
  TAG_NAME = HTTParty.name
7
+ LOG_ESCAPES = /[[:cntrl:]"\\]/
7
8
 
8
9
  attr_accessor :level, :logger
9
10
 
@@ -24,7 +25,7 @@ module HTTParty
24
25
  attr_reader :request, :response
25
26
 
26
27
  def message
27
- "[#{TAG_NAME}] [#{current_time}] #{response.code} \"#{http_method} #{path}\" #{content_length || '-'} "
28
+ "[#{TAG_NAME}] [#{current_time}] #{response.code} \"#{http_method} #{sanitize(path)}\" #{content_length || '-'} "
28
29
  end
29
30
 
30
31
  def current_time
@@ -39,6 +40,18 @@ module HTTParty
39
40
  request.path.to_s
40
41
  end
41
42
 
43
+ # Escape the request path the way Apache's own access log does: control
44
+ # characters (e.g. CR/LF) become \xhh, and " and \ get a backslash. A
45
+ # crafted path then can't split or forge a log line (CWE-117), and the
46
+ # attempt stays visible. This matters with a permissive URI adapter
47
+ # such as Addressable; the default URI adapter rejects these characters.
48
+ # Invalid bytes are scrubbed first so the regex can't raise.
49
+ def sanitize(value)
50
+ value.to_s.scrub.gsub(LOG_ESCAPES) do |char|
51
+ char == '"' || char == '\\' ? "\\#{char}" : '\\x%02x' % char.ord
52
+ end
53
+ end
54
+
42
55
  def content_length
43
56
  response.respond_to?(:headers) ? response.headers['Content-Length'] : response['Content-Length']
44
57
  end
@@ -41,7 +41,7 @@ module HTTParty
41
41
  def log_response
42
42
  log IN, "HTTP/#{response.http_version} #{response.code}"
43
43
  log_response_headers
44
- log IN, "\n#{response.body}"
44
+ log IN, "\n", response.body
45
45
  log IN
46
46
  end
47
47
 
@@ -53,7 +53,7 @@ module HTTParty
53
53
  request.path.to_s
54
54
  end
55
55
 
56
- log OUT, "#{http_method} #{uri}"
56
+ log OUT, http_method, ' ', uri
57
57
  end
58
58
 
59
59
  def log_headers
@@ -74,28 +74,39 @@ module HTTParty
74
74
  def log_response_headers
75
75
  headers = response.respond_to?(:headers) ? response.headers : response
76
76
  response.each_header do |response_header|
77
- log IN, "#{response_header.capitalize}: #{headers[response_header]}"
77
+ log IN, response_header.capitalize, ': ', headers[response_header]
78
78
  end
79
79
  end
80
80
 
81
81
  def log_hash(hash)
82
- hash.each { |k, v| log(OUT, "#{k}: #{v}") }
82
+ hash.each { |k, v| log(OUT, k, ': ', v) }
83
83
  end
84
84
 
85
- def log(direction, line = '')
86
- messages << "[#{TAG_NAME}] [#{current_time}] #{direction} #{encode_log(line)}"
85
+ # Each part is converted before it is joined, so values in mixed
86
+ # encodings can't raise and fail the request just to write a log line.
87
+ def log(direction, *parts)
88
+ line = parts.map { |part| encode_log(part) }.join
89
+ messages << "[#{TAG_NAME}] [#{current_time}] #{direction} #{line}"
87
90
  end
88
91
 
89
- # Logger lines are joined later, which raises if they mix encodings (a
90
- # binary body next to a non-ASCII header). Binary is relabeled as UTF-8;
91
- # other charsets are converted so their text survives.
92
- def encode_log(line)
93
- text = line.to_s
94
- if text.encoding == Encoding::BINARY
95
- text.dup.force_encoding(Encoding::UTF_8).scrub
92
+ # Binary and broken UTF-8 are relabeled and scrubbed so readable text
93
+ # survives; other charsets are converted.
94
+ def encode_log(part)
95
+ text = part.to_s
96
+ return text if text.encoding == Encoding::UTF_8 && text.valid_encoding?
97
+
98
+ if text.encoding == Encoding::BINARY || text.encoding == Encoding::UTF_8
99
+ relabel_utf8(text)
96
100
  else
97
101
  text.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
98
102
  end
103
+ rescue Encoding::ConverterNotFoundError
104
+ relabel_utf8(text)
105
+ end
106
+
107
+ def relabel_utf8(text)
108
+ utf8 = text.dup.force_encoding(Encoding::UTF_8)
109
+ utf8.valid_encoding? ? utf8 : utf8.scrub
99
110
  end
100
111
 
101
112
  def current_time
@@ -20,6 +20,9 @@ module HTTParty
20
20
  ]
21
21
 
22
22
  SupportedURISchemes = ['http', 'https', 'webcal', nil]
23
+ DEFAULT_PORTS = { 'http' => 80, 'https' => 443 }.freeze
24
+ # Headers that describe a request body, and so are dropped along with it
25
+ CONTENT_HEADERS = %w[content-encoding content-language content-length content-location content-type transfer-encoding].freeze
23
26
 
24
27
  NON_RAILS_QUERY_STRING_NORMALIZER = proc do |query|
25
28
  Array(query).sort_by { |a| a[0].to_s }.map do |key, value|
@@ -60,6 +63,7 @@ module HTTParty
60
63
 
61
64
  def initialize(http_method, path, o = {})
62
65
  @changed_hosts = false
66
+ @cross_origin_redirect = false
63
67
  @credentials_sent = false
64
68
 
65
69
  self.http_method = http_method
@@ -188,7 +192,7 @@ module HTTParty
188
192
  end
189
193
 
190
194
  def handle_unauthorized(&block)
191
- return unless digest_auth? && response_unauthorized? && response_has_digest_auth_challenge?
195
+ return unless digest_auth? && send_authorization_header? && response_unauthorized? && response_has_digest_auth_challenge?
192
196
  return if @credentials_sent
193
197
  @credentials_sent = true
194
198
  perform(&block)
@@ -236,6 +240,10 @@ module HTTParty
236
240
  def setup_raw_request
237
241
  if options[:headers].respond_to?(:to_hash)
238
242
  headers_hash = options[:headers].to_hash
243
+ unless send_authorization_header?
244
+ # Header names are case-insensitive, and reject leaves the caller's hash alone
245
+ headers_hash = headers_hash.reject { |name, _| name.to_s.casecmp?('authorization') }
246
+ end
239
247
  else
240
248
  headers_hash = nil
241
249
  end
@@ -272,7 +280,7 @@ module HTTParty
272
280
  @raw_request.basic_auth(username, password)
273
281
  @credentials_sent = true
274
282
  end
275
- setup_digest_auth if digest_auth? && response_unauthorized? && response_has_digest_auth_challenge?
283
+ setup_digest_auth if digest_auth? && send_authorization_header? && response_unauthorized? && response_has_digest_auth_challenge?
276
284
  end
277
285
 
278
286
  def digest_auth?
@@ -359,15 +367,36 @@ module HTTParty
359
367
  if http_method == Net::HTTP::Get
360
368
  clear_body
361
369
  end
362
- capture_cookies(last_response)
370
+ # Cookies stay with the origin they came from. HTTParty doesn't track the
371
+ # Set-Cookie Domain attribute, so a redirect to another origin gets none
372
+ # of them; that origin's own Set-Cookie cookies are collected from here on.
373
+ @cross_origin_redirect ? drop_cookies : capture_cookies(last_response)
363
374
  perform(&block)
364
375
  end
365
376
 
366
377
  def handle_host_redirection
367
378
  check_duplicate_location_header
368
379
  redirect_path = options[:uri_adapter].parse(last_response['location']).normalize
369
- return if redirect_path.relative? || path.host == redirect_path.host || uri.host == redirect_path.host
370
- @changed_hosts = true
380
+ # A Location without a host ("/x", "../x", "?a") stays on the same origin.
381
+ # A scheme-relative one ("//other.example.com/x") has a host to compare.
382
+ @cross_origin_redirect = !(redirect_path.relative? && redirect_path.host.nil?) &&
383
+ cross_origin_redirect?(@last_uri, redirect_path)
384
+ @changed_hosts = true if @cross_origin_redirect
385
+ end
386
+
387
+ # Credentials only follow a redirect to the same scheme, host and port, as
388
+ # in curl and Python requests. An http -> https upgrade on the default
389
+ # ports keeps them, and once dropped they stay dropped for the request.
390
+ def cross_origin_redirect?(from, to)
391
+ return true unless from.host.to_s.casecmp?(to.host.to_s)
392
+
393
+ to_scheme = (to.scheme || from.scheme).to_s.downcase
394
+ from_scheme = from.scheme.to_s.downcase
395
+ from_port = from.port || DEFAULT_PORTS[from_scheme]
396
+ to_port = to.port || DEFAULT_PORTS[to_scheme]
397
+ return false if from_scheme == to_scheme && from_port == to_port
398
+
399
+ !(from_scheme == 'http' && from_port == 80 && to_scheme == 'https' && to_port == 443)
371
400
  end
372
401
 
373
402
  def check_duplicate_location_header
@@ -395,21 +424,36 @@ module HTTParty
395
424
  end
396
425
 
397
426
  # Some Web Application Firewalls reject incoming GET requests that have a body
398
- # if we redirect, and the resulting verb is GET then we will clear the body that
399
- # may be left behind from the initiating request
427
+ # if we redirect, and the resulting verb is GET then we will clear the body (or
428
+ # body_stream) that may be left behind from the initiating request, along with
429
+ # the header fields that described it (RFC 9110, section 15.4)
400
430
  def clear_body
401
431
  options[:body] = nil
432
+ options[:body_stream] = nil
402
433
  @raw_request.body = nil
434
+ return unless options[:headers].respond_to?(:to_hash)
435
+
436
+ options[:headers] = options[:headers].to_hash.reject do |name, _|
437
+ CONTENT_HEADERS.include?(name.to_s.downcase)
438
+ end
403
439
  end
404
440
 
405
441
  def capture_cookies(response)
406
442
  return unless response['Set-Cookie']
443
+ headers = options[:headers].respond_to?(:to_hash) ? options[:headers].to_hash : {}
444
+ # Class and request cookies are stored as "cookie", so match any case
445
+ cookie_names = headers.keys.select { |name| name.to_s.casecmp?('cookie') }
407
446
  cookies_hash = HTTParty::CookieHash.new
408
- cookies_hash.add_cookies(options[:headers].to_hash['Cookie']) if options[:headers] && options[:headers].to_hash['Cookie']
447
+ cookie_names.each { |name| cookies_hash.add_cookies(headers[name]) }
409
448
  response.get_fields('Set-Cookie').each { |cookie| cookies_hash.add_cookies(cookie) }
410
449
 
411
- options[:headers] ||= {}
412
- options[:headers]['Cookie'] = cookies_hash.to_cookie_string
450
+ options[:headers] = headers.reject { |name, _| cookie_names.include?(name) }
451
+ .merge('Cookie' => cookies_hash.to_cookie_string)
452
+ end
453
+
454
+ def drop_cookies
455
+ return unless options[:headers].respond_to?(:to_hash)
456
+ options[:headers] = options[:headers].to_hash.reject { |name, _| name.to_s.casecmp?('cookie') }
413
457
  end
414
458
 
415
459
  # Uses the HTTP Content-Type header to determine the format of the
@@ -48,7 +48,11 @@ module HTTParty
48
48
  # NOTE: This will raise an argument error if the
49
49
  # charset does not exist
50
50
  encoding = Encoding.find(charset)
51
- text.force_encoding(encoding.to_s)
51
+ # Dummy encodings (UTF-7, UTF-32, ISO-2022-JP) raise on strip, regexes
52
+ # and parsing, so the body keeps the bytes it arrived with.
53
+ return text if encoding.dummy?
54
+
55
+ text.force_encoding(encoding)
52
56
  rescue ArgumentError
53
57
  text
54
58
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HTTParty
4
- VERSION = '0.25.0'
4
+ VERSION = '0.25.1'
5
5
  end
data/lib/httparty.rb CHANGED
@@ -80,7 +80,7 @@ module HTTParty
80
80
  #
81
81
  # class Foo
82
82
  # include HTTParty
83
- # logger Logger.new('http_logger'), :info, :apache
83
+ # logger ::Logger.new('http_logger'), :info, :apache
84
84
  # end
85
85
  def logger(logger, level = :info, format = :apache)
86
86
  default_options[:logger] = logger
@@ -104,7 +104,7 @@ module HTTParty
104
104
  #
105
105
  # class Foo
106
106
  # include HTTParty
107
- # http_proxy 'http://foo.com', 80, 'user', 'pass'
107
+ # http_proxy 'foo.com', 80, 'user', 'pass'
108
108
  # end
109
109
  def http_proxy(addr = nil, port = nil, user = nil, pass = nil)
110
110
  default_options[:http_proxyaddr] = addr
@@ -381,6 +381,8 @@ module HTTParty
381
381
  # Let's change it to this:
382
382
  # /?selected_ids=1&selected_ids=2&selected_ids=3
383
383
  #
384
+ # (For exactly this format, call disable_rails_query_string_format.)
385
+ #
384
386
  # Pass a Proc to the query normalizer which accepts the yielded query.
385
387
  #
386
388
  # @example Modifying Array query strings
@@ -388,9 +390,7 @@ module HTTParty
388
390
  # include HTTParty
389
391
  #
390
392
  # query_string_normalizer proc { |query|
391
- # query.map do |key, value|
392
- # value.map {|v| "#{key}=#{v}"}
393
- # end.join('&')
393
+ # URI.encode_www_form(query)
394
394
  # }
395
395
  # end
396
396
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httparty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.0
4
+ version: 0.25.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2026-11-20 00:00:00.000000000 Z
12
+ date: 2026-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: csv