metainspector 5.21.0 → 5.21.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: 486be410cc1d8f68b50d6f61ed2bb158487e2461dfcdda8d2287647d447313c9
4
- data.tar.gz: da722ae3063738ed07aa0287bfdb4102ba2c000ec9cf29d9b7b82e245b7e5f2e
3
+ metadata.gz: e84dae185b72b0ea7bec6e573db063bf2a0fe3632d978222f2c00cb2a0111075
4
+ data.tar.gz: 48799a4f6609282828b6161a4333231e0a0d0d762916fedf5e1ab854dc19e074
5
5
  SHA512:
6
- metadata.gz: b5249e8927148dc832343679a72474ad8396772dfa00c4657c856406b88619897947cbcc3e2423b77a60b2585357f19a6519c0fe8b49abc88147fa8fba1fe80b
7
- data.tar.gz: b2285a8dd0fb31f48c702b0623b40b93fbd5d626812074b761e25293f240b56685a97c8497570d0306b53222840927c3e020c11f953b73ef71aee064deb2ecbe
6
+ metadata.gz: 7af14afd571dc86d85a04e88a85a81b1386919a2b156a40ce5c57d59b4ec7084f9af4a40def4158797530ddeb99aca0406a628bf1f17c1d65dfb3761c3a5108f
7
+ data.tar.gz: e11c0c31565f180b1df73e4c291d4f1840dc1c7be48bbfe7b9507cda3383a593b0412a158edda73099288a7f4d0b3acd28b4b7764fdb21292f00bc9345aae134
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # MetaInspector Changelog
2
2
 
3
+ ## [Changes in 5.21.1](https://github.com/jaimeiniesta/metainspector/compare/v5.21.0...v5.21.1)
4
+
5
+ * Security: forward only `User-Agent`, `Accept`, `Accept-Language` and `Accept-Encoding` when page redirects change the origin (scheme, host or port), preventing custom credentials from reaching another origin. Apply the policy to GET and HEAD, including headers supplied through `faraday_options`, and keep the initial credentials out of image requests on the redirected origin. Custom headers remain available within the initial origin ([GHSA-mcmp-j8cq-h65f](https://github.com/jaimeiniesta/metainspector/security/advisories/GHSA-mcmp-j8cq-h65f)).
6
+
3
7
  ## [Changes in 5.21.0](https://github.com/jaimeiniesta/metainspector/compare/v5.20.0...v5.21.0)
4
8
 
5
9
  * Restore working `page.feed` and `page.favicon` compatibility methods with deprecation warnings and removal scheduled for 6.0. `feed` returns the first RSS/Atom URL (or `nil`), and `favicon` returns `images.favicon`. Migrate to `page.feeds` and `page.images.favicon` (#547).
data/README.md CHANGED
@@ -375,6 +375,14 @@ end
375
375
 
376
376
  By default, MetaInspector will follow redirects (up to a limit of 10).
377
377
 
378
+ Custom headers are retained only on the initial page origin (scheme, host and port).
379
+ For redirects to a different origin, including subdomains, different ports and
380
+ HTTPS-to-HTTP redirects, only `User-Agent`, `Accept`, `Accept-Language` and
381
+ `Accept-Encoding` are forwarded. This applies to GET and HEAD requests and to
382
+ headers supplied through either `headers:` or `faraday_options[:headers]`.
383
+ Credentials from the initial request are also withheld from images on a redirected
384
+ page at a different origin. Image requests retain their generated `Range` header.
385
+
378
386
  If you want to disallow redirects, you can do it like this:
379
387
 
380
388
  ```ruby
@@ -400,6 +408,8 @@ page = MetaInspector.new(url, faraday_options: { redirect: { callback: callback
400
408
 
401
409
  The `faraday_options[:redirect]` hash is passed to the `FollowRedirects` middleware used by `Faraday`, so that we can use all available options.
402
410
  Check them [here](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/response/follow_redirects.rb#L44).
411
+ The origin header policy also applies after your redirect callback runs and when
412
+ `clear_authorization_header: false` is set.
403
413
 
404
414
  ### Filtering request URLs
405
415
 
@@ -25,6 +25,8 @@ module MetaInspector
25
25
 
26
26
  def build(url, image: false, origin: nil, protect_redirect_callback: false)
27
27
  URLPolicy.validate!(url)
28
+ # Image requests must keep trusting the initial origin after page redirects.
29
+ @page_origin ||= url.to_s unless image
28
30
  faraday_options = @faraday_options.merge(url: url)
29
31
  redirects = (faraday_options.delete(:redirect) || {}).dup
30
32
  if protect_redirect_callback && redirects[:callback]
@@ -47,7 +49,8 @@ module MetaInspector
47
49
  faraday.use Faraday::FollowRedirects::Middleware, **redirects
48
50
  faraday.use :cookie_jar
49
51
  end
50
- faraday.use URLPolicy, @url_filter, image ? origin : nil
52
+ faraday.use URLPolicy, @url_filter, image ? origin : nil,
53
+ request_origin: @page_origin || origin
51
54
  if !image && @cache.is_a?(Hash)
52
55
  cache_options = @cache.dup
53
56
  cache_options[:serializer] ||= Marshal
@@ -12,7 +12,8 @@ module MetaInspector
12
12
  end
13
13
  end
14
14
 
15
- IMAGE_HEADERS = %w[user-agent accept accept-language accept-encoding range].freeze
15
+ PAGE_HEADERS = %w[user-agent accept accept-language accept-encoding].freeze
16
+ IMAGE_HEADERS = (PAGE_HEADERS + %w[range]).freeze
16
17
 
17
18
  def self.validate!(value)
18
19
  uri = URI.parse(value.to_s)
@@ -24,16 +25,22 @@ module MetaInspector
24
25
  raise InvalidURL.new(error)
25
26
  end
26
27
 
27
- def initialize(app, filter, image_origin = nil)
28
+ def initialize(app, filter, image_origin = nil, request_origin: nil)
28
29
  super(app)
29
30
  @filter = filter
30
31
  @image_origin = image_origin && self.class.validate!(image_origin)
32
+ @request_origin = request_origin && self.class.validate!(request_origin)
33
+ @request_origin ||= @image_origin
34
+ @allowed_headers = @image_origin ? IMAGE_HEADERS : PAGE_HEADERS
31
35
  end
32
36
 
33
37
  def call(env)
34
38
  uri = self.class.validate!(env.url)
35
- if @image_origin && origin(uri) != origin(@image_origin)
36
- env.request_headers.delete_if { |key, _| !IMAGE_HEADERS.include?(key.downcase) }
39
+ cross_image_origin = @image_origin && origin(uri) != origin(@image_origin)
40
+ if cross_image_origin || (@request_origin && origin(uri) != origin(@request_origin))
41
+ env.request_headers.delete_if { |key, _| !@allowed_headers.include?(key.downcase) }
42
+ end
43
+ if cross_image_origin
37
44
  unless @image_origin.scheme == 'https' && uri.scheme == 'http'
38
45
  referer = @image_origin.dup
39
46
  referer.userinfo = nil
@@ -1,3 +1,3 @@
1
1
  module MetaInspector
2
- VERSION = '5.21.0'
2
+ VERSION = '5.21.1'
3
3
  end
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'page redirect headers' do
4
+ let(:initial) { 'https://app.example/start' }
5
+ let(:headers) do
6
+ {
7
+ 'authorization' => 'Bearer secret',
8
+ 'cookie' => 'session=secret',
9
+ 'proxy-authorization' => 'Basic secret',
10
+ 'x-api-key' => 'secret',
11
+ 'referer' => 'https://private.example/secret',
12
+ 'range' => 'bytes=0-100',
13
+ 'user-agent' => 'Policy app',
14
+ 'accept' => 'text/html',
15
+ 'accept-language' => 'en',
16
+ 'accept-encoding' => 'identity'
17
+ }.freeze
18
+ end
19
+ let(:safe_headers) { headers.slice('user-agent', 'accept', 'accept-language', 'accept-encoding') }
20
+ let(:html) { '<title>landing</title>' }
21
+
22
+ def expect_safe_headers(request)
23
+ expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'Proxy-Authorization',
24
+ 'X-Api-Key', 'X-Faraday-Secret', 'Referer', 'Range')
25
+ safe_headers.each { |key, value| expect(request.headers[key.split('-').map(&:capitalize).join('-')]).to eq(value) }
26
+ true
27
+ end
28
+
29
+ [false, true].each do |head_first|
30
+ [
31
+ 'https://other.example/landing',
32
+ 'http://app.example/landing',
33
+ 'https://sub.app.example/landing',
34
+ 'https://app.example:8443/landing',
35
+ '//other.example/landing'
36
+ ].each do |destination|
37
+ it "strips credentials for #{destination} with head_first: #{head_first}" do
38
+ target = URI.join(initial, destination).to_s
39
+ methods = head_first ? [:head, :get] : [:get]
40
+ methods.each do |method|
41
+ stub_request(method, initial).with(headers: headers)
42
+ .to_return(status: 302, headers: { 'Location' => destination })
43
+ stub_request(method, target).with { |request| expect_safe_headers(request) }
44
+ .to_return(body: html, headers: { 'Content-Type' => 'text/html' })
45
+ end
46
+
47
+ page = MetaInspector.new(initial, headers: headers, head_first: head_first,
48
+ faraday_options: { headers: { 'X-Faraday-Secret' => 'secret' }.freeze }.freeze)
49
+
50
+ expect(page.title).to eq('landing')
51
+ expect(page.headers).to eq(headers)
52
+ methods.each { |method| expect(a_request(method, target)).to have_been_made.once }
53
+ end
54
+ end
55
+ end
56
+
57
+ ['/landing', 'https://app.example:443/landing'].each do |destination|
58
+ it "preserves custom headers for same-origin redirect #{destination}" do
59
+ stub_request(:get, initial).with(headers: headers)
60
+ .to_return(status: 302, headers: { 'Location' => destination, 'Set-Cookie' => 'server=value; Path=/; Secure' })
61
+ stub_request(:get, 'https://app.example/landing').with do |request|
62
+ expect(request.headers['Cookie']).to include('server=value', 'session=secret')
63
+ expect(request.headers['X-Api-Key']).to eq('secret')
64
+ expect(request.headers['Authorization']).to eq('Bearer secret')
65
+ true
66
+ end.to_return(body: html)
67
+
68
+ expect(MetaInspector.new(initial, headers: headers).title).to eq('landing')
69
+ end
70
+ end
71
+
72
+ it 'applies the policy after redirect callbacks change the destination and headers' do
73
+ stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => '/landing' })
74
+ stub_request(:get, 'https://other.example/landing').with { |request| expect_safe_headers(request) }
75
+ .to_return(body: html)
76
+ callback = lambda do |_, following|
77
+ following.url = URI('https://other.example/landing')
78
+ following.request_headers['X-Api-Key'] = 'callback-secret'
79
+ end
80
+
81
+ page = MetaInspector.new(initial, headers: headers,
82
+ faraday_options: { redirect: { callback: callback, clear_authorization_header: false }.freeze }.freeze)
83
+
84
+ expect(page.title).to eq('landing')
85
+ end
86
+
87
+ it 'keeps credentials stripped throughout a redirect chain' do
88
+ stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => 'https://other.example/step' })
89
+ stub_request(:get, 'https://other.example/step').with { |request| expect_safe_headers(request) }
90
+ .to_return(status: 302, headers: { 'Location' => '/landing' })
91
+ stub_request(:get, 'https://other.example/landing').with { |request| expect_safe_headers(request) }
92
+ .to_return(status: 302, headers: { 'Location' => 'https://app.example/landing' })
93
+ stub_request(:get, 'https://app.example/landing').with { |request| expect_safe_headers(request) }
94
+ .to_return(body: html)
95
+
96
+ expect(MetaInspector.new(initial, headers: headers).title).to eq('landing')
97
+ end
98
+
99
+ it 'does not reintroduce the initial credentials when fetching images on the redirected page' do
100
+ png = "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b
101
+ stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => 'https://other.example/landing' })
102
+ stub_request(:get, 'https://other.example/landing')
103
+ .to_return(body: '<img src="/image.png">', headers: { 'Content-Type' => 'text/html' })
104
+ stub_request(:get, 'https://other.example/image.png').with do |request|
105
+ expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'Proxy-Authorization',
106
+ 'X-Api-Key', 'X-Faraday-Secret', 'Referer')
107
+ expect(request.headers['Range']).to eq('bytes=0-262143')
108
+ true
109
+ end.to_return(body: png)
110
+
111
+ page = MetaInspector.new(initial, headers: headers,
112
+ faraday_options: { headers: { 'X-Faraday-Secret' => 'secret' } })
113
+
114
+ expect(page.images.with_size).to eq([['https://other.example/image.png', 37, 29]])
115
+ end
116
+
117
+ it 'can reuse caller headers across independent page requests' do
118
+ stub_request(:get, initial).with(headers: headers)
119
+ .to_return(status: 302, headers: { 'Location' => 'https://other.example/landing' })
120
+ stub_request(:get, 'https://other.example/landing').with { |request| expect_safe_headers(request) }
121
+ .to_return(body: html)
122
+
123
+ 2.times { expect(MetaInspector.new(initial, headers: headers).title).to eq('landing') }
124
+
125
+ expect(a_request(:get, initial).with(headers: headers)).to have_been_made.twice
126
+ end
127
+
128
+ it 'preserves image header protection when an image points back to the initial page origin' do
129
+ png = "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b
130
+ stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => 'https://other.example/landing' })
131
+ stub_request(:get, 'https://other.example/landing')
132
+ .to_return(body: '<img src="https://app.example/image.png">', headers: { 'Content-Type' => 'text/html' })
133
+ stub_request(:get, 'https://app.example/image.png').with do |request|
134
+ expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'Proxy-Authorization', 'X-Api-Key')
135
+ expect(request.headers['Referer']).to eq('https://other.example/')
136
+ true
137
+ end.to_return(body: png)
138
+
139
+ page = MetaInspector.new(initial, headers: headers)
140
+
141
+ expect(page.images.with_size).to eq([['https://app.example/image.png', 37, 29]])
142
+ end
143
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metainspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.21.0
4
+ version: 5.21.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Iniesta
@@ -409,6 +409,7 @@ files:
409
409
  - spec/meta_inspector/redirections_spec.rb
410
410
  - spec/meta_inspector/texts_spec.rb
411
411
  - spec/parser_spec.rb
412
+ - spec/redirect_headers_spec.rb
412
413
  - spec/request_spec.rb
413
414
  - spec/spec_helper.rb
414
415
  - spec/unicode_image_urls_spec.rb
@@ -432,7 +433,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
432
433
  - !ruby/object:Gem::Version
433
434
  version: '0'
434
435
  requirements: []
435
- rubygems_version: 4.0.3
436
+ rubygems_version: 4.0.15
436
437
  specification_version: 4
437
438
  summary: MetaInspector is a ruby gem for web scraping purposes, that returns metadata
438
439
  from a given URL