Skip to content

Hardening: apply Rails' ImageMagick security policy in every cell - #129

Open
flavorjones wants to merge 1 commit into
masterfrom
card-944-rails-security-fixes
Open

flavorjones wants to merge 1 commit into
masterfrom
card-944-rails-security-fixes

Conversation

@flavorjones

Copy link
Copy Markdown
Member

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. MSL runs a script that reads and writes files. The team fixed it in rails/rails@468aa9a by shipping a policy.xml that denies those coders and putting its directory on MAGICK_CONFIGURE_PATH.

Hot Cell ships no policy, so a cell applies whatever policy.xml its image installs. The stock policies in Debian 12, Debian 13 and Ubuntu 24.04 don't deny MSL. Hot Cell runs ImageMagick in these ways:

  • mini_magick spawns identify and magick for analyzers.image.magick and transformers.image.magick. MagickOperation.cli_env names the variables mini_magick passes those processes besides HOME, PATH and LANG, and leaves MAGICK_CONFIGURE_PATH out, so an operator could apply their own policy to those processes only by replacing the system policy.xml.
  • magickload runs ImageMagick inside the worker when libvips delegates a format to it, and reads the worker's environment. libvips refuses magickload on 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 magick directly (#7) would return its environment to run_tool.

Details

  • activestorage-hotcell-server/lib/active_storage/hot_cell/server/image_magick/policy.xml is Rails' file, byte for byte. It denies MSL; TEXT, EPHEMERAL, FILE and MPC, which read or delete other files; the network and display coders; the Ghostscript, GhostXPS and GhostPCL coders and the gs delegate; and @filename indirection. It sets no resource limits.
  • ActiveStorage::HotCell::Server::ImageMagick.configure_path builds MAGICK_CONFIGURE_PATH the way Rails' ActiveStorage::ImageMagick.configure_path does: the shipped directory first, then the directories the image set, without the shipped one again or empty entries. Unlike Rails, it raises HotCell::ConfigurationError at load when the gem's path contains :, because ImageMagick would split the path and find no policy.
  • When vips_operation.rb or magick_operation.rb loads, the gem sets that value in the supervisor's environment, before image_processing loads libvips and before any worker forks. Every worker inherits it, so magickload reads it. Setting an environment variable evaluates no image, so invariant 3 holds.
  • MagickOperation.cli_env passes the same value to identify and magick on every request.
  • ImageMagick reads every policy.xml on MAGICK_CONFIGURE_PATH in order, then its system policy.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 in MAGICK_CONFIGURE_PATH or from a replaced system policy.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 denies PDF) by replacing it.
  • An application that renders PostScript, PDF, XPS or PCL through ImageMagick must allow those coders and the gs delegate again. CHANGELOG.md lists this under Upgrading.
  • Docs: docs/imagemagick.md has 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.md adds MAGICK_CONFIGURE_PATH, docs/active-storage.md points to the section, and the installed Dockerfile has a commented example of each customization.
  • Tests: test/magick_policy_test.rb ports Rails' tests. They check that every rule loads, that each denied coder and @filename is 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 that magickload in a worker that loads only the vips operations runs under the policy.
  • test/magick_environment_test.rb checks invariant 9, that the worker's environment doesn't reach magick. It set MAGICK_CONFIGURE_PATH in the worker and expected magick not to see it. cli_env now passes that variable on, so the test sets XDG_CONFIG_HOME instead, which ImageMagick also reads a policy from.

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
Copilot AI balanced review requested due to automatic review settings November 4, 2026 16:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants