metainspector 5.21.1 → 5.22.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 +32 -0
- data/README.md +207 -36
- data/lib/meta_inspector/attempt_timeout.rb +40 -0
- data/lib/meta_inspector/document.rb +27 -4
- data/lib/meta_inspector/html_encoding.rb +177 -0
- data/lib/meta_inspector/http_session.rb +33 -5
- data/lib/meta_inspector/image_downloader.rb +2 -0
- data/lib/meta_inspector/limited_response.rb +125 -0
- data/lib/meta_inspector/parser.rb +112 -1
- data/lib/meta_inspector/parsers/head_links.rb +10 -3
- data/lib/meta_inspector/parsers/links.rb +10 -4
- data/lib/meta_inspector/parsers/meta_tags.rb +6 -2
- data/lib/meta_inspector/parsers/texts.rb +2 -2
- data/lib/meta_inspector/request.rb +45 -30
- data/lib/meta_inspector/url.rb +32 -5
- data/lib/meta_inspector/url_policy.rb +1 -1
- data/lib/meta_inspector/version.rb +1 -1
- data/meta_inspector.gemspec +1 -1
- data/spec/document_spec.rb +118 -0
- data/spec/meta_inspector/content_type_spec.rb +47 -0
- data/spec/meta_inspector/head_links_spec.rb +3 -3
- data/spec/meta_inspector/links_spec.rb +98 -2
- data/spec/meta_inspector/meta_tags_spec.rb +53 -0
- data/spec/meta_inspector/texts_spec.rb +82 -0
- data/spec/meta_inspector/timeouts_spec.rb +107 -0
- data/spec/noscript_spec.rb +179 -0
- data/spec/parser_spec.rb +382 -2
- data/spec/query_string_spec.rb +125 -0
- data/spec/request_errors_spec.rb +91 -0
- data/spec/request_spec.rb +28 -10
- data/spec/response_encoding_spec.rb +487 -0
- data/spec/response_size_spec.rb +176 -0
- data/spec/url_filter_spec.rb +2 -1
- data/spec/url_spec.rb +37 -0
- metadata +12 -2
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'zlib'
|
|
3
|
+
|
|
4
|
+
RSpec.describe 'conflicting page charset declarations' do
|
|
5
|
+
let(:url) { 'https://encoding.example/page' }
|
|
6
|
+
let(:html) do
|
|
7
|
+
'<html><head><meta charset=utf-8><title>中国</title></head>' \
|
|
8
|
+
'<body><p>新闻</p><meta charset=gbk><a href="/a">a</a></body></html>'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'uses the first meta charset when Content-Type has no charset' do
|
|
12
|
+
body = html.b
|
|
13
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
14
|
+
|
|
15
|
+
page = MetaInspector.new(url)
|
|
16
|
+
|
|
17
|
+
expect(page.title).to eq('中国')
|
|
18
|
+
expect(page.links.raw).to eq(['/a'])
|
|
19
|
+
expect(page.response.body).to eq(body)
|
|
20
|
+
expect(page.to_s.encoding).to eq(Encoding::BINARY)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'preserves the HTTP charset over conflicting meta declarations' do
|
|
24
|
+
body = html.encode('GBK').b
|
|
25
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html; charset=GBK' })
|
|
26
|
+
|
|
27
|
+
page = MetaInspector.new(url)
|
|
28
|
+
|
|
29
|
+
expect(page.title).to eq('中国')
|
|
30
|
+
expect(page.links.raw).to eq(['/a'])
|
|
31
|
+
expect(page.to_s.encoding).to eq(Encoding.find('GBK'))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'preserves a forced encoding over conflicting meta declarations' do
|
|
35
|
+
stub_request(:get, url).to_return(body: html.b, headers: { 'Content-Type' => 'text/html; charset=GBK' })
|
|
36
|
+
|
|
37
|
+
page = MetaInspector.new(url, encoding: 'UTF-8')
|
|
38
|
+
|
|
39
|
+
expect(page.title).to eq('中国')
|
|
40
|
+
expect(page.links.raw).to eq(['/a'])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'honors a UTF-8 BOM when Content-Type has no charset' do
|
|
44
|
+
body = "\xEF\xBB\xBF".b + html.sub('charset=utf-8', 'charset=gbk').b
|
|
45
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
46
|
+
|
|
47
|
+
page = MetaInspector.new(url)
|
|
48
|
+
|
|
49
|
+
expect(page.title).to eq('中国')
|
|
50
|
+
expect(page.links.raw).to eq(['/a'])
|
|
51
|
+
expect(page.response.body).to eq(body)
|
|
52
|
+
expect(page.to_s.encoding).to eq(Encoding::BINARY)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
RSpec.describe 'byte-order-mark response encoding' do
|
|
57
|
+
let(:url) { 'https://encoding.example/page' }
|
|
58
|
+
|
|
59
|
+
{
|
|
60
|
+
'UTF-16LE' => "\xFF\xFE".b,
|
|
61
|
+
'UTF-16BE' => "\xFE\xFF".b,
|
|
62
|
+
'UTF-32LE' => "\xFF\xFE\x00\x00".b,
|
|
63
|
+
'UTF-32BE' => "\x00\x00\xFE\xFF".b
|
|
64
|
+
}.each do |encoding, mark|
|
|
65
|
+
['', '<meta charset=ISO-8859-1>'].each do |meta|
|
|
66
|
+
it "parses #{encoding} without an HTTP charset and with #{meta.empty? ? 'no' : 'a conflicting'} meta charset" do
|
|
67
|
+
html = "<html><head>#{meta}<title>Título 日本語</title></head>" \
|
|
68
|
+
'<body><h1>Heading</h1><a href="/one">one</a><img src="/image.png"></body></html>'
|
|
69
|
+
body = (mark + html.encode(encoding).b).freeze
|
|
70
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
71
|
+
|
|
72
|
+
page = MetaInspector.new(url, download_images: false)
|
|
73
|
+
supplied = MetaInspector.new(url, document: body, download_images: false)
|
|
74
|
+
|
|
75
|
+
expect(page.title).to eq('Título 日本語')
|
|
76
|
+
expect(page.h1).to eq(['Heading'])
|
|
77
|
+
expect(page.links.raw).to eq(['/one'])
|
|
78
|
+
expect(page.images.to_a).to eq(['https://encoding.example/image.png'])
|
|
79
|
+
expect([page.title, page.h1, page.links.raw, page.images.to_a])
|
|
80
|
+
.to eq([supplied.title, supplied.h1, supplied.links.raw, supplied.images.to_a])
|
|
81
|
+
expect(page.to_s.encoding).to eq(Encoding::UTF_8)
|
|
82
|
+
expect(page.response.body).to eq(body)
|
|
83
|
+
expect(page.response.body.encoding).to eq(Encoding::BINARY)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "removes NUL characters after decoding a #{encoding} byte-order mark" do
|
|
88
|
+
html = "<title>Hel\u0000lo 日本語</title><a href=\"/a\u0000fter\">after</a>"
|
|
89
|
+
body = mark + html.encode(encoding).b
|
|
90
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
91
|
+
|
|
92
|
+
page = MetaInspector.new(url)
|
|
93
|
+
|
|
94
|
+
expect(page.title).to eq('Hello 日本語')
|
|
95
|
+
expect(page.links.raw).to eq(['/after'])
|
|
96
|
+
expect(page.to_s).not_to include("\u0000")
|
|
97
|
+
expect(page.response.body).to eq(body)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
[false, true].each do |forced|
|
|
102
|
+
it "preserves #{forced ? 'a forced' : 'the HTTP'} charset over a byte-order mark" do
|
|
103
|
+
body = "\xFF\xFE".b + '<title>Hello</title>'.encode('UTF-16LE').b
|
|
104
|
+
stub_request(:get, url).to_return(body: body, headers: {
|
|
105
|
+
'Content-Type' => forced ? 'text/html' : 'text/html; charset=ISO-8859-1'
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
page = MetaInspector.new(url, **(forced ? { encoding: 'ISO-8859-1' } : {}))
|
|
109
|
+
|
|
110
|
+
expect(page.to_s).to eq('ÿþ<title>Hello</title>')
|
|
111
|
+
expect(page.response.body.b).to eq(body)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'decodes a byte-order mark after gzip decompression' do
|
|
116
|
+
body = "\xFF\xFE".b + '<title>Título</title>'.encode('UTF-16LE').b
|
|
117
|
+
stub_request(:get, url).to_return(body: Zlib.gzip(body), headers: {
|
|
118
|
+
'Content-Type' => 'text/html', 'Content-Encoding' => 'gzip'
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
page = MetaInspector.new(url)
|
|
122
|
+
|
|
123
|
+
expect(page.title).to eq('Título')
|
|
124
|
+
expect(page.response.body).to eq(body)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it 'replaces invalid BOM-declared UTF-16 bytes' do
|
|
128
|
+
body = "\xFF\xFE".b + '<title>Hello</title>'.encode('UTF-16LE').b + "\x00".b
|
|
129
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
130
|
+
|
|
131
|
+
page = MetaInspector.new(url)
|
|
132
|
+
|
|
133
|
+
expect(page.title).to eq('Hello')
|
|
134
|
+
expect(page.to_s).to end_with("\u{FFFD}")
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
RSpec.describe 'legacy HTML charset labels' do
|
|
139
|
+
let(:url) { 'https://encoding.example/page' }
|
|
140
|
+
|
|
141
|
+
[
|
|
142
|
+
['gb2312', 'GBK', '朱镕基'],
|
|
143
|
+
['shift_jis', 'Windows-31J', '①㈱髙'],
|
|
144
|
+
['euc-kr', 'CP949', '똠'],
|
|
145
|
+
['big5', 'Big5-HKSCS', '嗰'],
|
|
146
|
+
['iso-8859-1', 'Windows-1252', 'It’s €5']
|
|
147
|
+
].each do |label, encoding, title|
|
|
148
|
+
context "with #{label} containing #{encoding} characters" do
|
|
149
|
+
let(:html) do
|
|
150
|
+
%(<html><head><meta charset="#{label}"><title>#{title}</title></head>) +
|
|
151
|
+
%(<body><h1><p>#{title}</p></h1><a href="/after">after</a></body></html>)
|
|
152
|
+
end
|
|
153
|
+
let(:bytes) { html.encode(encoding).b }
|
|
154
|
+
|
|
155
|
+
def expect_complete_page(page, title)
|
|
156
|
+
expect(page.title).to eq(title)
|
|
157
|
+
expect(page.h1).to eq([title])
|
|
158
|
+
expect(page.links.raw).to eq(['/after'])
|
|
159
|
+
expect(page.parsed.errors.select(&:fatal?)).to be_empty
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
it 'parses a supplied binary document without changing its bytes or declared charset' do
|
|
163
|
+
original = bytes.dup
|
|
164
|
+
page = MetaInspector.new(url, document: bytes.freeze)
|
|
165
|
+
|
|
166
|
+
expect_complete_page(page, title)
|
|
167
|
+
expect(page.charset).to eq(label)
|
|
168
|
+
expect(page.to_s).to equal(bytes)
|
|
169
|
+
expect(bytes).to eq(original)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
it 'parses a supplied string tagged with the strict encoding before scrubbing' do
|
|
173
|
+
tagged = bytes.dup.force_encoding(label).freeze
|
|
174
|
+
page = MetaInspector.new(url, document: tagged)
|
|
175
|
+
|
|
176
|
+
expect_complete_page(page, title)
|
|
177
|
+
expect(page.to_s).to equal(tagged)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it 'parses a response declaring its charset only in HTML' do
|
|
181
|
+
stub_request(:get, url).to_return(body: bytes, headers: { 'Content-Type' => 'text/html' })
|
|
182
|
+
|
|
183
|
+
expect_complete_page(MetaInspector.new(url), title)
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it 'preserves the first legacy declaration when a later meta declares UTF-8' do
|
|
187
|
+
body = html.sub('</head>', '<meta charset="UTF-8"></head>').encode(encoding).b
|
|
188
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
189
|
+
|
|
190
|
+
expect_complete_page(MetaInspector.new(url, document: body.freeze), title)
|
|
191
|
+
expect_complete_page(MetaInspector.new(url), title)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it 'parses an http-equiv declaration with mixed case and reordered attributes' do
|
|
195
|
+
body = html.sub(%(<meta charset="#{label}">),
|
|
196
|
+
%(<META content="text/html; charset=#{label.upcase}" HTTP-EQUIV="Content-Type">))
|
|
197
|
+
.encode(encoding).b
|
|
198
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
199
|
+
|
|
200
|
+
expect_complete_page(MetaInspector.new(url), title)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it 'parses a charset declared only in the HTTP header, preserving the raw body' do
|
|
204
|
+
body = html.sub(%(<meta charset="#{label}">), '').encode(encoding).b
|
|
205
|
+
stub_request(:get, url).to_return(body: body, headers: {
|
|
206
|
+
'Content-Type' => "text/html; charset=#{label}"
|
|
207
|
+
})
|
|
208
|
+
page = MetaInspector.new(url)
|
|
209
|
+
|
|
210
|
+
expect_complete_page(page, title)
|
|
211
|
+
expect(page.response.body.b).to eq(body)
|
|
212
|
+
expect(page.response.body.encoding).to eq(Encoding.find(label))
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
it 'parses the HTTP charset after gzip decompression' do
|
|
216
|
+
stub_request(:get, url).to_return(body: Zlib.gzip(bytes), headers: {
|
|
217
|
+
'Content-Type' => "text/html; charset=#{label}", 'Content-Encoding' => 'gzip'
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
expect_complete_page(MetaInspector.new(url), title)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
it 'supports a forced legacy encoding without discarding superset characters' do
|
|
224
|
+
stub_request(:get, url).to_return(body: bytes, headers: { 'Content-Type' => 'text/html' })
|
|
225
|
+
|
|
226
|
+
expect_complete_page(MetaInspector.new(url, encoding: label), title)
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
it 'keeps a UTF-8 string authoritative over a legacy meta declaration' do
|
|
232
|
+
html = '<meta charset="gb2312"><title>朱镕基</title><a href="/after">after</a>'
|
|
233
|
+
|
|
234
|
+
page = MetaInspector.new(url, document: html)
|
|
235
|
+
|
|
236
|
+
expect(page.title).to eq('朱镕基')
|
|
237
|
+
expect(page.links.raw).to eq(['/after'])
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it 'uses Windows-31J character mappings for both binary and tagged strings' do
|
|
241
|
+
title = '~∥-¢£¬'
|
|
242
|
+
html = %(<meta charset="shift_jis"><title>#{title}</title><a href="/after">after</a>)
|
|
243
|
+
bytes = html.encode('Windows-31J')
|
|
244
|
+
|
|
245
|
+
[bytes.b, bytes.dup.force_encoding('Shift_JIS')].each do |document|
|
|
246
|
+
page = MetaInspector.new(url, document: document)
|
|
247
|
+
|
|
248
|
+
expect(page.title).to eq(title)
|
|
249
|
+
expect(page.links.raw).to eq(['/after'])
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
it 'keeps a forced UTF-8 encoding authoritative over the HTTP charset' do
|
|
254
|
+
stub_request(:get, url).to_return(body: '<title>朱镕基</title>'.b, headers: {
|
|
255
|
+
'Content-Type' => 'text/html; charset=gb2312'
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
expect(MetaInspector.new(url, encoding: 'UTF-8').title).to eq('朱镕基')
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# https://encoding.spec.whatwg.org/#names-and-labels
|
|
263
|
+
RSpec.describe 'Encoding Standard charset labels' do
|
|
264
|
+
let(:url) { 'https://encoding.example/page' }
|
|
265
|
+
|
|
266
|
+
def page_with(title_bytes, meta: nil, header: nil)
|
|
267
|
+
declaration = meta ? %(<meta http-equiv="Content-Type" content="text/html; charset=#{meta}">) : ''
|
|
268
|
+
body = %(<html><head>#{declaration}<title>).b + title_bytes.b +
|
|
269
|
+
'</title></head><body><a href="/one">one</a></body></html>'.b
|
|
270
|
+
content_type = header ? "text/html; charset=#{header}" : 'text/html'
|
|
271
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => content_type })
|
|
272
|
+
MetaInspector.new(url)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
[
|
|
276
|
+
['ks_c_5601-1987', "\xC7\xD1\xB1\xDB", '한글'],
|
|
277
|
+
['korean', "\xC7\xD1\xB1\xDB", '한글'],
|
|
278
|
+
['windows-949', "\xC7\xD1\xB1\xDB", '한글'],
|
|
279
|
+
['x-sjis', "\x93\xFA\x96\x7B", '日本'],
|
|
280
|
+
['ms932', "\x93\xFA\x96\x7B", '日本'],
|
|
281
|
+
['x-euc-jp', "\xC6\xFC\xCB\xDC", '日本'],
|
|
282
|
+
['x-gbk', "\xD6\xD0", '中'],
|
|
283
|
+
['chinese', "\xD6\xD0", '中'],
|
|
284
|
+
['us-ascii', "caf\xE9 \x80", 'café €'],
|
|
285
|
+
['ascii', "caf\xE9", 'café'],
|
|
286
|
+
['iso-8859-9', "\x80 \xFD", '€ ı'],
|
|
287
|
+
['x-cp1251', "\xC4\xE0", 'Да'],
|
|
288
|
+
['unicode-1-1-utf-8', "caf\xC3\xA9", 'café']
|
|
289
|
+
].each do |label, bytes, title|
|
|
290
|
+
it "decodes #{label} declared in a meta tag" do
|
|
291
|
+
page = page_with(bytes, meta: label)
|
|
292
|
+
|
|
293
|
+
expect(page.title).to eq(title)
|
|
294
|
+
expect(page.links.raw).to eq(['/one'])
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
it "decodes #{label} declared in the Content-Type header" do
|
|
298
|
+
page = page_with(bytes, header: label)
|
|
299
|
+
|
|
300
|
+
expect(page.title).to eq(title)
|
|
301
|
+
expect(page.links.raw).to eq(['/one'])
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
%w[utf-16 utf-16le utf-16be unicode].each do |label|
|
|
306
|
+
it "reads a #{label} meta declaration as UTF-8" do
|
|
307
|
+
page = page_with("caf\xC3\xA9", meta: label)
|
|
308
|
+
|
|
309
|
+
expect(page.title).to eq('café')
|
|
310
|
+
expect(page.links.raw).to eq(['/one'])
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
it 'ignores an unknown meta label instead of truncating the document' do
|
|
315
|
+
page = page_with("caf\xE9", meta: 'no-such-charset')
|
|
316
|
+
|
|
317
|
+
expect(page.title).to eq('café')
|
|
318
|
+
expect(page.links.raw).to eq(['/one'])
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
it 'uses a later meta declaration when the first label is unknown' do
|
|
322
|
+
body = '<meta charset="no-such-charset"><meta charset="x-sjis"><title>'.b + "\x93\xFA\x96\x7B".b +
|
|
323
|
+
'</title><a href="/one">one</a>'.b
|
|
324
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
|
|
325
|
+
|
|
326
|
+
expect(MetaInspector.new(url).title).to eq('日本')
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
RSpec.describe 'compressed page encoding' do
|
|
331
|
+
let(:url) { 'https://encoding.example/page' }
|
|
332
|
+
let(:title) { 'Café – £5' }
|
|
333
|
+
let(:description) { 'Gonçalves – café' }
|
|
334
|
+
let(:html) do
|
|
335
|
+
%(<html><head><title>#{title}</title>) +
|
|
336
|
+
%(<meta name="description" content="#{description}"></head></html>)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
%w[gzip deflate].each do |compression|
|
|
340
|
+
context "with #{compression} responses" do
|
|
341
|
+
%w[UTF-8 Windows-1252].each do |charset|
|
|
342
|
+
context "declaring #{charset} only in Content-Type" do
|
|
343
|
+
before do
|
|
344
|
+
body = html.encode(charset)
|
|
345
|
+
compressed = compression == 'gzip' ? Zlib.gzip(body) : Zlib::Deflate.deflate(body)
|
|
346
|
+
stub_request(:get, url).to_return(body: compressed, headers: {
|
|
347
|
+
'Content-Type' => "text/html; charset=#{charset}",
|
|
348
|
+
'Content-Encoding' => compression
|
|
349
|
+
})
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
it 'preserves the charset when the server compresses despite the default headers' do
|
|
353
|
+
page = MetaInspector.new(url)
|
|
354
|
+
|
|
355
|
+
expect(page.title).to eq(title)
|
|
356
|
+
expect(page.description).to eq(description)
|
|
357
|
+
expect(page.to_s.encoding).to eq(Encoding.find(charset))
|
|
358
|
+
expect(a_request(:get, url).with(headers: { 'Accept-Encoding' => 'identity' })).to have_been_made.once
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
it 'preserves the charset with a custom User-Agent' do
|
|
362
|
+
page = MetaInspector.new(url, headers: { 'User-Agent' => 'MyApp/1.0' })
|
|
363
|
+
|
|
364
|
+
expect(page.title).to eq(title)
|
|
365
|
+
expect(page.description).to eq(description)
|
|
366
|
+
expect(page.to_s.encoding).to eq(Encoding.find(charset))
|
|
367
|
+
expect(a_request(:get, url).with(headers: { 'User-Agent' => 'MyApp/1.0' })).to have_been_made.once
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
end
|
|
371
|
+
end
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
RSpec.describe 'non-ASCII-compatible page encoding' do
|
|
376
|
+
let(:url) { 'https://encoding.example/page' }
|
|
377
|
+
let(:html) do
|
|
378
|
+
"<html><head><title>Hel\u0000lo 日本語</title></head>" \
|
|
379
|
+
"<body><a href=\"/a\u0000fter\">after</a></body></html>"
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
%w[UTF-16LE UTF-16BE UTF-16 UTF-32LE UTF-32BE UTF-32 ISO-2022-JP].each do |charset|
|
|
383
|
+
it "parses #{charset} responses and removes NUL characters without changing the raw body" do
|
|
384
|
+
body = html.encode(charset).b
|
|
385
|
+
stub_request(:get, url).to_return(body: body, headers: {
|
|
386
|
+
'Content-Type' => "text/html; charset=#{charset}"
|
|
387
|
+
})
|
|
388
|
+
|
|
389
|
+
page = MetaInspector.new(url)
|
|
390
|
+
|
|
391
|
+
expect(page.title).to eq('Hello 日本語')
|
|
392
|
+
expect(page.links.raw).to eq(['/after'])
|
|
393
|
+
expect(page.to_s.encoding).to eq(Encoding::UTF_8)
|
|
394
|
+
expect(page.to_s).not_to include("\u0000")
|
|
395
|
+
expect(page.response.body.b).to eq(body)
|
|
396
|
+
expect(page.response.body.encoding).to eq(Encoding.find(charset))
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
it 'normalizes a forced UTF-16LE encoding before removing NUL characters' do
|
|
401
|
+
stub_request(:get, url).to_return(body: html.encode('UTF-16LE').b, headers: {
|
|
402
|
+
'Content-Type' => 'text/html'
|
|
403
|
+
})
|
|
404
|
+
|
|
405
|
+
page = MetaInspector.new(url, encoding: 'UTF-16LE')
|
|
406
|
+
|
|
407
|
+
expect(page.title).to eq('Hello 日本語')
|
|
408
|
+
expect(page.links.raw).to eq(['/after'])
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
it 'normalizes a gzip-compressed UTF-16LE response after decompression' do
|
|
412
|
+
body = html.encode('UTF-16LE').b
|
|
413
|
+
stub_request(:get, url).to_return(body: Zlib.gzip(body), headers: {
|
|
414
|
+
'Content-Type' => 'text/html; charset=UTF-16LE', 'Content-Encoding' => 'gzip'
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
page = MetaInspector.new(url)
|
|
418
|
+
|
|
419
|
+
expect(page.title).to eq('Hello 日本語')
|
|
420
|
+
expect(page.links.raw).to eq(['/after'])
|
|
421
|
+
expect(page.response.body.b).to eq(body)
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
it 'replaces invalid UTF-16LE bytes' do
|
|
425
|
+
body = html.encode('UTF-16LE').b + "\x00".b
|
|
426
|
+
stub_request(:get, url).to_return(body: body, headers: {
|
|
427
|
+
'Content-Type' => 'text/html; charset=UTF-16LE'
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
page = MetaInspector.new(url)
|
|
431
|
+
|
|
432
|
+
expect(page.links.raw).to eq(['/after'])
|
|
433
|
+
expect(page.to_s).to end_with("\u{FFFD}")
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
it 'wraps unsupported UTF-7 conversion in RequestError' do
|
|
437
|
+
stub_request(:get, url).to_return(body: '<title>Hello</title>', headers: {
|
|
438
|
+
'Content-Type' => 'text/html; charset=UTF-7'
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
expect { MetaInspector.new(url) }.to raise_error(MetaInspector::RequestError) { |error|
|
|
442
|
+
expect(error.cause).to be_a(Encoding::ConverterNotFoundError)
|
|
443
|
+
expect(error.message).to eq(error.cause.message)
|
|
444
|
+
}
|
|
445
|
+
end
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
RSpec.describe 'invalid bytes in a fetched page' do
|
|
449
|
+
let(:url) { 'https://encoding.example/page' }
|
|
450
|
+
let(:body) do
|
|
451
|
+
"<html><head><title>caf\xC3\xA9</title></head>" \
|
|
452
|
+
"<body><p>one bad byte: \xE9</p><a href='/a'>a</a></body></html>".b
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
it 'replaces invalid bytes when the charset comes from the Content-Type header' do
|
|
456
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html; charset=utf-8' })
|
|
457
|
+
|
|
458
|
+
page = MetaInspector.new(url)
|
|
459
|
+
|
|
460
|
+
expect(page.title).to eq('café')
|
|
461
|
+
expect(page.links.raw).to eq(['/a'])
|
|
462
|
+
expect(page.parsed.at('p').text).to eq("one bad byte: \u{FFFD}")
|
|
463
|
+
expect(page.to_s).to be_valid_encoding
|
|
464
|
+
expect(page.response.body.b).to eq(body)
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
it 'replaces invalid bytes in a compressed page' do
|
|
468
|
+
stub_request(:get, url).to_return(body: Zlib.gzip(body), headers: {
|
|
469
|
+
'Content-Type' => 'text/html; charset=utf-8', 'Content-Encoding' => 'gzip'
|
|
470
|
+
})
|
|
471
|
+
|
|
472
|
+
page = MetaInspector.new(url)
|
|
473
|
+
|
|
474
|
+
expect(page.title).to eq('café')
|
|
475
|
+
expect(page.links.raw).to eq(['/a'])
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
it 'still lets a forced encoding override a wrong HTTP charset' do
|
|
479
|
+
stub_request(:get, url).to_return(body: "<title>caf\xE9</title>".b, headers: {
|
|
480
|
+
'Content-Type' => 'text/html; charset=utf-8'
|
|
481
|
+
})
|
|
482
|
+
|
|
483
|
+
page = MetaInspector.new(url, encoding: 'Windows-1252')
|
|
484
|
+
|
|
485
|
+
expect(page.title).to eq('café')
|
|
486
|
+
end
|
|
487
|
+
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'socket'
|
|
3
|
+
|
|
4
|
+
RSpec.describe 'bounded page responses' do
|
|
5
|
+
let(:url) { 'https://size.example/page' }
|
|
6
|
+
|
|
7
|
+
[nil, 'gzip', 'deflate', 'raw deflate', 'gzip, deflate'].each do |compression|
|
|
8
|
+
[false, true].each do |head_first|
|
|
9
|
+
it "preserves #{compression || 'plain'} HTML and its charset with head_first: #{head_first}" do
|
|
10
|
+
html = '<title>Café</title>'.encode('Windows-1252')
|
|
11
|
+
body = case compression
|
|
12
|
+
when 'gzip' then Zlib.gzip(html)
|
|
13
|
+
when 'deflate' then Zlib::Deflate.deflate(html)
|
|
14
|
+
when 'raw deflate'
|
|
15
|
+
deflater = Zlib::Deflate.new(nil, -Zlib::MAX_WBITS)
|
|
16
|
+
begin
|
|
17
|
+
deflater.deflate(html, Zlib::FINISH)
|
|
18
|
+
ensure
|
|
19
|
+
deflater.close
|
|
20
|
+
end
|
|
21
|
+
when 'gzip, deflate' then Zlib::Deflate.deflate(Zlib.gzip(html))
|
|
22
|
+
else html
|
|
23
|
+
end
|
|
24
|
+
headers = { 'Content-Type' => 'text/html; charset=Windows-1252', 'Content-Length' => '999999' }
|
|
25
|
+
headers['Content-Encoding'] = compression == 'raw deflate' ? 'deflate' : compression if compression
|
|
26
|
+
stub_request(:head, url).to_return(headers: headers)
|
|
27
|
+
stub_request(:get, url).to_return(body: body, headers: headers)
|
|
28
|
+
|
|
29
|
+
page = MetaInspector.new(url, max_html_bytes: 128, head_first: head_first)
|
|
30
|
+
expect(page.title).to eq('Café')
|
|
31
|
+
expect(page.response.body).to eq(html)
|
|
32
|
+
expect(page.to_s.encoding).to eq(Encoding::Windows_1252)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'accepts exactly the limit in bytes' do
|
|
38
|
+
html = '<title>é</title>'
|
|
39
|
+
stub_request(:get, url).to_return(body: html, headers: { 'Content-Type' => 'text/html; charset=UTF-8' })
|
|
40
|
+
expect(MetaInspector.new(url, max_html_bytes: html.bytesize).title).to eq('é')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'preserves a quoted HTTP charset on streamed uncompressed HTML' do
|
|
44
|
+
html = '<title>Café</title>'.encode('Windows-1252')
|
|
45
|
+
stub_request(:get, url).to_return(body: html, headers: { 'Content-Type' => 'text/html; charset="Windows-1252"' })
|
|
46
|
+
page = MetaInspector.new(url, max_html_bytes: 128)
|
|
47
|
+
expect(page.title).to eq('Café')
|
|
48
|
+
expect(page.to_s.encoding).to eq(Encoding::Windows_1252)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'caches complete streamed responses for subsequent requests' do
|
|
52
|
+
cache = Class.new(Hash) do
|
|
53
|
+
def read(key); self[key]; end
|
|
54
|
+
def write(key, value); self[key] = value; end
|
|
55
|
+
end.new
|
|
56
|
+
stub_request(:get, url).to_return(body: Zlib.gzip('<title>cached</title>'), headers: {
|
|
57
|
+
'Content-Encoding' => 'gzip', 'Cache-Control' => 'public, max-age=3600'
|
|
58
|
+
})
|
|
59
|
+
2.times do
|
|
60
|
+
page = MetaInspector.new(url, max_html_bytes: 128, faraday_options: { faraday_http_cache: { store: cache } })
|
|
61
|
+
expect(page.title).to eq('cached')
|
|
62
|
+
end
|
|
63
|
+
expect(a_request(:get, url)).to have_been_made.once
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'accepts an empty response with a zero limit' do
|
|
67
|
+
stub_request(:get, url).to_return(body: '')
|
|
68
|
+
expect(MetaInspector.new(url, max_html_bytes: 0).to_s).to eq('')
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it 'limits compressed bytes even if the decoded HTML fits' do
|
|
72
|
+
body = Zlib.gzip('<title>x</title>')
|
|
73
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Encoding' => 'gzip' })
|
|
74
|
+
expect { MetaInspector.new(url, max_html_bytes: 16) }.to raise_error(MetaInspector::ParserError)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it 'reads concatenated gzip members' do
|
|
78
|
+
body = Zlib.gzip('<title>') + Zlib.gzip('joined</title>')
|
|
79
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Encoding' => 'gzip' })
|
|
80
|
+
expect(MetaInspector.new(url, max_html_bytes: 128).title).to eq('joined')
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it 'limits the combined output of concatenated gzip members' do
|
|
84
|
+
body = Zlib.gzip('x' * 100) * 2
|
|
85
|
+
stub_request(:get, url).to_return(body: body, headers: { 'Content-Encoding' => 'gzip' })
|
|
86
|
+
expect { MetaInspector.new(url, max_html_bytes: 128) }.to raise_error(MetaInspector::ParserError)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
%w[gzip deflate].each do |compression|
|
|
90
|
+
it "keeps malformed #{compression} errors as RequestError" do
|
|
91
|
+
stub_request(:get, url).to_return(body: 'not compressed', headers: { 'Content-Encoding' => compression })
|
|
92
|
+
expect { MetaInspector.new(url, max_html_bytes: 128) }.to raise_error(MetaInspector::RequestError) { |error|
|
|
93
|
+
expect(error.cause).to be_a(Zlib::Error)
|
|
94
|
+
}
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it 'keeps a fresh body for each redirect' do
|
|
99
|
+
stub_request(:get, url).to_return(status: 302, body: 'redirect', headers: { 'Location' => '/next' })
|
|
100
|
+
stub_request(:get, 'https://size.example/next').to_return(body: '<title>next</title>')
|
|
101
|
+
expect(MetaInspector.new(url, max_html_bytes: 32).to_s).to eq('<title>next</title>')
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
it 'rejects oversized redirect bodies before following their location' do
|
|
105
|
+
stub_request(:get, url).to_return(status: 302, body: 'x' * 100, headers: { 'Location' => '/next' })
|
|
106
|
+
expect { MetaInspector.new(url, max_html_bytes: 32) }.to raise_error(MetaInspector::ParserError)
|
|
107
|
+
expect(a_request(:get, 'https://size.example/next')).not_to have_been_made
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it 'checks cached bodies against the limit of the current request' do
|
|
111
|
+
cache = Class.new(Hash) do
|
|
112
|
+
def read(key); self[key]; end
|
|
113
|
+
def write(key, value); self[key] = value; end
|
|
114
|
+
end.new
|
|
115
|
+
stub_request(:get, url).to_return(body: Zlib.gzip('<title>cached</title>' + ' ' * 1024), headers: {
|
|
116
|
+
'Content-Encoding' => 'gzip', 'Cache-Control' => 'public, max-age=3600'
|
|
117
|
+
})
|
|
118
|
+
options = { faraday_options: { faraday_http_cache: { store: cache } } }
|
|
119
|
+
expect(MetaInspector.new(url, **options).title).to eq('cached')
|
|
120
|
+
expect { MetaInspector.new(url, **options, max_html_bytes: 128) }.to raise_error(MetaInspector::ParserError)
|
|
121
|
+
expect(a_request(:get, url)).to have_been_made.once
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
%w[gzip deflate].each do |compression|
|
|
125
|
+
it "stops expanding #{compression} before reaching a corrupt trailer" do
|
|
126
|
+
body = '<p>' + 'x' * (4 * 1024 * 1024) + '</p>'
|
|
127
|
+
compressed = compression == 'gzip' ? Zlib.gzip(body) : Zlib::Deflate.deflate(body)
|
|
128
|
+
compressed.setbyte(-1, compressed.getbyte(-1) ^ 255)
|
|
129
|
+
stub_request(:get, url).to_return(body: compressed, headers: {
|
|
130
|
+
'Content-Type' => 'text/html', 'Content-Encoding' => compression
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
expect { MetaInspector.new(url, max_html_bytes: 64 * 1024) }
|
|
134
|
+
.to raise_error(MetaInspector::ParserError, /limit.*65536 bytes/)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
context 'with a real HTTP server' do
|
|
139
|
+
around do |example|
|
|
140
|
+
@server = TCPServer.new('127.0.0.1', 0)
|
|
141
|
+
@url = "http://127.0.0.1:#{@server.addr[1]}/page"
|
|
142
|
+
# WebMock buffers real responses, hiding incremental reads.
|
|
143
|
+
WebMock.disable!
|
|
144
|
+
example.run
|
|
145
|
+
ensure
|
|
146
|
+
@server&.close
|
|
147
|
+
@worker&.kill
|
|
148
|
+
@worker&.join
|
|
149
|
+
WebMock.enable!
|
|
150
|
+
WebMock.disable_net_connect!
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
it 'stops an oversized chunked response before the server finishes sending' do
|
|
154
|
+
sent = 0
|
|
155
|
+
@worker = Thread.new do
|
|
156
|
+
socket = @server.accept
|
|
157
|
+
while (line = socket.gets) && line != "\r\n"; end
|
|
158
|
+
socket.write("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n")
|
|
159
|
+
1024.times do
|
|
160
|
+
socket.write("1000\r\n#{'x' * 4096}\r\n")
|
|
161
|
+
sent += 4096
|
|
162
|
+
sleep 0.001
|
|
163
|
+
end
|
|
164
|
+
socket.write("0\r\n\r\n")
|
|
165
|
+
rescue Errno::EPIPE, Errno::ECONNRESET, IOError
|
|
166
|
+
# The size limit closes the connection.
|
|
167
|
+
ensure
|
|
168
|
+
socket&.close
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
expect { MetaInspector.new(@url, max_html_bytes: 64 * 1024) }
|
|
172
|
+
.to raise_error(MetaInspector::ParserError)
|
|
173
|
+
expect(sent).to be < 4 * 1024 * 1024
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
data/spec/url_filter_spec.rb
CHANGED
|
@@ -32,7 +32,8 @@ RSpec.describe 'outbound URL policy' do
|
|
|
32
32
|
expect(a_request(:get, 'https://internal.example/page')).not_to have_been_made
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
[RuntimeError, Faraday::TimeoutError, Faraday::ConnectionFailed, URI::InvalidURIError,
|
|
35
|
+
[RuntimeError, Faraday::TimeoutError, Faraday::ConnectionFailed, URI::InvalidURIError,
|
|
36
|
+
URI::InvalidComponentError, Zlib::DataError, Timeout::Error].each do |error_class|
|
|
36
37
|
[:page, :image].each do |target|
|
|
37
38
|
it "preserves a filter's #{error_class} on a #{target}" do
|
|
38
39
|
error = error_class.new('policy failed')
|