Sha256: 82254951c4054835e1ec2d3c8058bf83d5a109add0ad89e125447aa2cad60e5b

Contents?: true

Size: 1.04 KB

Versions: 7

Compression:

Stored size: 1.04 KB

Contents

module Refile
  # Macros which make it easier to write secure backends.
  #
  # @api private
  module BackendMacros
    def verify_id(method)
      mod = Module.new do
        define_method(method) do |id|
          id = self.class.decode_id(id)
          if self.class.valid_id?(id)
            super(id)
          else
            raise Refile::InvalidID
          end
        end
      end
      prepend mod
    end

    def verify_uploadable(method)
      mod = Module.new do
        define_method(method) do |uploadable|
          [:size, :read, :eof?, :rewind, :close].each do |m|
            unless uploadable.respond_to?(m)
              raise Refile::InvalidFile, "does not respond to `#{m}`."
            end
          end
          if max_size and uploadable.size > max_size
            raise Refile::InvalidMaxSize, "#{uploadable.inspect} is too large"
          end
          super(uploadable)
        end
      end
      prepend mod
    end

    def valid_id?(id)
      id =~ /\A[a-z0-9]+\z/i
    end

    def decode_id(id)
      id.to_s
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
leifcr-refile-0.7.3 lib/refile/backend_macros.rb
leifcr-refile-0.7.1 lib/refile/backend_macros.rb
leifcr-refile-0.7.0 lib/refile/backend_macros.rb
leifcr-refile-0.6.3 lib/refile/backend_macros.rb
refile-0.6.2 lib/refile/backend_macros.rb
refile-0.6.1 lib/refile/backend_macros.rb
refile-0.6.0 lib/refile/backend_macros.rb