metainspector 5.19.0 → 5.21.0
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 +4 -4
- data/CHANGELOG.md +21 -0
- data/Gemfile +1 -1
- data/README.md +214 -9
- data/examples/basic_scraping.rb +4 -3
- data/examples/faraday_redirect_options.rb +23 -31
- data/examples/html_size_limit.rb +17 -0
- data/examples/iframes.rb +15 -0
- data/examples/image_details.rb +17 -0
- data/examples/image_validation.rb +25 -0
- data/examples/link_checker.rb +2 -2
- data/examples/responsive_images.rb +15 -0
- data/examples/spider.rb +2 -2
- data/lib/meta_inspector/document.rb +23 -4
- data/lib/meta_inspector/http_session.rb +75 -0
- data/lib/meta_inspector/image_downloader.rb +98 -0
- data/lib/meta_inspector/parser.rb +22 -2
- data/lib/meta_inspector/parsers/head_links.rb +18 -3
- data/lib/meta_inspector/parsers/iframes.rb +21 -0
- data/lib/meta_inspector/parsers/images.rb +196 -27
- data/lib/meta_inspector/parsers/texts.rb +2 -2
- data/lib/meta_inspector/request.rb +17 -39
- data/lib/meta_inspector/url_policy.rb +68 -0
- data/lib/meta_inspector/version.rb +1 -1
- data/lib/meta_inspector.rb +1 -0
- data/meta_inspector.gemspec +1 -2
- data/spec/document_spec.rb +139 -0
- data/spec/examples_spec.rb +206 -0
- data/spec/image_download_spec.rb +125 -0
- data/spec/image_metadata_spec.rb +110 -0
- data/spec/image_validity_spec.rb +195 -0
- data/spec/inline_images_spec.rb +75 -0
- data/spec/meta_inspector/head_links_spec.rb +69 -0
- data/spec/meta_inspector/iframes_spec.rb +50 -0
- data/spec/meta_inspector/images_spec.rb +130 -0
- data/spec/meta_inspector/meta_inspector_spec.rb +58 -7
- data/spec/meta_inspector/texts_spec.rb +50 -0
- data/spec/parser_spec.rb +10 -0
- data/spec/request_spec.rb +1 -1
- data/spec/unicode_image_urls_spec.rb +74 -0
- data/spec/url_filter_spec.rb +206 -0
- metadata +32 -36
- data/examples/redirect_web_server.rb +0 -5
|
@@ -2,6 +2,93 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe MetaInspector do
|
|
4
4
|
describe "#images" do
|
|
5
|
+
it 'preserves fallback dimensions when an earlier picture source repeats its URL' do
|
|
6
|
+
html = '<picture><source srcset="/same.jpg"><img src="/same.jpg" width="200" height="100"></picture>'
|
|
7
|
+
page = MetaInspector.new('http://example.com/', document: html, download_images: false)
|
|
8
|
+
|
|
9
|
+
expect(page.images.with_size).to eq([['http://example.com/same.jpg', 200, 100]])
|
|
10
|
+
expect(page.images.best).to eq('http://example.com/same.jpg')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'does not download inline srcset images or spend the download budget on them' do
|
|
14
|
+
html = '<img src="/fallback.jpg" width="20" height="20" srcset="data:image/png;base64,AAAA 1x, /100x100 2x">'
|
|
15
|
+
page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 1)
|
|
16
|
+
|
|
17
|
+
expect(page.images.to_a).to include('data:image/png;base64')
|
|
18
|
+
expect(page.images.best).to eq('http://example.com/100x100')
|
|
19
|
+
expect(WebMock).to have_requested(:get, 'http://example.com/100x100').once
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'skips non-HTTP responsive candidates when selecting the best image' do
|
|
23
|
+
html = '<picture><source srcset="blob:https://example.com/123 1x"><img src="/fallback.jpg" width="100" height="100"></picture>'
|
|
24
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
25
|
+
|
|
26
|
+
expect(page.images.to_a).to include('blob:https://example.com/123')
|
|
27
|
+
expect(page.images.best).to eq('http://example.com/fallback.jpg')
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'accepts positive width descriptors with leading zeroes' do
|
|
31
|
+
html = '<img srcset="/small.jpg 0400w, /large.jpg 0800w, /invalid.jpg 000w">'
|
|
32
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
33
|
+
|
|
34
|
+
expect(page.images.to_a).to eq(['http://example.com/small.jpg', 'http://example.com/large.jpg'])
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'collects responsive candidates in document order and resolves duplicates' do
|
|
38
|
+
html = <<~HTML
|
|
39
|
+
<base href="/assets/">
|
|
40
|
+
<picture><source srcset="small.webp 400w, large.webp 800w">
|
|
41
|
+
<img src="fallback.jpg" srcset="fallback.jpg 1x, large.jpg 2x">
|
|
42
|
+
</picture>
|
|
43
|
+
<img srcset="large.jpg 1x, other.jpg 2x">
|
|
44
|
+
<source srcset="outside.jpg 1x">
|
|
45
|
+
HTML
|
|
46
|
+
page = MetaInspector.new('http://example.com/page', document: html)
|
|
47
|
+
|
|
48
|
+
expect(page.images.to_a).to eq(%w[
|
|
49
|
+
http://example.com/assets/small.webp http://example.com/assets/large.webp
|
|
50
|
+
http://example.com/assets/fallback.jpg http://example.com/assets/large.jpg
|
|
51
|
+
http://example.com/assets/other.jpg
|
|
52
|
+
])
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'ranks responsive candidates by downloaded dimensions without assigning fallback dimensions to them' do
|
|
56
|
+
html = '<img src="/10x10" width="10" height="10" srcset="/100x100 2x">'
|
|
57
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
58
|
+
|
|
59
|
+
expect(page.images.with_size).to eq([
|
|
60
|
+
['http://example.com/100x100', 100, 100],
|
|
61
|
+
['http://example.com/10x10', 10, 10]
|
|
62
|
+
])
|
|
63
|
+
expect(page.images.best).to eq('http://example.com/100x100')
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'keeps commas within candidate URLs and skips invalid descriptors' do
|
|
67
|
+
html = '<img srcset="/image?a=1,2 1x, /bad.jpg nope, /good.jpg 2x">'
|
|
68
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
69
|
+
|
|
70
|
+
expect(page.images.to_a).to eq(['http://example.com/image?a=1,2', 'http://example.com/good.jpg'])
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'does not treat commas inside invalid parenthesized descriptors as candidate separators' do
|
|
74
|
+
html = '<img srcset="bad.jpg foo(bar,baz), good.jpg 1x">'
|
|
75
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
76
|
+
|
|
77
|
+
expect(page.images.to_a).to eq(['http://example.com/good.jpg'])
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'applies the download limit to responsive candidates in document order' do
|
|
81
|
+
html = '<picture><source srcset="/10x10 1x, /100x100 2x"><img src="/fallback.jpg" width="20" height="20"></picture>'
|
|
82
|
+
page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 1)
|
|
83
|
+
|
|
84
|
+
expect(page.images.with_size).to eq([
|
|
85
|
+
['http://example.com/fallback.jpg', 20, 20],
|
|
86
|
+
['http://example.com/10x10', 10, 10],
|
|
87
|
+
['http://example.com/100x100', 0, 0]
|
|
88
|
+
])
|
|
89
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/100x100')
|
|
90
|
+
end
|
|
91
|
+
|
|
5
92
|
describe "returns an Enumerable" do
|
|
6
93
|
let(:page) { MetaInspector.new('https://twitter.com/markupvalidator') }
|
|
7
94
|
|
|
@@ -426,6 +513,49 @@ describe MetaInspector do
|
|
|
426
513
|
end
|
|
427
514
|
|
|
428
515
|
describe '#favicon' do
|
|
516
|
+
['alternate icon', 'icon alternate', 'alternate icon stylesheet', 'ICON', 'AlTeRnAtE IcOn'].each do |rel|
|
|
517
|
+
it "recognizes the icon token in #{rel.inspect}" do
|
|
518
|
+
html = %(<html><head><link rel="#{rel}" href="/favicon.png"></head></html>)
|
|
519
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
520
|
+
|
|
521
|
+
expect(page.images.favicon).to eq('https://githubuniverse.com/favicon.png')
|
|
522
|
+
end
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
[" ", "\t", "\n", "\f", "\r"].each do |whitespace|
|
|
526
|
+
it "splits rel tokens on ASCII whitespace #{whitespace.inspect}" do
|
|
527
|
+
html = %(<html><head><link rel="#{whitespace}alternate#{whitespace}ICON#{whitespace}" href="/favicon.png"></head></html>)
|
|
528
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
529
|
+
|
|
530
|
+
expect(page.images.favicon).to eq('https://githubuniverse.com/favicon.png')
|
|
531
|
+
end
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
['apple-touch-icon', 'mask-icon', 'iconic', 'not-shortcut', 'shortcut-other',
|
|
535
|
+
"alternate\u00a0icon", "alternate\vicon", '', nil].each do |rel|
|
|
536
|
+
it "does not treat #{rel.inspect} as a favicon relationship" do
|
|
537
|
+
attribute = rel.nil? ? '' : %(rel="#{rel}")
|
|
538
|
+
html = %(<html><head><link #{attribute} href="/other.png"></head></html>)
|
|
539
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
540
|
+
|
|
541
|
+
expect(page.images.favicon).to be_nil
|
|
542
|
+
end
|
|
543
|
+
end
|
|
544
|
+
|
|
545
|
+
it 'uses the first matching link in document order' do
|
|
546
|
+
html = <<~HTML
|
|
547
|
+
<html><head>
|
|
548
|
+
<link rel="apple-touch-icon" href="/touch.png">
|
|
549
|
+
<link rel="not-shortcut" href="/other.png">
|
|
550
|
+
<link rel="alternate ICON" href="/first.png">
|
|
551
|
+
<link rel="icon" href="/second.png">
|
|
552
|
+
</head></html>
|
|
553
|
+
HTML
|
|
554
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
555
|
+
|
|
556
|
+
expect(page.images.favicon).to eq('https://githubuniverse.com/first.png')
|
|
557
|
+
end
|
|
558
|
+
|
|
429
559
|
it "should get favicon link when marked as icon" do
|
|
430
560
|
page = MetaInspector.new('http://pagerankalert.com/')
|
|
431
561
|
|
|
@@ -5,14 +5,65 @@ describe MetaInspector do
|
|
|
5
5
|
expect(MetaInspector.new('http://example.com').class).to eq(MetaInspector::Document)
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
#
|
|
10
|
-
cache
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
describe "response caching" do
|
|
9
|
+
# A memory cache (a Hash that responds to #read and #write)
|
|
10
|
+
let(:cache) do
|
|
11
|
+
store = Hash.new
|
|
12
|
+
def store.read(k) self[k]; end
|
|
13
|
+
def store.write(k, v) self[k] = v; end
|
|
14
|
+
store
|
|
15
|
+
end
|
|
13
16
|
|
|
14
|
-
|
|
17
|
+
# The deprecation warning prints once per process, so each example starts as a fresh process would
|
|
18
|
+
before { MetaInspector::Request.instance_variable_set(:@faraday_http_cache_deprecation_warned, nil) }
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
it "caches responses with faraday_options[:faraday_http_cache]" do
|
|
21
|
+
MetaInspector.new('http://example.com', faraday_options: { faraday_http_cache: { store: cache } })
|
|
22
|
+
|
|
23
|
+
expect(cache.keys).not_to be_empty
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "does not warn when the cache is configured inside faraday_options" do
|
|
27
|
+
expect do
|
|
28
|
+
MetaInspector.new('http://example.com', faraday_options: { faraday_http_cache: { store: cache } })
|
|
29
|
+
end.not_to output.to_stderr
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "leaves the faraday_options hash it was given unchanged" do
|
|
33
|
+
cache_options = { store: cache }
|
|
34
|
+
faraday_options = { faraday_http_cache: cache_options }
|
|
35
|
+
|
|
36
|
+
MetaInspector.new('http://example.com', faraday_options: faraday_options)
|
|
37
|
+
|
|
38
|
+
expect(faraday_options).to eq({ faraday_http_cache: { store: cache } })
|
|
39
|
+
expect(cache_options).to eq({ store: cache })
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "still caches responses with the deprecated top-level faraday_http_cache option" do
|
|
43
|
+
expect do
|
|
44
|
+
MetaInspector.new('http://example.com', faraday_http_cache: { store: cache })
|
|
45
|
+
end.to output.to_stderr
|
|
46
|
+
|
|
47
|
+
expect(cache.keys).not_to be_empty
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "warns that the top-level faraday_http_cache option is deprecated" do
|
|
51
|
+
expect do
|
|
52
|
+
MetaInspector.new('http://example.com', faraday_http_cache: { store: cache })
|
|
53
|
+
end.to output(/DEPRECATION.*faraday_http_cache.*faraday_options/).to_stderr
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "prints the deprecation warning only once per process" do
|
|
57
|
+
expect do
|
|
58
|
+
3.times { MetaInspector.new('http://example.com', faraday_http_cache: { store: cache }) }
|
|
59
|
+
end.to output(/\ADEPRECATION WARNING: [^\n]*\n\z/).to_stderr
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "raises ArgumentError when the cache is configured in both places" do
|
|
63
|
+
expect do
|
|
64
|
+
MetaInspector.new('http://example.com', faraday_http_cache: { store: cache },
|
|
65
|
+
faraday_options: { faraday_http_cache: { store: cache } })
|
|
66
|
+
end.to raise_error(ArgumentError, /faraday_http_cache/)
|
|
67
|
+
end
|
|
17
68
|
end
|
|
18
69
|
end
|
|
@@ -287,6 +287,56 @@ describe MetaInspector do
|
|
|
287
287
|
expect(page.best_description).to eq("the twitter description")
|
|
288
288
|
end
|
|
289
289
|
|
|
290
|
+
['', ' ', "\n\t "].each do |blank|
|
|
291
|
+
it "should skip a blank standard description (#{blank.inspect})" do
|
|
292
|
+
html = %(<html><head><meta name="description" content="#{blank}"><meta property="og:description" content="Useful Open Graph description"></head></html>)
|
|
293
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
294
|
+
|
|
295
|
+
expect(page.best_description).to eq('Useful Open Graph description')
|
|
296
|
+
expect(page.description).to eq(blank)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
it "should skip a blank Open Graph description (#{blank.inspect})" do
|
|
300
|
+
html = %(<html><head><meta property="og:description" content="#{blank}"><meta name="twitter:description" content="Useful Twitter description"></head></html>)
|
|
301
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
302
|
+
|
|
303
|
+
expect(page.best_description).to eq('Useful Twitter description')
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
it "should skip a blank Twitter description (#{blank.inspect})" do
|
|
307
|
+
paragraph = 'This paragraph provides a useful description when the Twitter description is blank and no other metadata descriptions exist.'
|
|
308
|
+
html = %(<html><head><meta name="twitter:description" content="#{blank}"></head><body><p>#{paragraph}</p></body></html>)
|
|
309
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
310
|
+
|
|
311
|
+
expect(page.best_description).to eq(paragraph)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
it "should use a paragraph when all metadata descriptions are blank (#{blank.inspect})" do
|
|
315
|
+
paragraph = 'This paragraph provides a useful description when the standard, Open Graph, and Twitter metadata descriptions are all blank.'
|
|
316
|
+
html = %(<html><head><meta name="description" content="#{blank}"><meta property="og:description" content="#{blank}"><meta name="twitter:description" content="#{blank}"></head><body><p>#{paragraph}</p></body></html>)
|
|
317
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
318
|
+
|
|
319
|
+
expect(page.best_description).to eq(paragraph)
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
it "should return nil when all metadata descriptions are blank and there is no paragraph (#{blank.inspect})" do
|
|
323
|
+
html = %(<html><head><meta name="description" content="#{blank}"><meta property="og:description" content="#{blank}"><meta name="twitter:description" content="#{blank}"></head></html>)
|
|
324
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
325
|
+
|
|
326
|
+
expect(page.best_description).to be_nil
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
['description', 'og:description', 'twitter:description'].each do |name|
|
|
331
|
+
it "should preserve whitespace in a useful #{name} value" do
|
|
332
|
+
description = " \n Useful description\t "
|
|
333
|
+
html = %(<html><head><meta name="#{name}" content="#{description}"></head></html>)
|
|
334
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
335
|
+
|
|
336
|
+
expect(page.best_description).to eq(description)
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
290
340
|
it "should return the secondary description if no meta tag is present" do
|
|
291
341
|
page = MetaInspector.new('http://theonion-no-description.com')
|
|
292
342
|
|
data/spec/parser_spec.rb
CHANGED
|
@@ -4,6 +4,16 @@ describe MetaInspector::Parser do
|
|
|
4
4
|
let(:doc) { MetaInspector::Document.new('http://pagerankalert.com') }
|
|
5
5
|
let(:parser) { MetaInspector::Parser.new(doc) }
|
|
6
6
|
|
|
7
|
+
it 'returns the first feed URL through the deprecated feed method' do
|
|
8
|
+
expect { expect(parser.feed).to eq('http://feeds.feedburner.com/PageRankAlert') }
|
|
9
|
+
.to output(/DEPRECATION.*page\.feed.*6\.0.*page\.feeds/).to_stderr
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'returns the favicon URL through the deprecated favicon method' do
|
|
13
|
+
expect { expect(parser.favicon).to eq('http://pagerankalert.com/src/favicon.ico') }
|
|
14
|
+
.to output(/DEPRECATION.*page\.favicon.*6\.0.*page\.images\.favicon/).to_stderr
|
|
15
|
+
end
|
|
16
|
+
|
|
7
17
|
it "should have a Nokogiri::HTML::Document as parsed" do
|
|
8
18
|
expect(parser.parsed.class).to eq(Nokogiri::HTML::Document)
|
|
9
19
|
end
|
data/spec/request_spec.rb
CHANGED
|
@@ -91,7 +91,7 @@ describe MetaInspector::Request do
|
|
|
91
91
|
def cache.write(k, v) self[k] = v; end
|
|
92
92
|
cache_options = { store: cache }
|
|
93
93
|
|
|
94
|
-
MetaInspector::Request.new(url('http://example.com'), faraday_http_cache: cache_options)
|
|
94
|
+
MetaInspector::Request.new(url('http://example.com'), faraday_options: { faraday_http_cache: cache_options })
|
|
95
95
|
|
|
96
96
|
expect(cache_options).to eq({ store: cache })
|
|
97
97
|
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
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe 'outbound URL policy' do
|
|
4
|
+
let(:url) { 'https://public.example/page' }
|
|
5
|
+
let(:image_url) { 'https://public.example/image.png' }
|
|
6
|
+
let(:html) { '<img src="/image.png">' }
|
|
7
|
+
let(:png) { "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b }
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
stub_request(:get, url).to_return(body: html, headers: { 'Content-Type' => 'text/html' })
|
|
11
|
+
stub_request(:get, image_url).to_return(body: png, headers: { 'Content-Type' => 'image/png; charset=UTF-8' })
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
[false, nil].each do |result|
|
|
15
|
+
it "blocks the initial GET when the filter returns #{result.inspect}" do
|
|
16
|
+
expect { MetaInspector.new(url, url_filter: ->(_) { result }) }.to raise_error(MetaInspector::RequestError)
|
|
17
|
+
expect(a_request(:get, url)).not_to have_been_made
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'passes a standard Ruby URI and permits truthy results' do
|
|
22
|
+
seen = []
|
|
23
|
+
page = MetaInspector.new(url, url_filter: ->(uri) { seen << uri; :allowed })
|
|
24
|
+
expect(page.images.with_size).to eq([[image_url, 37, 29]])
|
|
25
|
+
expect(seen.map(&:to_s)).to eq([url, image_url])
|
|
26
|
+
expect(seen).to all(be_a(URI::HTTP))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'does not permit mutations of the filter URI to change the request host' do
|
|
30
|
+
filter = ->(uri) { uri.host.replace('internal.example'); true }
|
|
31
|
+
expect(MetaInspector.new(url, url_filter: filter).url).to eq(url)
|
|
32
|
+
expect(a_request(:get, 'https://internal.example/page')).not_to have_been_made
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
[RuntimeError, Faraday::TimeoutError, Faraday::ConnectionFailed, URI::InvalidURIError, Timeout::Error].each do |error_class|
|
|
36
|
+
[:page, :image].each do |target|
|
|
37
|
+
it "preserves a filter's #{error_class} on a #{target}" do
|
|
38
|
+
error = error_class.new('policy failed')
|
|
39
|
+
filter = ->(_) { raise error }
|
|
40
|
+
expect do
|
|
41
|
+
if target == :page
|
|
42
|
+
MetaInspector.new(url, url_filter: filter)
|
|
43
|
+
else
|
|
44
|
+
MetaInspector.new(url, document: html, url_filter: filter).images.with_size
|
|
45
|
+
end
|
|
46
|
+
end.to raise_error { |actual| expect(actual).to equal(error) }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'checks HEAD and checks GET again after HEAD' do
|
|
52
|
+
stub_request(:head, url).to_return(headers: { 'Content-Type' => 'text/html' })
|
|
53
|
+
calls = 0
|
|
54
|
+
expect do
|
|
55
|
+
MetaInspector.new(url, head_first: true, url_filter: ->(_) { calls += 1; calls == 1 })
|
|
56
|
+
end.to raise_error(MetaInspector::RequestError)
|
|
57
|
+
expect(a_request(:head, url)).to have_been_made.once
|
|
58
|
+
expect(a_request(:get, url)).not_to have_been_made
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'blocks HEAD before making a request' do
|
|
62
|
+
expect { MetaInspector.new(url, head_first: true, url_filter: ->(_) { false }) }.to raise_error(MetaInspector::RequestError)
|
|
63
|
+
expect(a_request(:head, url)).not_to have_been_made
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
[:head, :get].each do |method|
|
|
67
|
+
it "checks every #{method} redirect after the existing redirects-only callback" do
|
|
68
|
+
urls = [url, 'https://public.example/next', 'https://internal.example/private']
|
|
69
|
+
urls.each_cons(2) { |from, to| stub_request(method, from).to_return(status: 302, headers: { 'Location' => to }) }
|
|
70
|
+
seen = []
|
|
71
|
+
redirects = []
|
|
72
|
+
callback = ->(previous, following) { redirects << [previous.url.to_s, following.url.to_s] }
|
|
73
|
+
filter = ->(uri) { seen << uri.to_s; uri.host == 'public.example' }
|
|
74
|
+
expect do
|
|
75
|
+
MetaInspector.new(url, head_first: method == :head, url_filter: filter,
|
|
76
|
+
faraday_options: { redirect: { callback: callback }.freeze }.freeze)
|
|
77
|
+
end.to raise_error(MetaInspector::RequestError)
|
|
78
|
+
expect(seen).to eq(urls)
|
|
79
|
+
expect(redirects).to eq(urls.each_cons(2).to_a)
|
|
80
|
+
expect(a_request(method, urls.last)).not_to have_been_made
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it 'validates redirect destinations changed by the existing callback' do
|
|
85
|
+
stub_request(:get, url).to_return(status: 302, headers: { 'Location' => '/next' })
|
|
86
|
+
callback = ->(_, following) { following.url.host = 'internal.example' }
|
|
87
|
+
expect do
|
|
88
|
+
MetaInspector.new(url, url_filter: ->(uri) { uri.host == 'public.example' }, faraday_options: { redirect: { callback: callback } })
|
|
89
|
+
end.to raise_error(MetaInspector::RequestError)
|
|
90
|
+
expect(a_request(:get, 'https://internal.example/next')).not_to have_been_made
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it 'runs policy before a cached response can be reused' do
|
|
94
|
+
cache = Class.new(Hash) do
|
|
95
|
+
def read(key); self[key]; end
|
|
96
|
+
def write(key, value); self[key] = value; end
|
|
97
|
+
end.new
|
|
98
|
+
stub_request(:get, url).to_return(body: html, headers: { 'Cache-Control' => 'public, max-age=3600' })
|
|
99
|
+
permitted = true
|
|
100
|
+
options = { url_filter: ->(_) { permitted }, faraday_options: { faraday_http_cache: { store: cache } } }
|
|
101
|
+
MetaInspector.new(url, options)
|
|
102
|
+
expect(cache).not_to be_empty
|
|
103
|
+
permitted = false
|
|
104
|
+
expect { MetaInspector.new(url, options) }.to raise_error(MetaInspector::RequestError)
|
|
105
|
+
expect(a_request(:get, url)).to have_been_made.once
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
it 'blocks images from inline HTML before downloading' do
|
|
109
|
+
page = MetaInspector.new(url, document: html, url_filter: ->(_) { false })
|
|
110
|
+
expect { page.images.with_size }.to raise_error(MetaInspector::RequestError)
|
|
111
|
+
expect(a_request(:get, url)).not_to have_been_made
|
|
112
|
+
expect(a_request(:get, image_url)).not_to have_been_made
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'checks every image redirect' do
|
|
116
|
+
stub_request(:get, image_url).to_return(status: 302, headers: { 'Location' => '/second.png' })
|
|
117
|
+
stub_request(:get, 'https://public.example/second.png').to_return(status: 302, headers: { 'Location' => 'http://internal.example/private' })
|
|
118
|
+
page = MetaInspector.new(url, document: html, url_filter: ->(uri) { uri.host == 'public.example' })
|
|
119
|
+
expect { page.images.with_size }.to raise_error(MetaInspector::RequestError)
|
|
120
|
+
expect(a_request(:get, 'http://internal.example/private')).not_to have_been_made
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
['file:///etc/passwd', 'ftp://files.example/image', 'data:image/png;base64,AAAA', 'ftp://files.example/http://public.example/'].each do |other|
|
|
124
|
+
it "rejects non-HTTP network URL #{other}" do
|
|
125
|
+
expect { MetaInspector.new(other) }.to raise_error(MetaInspector::RequestError)
|
|
126
|
+
page = MetaInspector.new(url, document: %(<img src="#{other}">))
|
|
127
|
+
expect(page.images.with_size.first.drop(1)).to eq([0, 0])
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it 'rejects non-HTTP redirect schemes' do
|
|
132
|
+
stub_request(:get, url).to_return(status: 302, headers: { 'Location' => 'file:///etc/passwd' })
|
|
133
|
+
expect { MetaInspector.new(url) }.to raise_error(MetaInspector::RequestError)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
it 'uses the page headers and redirects-only callback for images' do
|
|
137
|
+
headers = { 'Authorization' => 'Bearer test', 'User-Agent' => 'Policy app' }.freeze
|
|
138
|
+
stub_request(:get, image_url).with(headers: headers).to_return(status: 302, headers: { 'Location' => '/final.png' })
|
|
139
|
+
stub_request(:get, 'https://public.example/final.png').with(headers: headers).to_return(body: png)
|
|
140
|
+
redirects = []
|
|
141
|
+
page = MetaInspector.new(url, document: html, headers: headers,
|
|
142
|
+
faraday_options: { redirect: { callback: ->(old, following) { redirects << [old.url.to_s, following.url.to_s] } } })
|
|
143
|
+
expect(page.images.with_size).to eq([[image_url, 37, 29]])
|
|
144
|
+
expect(redirects).to eq([[image_url, 'https://public.example/final.png']])
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
it 'does not follow image redirects when disabled' do
|
|
148
|
+
stub_request(:get, image_url).to_return(status: 302, headers: { 'Location' => '/final.png' })
|
|
149
|
+
page = MetaInspector.new(url, document: html, allow_redirections: false)
|
|
150
|
+
expect(page.images.with_size).to eq([[image_url, 0, 0]])
|
|
151
|
+
expect(a_request(:get, 'https://public.example/final.png')).not_to have_been_made
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
it 'uses binary IO rather than a URL for image parsing' do
|
|
155
|
+
expect(FastImage).to receive(:size).with(an_instance_of(StringIO)).and_call_original
|
|
156
|
+
expect(MetaInspector.new(url, document: html).images.with_size).to eq([[image_url, 37, 29]])
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
[:to_timeout, :connection_failure, :unparseable].each do |failure|
|
|
160
|
+
it "returns zero dimensions for image #{failure}" do
|
|
161
|
+
request = stub_request(:get, image_url)
|
|
162
|
+
case failure
|
|
163
|
+
when :to_timeout then request.to_timeout
|
|
164
|
+
when :connection_failure then request.to_raise(Faraday::ConnectionFailed)
|
|
165
|
+
when :unparseable then request.to_return(body: 'not an image')
|
|
166
|
+
end
|
|
167
|
+
expect(MetaInspector.new(url, document: html).images.with_size).to eq([[image_url, 0, 0]])
|
|
168
|
+
expect(request).to have_been_requested.once
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
it 'rejects a non-callable filter even for inline HTML' do
|
|
172
|
+
expect { MetaInspector.new(url, document: html, url_filter: false) }.to raise_error(ArgumentError, /url_filter/)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
[
|
|
176
|
+
'https://other.example/image.png',
|
|
177
|
+
'http://public.example/image.png',
|
|
178
|
+
'https://public.example:8443/image.png'
|
|
179
|
+
].each do |external|
|
|
180
|
+
it "does not send custom page headers to a different image origin #{external}" do
|
|
181
|
+
secret_headers = { 'Authorization' => 'Bearer secret', 'Cookie' => 'session=secret', 'X-Api-Key' => 'secret' }
|
|
182
|
+
stub_request(:get, external).with do |request|
|
|
183
|
+
expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'X-Api-Key')
|
|
184
|
+
true
|
|
185
|
+
end.to_return(body: png)
|
|
186
|
+
page = MetaInspector.new(url, document: %(<img src="#{external}">),
|
|
187
|
+
headers: secret_headers.merge('User-Agent' => 'Policy app'),
|
|
188
|
+
faraday_options: { headers: { 'X-Faraday-Secret' => 'secret' } })
|
|
189
|
+
expect(page.images.with_size).to eq([[external, 37, 29]])
|
|
190
|
+
expect(a_request(:get, external).with { |r| r.headers.key?('X-Faraday-Secret') }).not_to have_been_made
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it 'retains custom headers on the page origin and strips them on image redirects' do
|
|
195
|
+
secrets = { 'Authorization' => 'Bearer secret', 'Cookie' => 'session=secret', 'X-Api-Key' => 'secret' }
|
|
196
|
+
external = 'https://other.example/image.png'
|
|
197
|
+
stub_request(:get, image_url).with(headers: secrets).to_return(status: 302, headers: { 'Location' => external })
|
|
198
|
+
stub_request(:get, external).with do |request|
|
|
199
|
+
expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'X-Api-Key')
|
|
200
|
+
true
|
|
201
|
+
end.to_return(body: png)
|
|
202
|
+
page = MetaInspector.new(url, document: html, headers: secrets)
|
|
203
|
+
expect(page.images.with_size).to eq([[image_url, 37, 29]])
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
end
|