brand_logo 2.0.0 → 2.0.2

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: 8ca5db19ef68c02172defd8e04ba4a228c5819c32e3f2aa8dd10de412b4889f1
4
- data.tar.gz: 6a1bf3a2bda344977f005a0c12ae07c884fa24bc19d5bdabc650ccb57f2e9806
3
+ metadata.gz: 675ae4f1f88674aaa5a369aa0c67e269890378b8f879ec6af95f11dab056d082
4
+ data.tar.gz: 27b599f25fba14c970f03a430f59d351e2605542299bc063aa86f29a7f65bbdf
5
5
  SHA512:
6
- metadata.gz: ee0e662864b0ede5a0a78b538109468efc96db273c41bb37ccb20c36f808e3869258e5e29792c8dfcb5a7410fc3c4fe45ffa8e9f6a3453197e421abc80a08173
7
- data.tar.gz: 74c9c3d4fe4102fdc86bd2070129dc93b9f7b09da79677278c1ea0b3e4bc1cfdc6779195f4b800e0ea9d410c71b0f2d72f89c3ab59354d40b34385bac48557f3
6
+ metadata.gz: 93159df49b1dd4e86721ace44166294e96458075978a5822c39debc019a6d1c9b2e3e001a6b125762e20c917f6f6ac43f6844f286b37de80f3c1b2444275f034
7
+ data.tar.gz: fff751a1e8a033eb6704c5d253c158e16c02f781eef6af6542540627a70eabd5d845392a696141eece0d1c5a45f24ea68e11aafa3370820cfdc217defef1a3a7
data/CHANGELOG.md CHANGED
@@ -3,6 +3,24 @@
3
3
  All notable changes to this project will be documented here.
4
4
  Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
5
 
6
+ ## [2.0.2] - 2026-10-27
7
+
8
+ ### Security
9
+
10
+ - `block_private_ips` is no longer vulnerable to DNS rebinding: the host is resolved once per request (and redirect hop) and the connection is pinned to the validated address.
11
+ - 6to4 (`2002::/16`), Teredo (`2001::/32`) and local-use NAT64 (`64:ff9b:1::/48`) addresses are blocked, as they can embed private IPv4 addresses.
12
+
13
+ ## [2.0.1] - 2026-10-09
14
+
15
+ ### Fixed
16
+
17
+ - JSON-LD and web app manifests containing duplicated keys or comments are parsed again with `json` 3, which rejects them by default.
18
+
19
+ ### Changed
20
+
21
+ - CI tests the minimum supported dependency versions (`gemfiles/minimum.gemfile`, Ruby 3.2) and Dependabot is enabled.
22
+ - Gem metadata: removed the `homepage_uri` duplicate of `source_code_uri`.
23
+
6
24
  ## [2.0.0] - 2026-10-02
7
25
 
8
26
  Rewrite focused on picking the right icon, speed and safety. Breaking changes: see *Upgrading*.
data/README.md CHANGED
@@ -107,9 +107,10 @@ Internationalized domains must be given in punycode (`xn--…`); IP addresses ar
107
107
 
108
108
  The gem fetches URLs chosen by third-party pages. With `block_private_ips: true` (default),
109
109
  every request — including each redirect — is refused unless its host resolves only to public
110
- addresses, and only `http`/`https` URLs are fetched. A DNS answer that changes between the check
111
- and the connection (DNS rebinding) is not covered; run lookups from a network without access to
112
- sensitive internal services if inputs are untrusted.
110
+ addresses, and only `http`/`https` URLs are fetched. The host is resolved once per request and the
111
+ connection is pinned to the validated address (TLS still verifies the original hostname), so a DNS
112
+ answer that changes between the check and the connection (DNS rebinding) cannot reach a private
113
+ address. Pinning does not apply through an HTTP proxy, which resolves the host itself.
113
114
 
114
115
  ## Logging
115
116
 
@@ -33,9 +33,12 @@ module BrandLogo
33
33
  def get(url, deadline:, max_bytes:, range: nil)
34
34
  current = url
35
35
  (@config.max_hops + 1).times do
36
- return nil if deadline.expired? || !allowed?(current)
36
+ return nil if deadline.expired?
37
37
 
38
- result = request(current, deadline, max_bytes, range)
38
+ socket_class = socket_class_for(current)
39
+ return nil unless socket_class
40
+
41
+ result = request(current, socket_class, deadline, max_bytes, range)
39
42
  return result if result.nil? || result.is_a?(Response)
40
43
 
41
44
  current = result
@@ -49,20 +52,44 @@ module BrandLogo
49
52
 
50
53
  private
51
54
 
52
- sig { params(url: String).returns(T::Boolean) }
53
- def allowed?(url)
54
- return true if UrlGuard.allowed?(url, block_private_ips: @config.block_private_ips)
55
+ sig { params(url: String).returns(T.nilable(T.class_of(TCPSocket))) }
56
+ def socket_class_for(url)
57
+ return (UrlGuard.web_url?(url) ? TCPSocket : blocked(url)) unless @config.block_private_ips
58
+
59
+ addresses = UrlGuard.validated_addresses(url)
60
+ addresses.empty? ? blocked(url) : pinned_socket(addresses)
61
+ end
62
+
63
+ sig { params(addresses: T::Array[IPAddr]).returns(T.class_of(TCPSocket)) }
64
+ def pinned_socket(addresses)
65
+ Class.new(TCPSocket) do
66
+ define_singleton_method(:open) do |_host, port, **options|
67
+ last_error = T.let(nil, T.nilable(SystemCallError))
68
+ addresses.each do |address|
69
+ return super(address.to_s, port, **options)
70
+ rescue SystemCallError => e
71
+ last_error = e
72
+ end
73
+ raise T.must(last_error)
74
+ end
75
+ end
76
+ end
55
77
 
78
+ sig { params(url: String).returns(NilClass) }
79
+ def blocked(url)
56
80
  log("blocked #{url}")
57
- false
81
+ nil
58
82
  end
59
83
 
60
84
  sig do
61
- params(url: String, deadline: Deadline, max_bytes: Integer, range: T.nilable(T::Range[Integer]))
62
- .returns(T.nilable(T.any(Response, String)))
85
+ params(
86
+ url: String, socket_class: T.class_of(TCPSocket), deadline: Deadline, max_bytes: Integer,
87
+ range: T.nilable(T::Range[Integer])
88
+ ).returns(T.nilable(T.any(Response, String)))
63
89
  end
64
- def request(url, deadline, max_bytes, range)
65
- client = HTTP.timeout([@config.timeout, deadline.remaining].min).headers(headers(range))
90
+ def request(url, socket_class, deadline, max_bytes, range)
91
+ client = HTTP::Client.new(socket_class: socket_class)
92
+ .timeout([@config.timeout, deadline.remaining].min).headers(headers(range))
66
93
  response = client.get(url)
67
94
  status = response.status.to_i
68
95
  return redirect_target(url, response) if REDIRECT_STATUSES.include?(status)
@@ -15,21 +15,28 @@ module BrandLogo
15
15
  BLOCKED_RANGES = T.let(
16
16
  %w[
17
17
  0.0.0.0/8 100.64.0.0/10 192.0.0.0/24 192.0.2.0/24 198.18.0.0/15 198.51.100.0/24
18
- 203.0.113.0/24 224.0.0.0/4 240.0.0.0/4 ::/128 64:ff9b::/96 100::/64 fc00::/7 fe80::/10 ff00::/8
18
+ 203.0.113.0/24 224.0.0.0/4 240.0.0.0/4 ::/128 64:ff9b::/96 64:ff9b:1::/48 100::/64 2001::/32 2002::/16
19
+ fc00::/7 fe80::/10 ff00::/8
19
20
  ].map { |cidr| IPAddr.new(cidr) }.freeze,
20
21
  T::Array[IPAddr]
21
22
  )
22
23
 
23
- sig { params(url: String, block_private_ips: T::Boolean).returns(T::Boolean) }
24
- def self.allowed?(url, block_private_ips:)
24
+ sig { params(url: String).returns(T::Boolean) }
25
+ def self.web_url?(url)
26
+ !web_host(URI.parse(url)).nil?
27
+ rescue URI::Error
28
+ false
29
+ end
30
+
31
+ sig { params(url: String).returns(T::Array[IPAddr]) }
32
+ def self.validated_addresses(url)
25
33
  host = web_host(URI.parse(url))
26
- return false unless host
27
- return true unless block_private_ips
34
+ return [] unless host
28
35
 
29
- addresses = resolve(host)
30
- !addresses.empty? && addresses.none? { |address| blocked?(address) }
36
+ resolved = addresses(host)
37
+ resolved.none? { |address| blocked?(address) } ? resolved : []
31
38
  rescue URI::Error
32
- false
39
+ []
33
40
  end
34
41
 
35
42
  sig { params(uri: URI::Generic).returns(T.nilable(String)) }
@@ -39,7 +46,7 @@ module BrandLogo
39
46
  end
40
47
 
41
48
  sig { params(host: String).returns(T::Array[IPAddr]) }
42
- def self.resolve(host)
49
+ def self.addresses(host)
43
50
  literal = parse_ip(host)
44
51
  return [literal] if literal
45
52
 
@@ -62,7 +69,7 @@ module BrandLogo
62
69
  BLOCKED_RANGES.any? { |range| range.family == address.family && range.include?(address) }
63
70
  end
64
71
 
65
- private_class_method :resolve, :parse_ip, :blocked?
72
+ private_class_method :addresses, :parse_ip, :blocked?
66
73
  end
67
74
  end
68
75
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ # typed: strict
3
+
4
+ require 'json'
5
+
6
+ module BrandLogo
7
+ module LenientJson
8
+ extend T::Sig
9
+
10
+ # json >= 3 rejects duplicate keys and comments by default; json 2.x ignores these options.
11
+ OPTIONS = T.let({ allow_duplicate_key: true, allow_comments: true }.freeze, T::Hash[Symbol, T::Boolean])
12
+
13
+ sig { params(text: String).returns(T.untyped) }
14
+ def self.parse(text)
15
+ JSON.parse(text, **OPTIONS)
16
+ end
17
+ end
18
+ end
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  # typed: strict
3
3
 
4
- require 'json'
5
-
6
4
  module BrandLogo
7
5
  module Strategies
8
6
  class JsonLd
@@ -25,7 +23,7 @@ module BrandLogo
25
23
 
26
24
  logos = []
27
25
  page.document.css('script[type="application/ld+json"]').each do |script|
28
- collect(JSON.parse(script.text), false, logos)
26
+ collect(LenientJson.parse(script.text), false, logos)
29
27
  rescue JSON::ParserError => e
30
28
  Logging.logger.warn("JsonLd: invalid JSON at #{page.url}: #{e.message}")
31
29
  end
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  # typed: strict
3
3
 
4
- require 'json'
5
-
6
4
  module BrandLogo
7
5
  module Strategies
8
6
  class Manifest
@@ -31,7 +29,7 @@ module BrandLogo
31
29
 
32
30
  sig { params(response: Http::Response).returns(T::Array[T.untyped]) }
33
31
  def parse(response)
34
- data = JSON.parse(response.body)
32
+ data = LenientJson.parse(response.body)
35
33
  icons = data['icons'] if data.is_a?(::Hash)
36
34
  icons.is_a?(::Array) ? icons : []
37
35
  rescue JSON::ParserError => e
@@ -2,5 +2,5 @@
2
2
  # typed: strict
3
3
 
4
4
  module BrandLogo
5
- VERSION = '2.0.0'
5
+ VERSION = '2.0.2'
6
6
  end
data/lib/brand_logo.rb CHANGED
@@ -20,6 +20,7 @@ require_relative 'brand_logo/http/client'
20
20
  require_relative 'brand_logo/http/url_guard'
21
21
  require_relative 'brand_logo/http/real_client'
22
22
 
23
+ require_relative 'brand_logo/lenient_json'
23
24
  require_relative 'brand_logo/url_resolver'
24
25
  require_relative 'brand_logo/image_probe'
25
26
  require_relative 'brand_logo/page'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brand_logo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - AdVitam
@@ -105,6 +105,7 @@ files:
105
105
  - lib/brand_logo/icon.rb
106
106
  - lib/brand_logo/image_format.rb
107
107
  - lib/brand_logo/image_probe.rb
108
+ - lib/brand_logo/lenient_json.rb
108
109
  - lib/brand_logo/logging.rb
109
110
  - lib/brand_logo/lookup.rb
110
111
  - lib/brand_logo/page.rb
@@ -126,7 +127,6 @@ licenses:
126
127
  - MIT
127
128
  metadata:
128
129
  rubygems_mfa_required: 'true'
129
- homepage_uri: https://github.com/AdVitam/brand_logo
130
130
  source_code_uri: https://github.com/AdVitam/brand_logo
131
131
  changelog_uri: https://github.com/AdVitam/brand_logo/blob/master/CHANGELOG.md
132
132
  bug_tracker_uri: https://github.com/AdVitam/brand_logo/issues