简体   繁体   English

Rails,Rackspace云文件,Referrer ACL

[英]Rails, Rackspace Cloud Files, Referrer ACL

I am using Rackspace Cloud Files as File Storage server for my application. 我正在使用Rackspace Cloud Files作为我的应用程序的文件存储服务器。 The files that users upload must be authorized from within my application, then from a controller it would redirect to the correct Rackspace Cloud Files CDN URL. 用户上传的文件必须在我的应用程序中授权,然后从控制器重定向到正确的Rackspace Cloud Files CDN URL。 I am trying to do authorization using Rackspace Cloud Files' Referrer ACL. 我正在尝试使用Rackspace Cloud Files的Referrer ACL进行授权。

So let me just add a very simple snippet to clarify what I am trying to accomplish. 所以,让我添加一个非常简单的片段来阐明我想要完成的事情。

class FilesController < ApplicationController
  def download
    redirect_to(some_url_to_a_file_on_cloud_files_url)
  end
end

The URL the user would access to get to this download action would be the following: 用户将访问以获取此下载操作的URL如下:

http://a-subdomain.domain.com/projects/:project_id/files/:file_id/download

So with the CloudFiles gem I have set up an ACL Referrer regular expression that should work. 因此,使用CloudFiles gem,我已经设置了一个应该有效的ACL Referrer正则表达式。

http\:\/\/.+\.domain\.com\/projects\/\d+\/files\/\d+\/download

When the user clicks on a link in the web UI, it routes them to the above URL and depending on the parameters, it will from the download action redirect the user to the correct Rackspace Cloud Files File URL. 当用户单击Web UI中的链接时,它会将它们路由到上面的URL,并且根据参数,它将从下载操作将用户重定向到正确的Rackspace云文件文件URL。

Well, what I get is an error, saying that I am unauthorized (wrong http referrer). 好吧,我得到的是一个错误,说我是未经授权的(错误的http推荐人)。 I have a hunch that because I am doing a redirect from the download action straight to cloud files, that it doesn't "count" as a HTTP Referrer and, rather than use this URL as a referrer, I think it might be using this URL: 我有预感,因为我正在从下载操作直接重定向到云文件,它不会“计数”为HTTP推荐者,而不是使用此URL作为引用者,我认为它可能正在使用此网址:

http\:\/\/.+\.domain\.com\/projects\/\d+\/files

Since this is the page you are on when you want to click on the "download" link, that directs the user to the download action in the FilesController. 由于这是您要单击“下载”链接时所在的页面,因此会将用户定向到FilesController中的下载操作。

When I set the HTTP Referrer for Rackspace ACL to just this: 当我为机架空间ACL设置HTTP Referrer时:

http\:\/\/.+\.domain\.com\/projects\/\d+\/files

And then click on a link, I am authorized to download. 然后点击链接,我被授权下载。 However, this isn't safe enough since then anyone could for example just firebug into the html and inject a raw link to the file and gain access. 然而,这是不够安全的,因为任何人都可以例如只是闯入html并注入文件的原始链接并获得访问权限。

So I guess my question is, does anyone have any clue how or why, what I am trying to accomplish is not working, and have any suggestions/ideas? 所以我想我的问题是,有没有人知道如何或为什么,我想要完成的是不工作,并有任何建议/想法? As I said I think it might be that when a user clicks the link, that the referrer is being set to the location of which the file is being clicked, not the url where the user is being redirected to the actual file on cloud files. 正如我所说,我认为可能是当用户点击链接时,引用者被设置为单击文件的位置,而不是用户被重定向到云文件上的实际文件的URL。

Is something like this possible? 这样的事情可能吗?

class FilesController < ApplicationController
  def download
    # Dynamically set a HTTP Referrer here before
    # redirecting the user to the actual file on cloud files
    # so the user is authorized to download the file?
    redirect_to(some_url_to_a_file_on_cloud_files_url)
  end
end

Any help, suggestions are much appreciated! 任何帮助,建议非常感谢!

Thanks! 谢谢!

Generally Micahel's comment is more than enough to explain why S3 tops rackspace for this matter, but if you'd really like to add some special HTTP headers to your Rackspace request - do an HTTP request of your own and fetch the file manually: 一般来说,Micahel的评论足以解释为什么S3在这方面会占据机架空间,但是如果您真的想在Rackspace请求中添加一些特殊的HTTP标头 - 请执行您自己的HTTP请求并手动获取文件:

class DownloadsController < ApplicationController
   def download
     send_data HTTParty.get(some_url_to_a_file_on_cloud_files_url, :headers => {"x-special-headers" => "AWESOME" }), :file_name => "myfile.something"
   end
end

Yes, you can code this example better but it's the general idea. 是的,你可以更好地编写这个例子,但这是一般的想法。

Although there is still no 'Referer' check, you can create temp urls (signed urls) with the current version of Rackspace CloudFiles. 虽然仍然没有“Referer”检查,但您可以使用当前版本的Rackspace CloudFiles创建临时URL (已签名的URL)。

The following code is taken from Rackspace documentation site . 以下代码取自Rackspace文档站点

require "openssl"
 unless ARGV.length == 4
 puts "Syntax: <method> <url> <seconds> <key>"
 puts ("Example: GET https://storage101.dfw1.clouddrive.com/v1/" +
 "MossoCloudFS_12345678-9abc-def0-1234-56789abcdef0/" +
 "container/path/to/object.file 60 my_shared_secret_key")
 else
 method, url, seconds, key = ARGV
 method = method.upcase
 base_url, object_path = url.split(/\/v1\//)
 object_path = '/v1/' + object_path
 seconds = seconds.to_i
 expires = (Time.now + seconds).to_i
 hmac_body = "#{method}\n#{expires}\n#{object_path}"
 sig = OpenSSL::HMAC.hexdigest("sha1", key, hmac_body)
 puts ("#{base_url}#{object_path}?" +
 "temp_url_sig=#{sig}&temp_url_expires=#{expires}")
 end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM