Sha256: 99d8687233642eda000f5376d15bd40a8ef9b40d36dd4811d196bd9bfdc2e30f

Contents?: true

Size: 1.6 KB

Versions: 53

Compression:

Stored size: 1.6 KB

Contents

require 'ndr_support/safe_file'

module NdrImport
  module Helpers
    module File
      # This mixin adds Zip functionality to unified importers.
      module Zip
        private

        # Unzip the file, creating the destination directory if necessary.
        # A pattern can be provided to only extract required files.
        def unzip_file(source, destination, pattern = //)
          # SECURE TVB Mon Aug 13 14:41:05 BST 2012 : SafePath will raise exception if insecure
          # path is constructed
          # SafeFile.safepath_to_string will make sure that the arguments are from type SafePath

          # SECURE: BNS 2010-09-21 (for external access)
          fail 'Not allowed in external environment' if defined?(::Rails) && ::Rails.env.external?

          require 'zip'
          # TODO: Abort if destination directory already exists...
          FileUtils.mkdir_p(SafeFile.safepath_to_string(destination))

          ::Zip::File.open(SafeFile.safepath_to_string(source)) do |zipfile|
            zipfile.entries.each do |entry|
              # SECURE: TPG 2010-11-1: The path is stripped from the zipfile entry when extracted
              basename = ::File.basename(entry.name)
              zipfile.extract(entry, destination.join(basename)) if entry.file? && basename.match(pattern)
            end
          end

        rescue ::Zip::ZipDestinationFileExistsError
          # I'm going to ignore this and just overwrite the files.
        rescue SecurityError => ex
          raise ex
        rescue ArgumentError => ex
          raise ex
        rescue => ex
          puts ex
        end
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
ndr_import-8.5.1 lib/ndr_import/helpers/file/zip.rb
ndr_import-8.5.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-8.4.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-8.3.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-8.2.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-8.1.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-8.0.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-7.0.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-6.4.1 lib/ndr_import/helpers/file/zip.rb
ndr_import-6.4.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-6.3.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-6.2.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-6.1.1 lib/ndr_import/helpers/file/zip.rb
ndr_import-6.1.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-6.0.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-5.1.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-5.0.0 lib/ndr_import/helpers/file/zip.rb
ndr_import-4.1.4 lib/ndr_import/helpers/file/zip.rb
ndr_import-4.1.3 lib/ndr_import/helpers/file/zip.rb
ndr_import-4.1.2 lib/ndr_import/helpers/file/zip.rb