如何在数据传输完成之前拒绝文件上传?

发布于 2024-10-19 17:28:36 字数 228 浏览 5 评论 0原文

我正在构建一个具有文件上传功能的 Rails 应用程序,并且只允许经过身份验证的用户上传文件。文件上传表单位于会员专用区域,但由于很容易获取上传 URL 并尝试从脚本发布数据,因此我担心未经身份验证的文件上传提交。当然,上传会被拒绝,但只有在数据传输完成并且宝贵的带宽被浪费之后。在将整个表单提交到我的服务器之前,如何拒绝未经身份验证的文件上传?由于我无法使用脚本或 CURL 控制用户,这将需要某种服务器端解决方案。我很感激任何反馈。谢谢。

I'm building a Rails application with file upload functionality and only authenticated users are allowed to upload files. The file upload form is in the members only area but since it is easy enough to grab the upload URL and attempt to post data from a script I am concerned about unauthenticated file upload submissions. Sure the upload would get rejected but only after the data transfer has completed and valuable bandwidth got wasted. How can I reject unauthenticated file uploads before the entire form has been submitted to my server? Since I can't control users using scripts or CURL this would require some sort of server side solution. I appreciate any feedback. Thanks.

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

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

发布评论

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

评论(1

×纯※雪 2024-10-26 17:28:36

这是一个简单的伪代码,说明您可以做什么。 before_filter 在执行操作之前执行(upload_form_action 是上传表单页面,upload_action 是接收文件 allowed_to_upload 的页面,

应该是返回的帮助程序/函数/等true/false,如果为 false,则会呈现 403 未经授权,否则只会跳过。

before_filter :check_upload, :only => [:upload_form_action, :upload_action]

protected
  def check_upload
    render :nothing, :status => 403 and return unless allowed_to_upload
  end

This is a simple pseudo-code on what you could do.. The before_filter is executed before the action is executed (upload_form_action being the upload form page, and upload_action being the page that recieves the file

allowed_to_upload should be helper/function/etc that returns true/false, if it's false, it will render 403 unauthorized, otherwise it will just skimp on.

before_filter :check_upload, :only => [:upload_form_action, :upload_action]

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