]> cat aescling's git repositories - mastodon.git/commitdiff
Add a restrictive ImageMagick security policy tailored for Mastodon
authorClaire <claire.github-309c@sitedethib.com>
Thu, 8 Jun 2023 07:34:45 +0000 (09:34 +0200)
committerClaire <claire.github-309c@sitedethib.com>
Thu, 6 Jul 2023 13:30:35 +0000 (15:30 +0200)
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
config/imagemagick/policy.xml [new file with mode: 0644]
config/initializers/paperclip.rb

diff --git a/config/imagemagick/policy.xml b/config/imagemagick/policy.xml
new file mode 100644 (file)
index 0000000..1052476
--- /dev/null
@@ -0,0 +1,27 @@
+<policymap>
+  <!-- Set some basic system resource limits -->
+  <policy domain="resource" name="time" value="60" />
+
+  <policy domain="module" rights="none" pattern="URL" />
+
+  <policy domain="filter" rights="none" pattern="*" />
+
+  <!--
+    Ideally, we would restrict ImageMagick to only accessing its own
+    disk-backed pixel cache as well as Mastodon-created Tempfiles.
+
+    However, those paths depend on the operating system and environment
+    variables, so they can only be known at runtime.
+
+    Furthermore, those paths are not necessarily shared across Mastodon
+    processes, so even creating a policy.xml at runtime is impractical.
+
+    For the time being, only disable indirect reads.
+  -->
+  <policy domain="path" rights="none" pattern="@*" />
+
+  <!-- Disallow any coder by default, and only enable ones required by Mastodon -->
+  <policy domain="coder" rights="none" pattern="*" />
+  <policy domain="coder" rights="read | write" pattern="{PNG,JPEG,GIF,HEIC,WEBP}" />
+  <policy domain="coder" rights="write" pattern="{HISTOGRAM,RGB,INFO}" />
+</policymap>
index 26b0a2f7cd9b6edc1ba35283b4a40f9709676d5d..7cccccd8ee70769e6ce75c3e8a9ec4535f739f59 100644 (file)
@@ -146,3 +146,10 @@ unless defined?(Seahorse)
     end
   end
 end
+
+# Set our ImageMagick security policy, but allow admins to override it
+ENV['MAGICK_CONFIGURE_PATH'] = begin
+  imagemagick_config_paths = ENV.fetch('MAGICK_CONFIGURE_PATH', '').split(File::PATH_SEPARATOR)
+  imagemagick_config_paths << Rails.root.join('config', 'imagemagick').expand_path.to_s
+  imagemagick_config_paths.join(File::PATH_SEPARATOR)
+end