Rails,如何再次使CarrierWave再次上传SVG?

发布于 2025-02-08 16:17:31 字数 2438 浏览 4 评论 0原文

我正在升级3到7的Rails应用程序。随着新应用程序载波停止,接受SVG文件以进行上传。以前,我可以在不处理此设置的情况下上传SVG文件,而无需处理版本:

class LogoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  process :set_content_type

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{model.id}"
  end

end

因为Carriwave Set_Content_Type的0.11.0版本似乎已被弃用。这使我的SVG上传器仅仅:

class LogoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{model.id}"
  end

  def content_type_allowlist
    [/image\//]
  end

end

我不想转换SVG,并且上传受保护,仅适用于管理员。目前,我没有超越提交按钮。

尝试上传时,我现在得到以下响应:

Started PATCH "/admin/logos/18" for ::1 at 2022-06-18 10:01:19 +0200
Processing by Admin::LogosController#update as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "logo"=>{"file_name"=>#<ActionDispatch::Http::UploadedFile:0x00007ff1803932c0 @tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20220618-29575-j47l79.svg>, @original_filename="some-name.svg", @content_type="image/svg+xml", @headers="Content-Disposition: form-data; name=\"logo[file_name]\"; filename=\"some-name.svg\"\r\nContent-Type: image/svg+xml\r\n">}, "commit"=>"Save", "id"=>"18"}
  ...
Completed 200 OK in 92ms (Views: 10.5ms | ActiveRecord: 0.5ms | Allocations: 8188)

我尝试添加[/image \/svg+xml //]> content_type_allowlist并超出了提交按钮,但收到了错误“ nil位置”提供的。

Parameters:

{"_method"=>"patch",
 "authenticity_token"=>"[FILTERED]",
 "logo"=>
  {"file_name"=>
    #<ActionDispatch::Http::UploadedFile:0x00007fb111dae5c0
     @content_type="image/svg+xml",
     @headers="Content-Disposition: form-data; name=\"logo[file_name]\"; filename=\"some-name.svg\"\r\n" + "Content-Type: image/svg+xml\r\n",
     @original_filename="some-name.svg",
     @tempfile=#<File:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20220618-86477-ij7mr4.svg>>},
 "commit"=>"Save",
 "id"=>"18"}

我想我遇到了CVE-2016-3714缓解载体。在 ruby​​docs 使用RSVG代表进行SVG处理。

如何禁用ImageMagick的默认SVG委托?如何使用RSVG委托?

先感谢您。

I am upgrading a Rails app fro 3 to 7. With the new App Carrierwave stopped to accept SVG files for upload. Previously I could upload svg files, without processing and the creation of versions with this setting:

class LogoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  process :set_content_type

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{model.id}"
  end

end

Since version 0.11.0 of Carriwave set_content_type appears to have been deprecated. Which leaves my SVG Uploader with just:

class LogoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{model.id}"
  end

  def content_type_allowlist
    [/image\//]
  end

end

I do not want to convert the svg and the upload is protected and only available to admins. Currently I do not get beyond the submit button.

When trying to upload I now get the following response:

Started PATCH "/admin/logos/18" for ::1 at 2022-06-18 10:01:19 +0200
Processing by Admin::LogosController#update as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "logo"=>{"file_name"=>#<ActionDispatch::Http::UploadedFile:0x00007ff1803932c0 @tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20220618-29575-j47l79.svg>, @original_filename="some-name.svg", @content_type="image/svg+xml", @headers="Content-Disposition: form-data; name=\"logo[file_name]\"; filename=\"some-name.svg\"\r\nContent-Type: image/svg+xml\r\n">}, "commit"=>"Save", "id"=>"18"}
  ...
Completed 200 OK in 92ms (Views: 10.5ms | ActiveRecord: 0.5ms | Allocations: 8188)

I tried to add [/image\/svg+xml//] to the content_type_allowlist and got beyond the submit button, but received the error "Nil location provided. Can't build URI."

Parameters:

{"_method"=>"patch",
 "authenticity_token"=>"[FILTERED]",
 "logo"=>
  {"file_name"=>
    #<ActionDispatch::Http::UploadedFile:0x00007fb111dae5c0
     @content_type="image/svg+xml",
     @headers="Content-Disposition: form-data; name=\"logo[file_name]\"; filename=\"some-name.svg\"\r\n" + "Content-Type: image/svg+xml\r\n",
     @original_filename="some-name.svg",
     @tempfile=#<File:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20220618-86477-ij7mr4.svg>>},
 "commit"=>"Save",
 "id"=>"18"}

I guess I have encountered the Carrierwave CVE-2016-3714 mitigation. In RubyDocs they say to either disable ImageMagick's default SVG delegate or use RSVG delegate for SVG processing.

How can I disable ImageMagick's default SVG delegate? How do I use RSVG delegate?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2025-02-15 16:17:31

我发现,就我的情况而言,它与ImageMagick无关,而是与Rails 5引入的属性的验证

。这意味着,在我的设置(无SVG文件的转换)中,您仍然可以上传SVG。

class Logo < ApplicationRecord

  mount_uploader :file_name, LogoUploader

  belongs_to :institution, optional: true

...

end

我仍然很好奇有人在Ubuntu和MacOS上都必须如何配置ImageMagick。如果您知道,请分享...

I discovered that in my case it was not related to ImageMagick, but to the validation of belongs_to associations introduced with Rails 5.

I had to put optional: true in the logo model and now it is uploading again. This means, that in my setting (no conversion of svg files) you can still upload svgs.

class Logo < ApplicationRecord

  mount_uploader :file_name, LogoUploader

  belongs_to :institution, optional: true

...

end

I am still curious how someone would have to configure ImageMagick both on Ubuntu and MacOS. If you know, please share ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文