onebox 1.7.7 → 1.7.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01da233142275cb809b6294116b09c1b98066c39
4
- data.tar.gz: 00e07dcdf031dc0b096d63d0d13a56a1e9769d92
3
+ metadata.gz: f4aaa05355640126f6ad6ad266ec44e444f23427
4
+ data.tar.gz: 1b3e3acc8b0d5a99fe99d5e09488cee2d2f98f7a
5
5
  SHA512:
6
- metadata.gz: 3170eb48911907b19d9c50692eb72bfd7eb4b7d9cc488e60d29dbf9ee86bcaca88daff818b45346545293ee23b880f6775b18866b19d45af828eae77fda66196
7
- data.tar.gz: 994a35dc02b560d1af3360c8965600251795569019d95956e5c62f10f747d993e94ec972146d71d71151469d22a7f7d1d07ccde67be95a6f1c0fb77317b8868f
6
+ metadata.gz: f287783f7c37eb59b2809170467f52e3104db29afed8e329ac16a60cf73acb1787aafa0dbfee15004b9f4dbca4dca02469d8efda740c8804964c3165f22b7a39
7
+ data.tar.gz: df5f79f7d6089ac381c50ca7ea6b39364066f85998c4a8b3dc70a0c61f5e35d162dd0d3ac41f768d8c50f5f9f5b9464383a926bae75fd49ff89e90b90961d059
@@ -15,8 +15,10 @@ module Onebox
15
15
  cache: Moneta.new(:Memory, expires: true, serializer: :json),
16
16
  connect_timeout: 5,
17
17
  timeout: 10,
18
- max_download_kb: (10 * 1024), # 10MB
19
- load_paths: [File.join(Gem::Specification.find_by_name("onebox").gem_dir, "templates")]
18
+ max_download_kb: (10 * 1024), # 10MB
19
+ load_paths: [File.join(Gem::Specification.find_by_name("onebox").gem_dir, "templates")],
20
+ allowed_ports: [80, 443],
21
+ allowed_schemes: ["http", "https"],
20
22
  }
21
23
 
22
24
  @@options = DEFAULTS
@@ -12,6 +12,8 @@ module Onebox
12
12
 
13
13
  def oneboxed
14
14
  uri = URI(@url)
15
+ return unless uri.port.nil? || Onebox.options.allowed_ports.include?(uri.port)
16
+ return unless uri.scheme.nil? || Onebox.options.allowed_schemes.include?(uri.scheme)
15
17
  ordered_engines.find { |engine| engine === uri }
16
18
  rescue URI::InvalidURIError
17
19
  nil
@@ -5,6 +5,8 @@ module Onebox
5
5
  class Preview
6
6
  attr_reader :cache
7
7
 
8
+ WEB_EXCEPTIONS ||= [Net::HTTPServerException, OpenURI::HTTPError, Timeout::Error, Net::HTTPError, Errno::ECONNREFUSED]
9
+
8
10
  def initialize(link, parameters = Onebox.options)
9
11
  @url = link
10
12
  @options = parameters
@@ -15,14 +17,14 @@ module Onebox
15
17
  def to_s
16
18
  return "" unless engine
17
19
  sanitize process_html engine_html
18
- rescue *Onebox::Preview.web_exceptions
20
+ rescue *WEB_EXCEPTIONS
19
21
  ""
20
22
  end
21
23
 
22
24
  def placeholder_html
23
25
  return "" unless engine
24
26
  sanitize process_html engine.placeholder_html
25
- rescue *Onebox::Preview.web_exceptions
27
+ rescue *WEB_EXCEPTIONS
26
28
  ""
27
29
  end
28
30
 
@@ -30,51 +32,46 @@ module Onebox
30
32
  OpenStruct.new(@options)
31
33
  end
32
34
 
33
- def self.web_exceptions
34
- [Net::HTTPServerException, OpenURI::HTTPError, Timeout::Error, Net::HTTPError, Errno::ECONNREFUSED]
35
- end
36
-
37
35
  private
38
36
 
39
- def engine_html
40
- engine.to_html
41
- end
37
+ def engine_html
38
+ engine.to_html
39
+ end
42
40
 
43
- def process_html(html)
44
- return "" unless html
41
+ def process_html(html)
42
+ return "" unless html
45
43
 
46
- if @options[:max_width]
47
- doc = Nokogiri::HTML::fragment(html)
48
- if doc
49
- doc.css('[width]').each do |e|
50
- width = e['width'].to_i
44
+ if @options[:max_width]
45
+ doc = Nokogiri::HTML::fragment(html)
46
+ if doc
47
+ doc.css('[width]').each do |e|
48
+ width = e['width'].to_i
51
49
 
52
- if width > @options[:max_width]
53
- height = e['height'].to_i
54
- if (height > 0)
55
- ratio = (height.to_f / width.to_f)
56
- e['height'] = (@options[:max_width] * ratio).floor
50
+ if width > @options[:max_width]
51
+ height = e['height'].to_i
52
+ if (height > 0)
53
+ ratio = (height.to_f / width.to_f)
54
+ e['height'] = (@options[:max_width] * ratio).floor
55
+ end
56
+ e['width'] = @options[:max_width]
57
57
  end
58
- e['width'] = @options[:max_width]
59
58
  end
59
+ return doc.to_html
60
60
  end
61
- return doc.to_html
62
61
  end
63
- end
64
62
 
65
- html
66
- end
63
+ html
64
+ end
67
65
 
68
- def sanitize(html)
69
- Sanitize.fragment(html, Sanitize::Config::ONEBOX)
70
- end
66
+ def sanitize(html)
67
+ Sanitize.fragment(html, Sanitize::Config::ONEBOX)
68
+ end
71
69
 
72
- def engine
73
- return nil unless @engine_class
74
- @engine ||= @engine_class.new(@url, cache)
75
- end
70
+ def engine
71
+ return nil unless @engine_class
72
+ @engine ||= @engine_class.new(@url, cache)
73
+ end
76
74
 
77
- class InvalidURI < StandardError
78
- end
75
+ class InvalidURI < StandardError; end
79
76
  end
80
77
  end
@@ -1,3 +1,3 @@
1
1
  module Onebox
2
- VERSION = "1.7.7"
2
+ VERSION = "1.7.8"
3
3
  end
@@ -34,7 +34,7 @@ describe Onebox::Matcher do
34
34
  let(:url) { "http://party.time.made.up-url.com/?article_id=1234" }
35
35
  let(:matcher) { Onebox::Matcher.new(url) }
36
36
 
37
- it "it finds an engine" do
37
+ it "finds an engine" do
38
38
  matcher.stubs(:ordered_engines).returns([TestEngine])
39
39
  expect(matcher.oneboxed).not_to be_nil
40
40
  end
@@ -44,11 +44,31 @@ describe Onebox::Matcher do
44
44
  let(:url) { "http://party.time.made.up-url.com/#article_id=1234" }
45
45
  let(:matcher) { Onebox::Matcher.new(url) }
46
46
 
47
- it "it finds an engine" do
47
+ it "finds an engine" do
48
48
  matcher.stubs(:ordered_engines).returns([TestEngine])
49
49
  expect(matcher.oneboxed).not_to be_nil
50
50
  end
51
51
  end
52
52
 
53
+ describe "with a whitelisted port/scheme" do
54
+ %w{http://example.com https://example.com http://example.com:80 //example.com}.each do |url|
55
+ it "finds an engine for '#{url}'" do
56
+ matcher = Onebox::Matcher.new(url)
57
+ matcher.stubs(:ordered_engines).returns([TestEngine])
58
+ expect(matcher.oneboxed).not_to be_nil
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "without a whitelisted port/scheme" do
64
+ %w{http://example.com:21 ftp://example.com}.each do |url|
65
+ it "doesn't find an engine for '#{url}'" do
66
+ matcher = Onebox::Matcher.new(url)
67
+ matcher.stubs(:ordered_engines).returns([TestEngine])
68
+ expect(matcher.oneboxed).to be_nil
69
+ end
70
+ end
71
+ end
72
+
53
73
  end
54
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onebox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.7
4
+ version: 1.7.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joanna Zeta
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-01-25 00:00:00.000000000 Z
13
+ date: 2017-01-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
@@ -486,7 +486,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
486
486
  version: '0'
487
487
  requirements: []
488
488
  rubyforge_project:
489
- rubygems_version: 2.6.7
489
+ rubygems_version: 2.5.1
490
490
  signing_key:
491
491
  specification_version: 4
492
492
  summary: A gem for turning URLs into previews.
@@ -550,4 +550,3 @@ test_files:
550
550
  - spec/lib/onebox_spec.rb
551
551
  - spec/spec_helper.rb
552
552
  - spec/support/html_spec_helper.rb
553
- has_rdoc: