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
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
module MetaInspector
|
|
2
|
+
# Runs inside redirect handling, before cache access or the network adapter.
|
|
3
|
+
class URLPolicy < Faraday::Middleware
|
|
4
|
+
class InvalidURL < MetaInspector::RequestError; end
|
|
5
|
+
|
|
6
|
+
class Failure < StandardError
|
|
7
|
+
attr_reader :original
|
|
8
|
+
|
|
9
|
+
def initialize(original)
|
|
10
|
+
@original = original
|
|
11
|
+
super('URL policy failed')
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
IMAGE_HEADERS = %w[user-agent accept accept-language accept-encoding range].freeze
|
|
16
|
+
|
|
17
|
+
def self.validate!(value)
|
|
18
|
+
uri = URI.parse(value.to_s)
|
|
19
|
+
unless uri.is_a?(URI::HTTP) && uri.hostname && !uri.hostname.empty?
|
|
20
|
+
raise InvalidURL, 'URL must be HTTP or HTTPS'
|
|
21
|
+
end
|
|
22
|
+
uri
|
|
23
|
+
rescue URI::InvalidURIError => error
|
|
24
|
+
raise InvalidURL.new(error)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(app, filter, image_origin = nil)
|
|
28
|
+
super(app)
|
|
29
|
+
@filter = filter
|
|
30
|
+
@image_origin = image_origin && self.class.validate!(image_origin)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def call(env)
|
|
34
|
+
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) }
|
|
37
|
+
unless @image_origin.scheme == 'https' && uri.scheme == 'http'
|
|
38
|
+
referer = @image_origin.dup
|
|
39
|
+
referer.userinfo = nil
|
|
40
|
+
referer.path = '/'
|
|
41
|
+
referer.query = nil
|
|
42
|
+
referer.fragment = nil
|
|
43
|
+
env.request_headers['Referer'] = referer.to_s
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
check_filter(uri) if @filter
|
|
47
|
+
@app.call(env)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def origin(uri)
|
|
53
|
+
[uri.scheme.downcase, uri.hostname.downcase, uri.port]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def check_filter(uri)
|
|
57
|
+
# Parsing detaches the strings too: URI#dup alone shares its hostname.
|
|
58
|
+
begin
|
|
59
|
+
allowed = @filter.call(URI.parse(uri.to_s).freeze)
|
|
60
|
+
rescue StandardError => error
|
|
61
|
+
raise Failure.new(error)
|
|
62
|
+
end
|
|
63
|
+
unless allowed
|
|
64
|
+
raise Failure.new(MetaInspector::RequestError.new('Request blocked by url_filter'))
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
data/lib/meta_inspector.rb
CHANGED
|
@@ -5,6 +5,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/url')
|
|
|
5
5
|
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/parser'))
|
|
6
6
|
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/parsers/base'))
|
|
7
7
|
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/parsers/images'))
|
|
8
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/parsers/iframes'))
|
|
8
9
|
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/parsers/links'))
|
|
9
10
|
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/parsers/head_links'))
|
|
10
11
|
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/parsers/meta_tags'))
|
data/meta_inspector.gemspec
CHANGED
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |gem|
|
|
|
24
24
|
gem.add_dependency 'faraday-http-cache', '>= 2.8', '< 4.0'
|
|
25
25
|
gem.add_dependency 'faraday-retry', '~> 2.0'
|
|
26
26
|
gem.add_dependency 'addressable', '~> 2.9.1'
|
|
27
|
+
gem.add_dependency 'base64', '~> 0.3'
|
|
27
28
|
gem.add_dependency 'fastimage', '~> 2.2'
|
|
28
29
|
gem.add_dependency 'nesty', '~> 1.0'
|
|
29
30
|
|
|
@@ -31,8 +32,6 @@ Gem::Specification.new do |gem|
|
|
|
31
32
|
gem.add_development_dependency 'webmock', '~> 3.17'
|
|
32
33
|
gem.add_development_dependency 'rake', '~> 13.0'
|
|
33
34
|
gem.add_development_dependency 'pry', '~> 0.14'
|
|
34
|
-
gem.add_development_dependency 'puma', '~> 8.0', '>= 8.0.2'
|
|
35
35
|
gem.add_development_dependency 'rubocop', '~> 1.34'
|
|
36
36
|
gem.add_development_dependency 'resolv', '~> 0.8'
|
|
37
|
-
gem.add_development_dependency 'sinatra', '~> 4.2'
|
|
38
37
|
end
|
data/spec/document_spec.rb
CHANGED
|
@@ -1,6 +1,144 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe MetaInspector::Document do
|
|
4
|
+
describe 'public API' do
|
|
5
|
+
let(:html) do
|
|
6
|
+
<<~HTML
|
|
7
|
+
<html><head>
|
|
8
|
+
<link rel="icon" href="/icon.png">
|
|
9
|
+
<link rel="alternate" type="application/rss+xml" title="News" href="/feed.xml">
|
|
10
|
+
</head><body></body></html>
|
|
11
|
+
HTML
|
|
12
|
+
end
|
|
13
|
+
let(:page) { MetaInspector.new('https://example.com/', document: html) }
|
|
14
|
+
|
|
15
|
+
it 'retains the deprecated feed and favicon methods' do
|
|
16
|
+
expect(page).to respond_to(:feed, :favicon)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'exposes feeds through the plural API' do
|
|
20
|
+
expect(page.feeds).to eq([
|
|
21
|
+
{ title: 'News', href: 'https://example.com/feed.xml', type: 'application/rss+xml' }
|
|
22
|
+
])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'exposes the favicon through the images API' do
|
|
26
|
+
expect(page.images.favicon).to eq('https://example.com/icon.png')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
{ feed: 'feeds', favicon: 'images.favicon' }.each do |method, replacement|
|
|
30
|
+
it "warns on each call to ##{method}, naming its replacement and removal version" do
|
|
31
|
+
2.times do
|
|
32
|
+
expect { page.public_send(method) }.to output(
|
|
33
|
+
"DEPRECATION WARNING: page.#{method} is deprecated and will be removed in 6.0. Use page.#{replacement} instead.\n"
|
|
34
|
+
).to_stderr
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "returns nil from ##{method} when the document has no matching metadata" do
|
|
39
|
+
empty_page = MetaInspector.new('https://example.com/', document: '<html></html>')
|
|
40
|
+
|
|
41
|
+
expect { expect(empty_page.public_send(method)).to be_nil }.to output(/DEPRECATION/).to_stderr
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'returns the favicon URL from the deprecated shortcut' do
|
|
46
|
+
expect { expect(page.favicon).to eq(page.images.favicon) }.to output(/DEPRECATION/).to_stderr
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'keeps the supported APIs and serialization free of deprecation warnings' do
|
|
50
|
+
stub_request(:get, 'https://example.com/').to_return(body: html)
|
|
51
|
+
fetched_page = MetaInspector.new('https://example.com/', download_images: false)
|
|
52
|
+
|
|
53
|
+
expect do
|
|
54
|
+
fetched_page.feeds
|
|
55
|
+
fetched_page.images.favicon
|
|
56
|
+
fetched_page.to_hash
|
|
57
|
+
end.not_to output.to_stderr
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe '#feed' do
|
|
61
|
+
let(:html) do
|
|
62
|
+
<<~HTML
|
|
63
|
+
<html><head>
|
|
64
|
+
<base href="https://example.com/blog/">
|
|
65
|
+
<link rel="alternate" type="application/json" href="feed.json">
|
|
66
|
+
<link rel="alternate" type="#{first_type}" href="first.xml">
|
|
67
|
+
<link rel="alternate" type="application/rss+xml" href="second.xml">
|
|
68
|
+
</head></html>
|
|
69
|
+
HTML
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
['application/rss+xml', 'application/atom+xml'].each do |type|
|
|
73
|
+
context "with #{type} first" do
|
|
74
|
+
let(:first_type) { type }
|
|
75
|
+
|
|
76
|
+
it 'returns the first RSS or Atom URL, skipping JSON and resolving the base URL' do
|
|
77
|
+
expect { expect(page.feed).to eq('https://example.com/blog/first.xml') }
|
|
78
|
+
.to output(/DEPRECATION/).to_stderr
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
context 'with only JSON feeds' do
|
|
84
|
+
let(:html) { '<link rel="alternate" type="application/json" href="/feed.json">' }
|
|
85
|
+
|
|
86
|
+
it 'returns nil' do
|
|
87
|
+
expect { expect(page.feed).to be_nil }.to output(/DEPRECATION/).to_stderr
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
['<html></html>', nil].each do |document|
|
|
93
|
+
context "with #{document ? 'empty' : 'populated'} HTML" do
|
|
94
|
+
before do
|
|
95
|
+
stub_request(:get, 'https://example.com/').to_return(
|
|
96
|
+
body: document || html, headers: { 'Content-Type' => 'text/html' }
|
|
97
|
+
)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
MetaInspector::Document.public_instance_methods(false).sort.each do |method|
|
|
101
|
+
it "can call ##{method}" do
|
|
102
|
+
fetched_page = MetaInspector.new('https://example.com/', download_images: false)
|
|
103
|
+
|
|
104
|
+
if [:feed, :favicon].include?(method)
|
|
105
|
+
expect { fetched_page.public_send(method) }.to output(/DEPRECATION/).to_stderr
|
|
106
|
+
else
|
|
107
|
+
expect { fetched_page.public_send(method) }.not_to raise_error
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
describe 'max_html_bytes option' do
|
|
116
|
+
it 'rejects oversized supplied HTML before parsing' do
|
|
117
|
+
expect do
|
|
118
|
+
MetaInspector.new('http://example.com/', document: '<p>é</p>', max_html_bytes: 8)
|
|
119
|
+
end.to raise_error(MetaInspector::ParserError, /HTML.*9 bytes.*8 bytes/)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it 'accepts HTML at the byte limit and without a limit' do
|
|
123
|
+
html = '<title>é</title>'
|
|
124
|
+
expect(MetaInspector.new('http://example.com/', document: html, max_html_bytes: html.bytesize).title).to eq('é')
|
|
125
|
+
expect(MetaInspector.new('http://example.com/', document: html).title).to eq('é')
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
it 'rejects oversized fetched HTML' do
|
|
129
|
+
stub_request(:get, 'http://example.com/').to_return(body: '<p>hello</p>', headers: { 'Content-Type' => 'text/html' })
|
|
130
|
+
expect { MetaInspector.new('http://example.com/', max_html_bytes: 5) }.to raise_error(MetaInspector::ParserError)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
[-1, 1.5, '1', true, false].each do |limit|
|
|
134
|
+
it "rejects invalid limit #{limit.inspect} before fetching" do
|
|
135
|
+
expect { MetaInspector.new('http://example.com/', max_html_bytes: limit) }
|
|
136
|
+
.to raise_error(ArgumentError, 'max_html_bytes must be a non-negative Integer or nil')
|
|
137
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/')
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
4
142
|
describe 'max_image_downloads option' do
|
|
5
143
|
[-1, 1.5, '1', true, false].each do |limit|
|
|
6
144
|
it "should reject #{limit.inspect} before fetching the document" do
|
|
@@ -62,6 +200,7 @@ describe MetaInspector::Document do
|
|
|
62
200
|
'non_http' => ["mailto:pagerankalert@gmail.com"]
|
|
63
201
|
},
|
|
64
202
|
"images" => ["http://pagerankalert.com/images/pagerank_alert.png?1305794559"],
|
|
203
|
+
"iframes" => [],
|
|
65
204
|
"charset" => "utf-8",
|
|
66
205
|
"feeds" => [{href: "http://feeds.feedburner.com/PageRankAlert", title: "PageRankAlert.com blog", type: "application/rss+xml"}],
|
|
67
206
|
"h1" => [],
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'stringio'
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
RSpec.describe 'runnable examples' do
|
|
6
|
+
def run_example(name, *args)
|
|
7
|
+
stub_const('ARGV', args)
|
|
8
|
+
original_stdout = $stdout
|
|
9
|
+
$stdout = StringIO.new
|
|
10
|
+
load(File.expand_path("../examples/#{name}.rb", __dir__), true)
|
|
11
|
+
$stdout.string
|
|
12
|
+
ensure
|
|
13
|
+
$stdout = original_stdout
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def stub_page(path, html, status: 200)
|
|
17
|
+
stub_request(:get, "http://site.example#{path}").to_return(
|
|
18
|
+
status: status, headers: { 'Content-Type' => 'text/html' }, body: html
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def stub_image(path, fixture)
|
|
23
|
+
stub_request(:get, "http://site.example#{path}").to_return(fixture_file(fixture))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe 'basic_scraping' do
|
|
27
|
+
it 'prints title and links' do
|
|
28
|
+
output = run_example('basic_scraping', 'http://example.com')
|
|
29
|
+
expect(output).to include('TITLE: An example page', 'internal links found')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'runs from the examples directory' do
|
|
33
|
+
Dir.chdir(File.expand_path('../examples', __dir__)) do
|
|
34
|
+
expect(run_example('basic_scraping', 'http://example.com')).to include('TITLE: An example page')
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'prints the page hash as JSON' do
|
|
39
|
+
output = Dir.chdir(File.expand_path('../examples', __dir__)) do
|
|
40
|
+
run_example('basic_scraping', 'http://example.com')
|
|
41
|
+
end
|
|
42
|
+
expect(JSON.parse(output.split("to_hash...\n", 2).last)).to include('title' => 'An example page')
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe 'spider' do
|
|
47
|
+
it 'visits every internal page once' do
|
|
48
|
+
stub_page('/', '<a href="/a">A</a><a href="/b">B</a>')
|
|
49
|
+
stub_page('/a', '<a href="/">Home</a>')
|
|
50
|
+
stub_page('/b', '<title>B</title>')
|
|
51
|
+
|
|
52
|
+
output = run_example('spider', 'http://site.example/')
|
|
53
|
+
expect(output.lines.grep(/^VISITED: /).map(&:strip)).to contain_exactly(
|
|
54
|
+
'VISITED: http://site.example/', 'VISITED: http://site.example/a', 'VISITED: http://site.example/b'
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
describe 'link_checker' do
|
|
60
|
+
it 'reports broken links with their origin' do
|
|
61
|
+
stub_page('/', '<a href="/ok">OK</a><a href="/missing">Missing</a>')
|
|
62
|
+
stub_page('/ok', '<title>OK</title>')
|
|
63
|
+
stub_page('/missing', '<title>Missing</title>', status: 404)
|
|
64
|
+
|
|
65
|
+
output = run_example('link_checker', 'http://site.example/')
|
|
66
|
+
expect(output).to include('1 broken links found.', "http://site.example/missing linked from\n - http://site.example/")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe 'faraday_redirect_options' do
|
|
71
|
+
it 'rejects an untrusted initial URL before making a request' do
|
|
72
|
+
target = 'http://127.0.0.1/'
|
|
73
|
+
stub_request(:get, target).to_return(status: 200)
|
|
74
|
+
|
|
75
|
+
expect { run_example('faraday_redirect_options', target) }
|
|
76
|
+
.to output(/Request blocked by url_filter/).to_stderr
|
|
77
|
+
expect(a_request(:get, target)).not_to have_been_made
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'rejects an untrusted redirect before making a request to its destination' do
|
|
81
|
+
target = 'http://127.0.0.1/'
|
|
82
|
+
stub_request(:get, 'https://example.com/').to_return(status: 302, headers: { 'Location' => target })
|
|
83
|
+
stub_request(:get, target).to_return(status: 200)
|
|
84
|
+
stdout = nil
|
|
85
|
+
|
|
86
|
+
expect { stdout = run_example('faraday_redirect_options', 'https://example.com/') }
|
|
87
|
+
.to output(/Request blocked by url_filter/).to_stderr
|
|
88
|
+
expect(stdout).to include('Redirect:')
|
|
89
|
+
expect(a_request(:get, target)).not_to have_been_made
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'follows redirects to allowed HTTPS hosts' do
|
|
93
|
+
stub_request(:get, 'https://example.com/').to_return(status: 302, headers: { 'Location' => 'https://www.example.com/' })
|
|
94
|
+
stub_request(:get, 'https://www.example.com/').to_return(status: 200,
|
|
95
|
+
headers: { 'Content-Type' => 'text/html' }, body: '<title>Allowed page</title>')
|
|
96
|
+
|
|
97
|
+
stdout = run_example('faraday_redirect_options', 'https://example.com/')
|
|
98
|
+
expect(stdout).to match(/Redirect:.*Allowed page/m)
|
|
99
|
+
expect(a_request(:get, 'https://www.example.com/')).to have_been_made.once
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
describe 'responsive_images' do
|
|
104
|
+
it 'lists src and srcset candidates in document order' do
|
|
105
|
+
stub_page('/', <<~HTML)
|
|
106
|
+
<meta property="og:image" content="/og.png">
|
|
107
|
+
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACw=" srcset="/medium.png 2x, /large.png 1000w">
|
|
108
|
+
<picture><source srcset="/wide.webp"><img src="/fallback.png"></picture>
|
|
109
|
+
HTML
|
|
110
|
+
|
|
111
|
+
stdout = run_example('responsive_images', 'http://site.example/')
|
|
112
|
+
expect(stdout.lines.grep(/^ ==> /).map(&:strip)).to eq([
|
|
113
|
+
'==> data:image/gif;base64',
|
|
114
|
+
'==> http://site.example/medium.png',
|
|
115
|
+
'==> http://site.example/large.png',
|
|
116
|
+
'==> http://site.example/wide.webp',
|
|
117
|
+
'==> http://site.example/fallback.png'
|
|
118
|
+
])
|
|
119
|
+
expect(stdout).to include('5 image candidates found', 'BEST: http://site.example/og.png')
|
|
120
|
+
expect(stdout).not_to include('R0lGOD')
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
describe 'image_details' do
|
|
125
|
+
before do
|
|
126
|
+
stub_page('/', '<img src="/100x100.jpg"><img src="/10x10.jpg">')
|
|
127
|
+
stub_image('/100x100.jpg', '100x100.jpg.response')
|
|
128
|
+
stub_image('/10x10.jpg', '10x10.jpg.response')
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it 'prints dimensions, type and file size, largest first' do
|
|
132
|
+
stub_page('/', '<img src="/10x10.jpg"><img src="/100x100.jpg">')
|
|
133
|
+
stdout = run_example('image_details', 'http://site.example/')
|
|
134
|
+
expect(stdout).to match(%r{100x100\.jpg 100x100 png, \d+ bytes.*10x10\.jpg 10x10 jpeg, \d+ bytes}m)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
it 'respects the download limit' do
|
|
138
|
+
stdout = run_example('image_details', 'http://site.example/', '1')
|
|
139
|
+
expect(stdout).to include('http://site.example/100x100.jpg 100x100 png,',
|
|
140
|
+
'http://site.example/10x10.jpg 0x0 unknown type, unknown size')
|
|
141
|
+
expect(a_request(:get, 'http://site.example/10x10.jpg')).not_to have_been_made
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
describe 'image_validation' do
|
|
146
|
+
before do
|
|
147
|
+
stub_page('/', <<~HTML)
|
|
148
|
+
<meta property="og:image" content="/missing.png">
|
|
149
|
+
<img src="/10x10.jpg"><img src="/broken.png">
|
|
150
|
+
HTML
|
|
151
|
+
stub_image('/10x10.jpg', '10x10.jpg.response')
|
|
152
|
+
stub_page('/broken.png', '<title>Not an image</title>')
|
|
153
|
+
stub_page('/missing.png', '<title>Missing</title>', status: 404)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it 'reports validity and skips invalid images' do
|
|
157
|
+
stdout = run_example('image_validation', 'http://site.example/')
|
|
158
|
+
expect(stdout).to include('http://site.example/10x10.jpg: valid',
|
|
159
|
+
'http://site.example/broken.png: invalid',
|
|
160
|
+
'BEST: http://site.example/10x10.jpg')
|
|
161
|
+
filtered = stdout.split("images remain after validation...\n", 2).last
|
|
162
|
+
expect(filtered).to include(' ==> http://site.example/10x10.jpg')
|
|
163
|
+
expect(filtered).not_to include('broken.png', 'missing.png')
|
|
164
|
+
expect(a_request(:get, 'http://site.example/missing.png')).to have_been_made.once
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
it 'reports images beyond the download limit as not checked' do
|
|
168
|
+
stdout = run_example('image_validation', 'http://site.example/', '1')
|
|
169
|
+
expect(stdout).to include('http://site.example/10x10.jpg: valid',
|
|
170
|
+
'http://site.example/broken.png: not checked')
|
|
171
|
+
expect(a_request(:get, 'http://site.example/broken.png')).not_to have_been_made
|
|
172
|
+
expect(a_request(:get, 'http://site.example/missing.png')).not_to have_been_made
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
it 'can run twice without warnings' do
|
|
176
|
+
expect {
|
|
177
|
+
2.times { run_example('image_validation', 'http://site.example/') }
|
|
178
|
+
}.not_to output.to_stderr
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
describe 'iframes' do
|
|
183
|
+
it 'prints iframe attributes without downloading embedded documents' do
|
|
184
|
+
stub_page('/', '<iframe src=" /embed " title="Video" allowfullscreen></iframe><iframe></iframe>')
|
|
185
|
+
|
|
186
|
+
stdout = run_example('iframes', 'http://site.example/')
|
|
187
|
+
expect(stdout).to include('2 iframes found', ' ==> http://site.example/embed',
|
|
188
|
+
'title="Video"', 'allowfullscreen=""', ' ==> (no source)')
|
|
189
|
+
expect(a_request(:get, 'http://site.example/embed')).not_to have_been_made
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
describe 'html_size_limit' do
|
|
194
|
+
before do
|
|
195
|
+
stub_page('/', '<title>Small page</title>'.ljust(200))
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it 'parses pages within the limit' do
|
|
199
|
+
expect(run_example('html_size_limit', 'http://site.example/')).to include('TITLE: Small page')
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it 'skips pages over the limit' do
|
|
203
|
+
expect(run_example('html_size_limit', 'http://site.example/', '50')).to include('Skipped:')
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'socket'
|
|
3
|
+
|
|
4
|
+
RSpec.describe 'bounded image downloads' do
|
|
5
|
+
let(:png) { "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b }
|
|
6
|
+
|
|
7
|
+
it 'inherits proxy and TLS options without modifying caller-owned hashes' do
|
|
8
|
+
image = 'https://images.example/image.png'
|
|
9
|
+
stub_request(:get, image).to_return(body: png)
|
|
10
|
+
ssl = { verify: false }.freeze
|
|
11
|
+
redirect = { limit: 2 }.freeze
|
|
12
|
+
options = { proxy: 'http://proxy.example:3128', ssl: ssl, redirect: redirect }.freeze
|
|
13
|
+
expect(Faraday).to receive(:new).with(hash_including(proxy: options[:proxy], ssl: ssl)).and_call_original
|
|
14
|
+
page = MetaInspector.new('https://page.example/', document: %(<img src="#{image}">), faraday_options: options)
|
|
15
|
+
expect(page.images.with_size).to eq([[image, 37, 29]])
|
|
16
|
+
expect(options).to eq(proxy: 'http://proxy.example:3128', ssl: ssl, redirect: redirect)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'does not cache streamed image responses' do
|
|
20
|
+
cache = Class.new(Hash) do
|
|
21
|
+
def read(key); self[key]; end
|
|
22
|
+
def write(key, value); self[key] = value; end
|
|
23
|
+
end.new
|
|
24
|
+
stub_request(:get, 'https://images.example/image.png').to_return(body: png,
|
|
25
|
+
headers: { 'Cache-Control' => 'public, max-age=3600' })
|
|
26
|
+
2.times do
|
|
27
|
+
page = MetaInspector.new('https://page.example/', document: '<img src="https://images.example/image.png">',
|
|
28
|
+
faraday_options: { faraday_http_cache: { store: cache } })
|
|
29
|
+
expect(page.images.with_size.first.last(2)).to eq([37, 29])
|
|
30
|
+
end
|
|
31
|
+
expect(cache).to be_empty
|
|
32
|
+
expect(a_request(:get, 'https://images.example/image.png')).to have_been_made.twice
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
context 'with a real HTTP server' do
|
|
36
|
+
around do |example|
|
|
37
|
+
@server = TCPServer.new('127.0.0.1', 0)
|
|
38
|
+
@url = "http://127.0.0.1:#{@server.addr[1]}/image"
|
|
39
|
+
# WebMock buffers real responses before yielding to Net::HTTP's block.
|
|
40
|
+
# Disable its adapter here to exercise actual incremental reads.
|
|
41
|
+
WebMock.disable!
|
|
42
|
+
example.run
|
|
43
|
+
ensure
|
|
44
|
+
@server&.close
|
|
45
|
+
@worker&.kill
|
|
46
|
+
@worker&.join
|
|
47
|
+
WebMock.enable!
|
|
48
|
+
WebMock.disable_net_connect!
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def serve(&block)
|
|
52
|
+
@worker = Thread.new do
|
|
53
|
+
socket = @server.accept
|
|
54
|
+
while (line = socket.gets) && line != "\r\n"; end
|
|
55
|
+
block.call(socket)
|
|
56
|
+
rescue Errno::EPIPE, Errno::ECONNRESET, IOError
|
|
57
|
+
# Reaching the byte/time limit closes the peer connection.
|
|
58
|
+
ensure
|
|
59
|
+
socket&.close
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'counts a fully received chunked image without a content length' do
|
|
64
|
+
serve do |socket|
|
|
65
|
+
socket.write("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n")
|
|
66
|
+
socket.write("#{png.bytesize.to_s(16)}\r\n")
|
|
67
|
+
socket.write(png)
|
|
68
|
+
socket.write("\r\n0\r\n\r\n")
|
|
69
|
+
end
|
|
70
|
+
page = MetaInspector.new('http://page.example/', document: %(<img src="#{@url}">))
|
|
71
|
+
expect(page.images.with_details.first).to include(type: :png, file_size: png.bytesize)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it 'bounds binary IO and stops a server that ignores Range before reading its full body' do
|
|
75
|
+
sent = 0
|
|
76
|
+
serve do |socket|
|
|
77
|
+
socket.write("HTTP/1.1 200 OK\r\nContent-Length: 4194304\r\nContent-Type: image/png\r\nConnection: close\r\n\r\n")
|
|
78
|
+
socket.write(png)
|
|
79
|
+
1024.times do
|
|
80
|
+
socket.write('x' * 4096)
|
|
81
|
+
sent += 4096
|
|
82
|
+
sleep 0.002
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
expect(FastImage).to receive(:size).with(an_instance_of(StringIO)) do |io|
|
|
86
|
+
expect(io.string.bytesize).to eq(256 * 1024)
|
|
87
|
+
expect(io.string.encoding).to eq(Encoding::BINARY)
|
|
88
|
+
[37, 29]
|
|
89
|
+
end
|
|
90
|
+
page = MetaInspector.new('http://page.example/', document: %(<img src="#{@url}">))
|
|
91
|
+
expect(page.images.with_size).to eq([[@url, 37, 29]])
|
|
92
|
+
expect(sent).to be < 4 * 1024 * 1024
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'also stops oversized redirect bodies before following their location' do
|
|
96
|
+
sent = 0
|
|
97
|
+
serve do |socket|
|
|
98
|
+
socket.write("HTTP/1.1 302 Found\r\nLocation: /next\r\nContent-Length: 4194304\r\nConnection: close\r\n\r\n")
|
|
99
|
+
1024.times do
|
|
100
|
+
socket.write('x' * 4096)
|
|
101
|
+
sent += 4096
|
|
102
|
+
sleep 0.0005
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
page = MetaInspector.new('http://page.example/', document: %(<img src="#{@url}">))
|
|
106
|
+
expect(page.images.with_size).to eq([[@url, 0, 0]])
|
|
107
|
+
expect(sent).to be < 4 * 1024 * 1024
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it 'bounds the entire image request even while response data keeps arriving' do
|
|
111
|
+
serve do |socket|
|
|
112
|
+
socket.write("HTTP/1.1 200 OK\r\nContent-Length: 1000000\r\nConnection: close\r\n\r\n")
|
|
113
|
+
loop do
|
|
114
|
+
socket.write('x')
|
|
115
|
+
sleep 0.02
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
119
|
+
page = MetaInspector.new('http://page.example/', document: %(<img src="#{@url}">))
|
|
120
|
+
expect(page.images.with_size).to eq([[@url, 0, 0]])
|
|
121
|
+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
|
|
122
|
+
expect(elapsed).to be < 2.8
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|