Repository navigation
Hardening: apply Rails' ImageMagick security policy in every cell - #129
Open
flavorjones wants to merge 1 commit into
Open
flavorjones wants to merge 1 commit into
flavorjones wants to merge 1 commit into
Conversation
A cell applied only the ImageMagick policy its image installed, so an uploaded file could reach coders such as `MSL`, which reads and writes any file the worker can open. Ship Rails' `policy.xml` unchanged and put its directory first in `MAGICK_CONFIGURE_PATH`, in the supervisor's environment and in `MagickOperation.cli_env`, keeping any directories the image sets after it. To customize it, see "Security policy" in `docs/imagemagick.md`. ref: GHSA-f3w6-w445-fwh9
There was a problem hiding this comment.
🔵 Needs a closer look
Security enforcement depends on ImageMagick policy precedence and libvips initialization that could not be verified here, requiring integration-test confirmation across supported ImageMagick 6/7 builds.
No open issues 🎉
What changed in this PR
This PR hardens Hot Cell’s Active Storage image processing by applying Rails’ ImageMagick security policy to both MiniMagick subprocesses and in-process libvips delegation, while preserving operator policy overrides.
Changes:
- Ships Rails’ policy and configures its path before image-processing libraries load.
- Adds policy enforcement, override, compatibility, and environment-isolation tests.
- Documents policy customization and upgrade requirements for applications using restricted formats.
[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or rungh pr ready --undo.
Click "Ready for review" or rungh pr readyto reengage.
| File | Description |
|---|---|
hotcell-client/lib/hot_cell/install/Dockerfile.tt |
Adds commented policy customization examples. |
docs/index.md |
Updates the ImageMagick reference description. |
docs/imagemagick.md |
Documents policy enforcement, precedence, customization, and inspection. |
docs/cell-settings.md |
Documents MAGICK_CONFIGURE_PATH. |
docs/active-storage.md |
Notes the image operations’ security policy. |
CHANGELOG.md |
Records security changes and upgrade requirements. |
activestorage-hotcell-server/test/magick_policy_test.rb |
Adds policy enforcement and operator override tests. |
activestorage-hotcell-server/test/magick_environment_test.rb |
Uses XDG_CONFIG_HOME to test environment isolation. |
activestorage-hotcell-server/lib/active_storage/hot_cell/server/vips_operation.rb |
Loads policy configuration before libvips. |
activestorage-hotcell-server/lib/active_storage/hot_cell/server/magick_operation.rb |
Loads policy configuration and passes it to MiniMagick. |
activestorage-hotcell-server/lib/active_storage/hot_cell/server/image_magick/policy.xml |
Adds Rails’ ImageMagick security policy. |
activestorage-hotcell-server/lib/active_storage/hot_cell/server/image_magick.rb |
Builds the policy search path and rejects incompatible installation paths. |
Review effort: Balanced
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The Rails team published GHSA-f3w6-w445-fwh9: Active Storage handed uploads to ImageMagick with no constraint on its coders, the readers and writers for each format. ImageMagick picks a coder from a file's content, and some coders act on files that the image names rather than on the image.
MSLruns a script that reads and writes files. The team fixed it in rails/rails@468aa9a by shipping apolicy.xmlthat denies those coders and putting its directory onMAGICK_CONFIGURE_PATH.Hot Cell ships no policy, so a cell applies whatever
policy.xmlits image installs. The stock policies in Debian 12, Debian 13 and Ubuntu 24.04 don't denyMSL. Hot Cell runs ImageMagick in these ways:identifyandmagickforanalyzers.image.magickandtransformers.image.magick.MagickOperation.cli_envnames the variables mini_magick passes those processes besidesHOME,PATHandLANG, and leavesMAGICK_CONFIGURE_PATHout, so an operator could apply their own policy to those processes only by replacing the systempolicy.xml.magickloadruns ImageMagick inside the worker when libvips delegates a format to it, and reads the worker's environment. libvips refusesmagickloadon untrusted input, so the shipped operations don't reach it, but an operation that allows it again does.A cell holds no credentials and has no network, so a crafted upload reaches less than it does in Rails: only the files a worker can open, which include the scratch files of other requests in flight in the same cell. The threat model already starts from an arbitrary file read inside a cell; this change closes the easiest way to get one.
Related: Landlock (#13) would narrow what a worker can open, and driving
magickdirectly (#7) would return its environment torun_tool.Details
activestorage-hotcell-server/lib/active_storage/hot_cell/server/image_magick/policy.xmlis Rails' file, byte for byte. It deniesMSL;TEXT,EPHEMERAL,FILEandMPC, which read or delete other files; the network and display coders; the Ghostscript, GhostXPS and GhostPCL coders and thegsdelegate; and@filenameindirection. It sets no resource limits.ActiveStorage::HotCell::Server::ImageMagick.configure_pathbuildsMAGICK_CONFIGURE_PATHthe way Rails'ActiveStorage::ImageMagick.configure_pathdoes: the shipped directory first, then the directories the image set, without the shipped one again or empty entries. Unlike Rails, it raisesHotCell::ConfigurationErrorat load when the gem's path contains:, because ImageMagick would split the path and find no policy.vips_operation.rbormagick_operation.rbloads, the gem sets that value in the supervisor's environment, beforeimage_processingloads libvips and before any worker forks. Every worker inherits it, somagickloadreads it. Setting an environment variable evaluates no image, so invariant 3 holds.MagickOperation.cli_envpasses the same value toidentifyandmagickon every request.policy.xmlonMAGICK_CONFIGURE_PATHin order, then its systempolicy.xml, and for a coder the last matching rule wins. An operator's policy loads after the shipped one, so it can deny more or allow again, either from a directory the image names inMAGICK_CONFIGURE_PATHor from a replaced systempolicy.xml. Both work on ImageMagick 6.9.11, 6.9.12, 7.1.1 and 7.1.2. The system policy overrides a directory the image names, so an operator allows a coder the system policy denies (Debian 12's deniesPDF) by replacing it.gsdelegate again.CHANGELOG.mdlists this underUpgrading.docs/imagemagick.mdhas a new "Security policy" section on what the policy denies, how it is applied, both ways to customize it, and how to list what a cell loads.docs/cell-settings.mdaddsMAGICK_CONFIGURE_PATH,docs/active-storage.mdpoints to the section, and the installedDockerfilehas a commented example of each customization.test/magick_policy_test.rbports Rails' tests. They check that every rule loads, that each denied coder and@filenameis refused, and that an operator policy can allow a coder again. Through a real cell, they check that the magick analyzer and transformer refuse PostScript and a pixel cache chosen by content, that every fixture still analyzes and transforms, and that a policy the image names reaches the cell. They also check thatmagickloadin a worker that loads only the vips operations runs under the policy.test/magick_environment_test.rbchecks invariant 9, that the worker's environment doesn't reachmagick. It setMAGICK_CONFIGURE_PATHin the worker and expectedmagicknot to see it.cli_envnow passes that variable on, so the test setsXDG_CONFIG_HOMEinstead, which ImageMagick also reads a policy from.