- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将一个权限敏感的应用程序迁移到 ActiveStorage。我需要确保文件只能由具有权限的人访问,并且这些权限只能持续指定的时间段。
使用 Paperclip,这就像使用专家授权调用定义路由一样简单:
def thumbnail
authorize @record
redirect_to @record.thumbnail.expiring_url(300)
end
任何对 /record/thumbnail
的调用都将由 devise 进行身份验证,由 pundit 授权,然后使用过期 url 重定向到 S3。这是一个有效的过程。
转向 ActiveStorage,我最初的想法是我会使用 service_url
,但它只在我使用 S3 时有效——它在测试中不起作用,它在以下情况下不起作用我正在使用磁盘服务进行本地开发。实际的“正确”方法是使用 url_for
,或者在上面的示例中使用 redirect_to url_for @record.thumbnail
。
问题是,url_for
似乎发布了一个永久 url -- 它生成的 url 对任何人都始终有效,无需身份验证或授权。更重要的是,一旦有人拥有那个 url,我就无法撤销它,我不能说它只能用一周。一旦有人拥有该 url,他们就可以永远访问该文件。 (或者,大概,至少在文件更新之前)。
我不认为这是一个巨大的安全漏洞,但与使用 Paperclip 可实现的功能相比,这绝对是倒退了一步。我是否遗漏了一个重要的细节,或者这真的是 ActiveStorage 所能做到的一切吗?
最佳答案
文档说:
If you need to enforce access protection beyond the security-through-obscurity factor of the signed blob references, you'll need to implement your own authenticated redirection controller.
这不是特别有用。为此,您需要创建自己的 Controller 来为 blob 提供服务:
class BlobsController < ApplicationController
include ActiveStorage::SetBlob
before_action :authorize_blob
def show
expires_in ActiveStorage::Blob.service.url_expires_in
redirect_to @blob.service_url(disposition: params[:disposition])
end
private
def authorize_blob
# Your authorization code goes here
end
end
然后您需要设置路由:
get '/blobs/:signed_id/*filename' => "blobs#show", as: "service_blob"
direct :blob do |blob, options|
route_for(:service_blob, blob.signed_id, blob.filename, options)
end
resolve("ActiveStorage::Blob") { |blob, options| route_for(:blob, blob, options) }
最后,您需要禁用不安全的默认 Controller ,以便知识渊博的用户无法规避您的授权。
如果您使用的是 Rails 6.1+,您可以通过在 application.rb 中将 config.active_storage.draw_routes
设置为 false 来实现。但是您需要确保其余的 ActiveStorage 路由是手动绘制的,并且其中有很多。你可以view them on Github .
否则,您将需要添加扩展 Controller 以重定向到您的新 Controller (或者完全关闭它)。您可以将以下内容添加到初始化程序中:
module ActiveStorageRedirect
def self.included(controller)
controller.before_action :redirect_to_authenticated
end
private
def redirect_to_authenticated
redirect_to Rails.application.routes.url_helpers.blob_path(@blob)
end
end
ActiveStorage::Blobs::RedirectController.include(ActiveStorageRedirect) # For Rails >= 6.1
ActiveStorage::Blobs::ProxyController.include(ActiveStorageRedirect) # For Rails >= 6.1
# ActiveStorage::BlobsController.include(ActiveStorageRedirect) # For Rails < 6.1
关于rails-activestorage - 如何为 ActiveStorage 添加权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369700/
我正在将一个权限敏感的应用程序迁移到 ActiveStorage。我需要确保文件只能由具有权限的人访问,并且这些权限只能持续指定的时间段。 使用 Paperclip,这就像使用专家授权调用定义路由一样
我想知道 ActiveRecord 模型对象上的图像是否已更改,以便我可以执行其他功能,但看不到任何可用的信息。 IE。 object.image.changed? or object.image_c
我计划在我的一个网站中将 Rails 升级到 5.2 并引入 ActiveStorage,现在我使用 Paperclip 和 paperclip_optimizer。不利的一面是,当用 ActiveS
在我们的 Rails 代码中没有任何名为 default 的队列。但似乎 Sidekiq 将 ActiveStorage::PurgeJob 的队列设置为 default。这就是为什么 purge_l
我有一个表单,用户可以在其中选择要上传的文件。一旦使用 ActiveStorage 直接上传选择了文件,就会上传该文件。有一个提交按钮,在上传完成之前不可点击。 该按钮在表单加载时被禁用。但是,如果由
我有一个基本的 ActiveStorage 设置,其中一个型号为 has_many_attached :file_attachments .在其他地方的服务中,我试图生成一个在主应用程序(电子邮件、工
我有一个简单的 Controller 规范,它应该在创建项目时检查新 Material 的处理是否排队。 materials_controller_spec.rb(#create): it 'qu
我使用 url 方法在 erb 文件中呈现我的事件记录附件的 url。 #controller class RecordMetadataController " width="600" heig
我有一个在 Heroku 上运行的 Rails 6 应用程序,用于捕获视频并上传它们。一个典型的用例是用户制作视频然后上传。为此,我成功地使用了 active storage 并直接上传到 Amazo
我需要给图片加水印,cmd ImageMagick 命令 convert image.png watermark.png -composite target.jpg 或 composite -wate
我使用Rails + ActiveStorage 上传图片文件,上传后想将宽高保存在数据库中。但是,我无法在任何地方找到任何此类示例。 这是我从各种 API 文档中拼凑出来的,但最终还是出现了这个错误
每当尝试附加文件时,我的应用程序(本地)都会引发ActiveStorage::IntegrityError错误。我如何摆脱这个错误? 我只有一个has_one_attached,但我不知道该错误是如何
我将ActiveStorage与默认磁盘服务一起使用。我的api返回图像URL,并且图像通过IMG标签嵌入在前端(Vue.js)中。当我打开图片网址时,图片会正确显示。 render json: {
有没有办法使用 ActiveStorage 验证附件?例如,如果我想验证内容类型或文件大小? 类似 Paperclip 的方法会很棒! validates_attachment_content_t
我正在将 Rails 应用程序从回形针迁移到 ActiveStorage,但它不接受 crop变体中的参数 这一行: @user.image.variant(crop: [180,135]) 导致此错
我没有看到任何关于此事的文件,但要详细说明: 我想使用事件存储在我的 rails 应用程序中上传文件,而不必使用浏览器的文件输入元素。无论是使用拖放,还是各种自定义文件选择器,最好告诉 ActiveS
我在 ActiveStorage 中遇到这个问题,我需要处理图像,我的要求是保存处理过的图像并在裁剪和其他转换后将其附加到新模型。 最佳答案 ActiveStorage::Blob#variant 适
我目前正在将一个 Rails 应用程序(驱动 screenshots.debian.net)从 Paperclip 迁移到 ActiveStorage。它允许用户上传 PNG 图像,然后向所有用户显示
目标 我想上传多个文件。因此,一个Intervention 可以有多个Uploads,每个Upload 都有一个附加文件。这样一来,每个Upload 都可以附加一个具有不同状态、名称、可见性等的文件,
使用 Rails 5.2 rc1,是否可以在开发中直接上传到本地主机? 这是我的表单new.html.erb: 这是我在控制台中得到的错误: ActiveSupport::Messag
我是一名优秀的程序员,十分优秀!