Skip to content

Consider removing dependency on mini_magick #7

Closed

Description

@flavorjones

The two ImageMagick operations copy their input onto scratch before using it. Every other shipped operation reads the descriptor in place.

transformers/image/magick.rb:34 returns source.path from source_path, and analyzers/image/magick.rb:28 passes source.path to identify. Both vips operations, both PDF previewers and the ffmpeg previewer read source.fd_path.

MagickOperation records why: mini_magick spawns its own magick, which does not inherit this worker's descriptors, so the input cannot be handed over as /dev/fd/N.

What it costs. Staging is a write, so RLIMIT_FSIZE bounds it, and an input larger than the operation's file_size dies as a permanent fsize verdict — a ceiling Rails does not have and the vips operations shed. It also consumes scratch: file_size × concurrency has to fit the tmpfs, and a staged input counts against that budget alongside the output.

The fix named in MagickOperation is to drive magick directly through run_tool's pass:, which is what every other exec'd operation already does.

The trade to weigh. Both operations reach magick through ImageProcessing::MiniMagick, which supplies the saver and its option handling. Driving the tool directly means restating that by hand — the same argument transforming.rb makes for keeping ImageProcessing on the vips path, so it deserves the same scrutiny here.

Scope note: this is about the input only. The output side of these operations cannot be improved the same way. ImageProcessing picks its saver from the destination's extension, and a /dev/fd path has none — verified, it fails with VipsForeignSave: "/dev/fd/5" is not a known file format even with convert("png") set. Of the five operations with outputs, only the ffmpeg previewer writes its destination by name, and the other four are each blocked for a tool-specific reason.

Found auditing which Active Storage operations re-open a descriptor by name, after the shared-group work. Previously tracked as Fizzy card #380.

Activity

  1. flavorjones commented on Aug 18, 2026

    @flavorjones
    MemberAuthor

    A second reason for the same change, from the pre-release security review.

    Invariant 9 gives a tool only the environment its operation wrote. These two operations do not use run_tool, so mini_magick spawns magick itself. MiniMagick.restricted_env defaults to false, and with it false the child inherited the worker's whole environment. 95aa6b3 sets restricted_env and cli_env, which closes it.

    That fix uses mini_magick's own mechanism, which filters the environment it was given. run_tool writes a fresh one with unsetenv_others: true. Driving magick directly would make the environment ours rather than the library's, on the code path this issue already proposes.

  2. changed the title [-]The two magick operations stage their input, restoring the file_size ceiling[/-] [+]Consider removing dependency on mini_magick[/+] on Aug 18, 2026
  3. flavorjones commented on Aug 18, 2026

    @flavorjones
    MemberAuthor

    A third reason, also from the pre-release security review.

    Operation#run_tool bounds what it reads from a tool at 64KB and discards the rest as it arrives. Its comment says why: an input that makes a tool print gigabytes of diagnostics costs the worker gigabytes of address space, takes RLIMIT_DATA with it, and arrives as a memory verdict — permanent — for a document whose only crime was being noisy.

    These two operations do not use run_tool. MiniMagick::Shell#execute runs Thread.new { stdout_io.read } per stream and accumulates both in full. mini_magick has no setting that bounds either; graphicsmagick, cli_prefix, cli_env, restricted_env, timeout, logger, tmpdir, errors and warnings are the whole list.

    So a hostile image that makes identify noisy enough is recorded against the blob as a permanent memory verdict and never retried, where the same input on any run_tool operation is bounded and answered normally.

    Marked as an accepted risk in MagickOperation for now, pointing here. That makes three reasons for this change: input staging, environment ownership, and output bounding.

  4. flavorjones commented on Aug 20, 2026

    @flavorjones
    MemberAuthor

    I think we should probably also consider sending a PR to minimagick adding support for file descriptor sharing, similar to what run_tool already does.

  5. flavorjones commented on Aug 25, 2026

    @flavorjones
    MemberAuthor

    🤖 A concrete case for this issue.

    What we saw

    Analyzing a large layered PSD was killed. Analyzers::Image::Magick reads source.path, and Input#path copies the descriptor onto worker scratch before magick runs. That copy is a write, RLIMIT_FSIZE bounds writes, and the operation sets file_size: 48MB — so any input over 48MB dies during the copy, before magick is ever started.

    Why that belongs on this issue

    The copy exists only because mini_magick cannot be handed an open descriptor. Give image_processing/mini_magick fd support and there is no copy, and therefore no ceiling. PSD is just the format that hits it first; the ceiling applies to every input MagickOperation stages.

    Evidence

    The fd route already works at the command line — magick identify psd:/dev/fd/3 on a 1GB 13-layer 6000x6000 PSD returned 6000x6000 in 0.19s.

    The blocker is the handoff: mini_magick spawns magick through Open3.popen3, Ruby marks our descriptors close-on-exec, so /dev/fd/N does not exist in that child.

    Don't bother retrying this

    Staging only a prefix of the input, on the theory that a small write fits under file_size. identify walks the PSD layer records and will not fall back to the header dimensions it has already read: 8KB, 64KB and 1MB prefixes of that 1GB file all exit 1 in ReadPSDLayersInternal. A 50KB fixture succeeds at 4KB, which is why it looks viable until it is tried at size.

    Full record: https://app.basecamp.com/2914079/buckets/1666/card_tables/cards/10236340587

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions