metainspector 5.20.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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -0
  3. data/Gemfile +1 -1
  4. data/README.md +157 -6
  5. data/examples/basic_scraping.rb +4 -3
  6. data/examples/faraday_redirect_options.rb +23 -31
  7. data/examples/html_size_limit.rb +17 -0
  8. data/examples/iframes.rb +15 -0
  9. data/examples/image_details.rb +17 -0
  10. data/examples/image_validation.rb +25 -0
  11. data/examples/link_checker.rb +2 -2
  12. data/examples/responsive_images.rb +15 -0
  13. data/examples/spider.rb +2 -2
  14. data/lib/meta_inspector/document.rb +14 -1
  15. data/lib/meta_inspector/http_session.rb +15 -2
  16. data/lib/meta_inspector/image_downloader.rb +48 -3
  17. data/lib/meta_inspector/parser.rb +20 -1
  18. data/lib/meta_inspector/parsers/head_links.rb +18 -3
  19. data/lib/meta_inspector/parsers/iframes.rb +21 -0
  20. data/lib/meta_inspector/parsers/images.rb +194 -26
  21. data/lib/meta_inspector/parsers/texts.rb +2 -2
  22. data/lib/meta_inspector/url_policy.rb +23 -6
  23. data/lib/meta_inspector/version.rb +1 -1
  24. data/lib/meta_inspector.rb +1 -0
  25. data/meta_inspector.gemspec +1 -2
  26. data/spec/document_spec.rb +139 -0
  27. data/spec/examples_spec.rb +206 -0
  28. data/spec/image_download_spec.rb +11 -0
  29. data/spec/image_metadata_spec.rb +110 -0
  30. data/spec/image_validity_spec.rb +195 -0
  31. data/spec/inline_images_spec.rb +75 -0
  32. data/spec/meta_inspector/head_links_spec.rb +69 -0
  33. data/spec/meta_inspector/iframes_spec.rb +50 -0
  34. data/spec/meta_inspector/images_spec.rb +130 -0
  35. data/spec/meta_inspector/texts_spec.rb +50 -0
  36. data/spec/parser_spec.rb +10 -0
  37. data/spec/redirect_headers_spec.rb +143 -0
  38. data/spec/unicode_image_urls_spec.rb +74 -0
  39. data/spec/url_filter_spec.rb +1 -1
  40. metadata +29 -37
  41. data/examples/redirect_web_server.rb +0 -5
@@ -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
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Unicode image URLs' do
4
+ let(:original) { 'https://example.com/ñ.png' }
5
+ let(:encoded) { 'https://example.com/%C3%B1.png' }
6
+ let(:png) { "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b }
7
+
8
+ before { stub_request(:get, encoded).to_return(body: png) }
9
+
10
+ ['src', 'srcset'].each do |attribute|
11
+ it "downloads a Unicode #{attribute} image while preserving its original URL" do
12
+ page = MetaInspector.new('https://example.com/', document: %(<img #{attribute}="#{original}">))
13
+
14
+ expect(page.images.with_size).to eq([[original, 37, 29]])
15
+ expect(a_request(:get, encoded)).to have_been_made.once
16
+ end
17
+ end
18
+
19
+ it 'validates Unicode image and suggested URLs without rejecting them' do
20
+ html = %(<meta property="og:image" content="#{original}"><img src="#{original}">)
21
+ page = MetaInspector.new('https://example.com/', document: html,
22
+ download_image_options: { reject_invalid_images: true }, fetch_all_image_meta: true)
23
+
24
+ expect(page.images.owner_suggested).to eq(original)
25
+ expect(page.images.to_a).to eq([original])
26
+ expect(page.images.with_metadata).to eq([{ url: original, valid: true }])
27
+ expect(a_request(:get, encoded)).to have_been_made.once
28
+ end
29
+
30
+ it 'applies URL filters to the encoded URL before making a request' do
31
+ seen = []
32
+ page = MetaInspector.new('https://example.com/', document: %(<img srcset="#{original} 1x">),
33
+ fetch_all_image_meta: true, url_filter: ->(uri) { seen << uri.to_s; false })
34
+
35
+ expect { page.images.with_metadata }.to raise_error(MetaInspector::RequestError, /blocked by url_filter/)
36
+ expect(seen).to eq([encoded])
37
+ expect(a_request(:get, encoded)).not_to have_been_made
38
+ end
39
+
40
+ it 'still applies URL filters to redirects from Unicode image URLs' do
41
+ target = 'https://other.example/image.png'
42
+ stub_request(:get, encoded).to_return(status: 302, headers: { 'Location' => target })
43
+ stub_request(:get, target).to_return(body: png)
44
+ page = MetaInspector.new('https://example.com/', document: %(<img src="#{original}">),
45
+ url_filter: ->(uri) { uri.hostname == 'example.com' })
46
+
47
+ expect { page.images.with_size }.to raise_error(MetaInspector::RequestError, /blocked by url_filter/)
48
+ expect(a_request(:get, encoded)).to have_been_made.once
49
+ expect(a_request(:get, target)).not_to have_been_made
50
+ end
51
+
52
+ {
53
+ 'an invalid UTF-8 host' => 'https://%C3/ñ.png',
54
+ 'an oversized Unicode host label' => "https://#{'ñ' * 300}.com/a.png"
55
+ }.each do |description, malformed|
56
+ context "with #{description}" do
57
+ it 'skips a srcset candidate that cannot be normalized' do
58
+ page = MetaInspector.new('https://example.com/', document: %(<img srcset="#{malformed} 1x">))
59
+
60
+ expect(page.images.with_size).to eq([[malformed, 0, 0]])
61
+ end
62
+
63
+ it 'rejects images that cannot be normalized when validating' do
64
+ html = %(<meta property="og:image" content="#{malformed}"><img src="#{malformed}">)
65
+ page = MetaInspector.new('https://example.com/', document: html,
66
+ download_image_options: { reject_invalid_images: true }, fetch_all_image_meta: true)
67
+
68
+ expect(page.images.owner_suggested).to be_nil
69
+ expect(page.images.to_a).to eq([])
70
+ expect(page.images.with_metadata).to eq([{ url: malformed, valid: false }])
71
+ end
72
+ end
73
+ end
74
+ end
@@ -124,7 +124,7 @@ RSpec.describe 'outbound URL policy' do
124
124
  it "rejects non-HTTP network URL #{other}" do
125
125
  expect { MetaInspector.new(other) }.to raise_error(MetaInspector::RequestError)
126
126
  page = MetaInspector.new(url, document: %(<img src="#{other}">))
127
- expect { page.images.with_size }.to raise_error(MetaInspector::RequestError)
127
+ expect(page.images.with_size.first.drop(1)).to eq([0, 0])
128
128
  end
129
129
  end
130
130
 
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.20.0
4
+ version: 5.21.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Iniesta
@@ -153,6 +153,20 @@ dependencies:
153
153
  - - "~>"
154
154
  - !ruby/object:Gem::Version
155
155
  version: 2.9.1
156
+ - !ruby/object:Gem::Dependency
157
+ name: base64
158
+ requirement: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - "~>"
161
+ - !ruby/object:Gem::Version
162
+ version: '0.3'
163
+ type: :runtime
164
+ prerelease: false
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: '0.3'
156
170
  - !ruby/object:Gem::Dependency
157
171
  name: fastimage
158
172
  requirement: !ruby/object:Gem::Requirement
@@ -237,26 +251,6 @@ dependencies:
237
251
  - - "~>"
238
252
  - !ruby/object:Gem::Version
239
253
  version: '0.14'
240
- - !ruby/object:Gem::Dependency
241
- name: puma
242
- requirement: !ruby/object:Gem::Requirement
243
- requirements:
244
- - - "~>"
245
- - !ruby/object:Gem::Version
246
- version: '8.0'
247
- - - ">="
248
- - !ruby/object:Gem::Version
249
- version: 8.0.2
250
- type: :development
251
- prerelease: false
252
- version_requirements: !ruby/object:Gem::Requirement
253
- requirements:
254
- - - "~>"
255
- - !ruby/object:Gem::Version
256
- version: '8.0'
257
- - - ">="
258
- - !ruby/object:Gem::Version
259
- version: 8.0.2
260
254
  - !ruby/object:Gem::Dependency
261
255
  name: rubocop
262
256
  requirement: !ruby/object:Gem::Requirement
@@ -285,20 +279,6 @@ dependencies:
285
279
  - - "~>"
286
280
  - !ruby/object:Gem::Version
287
281
  version: '0.8'
288
- - !ruby/object:Gem::Dependency
289
- name: sinatra
290
- requirement: !ruby/object:Gem::Requirement
291
- requirements:
292
- - - "~>"
293
- - !ruby/object:Gem::Version
294
- version: '4.2'
295
- type: :development
296
- prerelease: false
297
- version_requirements: !ruby/object:Gem::Requirement
298
- requirements:
299
- - - "~>"
300
- - !ruby/object:Gem::Version
301
- version: '4.2'
302
282
  description: MetaInspector lets you scrape a web page and get its links, images, texts,
303
283
  meta tags...
304
284
  email: jaimeiniesta@gmail.com
@@ -320,8 +300,12 @@ files:
320
300
  - bin/console
321
301
  - examples/basic_scraping.rb
322
302
  - examples/faraday_redirect_options.rb
303
+ - examples/html_size_limit.rb
304
+ - examples/iframes.rb
305
+ - examples/image_details.rb
306
+ - examples/image_validation.rb
323
307
  - examples/link_checker.rb
324
- - examples/redirect_web_server.rb
308
+ - examples/responsive_images.rb
325
309
  - examples/spider.rb
326
310
  - lib/meta_inspector.rb
327
311
  - lib/meta_inspector/document.rb
@@ -331,6 +315,7 @@ files:
331
315
  - lib/meta_inspector/parser.rb
332
316
  - lib/meta_inspector/parsers/base.rb
333
317
  - lib/meta_inspector/parsers/head_links.rb
318
+ - lib/meta_inspector/parsers/iframes.rb
334
319
  - lib/meta_inspector/parsers/images.rb
335
320
  - lib/meta_inspector/parsers/links.rb
336
321
  - lib/meta_inspector/parsers/meta_tags.rb
@@ -342,6 +327,7 @@ files:
342
327
  - lib/metainspector.rb
343
328
  - meta_inspector.gemspec
344
329
  - spec/document_spec.rb
330
+ - spec/examples_spec.rb
345
331
  - spec/fixtures/100x100.jpg.response
346
332
  - spec/fixtures/10x10.jpg.response
347
333
  - spec/fixtures/404.response
@@ -410,8 +396,12 @@ files:
410
396
  - spec/fixtures/youtube.response
411
397
  - spec/fixtures/youtube_short_title.response
412
398
  - spec/image_download_spec.rb
399
+ - spec/image_metadata_spec.rb
400
+ - spec/image_validity_spec.rb
401
+ - spec/inline_images_spec.rb
413
402
  - spec/meta_inspector/head_first_spec.rb
414
403
  - spec/meta_inspector/head_links_spec.rb
404
+ - spec/meta_inspector/iframes_spec.rb
415
405
  - spec/meta_inspector/images_spec.rb
416
406
  - spec/meta_inspector/links_spec.rb
417
407
  - spec/meta_inspector/meta_inspector_spec.rb
@@ -419,8 +409,10 @@ files:
419
409
  - spec/meta_inspector/redirections_spec.rb
420
410
  - spec/meta_inspector/texts_spec.rb
421
411
  - spec/parser_spec.rb
412
+ - spec/redirect_headers_spec.rb
422
413
  - spec/request_spec.rb
423
414
  - spec/spec_helper.rb
415
+ - spec/unicode_image_urls_spec.rb
424
416
  - spec/url_filter_spec.rb
425
417
  - spec/url_spec.rb
426
418
  homepage: https://github.com/jaimeiniesta/metainspector
@@ -441,7 +433,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
441
433
  - !ruby/object:Gem::Version
442
434
  version: '0'
443
435
  requirements: []
444
- rubygems_version: 4.0.3
436
+ rubygems_version: 4.0.15
445
437
  specification_version: 4
446
438
  summary: MetaInspector is a ruby gem for web scraping purposes, that returns metadata
447
439
  from a given URL
@@ -1,5 +0,0 @@
1
- require 'sinatra'
2
-
3
- get '/' do
4
- redirect 'http://10.0.0.0.nip.io/'
5
- end