LoginSignup
2
2

More than 5 years have passed since last update.

paperclipのURI Obfuscationするときはmysqlのmillisecondsに気をつけよう

Posted at

URIを曖昧化したいので、よくやります。

require

やろうとしていること

avatar画像をユーザーごとに一意性を担保+セキュリティ担保のために曖昧化したい。

問題点

更新時にインスタンス上に持っていた値と、
なんでかDB上のavatar_file_updated_atとインスタンス上に持っていたavatar_file_updated_atが1秒ずれる

  def update_resource(resource, params)
    unless params[:password].blank?
      resource.update_with_password(params)
    else
      resource.update_without_password(params)
      puts "###############################"
      puts resource.avatar.updated_at.to_i
      puts resource.reload.avatar.updated_at.to_i
      puts "###############################"
      binding.pry
      resource
    end
  end

###############################
1427116216 
1427116217 #-> あれぇぇぇ?
###############################

原因

で、そのハッシュを作るには主に、hash_secretとhash_dataを使います。

    def hash_key(style_name = default_style)
      raise ArgumentError, "Unable to generate hash without :hash_secret" unless @options[:hash_secret]
      require 'openssl' unless defined?(OpenSSL)
      data = interpolate(@options[:hash_data], style_name)
      OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
    end

そのhash_dataはこんな感じです。

        :hash_data             => ":class/:attachment/:id/:style/:updated_at",

で、これのupdated_atが含まれているからhash値が変わってしまう。

解決策

generate migrationにあるt.attachment :avatarはよく考えればmicrosecond対応していない。

こいつのせいで丸められてしまっている忘れてた。なので、mysql側をちゃんとmicrosecondに対応させるだけです。

  def up
    change_column :suppliers, :avatar_updated_at, :datetime, limit: 6
  end

postgreに早く変えたい。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2