about summary refs log tree commit diff
path: root/app/lib/sanitize_config.rb
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2020-02-09 12:15:55 +0100
committerThibaut Girka <thib@sitedethib.com>2020-02-09 12:15:55 +0100
commitdae5e446fe7294dba0e14311ef3da4dc8fff6a3a (patch)
tree33a76386b1e8bc9c968bfc49409f72000b7ec410 /app/lib/sanitize_config.rb
parenta2cfe3daaadabfaad71969a44c460bd76b8405ff (diff)
parent57c42c20c01b46f0d7cad5a357d56190274a3fa1 (diff)
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts:
- `Gemfile`:
  We updated httplog in a separate commit.
  Took upstream's change which updated it further.
- `Gemfile.lock`:
  We updated httplog in a separate commit.
  Took upstream's change which updated it further.
- `app/lib/sanitize_config.rb`:
  Upstream added better unsupported link stripping,
  while we had different sanitizing configs.
  Took only upstream's link stripping code.
- `config/locales/simple_form.pl.yml`:
  Strings unused in glitch-soc had been removed from
  glitch-soc, reintroduced them even if they are not
  useful, to reduce the risk of later merge conflicts.
Diffstat (limited to 'app/lib/sanitize_config.rb')
-rw-r--r--app/lib/sanitize_config.rb39
1 files changed, 36 insertions, 3 deletions
diff --git a/app/lib/sanitize_config.rb b/app/lib/sanitize_config.rb
index 2b5d554b5..e3fc94ba6 100644
--- a/app/lib/sanitize_config.rb
+++ b/app/lib/sanitize_config.rb
@@ -2,7 +2,23 @@
 
 class Sanitize
   module Config
-    HTTP_PROTOCOLS ||= ['http', 'https', 'dat', 'dweb', 'ipfs', 'ipns', 'ssb', 'gopher', 'xmpp', 'magnet', :relative].freeze
+    HTTP_PROTOCOLS = %w(
+      http
+      https
+    ).freeze
+
+    LINK_PROTOCOLS = %w(
+      http
+      https
+      dat
+      dweb
+      ipfs
+      ipns
+      ssb
+      gopher
+      xmpp
+      magnet
+    ).freeze
 
     CLASS_WHITELIST_TRANSFORMER = lambda do |env|
       node = env[:node]
@@ -38,6 +54,22 @@ class Sanitize
       end
     end
 
+    UNSUPPORTED_HREF_TRANSFORMER = lambda do |env|
+      return unless env[:node_name] == 'a'
+
+      current_node = env[:node]
+
+      scheme = begin
+        if current_node['href'] =~ Sanitize::REGEX_PROTOCOL
+          Regexp.last_match(1).downcase
+        else
+          :relative
+        end
+      end
+
+      current_node.replace(current_node.text) unless LINK_PROTOCOLS.include?(scheme)
+    end
+
     MASTODON_STRICT ||= freeze_config(
       elements: %w(p br span a abbr del pre blockquote code b strong u sub sup i em h1 h2 h3 h4 h5 ul ol li),
 
@@ -56,13 +88,14 @@ class Sanitize
       },
 
       protocols: {
-        'a'          => { 'href' => HTTP_PROTOCOLS },
-        'blockquote' => { 'cite' => HTTP_PROTOCOLS },
+        'a'          => { 'href' => LINK_PROTOCOLS },
+        'blockquote' => { 'cite' => LINK_PROTOCOLS },
       },
 
       transformers: [
         CLASS_WHITELIST_TRANSFORMER,
         IMG_TAG_TRANSFORMER,
+        UNSUPPORTED_HREF_TRANSFORMER,
       ]
     )