2010-10-26 42 views
3

我正在使用Rackspace Cloud文件作爲我的應用程序的文件存儲服務器。用戶上傳的文件必須在我的應用程序中授權,然後從控制器重新導向到正確的Rackspace Cloud Files CDN URL。我正在嘗試使用Rackspace雲文件的Referrer ACL進行授權。Rails,Rackspace Cloud文件,推薦人ACL

因此,讓我只是添加一個非常簡單的片段,以澄清我想要完成的事情。

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

用戶將訪問的網址去下載這個行動是以下幾點:

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

所以與CloudFiles寶石,我設置了一個ACL引薦正則表達式應該工作。

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

當用戶單擊Web UI中的鏈接,其路由他們到上述網址,並根據這些參數,它將從下載操作將用戶重定向到正確的Rackspace的雲文件的文件URL。

那麼,我得到的是一個錯誤,說我是未經授權的(錯誤的http referrer)。我有一個預感,因爲我正在做一個從下載操作直接到雲文件的重定向,它不會「計數」爲HTTP Referrer,而不是將此URL用作引薦來源,我認爲它可能會使用這個網址:

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

因爲這是你的頁面是當你要點擊「下載」鏈接,引導用戶在FilesController下載行動。

當我設置HTTP推薦爲Rackspace公司ACL,只是這樣的:

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

然後點擊一個鏈接,我被授權下載。然而,這是不夠安全的,因爲任何人都可以例如只是firebug到HTML並注入原始鏈接到文件並獲得訪問權限。

所以我想我的問題是,有沒有人有任何線索如何或爲什麼,我試圖完成的是不工作,並有任何建議/想法?正如我所說,我認爲可能是當用戶點擊鏈接時,引用者被設置爲文件被點擊的位置,而不是用戶被重定向到雲文件實際文件的網址。

是這樣的可能嗎?

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 

任何幫助,建議非常感謝!

謝謝!

+1

由於幾個原因,我實際上使用Amazon S3而不是Rackspace Cloud Files。但是與上述主題相關的是,使用Paperclip gem在Ruby on Rails中添加對Amazon S3的授權非常簡單。基本上你所擁有的是一個私人存儲桶,你使用Paperclips__expiring_url__方法爲每次下載生成一個唯一的密鑰,並在幾秒鐘內過期。上述問題的一個非常簡單,快速和安全的解決方案。授權可以在應用程序層完成。 – 2010-10-31 10:51:42

回答

0

一般來說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 

是的,你可以更好地編寫這個例子,但它是一般的想法。

0

雖然仍然沒有「Referer」檢查,但您可以使用Rackspace CloudFiles的當前版本創建temp urls(已簽名的URL)。

以下代碼摘自Rackspace documentation site

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 
相關問題