metainspector 5.21.0 → 5.21.2
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 +8 -0
- data/README.md +24 -5
- data/lib/meta_inspector/document.rb +1 -1
- data/lib/meta_inspector/http_session.rb +12 -3
- data/lib/meta_inspector/limited_response.rb +125 -0
- data/lib/meta_inspector/url_policy.rb +11 -4
- data/lib/meta_inspector/version.rb +1 -1
- data/spec/redirect_headers_spec.rb +143 -0
- data/spec/response_size_spec.rb +176 -0
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1a32c7da227fe7a2fbf3f106e6eb03af652694e293e4ea47928ee3adb71f21e5
|
|
4
|
+
data.tar.gz: 6777d770a847f417cfa20a8cf70667e22db82393e2f6d5a839ac7d22406094ff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 239049c807eaf8c178fe386be88b79fac5888934191f372ab3995fefe443026389e0b99d2f92f03e616eb93104acc24e6da15fa616da7172c2f067dc9da41aef
|
|
7
|
+
data.tar.gz: 4676aaa3467c872f19db8b584eacf8cd66bfe9351adbe4f2fb882233043ba9aece759d93cb859f2aefaa41dca109becbaab25ca657bf66c6a37be6d7518b8606
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# MetaInspector Changelog
|
|
2
2
|
|
|
3
|
+
## [Changes in 5.21.2](https://github.com/jaimeiniesta/metainspector/compare/v5.21.1...v5.21.2)
|
|
4
|
+
|
|
5
|
+
* Security: apply `max_html_bytes` while downloading page and redirect responses and during gzip/deflate decompression, before charset conversion and parsing. Oversized responses raise `MetaInspector::ParserError`; cached responses are checked as well. The 5.x default remains `nil` (unlimited); set a finite limit for untrusted URLs. Other content encodings are rejected with a finite limit ([GHSA-36j3-rh89-4gm2](https://github.com/jaimeiniesta/metainspector/security/advisories/GHSA-36j3-rh89-4gm2)).
|
|
6
|
+
|
|
7
|
+
## [Changes in 5.21.1](https://github.com/jaimeiniesta/metainspector/compare/v5.21.0...v5.21.1)
|
|
8
|
+
|
|
9
|
+
* Security: forward only `User-Agent`, `Accept`, `Accept-Language` and `Accept-Encoding` when page redirects change the origin (scheme, host or port), preventing custom credentials from reaching another origin. Apply the policy to GET and HEAD, including headers supplied through `faraday_options`, and keep the initial credentials out of image requests on the redirected origin. Custom headers remain available within the initial origin ([GHSA-mcmp-j8cq-h65f](https://github.com/jaimeiniesta/metainspector/security/advisories/GHSA-mcmp-j8cq-h65f)).
|
|
10
|
+
|
|
3
11
|
## [Changes in 5.21.0](https://github.com/jaimeiniesta/metainspector/compare/v5.20.0...v5.21.0)
|
|
4
12
|
|
|
5
13
|
* Restore working `page.feed` and `page.favicon` compatibility methods with deprecation warnings and removal scheduled for 6.0. `feed` returns the first RSS/Atom URL (or `nil`), and `favicon` returns `images.favicon`. Migrate to `page.feeds` and `page.images.favicon` (#547).
|
data/README.md
CHANGED
|
@@ -339,11 +339,20 @@ documents, set `max_html_bytes` to a non-negative integer:
|
|
|
339
339
|
page = MetaInspector.new(url, max_html_bytes: 2 * 1024 * 1024)
|
|
340
340
|
```
|
|
341
341
|
|
|
342
|
-
The default is `nil` (no limit).
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
342
|
+
The default in 5.x is `nil` (no limit). Set a finite limit when fetching URLs you
|
|
343
|
+
do not control. Exceeding it raises `MetaInspector::ParserError` during
|
|
344
|
+
initialization. The limit applies separately to the bytes received for each
|
|
345
|
+
response (including redirect bodies), each stage of gzip/deflate decompression,
|
|
346
|
+
and the HTML after charset conversion and processing, before parsing. Downloads
|
|
347
|
+
stop as soon as a received chunk exceeds the limit; decompression stops as soon
|
|
348
|
+
as an output chunk exceeds it, without expanding the entire body. Cached
|
|
349
|
+
responses are checked too. With a finite limit, only gzip and deflate compression
|
|
350
|
+
are supported; other content encodings raise `MetaInspector::RequestError`.
|
|
351
|
+
HTML passed with `document:` is checked before parsing as well.
|
|
352
|
+
|
|
353
|
+
This bounds response buffers, not total process memory: charset conversion and
|
|
354
|
+
DOM construction still need additional memory. It also does not guarantee a
|
|
355
|
+
parsing deadline: even a small document can be
|
|
347
356
|
slow to parse. Ruby's `Timeout.timeout` cannot reliably interrupt Nokogiri's
|
|
348
357
|
native parser; use a separately supervised process if a hard deadline is needed.
|
|
349
358
|
|
|
@@ -375,6 +384,14 @@ end
|
|
|
375
384
|
|
|
376
385
|
By default, MetaInspector will follow redirects (up to a limit of 10).
|
|
377
386
|
|
|
387
|
+
Custom headers are retained only on the initial page origin (scheme, host and port).
|
|
388
|
+
For redirects to a different origin, including subdomains, different ports and
|
|
389
|
+
HTTPS-to-HTTP redirects, only `User-Agent`, `Accept`, `Accept-Language` and
|
|
390
|
+
`Accept-Encoding` are forwarded. This applies to GET and HEAD requests and to
|
|
391
|
+
headers supplied through either `headers:` or `faraday_options[:headers]`.
|
|
392
|
+
Credentials from the initial request are also withheld from images on a redirected
|
|
393
|
+
page at a different origin. Image requests retain their generated `Range` header.
|
|
394
|
+
|
|
378
395
|
If you want to disallow redirects, you can do it like this:
|
|
379
396
|
|
|
380
397
|
```ruby
|
|
@@ -400,6 +417,8 @@ page = MetaInspector.new(url, faraday_options: { redirect: { callback: callback
|
|
|
400
417
|
|
|
401
418
|
The `faraday_options[:redirect]` hash is passed to the `FollowRedirects` middleware used by `Faraday`, so that we can use all available options.
|
|
402
419
|
Check them [here](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/response/follow_redirects.rb#L44).
|
|
420
|
+
The origin header policy also applies after your redirect callback runs and when
|
|
421
|
+
`clear_authorization_header: false` is set.
|
|
403
422
|
|
|
404
423
|
### Filtering request URLs
|
|
405
424
|
|
|
@@ -18,7 +18,7 @@ module MetaInspector
|
|
|
18
18
|
# * download_image_options: supports reject_invalid_images (default false)
|
|
19
19
|
# * inline_image_data: return full data URLs in image results (default false)
|
|
20
20
|
# * fetch_all_image_meta: enable image validity metadata (default false)
|
|
21
|
-
# * max_html_bytes: maximum
|
|
21
|
+
# * max_html_bytes: maximum received, decompressed and parsed HTML bytes, or nil for unlimited
|
|
22
22
|
# * url_filter: callable receiving a URI before each request; false/nil rejects it
|
|
23
23
|
# * faraday_options: an optional hash of options to pass to Faraday on the request.
|
|
24
24
|
# Its faraday_http_cache key configures response caching with Faraday::HttpCache
|
|
@@ -7,6 +7,7 @@ require 'faraday/follow_redirects'
|
|
|
7
7
|
require 'faraday/gzip'
|
|
8
8
|
require 'faraday/retry'
|
|
9
9
|
require_relative 'url_policy'
|
|
10
|
+
require_relative 'limited_response'
|
|
10
11
|
|
|
11
12
|
module MetaInspector
|
|
12
13
|
# Builds page and binary image transports from the same caller configuration.
|
|
@@ -15,6 +16,7 @@ module MetaInspector
|
|
|
15
16
|
@allow_redirections = options[:allow_redirections]
|
|
16
17
|
@retries = options[:retries]
|
|
17
18
|
@headers = options[:headers]
|
|
19
|
+
@max_html_bytes = options[:max_html_bytes]
|
|
18
20
|
@url_filter = options[:url_filter]
|
|
19
21
|
if !@url_filter.nil? && !@url_filter.respond_to?(:call)
|
|
20
22
|
raise ArgumentError, 'url_filter must respond to call'
|
|
@@ -25,6 +27,8 @@ module MetaInspector
|
|
|
25
27
|
|
|
26
28
|
def build(url, image: false, origin: nil, protect_redirect_callback: false)
|
|
27
29
|
URLPolicy.validate!(url)
|
|
30
|
+
# Image requests must keep trusting the initial origin after page redirects.
|
|
31
|
+
@page_origin ||= url.to_s unless image
|
|
28
32
|
faraday_options = @faraday_options.merge(url: url)
|
|
29
33
|
redirects = (faraday_options.delete(:redirect) || {}).dup
|
|
30
34
|
if protect_redirect_callback && redirects[:callback]
|
|
@@ -40,21 +44,26 @@ module MetaInspector
|
|
|
40
44
|
Faraday.new(faraday_options) do |faraday|
|
|
41
45
|
unless image
|
|
42
46
|
faraday.request :retry, max: @retries
|
|
43
|
-
|
|
47
|
+
# Responses run in reverse order: decompress before applying the charset.
|
|
48
|
+
faraday.response :encoding if @max_html_bytes
|
|
49
|
+
faraday.request :gzip if @max_html_bytes.nil?
|
|
44
50
|
end
|
|
45
51
|
if @allow_redirections
|
|
46
52
|
redirects[:limit] ||= 10
|
|
47
53
|
faraday.use Faraday::FollowRedirects::Middleware, **redirects
|
|
48
54
|
faraday.use :cookie_jar
|
|
49
55
|
end
|
|
50
|
-
faraday.use URLPolicy, @url_filter, image ? origin : nil
|
|
56
|
+
faraday.use URLPolicy, @url_filter, image ? origin : nil,
|
|
57
|
+
request_origin: @page_origin || origin
|
|
58
|
+
faraday.use LimitedResponse, @max_html_bytes if !image && @max_html_bytes
|
|
51
59
|
if !image && @cache.is_a?(Hash)
|
|
52
60
|
cache_options = @cache.dup
|
|
53
61
|
cache_options[:serializer] ||= Marshal
|
|
54
62
|
faraday.use Faraday::HttpCache, **cache_options
|
|
55
63
|
end
|
|
64
|
+
faraday.use LimitedResponse::Streaming, @max_html_bytes if !image && @max_html_bytes
|
|
56
65
|
faraday.headers.merge!(@headers || {})
|
|
57
|
-
faraday.response :encoding unless image
|
|
66
|
+
faraday.response :encoding unless image || @max_html_bytes
|
|
58
67
|
faraday.adapter :net_http
|
|
59
68
|
end
|
|
60
69
|
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
require 'zlib'
|
|
2
|
+
require 'stringio'
|
|
3
|
+
|
|
4
|
+
module MetaInspector
|
|
5
|
+
# Enforces max_html_bytes before charset conversion or parsing. Keep this
|
|
6
|
+
# outside the HTTP cache, and Streaming inside it, so cached bodies are checked
|
|
7
|
+
# too and the cache stores the received bytes rather than an empty stream.
|
|
8
|
+
class LimitedResponse < Faraday::Middleware
|
|
9
|
+
CHUNK_SIZE = 16 * 1024
|
|
10
|
+
|
|
11
|
+
def self.check_size!(size, limit)
|
|
12
|
+
return if size <= limit
|
|
13
|
+
|
|
14
|
+
raise MetaInspector::ParserError, "HTML response exceeds the limit of #{limit} bytes"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(app, limit)
|
|
18
|
+
super(app)
|
|
19
|
+
@limit = limit
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(env)
|
|
23
|
+
env.request_headers['Accept-Encoding'] ||= 'gzip,deflate'
|
|
24
|
+
@app.call(env).on_complete do |response|
|
|
25
|
+
body = response.body
|
|
26
|
+
self.class.check_size!(body.bytesize, @limit)
|
|
27
|
+
next if body.empty? || response.status == 206
|
|
28
|
+
|
|
29
|
+
encodings = response.response_headers['Content-Encoding'].to_s.split(',').map { |value| value.strip.downcase }
|
|
30
|
+
next if encodings.empty? || encodings.include?('identity')
|
|
31
|
+
|
|
32
|
+
encodings.reverse_each do |encoding|
|
|
33
|
+
body = case encoding
|
|
34
|
+
when 'gzip' then gunzip(body)
|
|
35
|
+
when 'deflate' then inflate(body)
|
|
36
|
+
else
|
|
37
|
+
raise MetaInspector::RequestError, "Unsupported Content-Encoding with max_html_bytes: #{encoding}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
response.body = body
|
|
41
|
+
response.response_headers['Content-Length'] = body.bytesize
|
|
42
|
+
response.response_headers.delete('Content-Encoding')
|
|
43
|
+
end
|
|
44
|
+
rescue Zlib::Error => error
|
|
45
|
+
raise MetaInspector::RequestError.new(error)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def append(body, chunk)
|
|
51
|
+
self.class.check_size!(body.bytesize + chunk.bytesize, @limit)
|
|
52
|
+
body << chunk
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def gunzip(bytes)
|
|
56
|
+
body = ''.b
|
|
57
|
+
io = StringIO.new(bytes)
|
|
58
|
+
# gzip can contain multiple members; apply one budget to all of them.
|
|
59
|
+
until io.eof?
|
|
60
|
+
reader = Zlib::GzipReader.new(io, encoding: Encoding::BINARY)
|
|
61
|
+
begin
|
|
62
|
+
while (chunk = reader.read(CHUNK_SIZE)) && !chunk.empty?
|
|
63
|
+
append(body, chunk)
|
|
64
|
+
end
|
|
65
|
+
unused = reader.unused
|
|
66
|
+
ensure
|
|
67
|
+
reader.finish
|
|
68
|
+
end
|
|
69
|
+
io.pos -= unused.bytesize if unused
|
|
70
|
+
end
|
|
71
|
+
body
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def inflate(bytes, window_bits = Zlib::MAX_WBITS)
|
|
75
|
+
body = ''.b
|
|
76
|
+
inflater = Zlib::Inflate.new(window_bits)
|
|
77
|
+
begin
|
|
78
|
+
# Block form yields bounded chunks instead of allocating the full output.
|
|
79
|
+
inflater.inflate(bytes) { |chunk| append(body, chunk) }
|
|
80
|
+
inflater.finish { |chunk| append(body, chunk) }
|
|
81
|
+
ensure
|
|
82
|
+
inflater.close
|
|
83
|
+
end
|
|
84
|
+
body
|
|
85
|
+
rescue Zlib::DataError
|
|
86
|
+
raise if window_bits < 0
|
|
87
|
+
|
|
88
|
+
# Some servers send raw DEFLATE rather than a zlib-wrapped stream.
|
|
89
|
+
inflate(bytes, -Zlib::MAX_WBITS)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
class Streaming < Faraday::Middleware
|
|
93
|
+
def initialize(app, limit)
|
|
94
|
+
super(app)
|
|
95
|
+
@limit = limit
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def call(env)
|
|
99
|
+
body = ''.b
|
|
100
|
+
# Redirects copy the environment but share request options. Each hop
|
|
101
|
+
# needs its own buffer and callback, as do retries and HEAD/GET pairs.
|
|
102
|
+
env.request = env.request.dup
|
|
103
|
+
env.request.on_data = proc do |chunk, _total, _response|
|
|
104
|
+
LimitedResponse.check_size!(body.bytesize + chunk.bytesize, @limit)
|
|
105
|
+
body << chunk.b
|
|
106
|
+
end
|
|
107
|
+
@app.call(env).on_complete do |response|
|
|
108
|
+
# Net::HTTP tags its buffered body with the HTTP charset, including
|
|
109
|
+
# quoted labels. Streaming replaces that body, so retain the tag.
|
|
110
|
+
charset = response.response_headers['Content-Type'].to_s[/(?:\A|;)\s*charset=([^;]+)/i, 1]
|
|
111
|
+
if charset
|
|
112
|
+
charset = charset.strip
|
|
113
|
+
charset = charset[1...-1] if charset.start_with?('"') && charset.end_with?('"')
|
|
114
|
+
begin
|
|
115
|
+
body.force_encoding(charset)
|
|
116
|
+
rescue ArgumentError
|
|
117
|
+
# Unknown charsets are ignored, as in the Net::HTTP adapter.
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
response.body = body
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -12,7 +12,8 @@ module MetaInspector
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
PAGE_HEADERS = %w[user-agent accept accept-language accept-encoding].freeze
|
|
16
|
+
IMAGE_HEADERS = (PAGE_HEADERS + %w[range]).freeze
|
|
16
17
|
|
|
17
18
|
def self.validate!(value)
|
|
18
19
|
uri = URI.parse(value.to_s)
|
|
@@ -24,16 +25,22 @@ module MetaInspector
|
|
|
24
25
|
raise InvalidURL.new(error)
|
|
25
26
|
end
|
|
26
27
|
|
|
27
|
-
def initialize(app, filter, image_origin = nil)
|
|
28
|
+
def initialize(app, filter, image_origin = nil, request_origin: nil)
|
|
28
29
|
super(app)
|
|
29
30
|
@filter = filter
|
|
30
31
|
@image_origin = image_origin && self.class.validate!(image_origin)
|
|
32
|
+
@request_origin = request_origin && self.class.validate!(request_origin)
|
|
33
|
+
@request_origin ||= @image_origin
|
|
34
|
+
@allowed_headers = @image_origin ? IMAGE_HEADERS : PAGE_HEADERS
|
|
31
35
|
end
|
|
32
36
|
|
|
33
37
|
def call(env)
|
|
34
38
|
uri = self.class.validate!(env.url)
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
cross_image_origin = @image_origin && origin(uri) != origin(@image_origin)
|
|
40
|
+
if cross_image_origin || (@request_origin && origin(uri) != origin(@request_origin))
|
|
41
|
+
env.request_headers.delete_if { |key, _| !@allowed_headers.include?(key.downcase) }
|
|
42
|
+
end
|
|
43
|
+
if cross_image_origin
|
|
37
44
|
unless @image_origin.scheme == 'https' && uri.scheme == 'http'
|
|
38
45
|
referer = @image_origin.dup
|
|
39
46
|
referer.userinfo = nil
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe 'page redirect headers' do
|
|
4
|
+
let(:initial) { 'https://app.example/start' }
|
|
5
|
+
let(:headers) do
|
|
6
|
+
{
|
|
7
|
+
'authorization' => 'Bearer secret',
|
|
8
|
+
'cookie' => 'session=secret',
|
|
9
|
+
'proxy-authorization' => 'Basic secret',
|
|
10
|
+
'x-api-key' => 'secret',
|
|
11
|
+
'referer' => 'https://private.example/secret',
|
|
12
|
+
'range' => 'bytes=0-100',
|
|
13
|
+
'user-agent' => 'Policy app',
|
|
14
|
+
'accept' => 'text/html',
|
|
15
|
+
'accept-language' => 'en',
|
|
16
|
+
'accept-encoding' => 'identity'
|
|
17
|
+
}.freeze
|
|
18
|
+
end
|
|
19
|
+
let(:safe_headers) { headers.slice('user-agent', 'accept', 'accept-language', 'accept-encoding') }
|
|
20
|
+
let(:html) { '<title>landing</title>' }
|
|
21
|
+
|
|
22
|
+
def expect_safe_headers(request)
|
|
23
|
+
expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'Proxy-Authorization',
|
|
24
|
+
'X-Api-Key', 'X-Faraday-Secret', 'Referer', 'Range')
|
|
25
|
+
safe_headers.each { |key, value| expect(request.headers[key.split('-').map(&:capitalize).join('-')]).to eq(value) }
|
|
26
|
+
true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
[false, true].each do |head_first|
|
|
30
|
+
[
|
|
31
|
+
'https://other.example/landing',
|
|
32
|
+
'http://app.example/landing',
|
|
33
|
+
'https://sub.app.example/landing',
|
|
34
|
+
'https://app.example:8443/landing',
|
|
35
|
+
'//other.example/landing'
|
|
36
|
+
].each do |destination|
|
|
37
|
+
it "strips credentials for #{destination} with head_first: #{head_first}" do
|
|
38
|
+
target = URI.join(initial, destination).to_s
|
|
39
|
+
methods = head_first ? [:head, :get] : [:get]
|
|
40
|
+
methods.each do |method|
|
|
41
|
+
stub_request(method, initial).with(headers: headers)
|
|
42
|
+
.to_return(status: 302, headers: { 'Location' => destination })
|
|
43
|
+
stub_request(method, target).with { |request| expect_safe_headers(request) }
|
|
44
|
+
.to_return(body: html, headers: { 'Content-Type' => 'text/html' })
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
page = MetaInspector.new(initial, headers: headers, head_first: head_first,
|
|
48
|
+
faraday_options: { headers: { 'X-Faraday-Secret' => 'secret' }.freeze }.freeze)
|
|
49
|
+
|
|
50
|
+
expect(page.title).to eq('landing')
|
|
51
|
+
expect(page.headers).to eq(headers)
|
|
52
|
+
methods.each { |method| expect(a_request(method, target)).to have_been_made.once }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
['/landing', 'https://app.example:443/landing'].each do |destination|
|
|
58
|
+
it "preserves custom headers for same-origin redirect #{destination}" do
|
|
59
|
+
stub_request(:get, initial).with(headers: headers)
|
|
60
|
+
.to_return(status: 302, headers: { 'Location' => destination, 'Set-Cookie' => 'server=value; Path=/; Secure' })
|
|
61
|
+
stub_request(:get, 'https://app.example/landing').with do |request|
|
|
62
|
+
expect(request.headers['Cookie']).to include('server=value', 'session=secret')
|
|
63
|
+
expect(request.headers['X-Api-Key']).to eq('secret')
|
|
64
|
+
expect(request.headers['Authorization']).to eq('Bearer secret')
|
|
65
|
+
true
|
|
66
|
+
end.to_return(body: html)
|
|
67
|
+
|
|
68
|
+
expect(MetaInspector.new(initial, headers: headers).title).to eq('landing')
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'applies the policy after redirect callbacks change the destination and headers' do
|
|
73
|
+
stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => '/landing' })
|
|
74
|
+
stub_request(:get, 'https://other.example/landing').with { |request| expect_safe_headers(request) }
|
|
75
|
+
.to_return(body: html)
|
|
76
|
+
callback = lambda do |_, following|
|
|
77
|
+
following.url = URI('https://other.example/landing')
|
|
78
|
+
following.request_headers['X-Api-Key'] = 'callback-secret'
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
page = MetaInspector.new(initial, headers: headers,
|
|
82
|
+
faraday_options: { redirect: { callback: callback, clear_authorization_header: false }.freeze }.freeze)
|
|
83
|
+
|
|
84
|
+
expect(page.title).to eq('landing')
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it 'keeps credentials stripped throughout a redirect chain' do
|
|
88
|
+
stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => 'https://other.example/step' })
|
|
89
|
+
stub_request(:get, 'https://other.example/step').with { |request| expect_safe_headers(request) }
|
|
90
|
+
.to_return(status: 302, headers: { 'Location' => '/landing' })
|
|
91
|
+
stub_request(:get, 'https://other.example/landing').with { |request| expect_safe_headers(request) }
|
|
92
|
+
.to_return(status: 302, headers: { 'Location' => 'https://app.example/landing' })
|
|
93
|
+
stub_request(:get, 'https://app.example/landing').with { |request| expect_safe_headers(request) }
|
|
94
|
+
.to_return(body: html)
|
|
95
|
+
|
|
96
|
+
expect(MetaInspector.new(initial, headers: headers).title).to eq('landing')
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it 'does not reintroduce the initial credentials when fetching images on the redirected page' do
|
|
100
|
+
png = "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b
|
|
101
|
+
stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => 'https://other.example/landing' })
|
|
102
|
+
stub_request(:get, 'https://other.example/landing')
|
|
103
|
+
.to_return(body: '<img src="/image.png">', headers: { 'Content-Type' => 'text/html' })
|
|
104
|
+
stub_request(:get, 'https://other.example/image.png').with do |request|
|
|
105
|
+
expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'Proxy-Authorization',
|
|
106
|
+
'X-Api-Key', 'X-Faraday-Secret', 'Referer')
|
|
107
|
+
expect(request.headers['Range']).to eq('bytes=0-262143')
|
|
108
|
+
true
|
|
109
|
+
end.to_return(body: png)
|
|
110
|
+
|
|
111
|
+
page = MetaInspector.new(initial, headers: headers,
|
|
112
|
+
faraday_options: { headers: { 'X-Faraday-Secret' => 'secret' } })
|
|
113
|
+
|
|
114
|
+
expect(page.images.with_size).to eq([['https://other.example/image.png', 37, 29]])
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it 'can reuse caller headers across independent page requests' do
|
|
118
|
+
stub_request(:get, initial).with(headers: headers)
|
|
119
|
+
.to_return(status: 302, headers: { 'Location' => 'https://other.example/landing' })
|
|
120
|
+
stub_request(:get, 'https://other.example/landing').with { |request| expect_safe_headers(request) }
|
|
121
|
+
.to_return(body: html)
|
|
122
|
+
|
|
123
|
+
2.times { expect(MetaInspector.new(initial, headers: headers).title).to eq('landing') }
|
|
124
|
+
|
|
125
|
+
expect(a_request(:get, initial).with(headers: headers)).to have_been_made.twice
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
it 'preserves image header protection when an image points back to the initial page origin' do
|
|
129
|
+
png = "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b
|
|
130
|
+
stub_request(:get, initial).to_return(status: 302, headers: { 'Location' => 'https://other.example/landing' })
|
|
131
|
+
stub_request(:get, 'https://other.example/landing')
|
|
132
|
+
.to_return(body: '<img src="https://app.example/image.png">', headers: { 'Content-Type' => 'text/html' })
|
|
133
|
+
stub_request(:get, 'https://app.example/image.png').with do |request|
|
|
134
|
+
expect(request.headers.keys).not_to include('Authorization', 'Cookie', 'Proxy-Authorization', 'X-Api-Key')
|
|
135
|
+
expect(request.headers['Referer']).to eq('https://other.example/')
|
|
136
|
+
true
|
|
137
|
+
end.to_return(body: png)
|
|
138
|
+
|
|
139
|
+
page = MetaInspector.new(initial, headers: headers)
|
|
140
|
+
|
|
141
|
+
expect(page.images.with_size).to eq([['https://app.example/image.png', 37, 29]])
|
|
142
|
+
end
|
|
143
|
+
end
|
|
@@ -0,0 +1,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
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: metainspector
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.21.
|
|
4
|
+
version: 5.21.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jaime Iniesta
|
|
@@ -312,6 +312,7 @@ files:
|
|
|
312
312
|
- lib/meta_inspector/errors.rb
|
|
313
313
|
- lib/meta_inspector/http_session.rb
|
|
314
314
|
- lib/meta_inspector/image_downloader.rb
|
|
315
|
+
- lib/meta_inspector/limited_response.rb
|
|
315
316
|
- lib/meta_inspector/parser.rb
|
|
316
317
|
- lib/meta_inspector/parsers/base.rb
|
|
317
318
|
- lib/meta_inspector/parsers/head_links.rb
|
|
@@ -409,7 +410,9 @@ files:
|
|
|
409
410
|
- spec/meta_inspector/redirections_spec.rb
|
|
410
411
|
- spec/meta_inspector/texts_spec.rb
|
|
411
412
|
- spec/parser_spec.rb
|
|
413
|
+
- spec/redirect_headers_spec.rb
|
|
412
414
|
- spec/request_spec.rb
|
|
415
|
+
- spec/response_size_spec.rb
|
|
413
416
|
- spec/spec_helper.rb
|
|
414
417
|
- spec/unicode_image_urls_spec.rb
|
|
415
418
|
- spec/url_filter_spec.rb
|
|
@@ -432,7 +435,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
432
435
|
- !ruby/object:Gem::Version
|
|
433
436
|
version: '0'
|
|
434
437
|
requirements: []
|
|
435
|
-
rubygems_version: 4.0.
|
|
438
|
+
rubygems_version: 4.0.15
|
|
436
439
|
specification_version: 4
|
|
437
440
|
summary: MetaInspector is a ruby gem for web scraping purposes, that returns metadata
|
|
438
441
|
from a given URL
|