Skip to content

Commit aa3fa37

Browse files
author
Simeon F. Willbanks
committed
Merge remote-tracking branch 'upstream/master' into configurable-schemes-bug-fix
2 parents 68a4e92 + 8307b18 commit aa3fa37

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ before_install:
77
script: "bundle exec rake"
88

99
rvm:
10-
- 1.8.7
1110
- 1.9.2
1211
- 1.9.3
1312
- 2.0.0

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# CHANGELOG
22

3-
## 1.2.0
3+
## 1.3.0
4+
5+
1.2.0 didn't actually include the following changes. Yanked that release.
46

57
* CamoFilter now camos https images. #96 josh
68

lib/html/pipeline/camo_filter.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ class Pipeline
1414
# Context options:
1515
# :asset_proxy (required) - Base URL for constructed asset proxy URLs.
1616
# :asset_proxy_secret_key (required) - The shared secret used to encode URLs.
17+
# :asset_proxy_whitelist - Array of host Strings or Regexps to skip
18+
# src rewriting.
1719
#
1820
# This filter does not write additional information to the context.
1921
class CamoFilter < Filter
2022
# Hijacks images in the markup provided, replacing them with URLs that
2123
# go through the github asset proxy.
2224
def call
25+
return unless asset_proxy_enabled?
26+
2327
doc.search("img").each do |element|
24-
next if context[:disable_asset_proxy]
2528
next if element['src'].nil?
2629

2730
begin
@@ -30,10 +33,11 @@ def call
3033
next
3134
end
3235

33-
next if uri.hostname.nil?
34-
next if uri.hostname.match(/(^|\.)github(app)?\.com$/)
36+
next if uri.host.nil?
37+
next if asset_host_whitelisted?(uri.host)
3538

3639
element['src'] = asset_proxy_url(uri.to_s)
40+
element['data-canonical-src'] = uri.to_s
3741
end
3842
doc
3943
end
@@ -55,7 +59,12 @@ def asset_url_hash(url)
5559
OpenSSL::HMAC.hexdigest(digest, asset_proxy_secret_key, url)
5660
end
5761

58-
# Private: the hostname to use for generated asset proxied URLs.
62+
# Private: Return true if asset proxy filter should be enabled
63+
def asset_proxy_enabled?
64+
!context[:disable_asset_proxy]
65+
end
66+
67+
# Private: the host to use for generated asset proxied URLs.
5968
def asset_proxy_host
6069
context[:asset_proxy]
6170
end
@@ -64,6 +73,16 @@ def asset_proxy_secret_key
6473
context[:asset_proxy_secret_key]
6574
end
6675

76+
def asset_proxy_whitelist
77+
context[:asset_proxy_whitelist] || []
78+
end
79+
80+
def asset_host_whitelisted?(host)
81+
asset_proxy_whitelist.any? do |test|
82+
test.is_a?(String) ? host == test : test.match(host)
83+
end
84+
end
85+
6786
# Private: helper to hexencode a string. Each byte ends up encoded into
6887
# two characters, zero padded value in the range [0-9a-f].
6988
def hexencode(str)

lib/html/pipeline/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module HTML
22
class Pipeline
3-
VERSION = "1.2.0"
3+
VERSION = "1.3.0"
44
end
55
end

test/html/pipeline/camo_filter_test.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ def setup
88
@asset_proxy_secret_key = 'ssssh-secret'
99
@options = {
1010
:asset_proxy => @asset_proxy_url,
11-
:asset_proxy_secret_key => @asset_proxy_secret_key
11+
:asset_proxy_secret_key => @asset_proxy_secret_key,
12+
:asset_proxy_whitelist => [/(^|\.)github\.com$/]
1213
}
1314
end
1415

1516
def test_camouflaging_http_image_urls
1617
orig = %(<p><img src="http://twitter.com/img.png"></p>)
1718
assert_includes 'img src="' + @asset_proxy_url,
1819
CamoFilter.call(orig, @options).to_s
20+
assert_includes 'data-canonical-src="http://twitter.com/img.png"',
21+
CamoFilter.call(orig, @options).to_s
1922
end
2023

2124
def test_doesnt_rewrite_dotcom_image_urls
@@ -42,12 +45,6 @@ def test_camouflaging_github_prefixed_image_urls
4245
CamoFilter.call(orig, @options).to_s
4346
end
4447

45-
def test_doesnt_rewrite_dotcom_app_image_urls
46-
orig = %(<p><img src="https://githubapp.com/img.png"></p>)
47-
assert_equal "<p><img src=\"https://githubapp.com/img.png\"></p>",
48-
CamoFilter.call(orig, @options).to_s
49-
end
50-
5148
def test_doesnt_rewrite_absolute_image_urls
5249
orig = %(<p><img src="/img.png"></p>)
5350
assert_equal "<p><img src=\"/img.png\"></p>",

0 commit comments

Comments
 (0)