From 92cd4e6b78284a2740964492382d4d10714056f2 Mon Sep 17 00:00:00 2001 From: Dennis Schubert Date: Sun, 19 Oct 2014 17:45:30 +0200 Subject: [PATCH] Use camo for Markdown images just a quick proof of concept --- app/presenters/post_presenter.rb | 2 +- config/defaults.yml | 4 +++- config/diaspora.yml.example | 18 ++++++++++++++++-- lib/diaspora/camo_url.rb | 16 ++++++++++++++++ lib/diaspora/message_renderer.rb | 13 +++++++++++++ 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 lib/diaspora/camo_url.rb diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index e6dd6fa31..a01c090b2 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -16,7 +16,7 @@ class PostPresenter { :id => @post.id, :guid => @post.guid, - :text => @post.raw_message, + :text => @post.message.plain_text_for_json, :public => @post.public, :created_at => @post.created_at, :interacted_at => @post.interacted_at, diff --git a/config/defaults.yml b/config/defaults.yml index ac1a7c55a..8cf35d41b 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -54,7 +54,9 @@ defaults: post_counts: false comment_counts: false camo: - enable: false + proxy_markdown_images: false + proxy_opengraph_thumbnails: false + proxy_remote_pod_images: false root: key: settings: diff --git a/config/diaspora.yml.example b/config/diaspora.yml.example index 6fb2b4a0e..9f3ee4335 100644 --- a/config/diaspora.yml.example +++ b/config/diaspora.yml.example @@ -216,8 +216,22 @@ configuration: ## Section ## your pod's frontend, but it will increase the traffic on your server. camo: ## Section - ## Enable Camo (default=false) - #enable: true + ## Proxy imaged embedded via markdown (default=false) + ## Embedded images are quite often from non-SSL sites and may cause a + ## partial content warning, so this is recommended. + #proxy_markdown_images: true + + ## Proxy Open Graph thumbnails (default=false) + ## Open Graph thumbnails may or may not be encrypted and loaded from + ## servers outside the network. Recommended. + #proxy_opengraph_thumbnails: true + + ## Proxy remote pod's images (default=false) + ## Profile pictures and photos from other pods usually are encrypted, + ## so enabling this is only useful if you want to avoid HTTP requests to + ## third-party servers. This will create a lot of traffic on your camo + ## instance. You have been warned. + #proxy_remote_pod_images: true ## Root of your Camo installation #root: "https://camo.example.com/" diff --git a/lib/diaspora/camo_url.rb b/lib/diaspora/camo_url.rb new file mode 100644 index 000000000..f0cbb0da2 --- /dev/null +++ b/lib/diaspora/camo_url.rb @@ -0,0 +1,16 @@ +# implicitly requires OpenSSL + +module Diaspora + module CamoUrl + def self.image_url(url) + digest = OpenSSL::HMAC.hexdigest( + OpenSSL::Digest.new("sha1"), + AppConfig.privacy.camo.key, + url + ) + encoded_url = url.to_enum(:each_byte).map {|byte| "%02x" % byte}.join + + "#{AppConfig.privacy.camo.root}#{digest}/#{encoded_url}" + end + end +end diff --git a/lib/diaspora/message_renderer.rb b/lib/diaspora/message_renderer.rb index 3714e4487..3549d3721 100644 --- a/lib/diaspora/message_renderer.rb +++ b/lib/diaspora/message_renderer.rb @@ -87,6 +87,12 @@ module Diaspora def render_tags @message = Diaspora::Taggable.format_tags message, no_escape: !options[:escape_tags] end + + def camo_urls + @message = @message.gsub(/!\[.*?\]\((.+?)\)/) do |link| + link.gsub($1, Diaspora::CamoUrl::image_url($1)) + end + end end DEFAULTS = {mentioned_people: [], @@ -165,6 +171,13 @@ module Diaspora } end + # @param [Hash] opts Override global output options, see {#initialize} + def plain_text_for_json opts={} + process(opts) { + camo_urls if AppConfig.privacy.camo.proxy_markdown_images? + } + end + # @param [Hash] opts Override global output options, see {#initialize} def html opts={} process(opts) {