activestorage 8.0.5 → 8.0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb8d04a9237523518b1a6b00aab432decc944e0432e7296d1f283126c73eb374
4
- data.tar.gz: 74fabd6db3bfefae7e51f9be208fafb37b76e5df3bdc238bafe60fa8357bd405
3
+ metadata.gz: 4a80631946acf9e13e4ec23935d8af2716be34402fde6e152b7729d265b7143f
4
+ data.tar.gz: 9f88ee639a12ee64597157ffb234cb6f1c1795c977d8d032a9b898056a8945dd
5
5
  SHA512:
6
- metadata.gz: e8555d671d585f19dcecb996f38d4aa4f182e3e98082856916e13cefa8f6f9c0288f11dbf83f7f80f641dbf82e5e93a39559bf7e86b98638b3737029c17660c4
7
- data.tar.gz: bd3b5a92c37258cecf1bca978f4a59a090c6bb20a30cf290479a173add2efa6de64ffe120d4649c1d4d9aa3b0e500c2ae7b24b44ee4b6413e65efafe7eac91e4
6
+ metadata.gz: e115b9bdf1dc8dc6a8b1b4308472be20ca9b8fb8f312ce6be4a953e1b193398b766acfb06e29fd5f91d3182dc93d988cb2a30b71b0a304151f44aea99947c433
7
+ data.tar.gz: f5674d75df03fda65bbfc9d57853732c1636ca2fd4d4ade3badd1ac0d62e799c17dde356104effa419865136e4d56a5d73fd2233e234349015d28f44600ee0e1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,171 @@
1
+ ## Rails 8.0.5.2 (November 03, 2026) ##
2
+
3
+ * Preserve committed proxy responses when a storage error interrupts a
4
+ streamed download.
5
+
6
+ [GHSA-4v2w-w6g6-f8f8]
7
+
8
+ *Jeremy Daer*
9
+
10
+ * Derive `ActiveStorage::Filename#base` and `#extension` from the sanitized
11
+ filename.
12
+
13
+ This prevents null bytes and invalid byte sequences in stored filename
14
+ metadata from raising when a blob is opened or represented. Existing blob
15
+ records are handled without a data migration.
16
+
17
+ [GHSA-p2pm-g2h6-7j5j]
18
+
19
+ *Jeremy Daer*
20
+
21
+ * Ship an ImageMagick security policy and apply it to every ImageMagick
22
+ process MiniMagick spawns.
23
+
24
+ ImageMagick chooses a coder from the content of the file it is given, and
25
+ some coders act on files named inside the image rather than on the image
26
+ itself. Active Storage constrained none of them, so an uploaded file could
27
+ reach any coder the host's policy allowed, through the ImageMagick analyzer
28
+ at attach time, with no variant or `analyze` call in the application.
29
+
30
+ Active Storage now ships a `policy.xml` in
31
+ `ActiveStorage::ImageMagick::POLICY_DIRECTORY` and prepends that directory
32
+ to `MAGICK_CONFIGURE_PATH` through `MiniMagick.cli_env`. ImageMagick applies
33
+ every policy file on its search path, so the shipped policy adds to the
34
+ host's policy rather than replacing it. A `MAGICK_CONFIGURE_PATH` set in the
35
+ environment or in `MiniMagick.cli_env`, including by an initializer or
36
+ dotenv, is kept after the shipped directory.
37
+
38
+ The shipped policy denies the coders that script ImageMagick (`MSL`), read
39
+ or delete files other than the image (`TEXT`, `EPHEMERAL`), fetch remote
40
+ references (`URL`, `HTTP`, `HTTPS`, `FTP`), display to a screen (`SHOW`,
41
+ `WIN`, `PLT`), or hand the file to Ghostscript (the `PS`, `EPS`, `EPT` and
42
+ `PDF` coders, every alias of them, and the `gs` delegate), or reach a file
43
+ directly (`FILE`, `MPC`, `XPS`, `PCL`), and it refuses `@filename`
44
+ indirection. The policy applies to every ImageMagick command MiniMagick
45
+ runs, not only Active Storage's.
46
+
47
+ These applications must act:
48
+
49
+ * One whose variant processor is `:mini_magick` has to use mini_magick 5.1
50
+ or later, which applying the policy requires. With an older mini_magick,
51
+ it raises at boot.
52
+ * One that renders PostScript or PDF through ImageMagick has to re-allow
53
+ the `gs` delegate and the coders it uses in the host's `policy.xml`. A
54
+ host policy that allows a coder overrides the shipped deny.
55
+ * One that replaces `MiniMagick.cli_env` after it boots, clears the
56
+ environment with `MiniMagick.cli_prefix`, or installs its own
57
+ `MiniMagick::Shell` never receives the shipped policy and has to carry
58
+ its rules in ImageMagick's system policy. So does one that uses
59
+ GraphicsMagick, which does not read ImageMagick policies.
60
+
61
+ Every other application needs no change. See "ImageMagick Security Policy"
62
+ in the Active Storage guide.
63
+
64
+ [GHSA-f3w6-w445-fwh9]
65
+
66
+ *Mike Dalessio*
67
+
68
+ * Enforce the declared content length on Google Cloud Storage direct uploads.
69
+
70
+ The signed URL now requires an `x-goog-content-length-range` header matching the blob's
71
+ `byte_size`, so Cloud Storage rejects a body of any other size.
72
+
73
+ Browsers send the new header, so a bucket's CORS configuration must list
74
+ `x-goog-content-length-range` in `responseHeader`, or direct uploads from the browser fail.
75
+
76
+ [GHSA-hfr4-8rjc-97m2]
77
+
78
+ *Mike Dalessio*
79
+
80
+ * Sign GCS direct uploads with V2 when `cache_control` is set.
81
+
82
+ V4 signatures did not bind the Content-Type or Content-MD5 headers, so the uploader could
83
+ choose the stored Content-Type. Direct-upload URLs with `cache_control` are no longer limited
84
+ to 7 days, and the client sends `Cache-Control` unsigned.
85
+
86
+ [GHSA-3w93-6hmw-whh4]
87
+
88
+ *Mike Dalessio*
89
+
90
+ * Stop an uploaded filename choosing the extension of the tempfile Active
91
+ Storage hands to an external tool.
92
+
93
+ `ActiveStorage::Blob#open` named its tempfile after the uploaded filename,
94
+ extension and all. Tools that select a decoder from the extension alone,
95
+ such as FFmpeg's AviSynth demuxer on `.avs`, therefore took their
96
+ instructions from the uploader. The extension is now kept only when it
97
+ denotes the blob's recorded media type, and dropped otherwise.
98
+
99
+ [GHSA-v924-7rp8-4mxh]
100
+
101
+ *Mike Dalessio*
102
+
103
+ * Restrict the formats ffmpeg and ffprobe will open when previewing and
104
+ analyzing video and audio.
105
+
106
+ Active Storage now passes `-format_whitelist` before the input at every
107
+ ffmpeg and ffprobe call site, limiting demuxer selection to common audio and
108
+ video container formats. Configure the list with
109
+ `config.active_storage.ffmpeg_allowed_formats`, or set it to `nil` to let
110
+ ffmpeg choose any demuxer it was built with.
111
+
112
+ Files in other formats, such as MXF, DV, AC3, WavPack, AU, W64, and raw
113
+ H.264 or MPEG-1/2 video streams, now fail to preview with
114
+ `ActiveStorage::PreviewError`, and their analysis stores no media metadata
115
+ and logs a warning. Add their demuxers to the list to keep them.
116
+
117
+ ```ruby
118
+ config.active_storage.ffmpeg_allowed_formats += %w[ mxf ]
119
+ ```
120
+
121
+ [GHSA-3f4q-8x5m-76v8]
122
+
123
+ *Mike Dalessio*
124
+
125
+ ## Rails 8.0.5.1 (July 29, 2026) ##
126
+
127
+ * Disable libvips's unfuzzed image loaders and savers.
128
+
129
+ libvips flags some of its loaders and savers as "unfuzzed" or "untrusted", meaning they are only
130
+ safe for trusted content. Active Storage will call `Vips.block_untrusted(true)` to disable them
131
+ while booting. An application that needs a specific loader or saver may re-enable it in an
132
+ initializer.
133
+
134
+ This is a breaking change for applications that process image types with an unfuzzed loader or
135
+ saver. Variant transformation of BMP, ICO, and PSD attachments will raise `Vips::Error`, and
136
+ analysis of these and other types such as SVG, JPEG XL, JPEG 2000, and Netpbm will no longer
137
+ record `width` and `height`. Requesting an unfuzzed output format, typically FITS, JXL, or
138
+ anything delegated to ImageMagick, will also raise `Vips::Error`. Attaching, storing, and
139
+ downloading are unchanged.
140
+
141
+ An application seeing `Vips::Error` raised during image transformation may wish to remove the
142
+ affected content types from `config.active_storage.variable_content_types` in an initializer.
143
+ Active Storage will then treat those attachments as not variable and will not generate variants
144
+ for them. This most often matters to an application that transforms images during a request
145
+ rather than in a background job, where the failure surfaces as an error response instead of a
146
+ failed job.
147
+
148
+ ```ruby
149
+ Rails.application.config.active_storage.variable_content_types -=
150
+ %w[ image/bmp image/vnd.microsoft.icon image/vnd.adobe.photoshop ]
151
+ ```
152
+
153
+ Applications using the `:mini_magick` variant processor will see no change in how their
154
+ attachments are processed, but the loaders and savers will be disabled process-wide whenever
155
+ ruby-vips is installed, and the version requirements below will still apply. Such an application
156
+ may remove ruby-vips from its Gemfile to avoid both.
157
+
158
+ The minimum supported version of libvips is now 8.13, and the minimum supported version of
159
+ ruby-vips is now 2.2.1. These are the earliest versions that are capable of disabling untrusted
160
+ operations. When ruby-vips is installed and either minimum is not met, Active Storage will raise
161
+ a `RuntimeError` while booting rather than run in an unsecurable environment.
162
+
163
+ [GHSA-xr9x-r78c-5hrm]
164
+ [CVE-2026-66066]
165
+
166
+ *Mike Dalessio*
167
+
168
+
1
169
  ## Rails 8.0.5 (March 24, 2026) ##
2
170
 
3
171
  * Fix `ActiveStorage::Blob` content type predicate methods to handle `nil`.
@@ -69,13 +69,18 @@ module ActiveStorage::Streaming
69
69
  stream.write chunk
70
70
  end
71
71
  rescue ActiveStorage::FileNotFoundError
72
+ raise if response.committed?
73
+
72
74
  expires_now
73
75
  head :not_found
74
76
  rescue
75
- # Status and caching headers are already set, but not committed.
76
- # Change the status to 500 manually.
77
- expires_now
78
- head :internal_server_error
77
+ # Until the first chunk is written, status and caching headers are set
78
+ # but not committed. Change the status to 500 manually.
79
+ unless response.committed?
80
+ expires_now
81
+ head :internal_server_error
82
+ end
83
+
79
84
  raise
80
85
  end
81
86
  end
@@ -295,7 +295,8 @@ class ActiveStorage::Blob < ActiveStorage::Record
295
295
 
296
296
  # Downloads the blob to a tempfile on disk. Yields the tempfile.
297
297
  #
298
- # The tempfile's name is prefixed with +ActiveStorage-+ and the blob's ID. Its extension matches that of the blob.
298
+ # The tempfile's name is prefixed with +ActiveStorage-+ and the blob's ID. It keeps the blob filename's
299
+ # extension when that extension denotes the blob's content type, and has no extension otherwise.
299
300
  #
300
301
  # By default, the tempfile is created in <tt>Dir.tmpdir</tt>. Pass +tmpdir:+ to create it in a different directory:
301
302
  #
@@ -311,7 +312,7 @@ class ActiveStorage::Blob < ActiveStorage::Record
311
312
  key,
312
313
  checksum: checksum,
313
314
  verify: !composed,
314
- name: [ "ActiveStorage-#{id}-", filename.extension_with_delimiter ],
315
+ name: [ "ActiveStorage-#{id}-", tempfile_extension ],
315
316
  tmpdir: tmpdir,
316
317
  &block
317
318
  )
@@ -350,6 +351,25 @@ class ActiveStorage::Blob < ActiveStorage::Record
350
351
  end
351
352
 
352
353
  private
354
+ # The extension is there for tools that cannot tell the type from the bytes (316d87e4: ImageMagick
355
+ # needs it for an SVG with no XML declaration), but some tools choose a decoder on it alone: FFmpeg
356
+ # runs a `.avs` file as an AviSynth script. So the uploaded extension is kept only when it denotes
357
+ # the blob's media type.
358
+ def tempfile_extension
359
+ extension = filename.extension_with_delimiter
360
+ return "" if extension.blank?
361
+
362
+ type_for_extension = Marcel::MimeType.for(name: filename.to_s)
363
+ return "" if type_for_extension == Marcel::MimeType::BINARY || type_for_extension != media_type
364
+
365
+ extension
366
+ end
367
+
368
+ # Marcel answers in canonical lowercase, so drop parameters and case before comparing.
369
+ def media_type
370
+ content_type.to_s.split(";").first.to_s.strip.downcase
371
+ end
372
+
353
373
  def compute_checksum_in_chunks(io)
354
374
  raise ArgumentError, "io must be rewindable" unless io.respond_to?(:rewind)
355
375
 
@@ -25,7 +25,7 @@ class ActiveStorage::Filename
25
25
  # ActiveStorage::Filename.new("racecar").base # => "racecar"
26
26
  # ActiveStorage::Filename.new(".gitignore").base # => ".gitignore"
27
27
  def base
28
- File.basename @filename, extension_with_delimiter
28
+ File.basename sanitized, extension_with_delimiter
29
29
  end
30
30
 
31
31
  # Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at the
@@ -35,7 +35,7 @@ class ActiveStorage::Filename
35
35
  # ActiveStorage::Filename.new("racecar").extension_with_delimiter # => ""
36
36
  # ActiveStorage::Filename.new(".gitignore").extension_with_delimiter # => ""
37
37
  def extension_with_delimiter
38
- File.extname @filename
38
+ File.extname sanitized
39
39
  end
40
40
 
41
41
  # Returns the extension of the filename (i.e. the substring following the last dot, excluding a dot at
@@ -55,9 +55,10 @@ class ActiveStorage::Filename
55
55
  # ActiveStorage::Filename.new("foo:bar.jpg").sanitized # => "foo-bar.jpg"
56
56
  # ActiveStorage::Filename.new("foo/bar.jpg").sanitized # => "foo-bar.jpg"
57
57
  #
58
- # Characters considered unsafe for storage (e.g. \, $, and the RTL override character) are replaced with a dash.
58
+ # Characters considered unsafe for storage (e.g. \, $, the null character, and the RTL override character) are
59
+ # replaced with a dash.
59
60
  def sanitized
60
- @filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}%$|:;/<>?*\"\t\r\n\\", "-")
61
+ @filename.encode(Encoding::UTF_8, invalid: :replace, undef: :replace, replace: "�").strip.tr("\u{202E}\u{0000}%$|:;/<>?*\"\t\r\n\\", "-")
61
62
  end
62
63
 
63
64
  # Returns the sanitized version of the filename.
@@ -60,9 +60,12 @@ module ActiveStorage
60
60
  "-show_streams",
61
61
  "-show_format",
62
62
  "-v", "error",
63
+ *ActiveStorage.ffmpeg_format_whitelist_arguments,
63
64
  file.path
64
65
  ]) do |output|
65
66
  JSON.parse(output.read)
67
+ end.tap do
68
+ logger.warn "Audio analysis failed because ffprobe exited with status #{$?.exitstatus}. The file may be corrupt, or in a format outside config.active_storage.ffmpeg_allowed_formats." unless $?.success?
66
69
  end
67
70
  end
68
71
  rescue Errno::ENOENT
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_storage/image_magick"
4
+
3
5
  module ActiveStorage
4
6
  # This analyzer relies on the third-party {MiniMagick}[https://github.com/minimagick/minimagick] gem. MiniMagick requires
5
7
  # the {ImageMagick}[http://www.imagemagick.org] system library.
@@ -17,6 +19,8 @@ module ActiveStorage
17
19
  return {}
18
20
  end
19
21
 
22
+ ActiveStorage.require_securable_mini_magick!
23
+
20
24
  download_blob_to_tempfile do |file|
21
25
  image = instrument("mini_magick") do
22
26
  MiniMagick::Image.new(file.path)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_storage/vips"
4
+
3
5
  module ActiveStorage
4
6
  # This analyzer relies on the third-party {ruby-vips}[https://github.com/libvips/ruby-vips] gem. Ruby-vips requires
5
7
  # the {libvips}[https://libvips.github.io/libvips/] system library.
@@ -140,9 +140,12 @@ module ActiveStorage
140
140
  "-show_streams",
141
141
  "-show_format",
142
142
  "-v", "error",
143
+ *ActiveStorage.ffmpeg_format_whitelist_arguments,
143
144
  file.path
144
145
  ]) do |output|
145
146
  JSON.parse(output.read)
147
+ end.tap do
148
+ logger.warn "Video analysis failed because ffprobe exited with status #{$?.exitstatus}. The file may be corrupt, or in a format outside config.active_storage.ffmpeg_allowed_formats." unless $?.success?
146
149
  end
147
150
  end
148
151
  rescue Errno::ENOENT
@@ -31,6 +31,7 @@ module ActiveStorage
31
31
  config.active_storage.paths = ActiveSupport::OrderedOptions.new
32
32
  config.active_storage.queues = ActiveSupport::InheritableOptions.new
33
33
  config.active_storage.precompile_assets = true
34
+ config.active_storage.ffmpeg_allowed_formats = ActiveStorage.ffmpeg_allowed_formats
34
35
 
35
36
  config.active_storage.variable_content_types = %w(
36
37
  image/png
@@ -93,6 +94,13 @@ module ActiveStorage
93
94
  ActiveStorage.variant_processor = app.config.active_storage.variant_processor || :mini_magick
94
95
  ActiveStorage.previewers = app.config.active_storage.previewers || []
95
96
  ActiveStorage.analyzers = app.config.active_storage.analyzers || []
97
+
98
+ if ActiveStorage::MINIMAGICK_AVAILABLE && !ActiveStorage::MINIMAGICK_UNSECURABLE
99
+ ActiveStorage::ImageMagick.apply_policy
100
+ end
101
+
102
+ ActiveStorage.require_securable_mini_magick! if ActiveStorage.variant_processor == :mini_magick
103
+
96
104
  ActiveStorage.paths = app.config.active_storage.paths || {}
97
105
  ActiveStorage.routes_prefix = app.config.active_storage.routes_prefix || "/rails/active_storage"
98
106
  ActiveStorage.draw_routes = app.config.active_storage.draw_routes != false
@@ -121,6 +129,7 @@ module ActiveStorage
121
129
  ActiveStorage.content_types_allowed_inline = app.config.active_storage.content_types_allowed_inline || []
122
130
  ActiveStorage.binary_content_type = app.config.active_storage.binary_content_type || "application/octet-stream"
123
131
  ActiveStorage.video_preview_arguments = app.config.active_storage.video_preview_arguments || "-y -vframes 1 -f image2"
132
+ ActiveStorage.ffmpeg_allowed_formats = app.config.active_storage.ffmpeg_allowed_formats
124
133
  ActiveStorage.track_variants = app.config.active_storage.track_variants || false
125
134
  ActiveStorage.streaming_chunk_max_size = app.config.active_storage.streaming_chunk_max_size || 100.megabytes
126
135
  end
@@ -10,7 +10,7 @@ module ActiveStorage
10
10
  MAJOR = 8
11
11
  MINOR = 0
12
12
  TINY = 5
13
- PRE = nil
13
+ PRE = "2"
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
16
16
  end
@@ -0,0 +1,68 @@
1
+ <!--
2
+ Active Storage's ImageMagick security policy. Applied in addition to the host's policy.xml,
3
+ through MAGICK_CONFIGURE_PATH, to every ImageMagick process MiniMagick spawns.
4
+
5
+ This file must work under both ImageMagick 6 and ImageMagick 7. Use the "coder", "delegate"
6
+ and "path" domains, which every release of both honors. A domain one release does not know,
7
+ such as "module" on older ImageMagick 6 releases, is ignored rather than rejected.
8
+
9
+ ImageMagick picks a coder from the file's content, and several coders name the same underlying
10
+ reader through different tokens (PS, EPS, EPI, EPSF, EPSI, EPT; PDF, AI, EPDF, PDFA, POCKETMOD). A
11
+ coder rule matches one token, so every token is denied by coder.
12
+
13
+ Do not put a backtick anywhere in this file. ImageMagick's tokenizer treats a backtick as an
14
+ opening quote and silently drops every rule up to the next apostrophe.
15
+ -->
16
+ <policymap>
17
+ <!-- Scripting coder that reads and writes files named inside the image. -->
18
+ <policy domain="coder" rights="none" pattern="MSL"/>
19
+
20
+ <!-- Coders that read a file, or delete one, other than the image being processed. -->
21
+ <policy domain="coder" rights="none" pattern="EPHEMERAL"/>
22
+ <policy domain="coder" rights="none" pattern="TEXT"/>
23
+
24
+ <!-- Coder that reads back a pixel cache from a file the image header names. -->
25
+ <policy domain="coder" rights="none" pattern="MPC"/>
26
+
27
+ <!-- Coders that display to a screen. -->
28
+ <policy domain="coder" rights="none" pattern="SHOW"/>
29
+ <policy domain="coder" rights="none" pattern="WIN"/>
30
+ <policy domain="coder" rights="none" pattern="PLT"/>
31
+
32
+ <!-- Coders that read a reference from outside the image, remote or local. FILE reads file://. -->
33
+ <policy domain="coder" rights="none" pattern="URL"/>
34
+ <policy domain="coder" rights="none" pattern="HTTP"/>
35
+ <policy domain="coder" rights="none" pattern="HTTPS"/>
36
+ <policy domain="coder" rights="none" pattern="FTP"/>
37
+ <policy domain="coder" rights="none" pattern="FILE"/>
38
+
39
+ <!-- Interpreters that read and write files of their own. Ghostscript backs the PS and PDF
40
+ coders and their token aliases; GhostXPS and GhostPCL back XPS and PCL. The "gs" delegate
41
+ covers ImageMagick builds that run Ghostscript as an external command, and the coder rules
42
+ cover builds that link it in-process, where the delegate rule never applies. An
43
+ application that transforms PostScript or PDF through ImageMagick must allow the "gs"
44
+ delegate, and the coders it uses, again in the host's policy.xml. -->
45
+ <policy domain="delegate" rights="none" pattern="gs"/>
46
+ <policy domain="coder" rights="none" pattern="PS"/>
47
+ <policy domain="coder" rights="none" pattern="PS2"/>
48
+ <policy domain="coder" rights="none" pattern="PS3"/>
49
+ <policy domain="coder" rights="none" pattern="EPS"/>
50
+ <policy domain="coder" rights="none" pattern="EPS2"/>
51
+ <policy domain="coder" rights="none" pattern="EPS3"/>
52
+ <policy domain="coder" rights="none" pattern="EPI"/>
53
+ <policy domain="coder" rights="none" pattern="EPSF"/>
54
+ <policy domain="coder" rights="none" pattern="EPSI"/>
55
+ <policy domain="coder" rights="none" pattern="EPT"/>
56
+ <policy domain="coder" rights="none" pattern="EPT2"/>
57
+ <policy domain="coder" rights="none" pattern="EPT3"/>
58
+ <policy domain="coder" rights="none" pattern="PDF"/>
59
+ <policy domain="coder" rights="none" pattern="AI"/>
60
+ <policy domain="coder" rights="none" pattern="EPDF"/>
61
+ <policy domain="coder" rights="none" pattern="PDFA"/>
62
+ <policy domain="coder" rights="none" pattern="POCKETMOD"/>
63
+ <policy domain="coder" rights="none" pattern="XPS"/>
64
+ <policy domain="coder" rights="none" pattern="PCL"/>
65
+
66
+ <!-- Indirect reads such as text:@/etc/passwd, which substitute a file for an argument. -->
67
+ <policy domain="path" rights="none" pattern="@*"/>
68
+ </policymap>
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :markup: markdown
4
+
5
+ require "active_storage"
6
+ require "active_support/core_ext/object/blank"
7
+ require "active_support/core_ext/string/filters"
8
+
9
+ begin
10
+ gem "mini_magick"
11
+ require "mini_magick"
12
+ ActiveStorage::MINIMAGICK_AVAILABLE = true # :nodoc:
13
+ rescue LoadError => error
14
+ ActiveStorage::MINIMAGICK_AVAILABLE = false # :nodoc:
15
+ raise error unless error.message.include?("mini_magick")
16
+ end
17
+
18
+ module ActiveStorage
19
+ # Adds Active Storage's ImageMagick security policy to every ImageMagick process MiniMagick spawns.
20
+ #
21
+ # ImageMagick reads every `policy.xml` on its configuration search path and applies all of them,
22
+ # so the shipped policy adds to the operator's policy rather than replacing it. `MAGICK_CONFIGURE_PATH`
23
+ # prepends to that search path; a value the operator has already set, in the environment or in
24
+ # `MiniMagick.cli_env`, is kept after the shipped directory.
25
+ module ImageMagick
26
+ POLICY_DIRECTORY = File.expand_path("image_magick", __dir__)
27
+
28
+ # Called when Active Storage loads, and again after the application initializes, so that a
29
+ # `MAGICK_CONFIGURE_PATH` or `MiniMagick.cli_env` set by an initializer or dotenv keeps the policy.
30
+ def self.apply_policy # :nodoc:
31
+ operator_paths = operator_paths(MiniMagick.cli_env["MAGICK_CONFIGURE_PATH"]).presence ||
32
+ operator_paths(ENV["MAGICK_CONFIGURE_PATH"])
33
+
34
+ MiniMagick.cli_env = MiniMagick.cli_env.merge("MAGICK_CONFIGURE_PATH" => configure_path(operator_paths)).freeze
35
+ end
36
+
37
+ def self.configure_path(operator_path)
38
+ [ POLICY_DIRECTORY, *operator_paths(operator_path) ].join(File::PATH_SEPARATOR)
39
+ end
40
+
41
+ def self.operator_paths(path)
42
+ Array(path).flat_map { |entry| entry.split(File::PATH_SEPARATOR) } - [ POLICY_DIRECTORY, "" ]
43
+ end
44
+ private_class_method :operator_paths
45
+ end
46
+ end
47
+
48
+ ActiveStorage::MINIMAGICK_UNSECURABLE = ActiveStorage::MINIMAGICK_AVAILABLE && !MiniMagick.respond_to?(:cli_env=) # :nodoc:
49
+
50
+ module ActiveStorage
51
+ # Raises when the installed mini_magick cannot apply the shipped ImageMagick security policy.
52
+ # Called before Active Storage hands untrusted content to ImageMagick.
53
+ def self.require_securable_mini_magick! # :nodoc:
54
+ if MINIMAGICK_UNSECURABLE
55
+ raise <<~ERROR.squish
56
+ Active Storage's ImageMagick security policy requires mini_magick 5.1 or later. Please add
57
+ `gem "mini_magick", "~> 5.1"` to your Gemfile, or use the :vips variant processor.
58
+ ERROR
59
+ end
60
+ end
61
+ end
62
+
63
+ ActiveStorage::ImageMagick.apply_policy if ActiveStorage::MINIMAGICK_AVAILABLE && !ActiveStorage::MINIMAGICK_UNSECURABLE
@@ -30,7 +30,7 @@ module ActiveStorage
30
30
 
31
31
  private
32
32
  def draw_relevant_frame_from(file, &block)
33
- draw self.class.ffmpeg_path, "-i", file.path, *Shellwords.split(ActiveStorage.video_preview_arguments), "-", &block
33
+ draw self.class.ffmpeg_path, *ActiveStorage.ffmpeg_format_whitelist_arguments, "-i", file.path, *Shellwords.split(ActiveStorage.video_preview_arguments), "-", &block
34
34
  end
35
35
  end
36
36
  end
@@ -89,27 +89,16 @@ module ActiveStorage
89
89
  end
90
90
  end
91
91
 
92
- def url_for_direct_upload(key, expires_in:, checksum:, custom_metadata: {}, **)
92
+ def url_for_direct_upload(key, expires_in:, checksum:, content_length:, custom_metadata: {}, **)
93
93
  instrument :url, key: key do |payload|
94
- headers = {}
95
- version = :v2
96
-
97
- if @config[:cache_control].present?
98
- headers["Cache-Control"] = @config[:cache_control]
99
- # v2 signing doesn't support non `x-goog-` headers. Only switch to v4 signing
100
- # if necessary for back-compat; v4 limits the expiration of the URL to 7 days
101
- # whereas v2 has no limit
102
- version = :v4
103
- end
104
-
105
- headers.merge!(custom_metadata_headers(custom_metadata))
106
-
94
+ # V2 signatures bind the Content-Type (empty) and Content-MD5 headers. GCS leaves headers
95
+ # other than x-goog-* out of V2 signatures, so the client sends Cache-Control unsigned.
107
96
  args = {
108
97
  content_md5: checksum,
109
98
  expires: expires_in,
110
- headers: headers,
99
+ headers: { "x-goog-content-length-range" => content_length_range(content_length), **custom_metadata_headers(custom_metadata) },
111
100
  method: "PUT",
112
- version: version,
101
+ version: :v2,
113
102
  }
114
103
 
115
104
  if @config[:iam]
@@ -125,10 +114,10 @@ module ActiveStorage
125
114
  end
126
115
  end
127
116
 
128
- def headers_for_direct_upload(key, checksum:, filename: nil, disposition: nil, custom_metadata: {}, **)
117
+ def headers_for_direct_upload(key, checksum:, content_length:, filename: nil, disposition: nil, custom_metadata: {}, **)
129
118
  content_disposition = content_disposition_with(type: disposition, filename: filename) if filename
130
119
 
131
- headers = { "Content-MD5" => checksum, "Content-Disposition" => content_disposition, **custom_metadata_headers(custom_metadata) }
120
+ headers = { "Content-MD5" => checksum, "Content-Disposition" => content_disposition, "x-goog-content-length-range" => content_length_range(content_length), **custom_metadata_headers(custom_metadata) }
132
121
  if @config[:cache_control].present?
133
122
  headers["Cache-Control"] = @config[:cache_control]
134
123
  end
@@ -228,5 +217,9 @@ module ActiveStorage
228
217
  def custom_metadata_headers(metadata)
229
218
  metadata.transform_keys { |key| "x-goog-meta-#{key}" }
230
219
  end
220
+
221
+ def content_length_range(content_length)
222
+ "#{content_length},#{content_length}"
223
+ end
231
224
  end
232
225
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_storage"
4
+ require "active_support/core_ext/string/filters"
5
+
6
+ begin
7
+ require "nokogiri"
8
+ rescue LoadError
9
+ # Ensure nokogiri is loaded before vips, which also depends on libxml2.
10
+ # See Nokogiri RFC: Stop exporting symbols:
11
+ # https://github.com/sparklemotion/nokogiri/discussions/2746
12
+ end
13
+
14
+ begin
15
+ gem "ruby-vips"
16
+ require "ruby-vips"
17
+ ActiveStorage::VIPS_AVAILABLE = true # :nodoc:
18
+ rescue LoadError => error
19
+ ActiveStorage::VIPS_AVAILABLE = false # :nodoc:
20
+ raise error unless error.message.match?(/libvips|ruby-vips/)
21
+ end
22
+
23
+ if ActiveStorage::VIPS_AVAILABLE
24
+ begin
25
+ # image_processing 2.0 calls Vips.block_untrusted(true) itself when it loads, so it has to load
26
+ # before the lines below. Leaving it to load later, when the transformer first asks for it,
27
+ # would disable the loaders again after an application's initializers had re-enabled them.
28
+ require "image_processing/vips"
29
+ rescue LoadError
30
+ # image_processing is only needed to generate variants, not to analyze blobs.
31
+ end
32
+
33
+ unless Vips.respond_to?(:block_untrusted)
34
+ raise <<~ERROR.squish
35
+ libvips's unfuzzed operations are not safe to use with untrusted content, and Active Storage
36
+ cannot disable them. Disabling them requires libvips 8.13 or later and ruby-vips 2.2.1 or
37
+ later. Please upgrade libvips and ruby-vips, or remove the ruby-vips gem from your Gemfile.
38
+ ERROR
39
+ end
40
+
41
+ Vips.block_untrusted(true)
42
+ end
@@ -376,6 +376,12 @@ module ActiveStorage
376
376
 
377
377
  mattr_accessor :video_preview_arguments, default: "-y -vframes 1 -f image2"
378
378
 
379
+ mattr_accessor :ffmpeg_allowed_formats, default: %w[ mov matroska avi mpeg mpegts flv asf ogg mp3 wav flac aac aiff caf ].freeze
380
+
381
+ def self.ffmpeg_format_whitelist_arguments # :nodoc:
382
+ [ "-format_whitelist", Array(ffmpeg_allowed_formats).join(",") ] unless ffmpeg_allowed_formats.nil?
383
+ end
384
+
379
385
  module Transformers
380
386
  extend ActiveSupport::Autoload
381
387
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activestorage
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.0.5
4
+ version: 8.0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -15,56 +15,56 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 8.0.5
18
+ version: 8.0.5.2
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 8.0.5
25
+ version: 8.0.5.2
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: actionpack
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 8.0.5
32
+ version: 8.0.5.2
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 8.0.5
39
+ version: 8.0.5.2
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: activejob
42
42
  requirement: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - '='
45
45
  - !ruby/object:Gem::Version
46
- version: 8.0.5
46
+ version: 8.0.5.2
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - '='
52
52
  - !ruby/object:Gem::Version
53
- version: 8.0.5
53
+ version: 8.0.5.2
54
54
  - !ruby/object:Gem::Dependency
55
55
  name: activerecord
56
56
  requirement: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - '='
59
59
  - !ruby/object:Gem::Version
60
- version: 8.0.5
60
+ version: 8.0.5.2
61
61
  type: :runtime
62
62
  prerelease: false
63
63
  version_requirements: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - '='
66
66
  - !ruby/object:Gem::Version
67
- version: 8.0.5
67
+ version: 8.0.5.2
68
68
  - !ruby/object:Gem::Dependency
69
69
  name: marcel
70
70
  requirement: !ruby/object:Gem::Requirement
@@ -166,6 +166,8 @@ files:
166
166
  - lib/active_storage/errors.rb
167
167
  - lib/active_storage/fixture_set.rb
168
168
  - lib/active_storage/gem_version.rb
169
+ - lib/active_storage/image_magick.rb
170
+ - lib/active_storage/image_magick/policy.xml
169
171
  - lib/active_storage/log_subscriber.rb
170
172
  - lib/active_storage/previewer.rb
171
173
  - lib/active_storage/previewer/mupdf_previewer.rb
@@ -183,16 +185,17 @@ files:
183
185
  - lib/active_storage/transformers/image_processing_transformer.rb
184
186
  - lib/active_storage/transformers/transformer.rb
185
187
  - lib/active_storage/version.rb
188
+ - lib/active_storage/vips.rb
186
189
  - lib/tasks/activestorage.rake
187
190
  homepage: https://rubyonrails.org
188
191
  licenses:
189
192
  - MIT
190
193
  metadata:
191
194
  bug_tracker_uri: https://github.com/rails/rails/issues
192
- changelog_uri: https://github.com/rails/rails/blob/v8.0.5/activestorage/CHANGELOG.md
193
- documentation_uri: https://api.rubyonrails.org/v8.0.5/
195
+ changelog_uri: https://github.com/rails/rails/blob/v8.0.5.2/activestorage/CHANGELOG.md
196
+ documentation_uri: https://api.rubyonrails.org/v8.0.5.2/
194
197
  mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
195
- source_code_uri: https://github.com/rails/rails/tree/v8.0.5/activestorage
198
+ source_code_uri: https://github.com/rails/rails/tree/v8.0.5.2/activestorage
196
199
  rubygems_mfa_required: 'true'
197
200
  rdoc_options: []
198
201
  require_paths:
@@ -208,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
211
  - !ruby/object:Gem::Version
209
212
  version: '0'
210
213
  requirements: []
211
- rubygems_version: 4.0.6
214
+ rubygems_version: 4.0.20
212
215
  specification_version: 4
213
216
  summary: Local and cloud file storage framework.
214
217
  test_files: []