Class: HTML::Pipeline::CamoFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/html/pipeline/camo_filter.rb

Overview

HTML Filter for replacing http image URLs with camo versions. See:

github.com/atmos/camo

All images provided in user content should be run through this filter so that http image sources do not cause mixed-content warnings in browser clients.

Context options:

:asset_proxy (required) - Base URL for constructed asset proxy URLs.
:asset_proxy_secret_key (required) - The shared secret used to encode URLs.

This filter does not write additional information to the context.

Instance Attribute Summary

Attributes inherited from Filter

#context, #result

Instance Method Summary collapse

Methods inherited from Filter

#base_url, call, #can_access_repo?, #current_user, #doc, #has_ancestor?, #html, #initialize, #needs, #parse_html, #repository, to_document, to_html

Constructor Details

This class inherits a constructor from HTML::Pipeline::Filter

Instance Method Details

#asset_proxy_hostObject

Private: the hostname to use for generated asset proxied URLs.



55
56
57
# File 'lib/html/pipeline/camo_filter.rb', line 55

def asset_proxy_host
  context[:asset_proxy]
end

#asset_proxy_secret_keyObject



59
60
61
# File 'lib/html/pipeline/camo_filter.rb', line 59

def asset_proxy_secret_key
  context[:asset_proxy_secret_key]
end

#asset_proxy_url(url) ⇒ Object

The camouflaged URL for a given image URL.



44
45
46
# File 'lib/html/pipeline/camo_filter.rb', line 44

def asset_proxy_url(url)
  "#{asset_proxy_host}/#{asset_url_hash(url)}/#{hexencode(url)}"
end

#asset_url_hash(url) ⇒ Object

Private: calculate the HMAC digest for a image source URL.



49
50
51
52
# File 'lib/html/pipeline/camo_filter.rb', line 49

def asset_url_hash(url)
  digest = OpenSSL::Digest::Digest.new('sha1')
  OpenSSL::HMAC.hexdigest(digest, asset_proxy_secret_key, url)
end

#callObject

Hijacks images in the markup provided, replacing them with URLs that go through the github asset proxy.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/html/pipeline/camo_filter.rb', line 21

def call
  doc.search("img").each do |element|
    next if element['src'].nil?
    src = element['src'].strip
    src = src.sub(%r!^http://github.com!, 'https://github.com')
    next if context[:disable_asset_proxy]

    if src =~ /^http:/ || src =~ /^https:\/\/img.skitch.com\//
      element['src'] = asset_proxy_url(src)
    else
      element['src'] = src
    end
  end
  doc
end

#hexencode(str) ⇒ Object

Private: helper to hexencode a string. Each byte ends up encoded into two characters, zero padded value in the range [0-9a-f].



65
66
67
# File 'lib/html/pipeline/camo_filter.rb', line 65

def hexencode(str)
  str.to_enum(:each_byte).map { |byte| "%02x" % byte }.join
end

#validateObject

Implementation of validate hook. Errors should raise exceptions or use an existing validator.



39
40
41
# File 'lib/html/pipeline/camo_filter.rb', line 39

def validate
  needs :asset_proxy, :asset_proxy_secret_key
end