bulkrax 9.5.1 → 10.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +49 -0
  3. data/app/controllers/bulkrax/application_controller.rb +83 -0
  4. data/app/controllers/bulkrax/entries_controller.rb +16 -13
  5. data/app/controllers/bulkrax/exporters_controller.rb +21 -25
  6. data/app/controllers/bulkrax/guided_imports_controller.rb +4 -8
  7. data/app/controllers/bulkrax/importers_controller.rb +84 -46
  8. data/app/controllers/concerns/bulkrax/datatables_behavior.rb +18 -12
  9. data/app/controllers/concerns/bulkrax/importer_file_handler.rb +55 -9
  10. data/app/errors/bulkrax/unsafe_cloud_source_error.rb +11 -0
  11. data/app/errors/bulkrax/unsafe_path_error.rb +8 -0
  12. data/app/factories/bulkrax/object_factory.rb +2 -2
  13. data/app/factories/bulkrax/valkyrie_object_factory.rb +29 -17
  14. data/app/helpers/bulkrax/validation_helper.rb +1 -1
  15. data/app/jobs/bulkrax/create_relationships_job.rb +1 -1
  16. data/app/jobs/bulkrax/download_cloud_file_job.rb +156 -21
  17. data/app/jobs/bulkrax/import_collection_job.rb +9 -2
  18. data/app/matchers/bulkrax/application_matcher.rb +5 -2
  19. data/app/models/bulkrax/csv_entry.rb +1 -1
  20. data/app/models/bulkrax/current.rb +7 -0
  21. data/app/models/bulkrax/export_scope.rb +21 -0
  22. data/app/models/bulkrax/exporter.rb +2 -2
  23. data/app/models/bulkrax/importer.rb +1 -1
  24. data/app/models/concerns/bulkrax/ability_behavior.rb +139 -0
  25. data/app/models/concerns/bulkrax/has_matchers.rb +1 -1
  26. data/app/models/concerns/bulkrax/import_behavior.rb +3 -3
  27. data/app/parsers/bulkrax/application_parser.rb +11 -11
  28. data/app/parsers/bulkrax/application_parser.rb.orig +545 -0
  29. data/app/parsers/bulkrax/bagit_parser.rb +10 -3
  30. data/app/parsers/bulkrax/csv_parser.rb +59 -27
  31. data/app/parsers/bulkrax/oai_dc_parser.rb +1 -1
  32. data/app/parsers/bulkrax/parser_export_record_set.rb +26 -8
  33. data/app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb +1 -1
  34. data/app/parsers/concerns/bulkrax/csv_parser/csv_validation.rb +1 -2
  35. data/app/parsers/concerns/bulkrax/csv_parser/csv_validation_helpers.rb +18 -1
  36. data/app/parsers/concerns/bulkrax/csv_parser/csv_validation_hierarchy.rb +2 -2
  37. data/app/search_builders/bulkrax/export_search_builder.rb +9 -0
  38. data/app/services/bulkrax/cloud_source_validator.rb +123 -0
  39. data/app/services/bulkrax/csv_template/column_builder.rb +2 -2
  40. data/app/services/bulkrax/csv_template/mapping_manager.rb +9 -7
  41. data/app/services/bulkrax/csv_template/schema_analyzer.rb +1 -5
  42. data/app/services/bulkrax/safe_path.rb +129 -0
  43. data/config/locales/bulkrax.en.yml +2 -0
  44. data/lib/bulkrax/engine.rb +1 -1
  45. data/lib/bulkrax/version.rb +1 -1
  46. data/lib/bulkrax.rb +77 -10
  47. data/lib/generators/bulkrax/install_generator.rb +22 -10
  48. data/lib/generators/bulkrax/templates/config/initializers/bulkrax.rb +1 -1
  49. metadata +58 -14
  50. data/app/factories/bulkrax/valkyrize-hyku.code-workspace +0 -19
  51. data/lib/wings/custom_queries/find_by_source_identifier.rb +0 -41
@@ -1,31 +1,166 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Bulkrax
3
4
  class DownloadCloudFileJob < ApplicationJob
4
5
  queue_as Bulkrax.config.ingest_queue_name
5
6
 
6
- include ActionView::Helpers::NumberHelper
7
-
8
- # Retrieve cloud file and write to the imports directory
9
- # Note: if using the file system, the mounted directory in
7
+ # Retrieve a cloud file and write it to the importer's workspace.
8
+ #
9
+ # `importer_id:` and `relative_filename:` are required keywords: the
10
+ # destination is always recomputed here, confined to this importer's
11
+ # own workspace, rather than trusted from a serialized job argument.
12
+ # `target_file` is a deprecated positional placeholder, kept only so
13
+ # that a pre-upgrade call shape (`file`, `target_file`) still binds
14
+ # without Ruby raising its own ArgumentError — it is never read, even
15
+ # when supplied alongside otherwise-valid keywords, so it can't be
16
+ # used to redirect the destination. A call made this way is missing
17
+ # the required keywords, so it raises below rather than running.
18
+ #
19
+ # Note: if using the file system provider, the mounted directory in
10
20
  # browse_everything MUST be shared by web and worker servers
11
- def perform(file, target_file)
12
- retriever = BrowseEverything::Retriever.new
13
- last_logged_time = Time.zone.now
14
- log_interval = 3.seconds
15
-
16
- retriever.download(file, target_file) do |filename, retrieved, total|
17
- percentage = (retrieved.to_f / total.to_f) * 100
18
- current_time = Time.zone.now
19
-
20
- if (current_time - last_logged_time) >= log_interval
21
- # Use number_to_human_size for formatting
22
- readable_retrieved = number_to_human_size(retrieved)
23
- readable_total = number_to_human_size(total)
24
- Rails.logger.info "Downloaded #{readable_retrieved} of #{readable_total}, #{filename}: #{percentage.round}% complete"
25
- last_logged_time = current_time
26
- end
21
+ def perform(file, target_file = nil, importer_id: nil, relative_filename: nil)
22
+ raise Bulkrax::UnsafePathError, I18n.t('bulkrax.importer.browse_everything.errors.missing_destination_context') if importer_id.blank? || relative_filename.blank?
23
+
24
+ # `file` and `relative_filename` are independent arguments and may
25
+ # not agree (a tampered or replayed job call could supply either
26
+ # one separately) — check the raw file_name directly rather than
27
+ # assuming it was already validated by whatever enqueued this job.
28
+ Bulkrax::SafePath.reject_unsafe_entry!(file['file_name'])
29
+
30
+ importer = Bulkrax::Importer.find(importer_id)
31
+ target_file = Bulkrax::SafePath.confine!(importer.parser.path_for_import, relative_filename, require_file: true)
32
+
33
+ # Re-validate the source here too: this job may run on a different
34
+ # process/host than the one that enqueued it, and could be invoked
35
+ # directly with a tampered or stale `file` argument.
36
+ Bulkrax::CloudSourceValidator.validate!(file)
37
+
38
+ download(file, target_file)
39
+ Rails.logger.info "Bulkrax: cloud file download complete for #{file['file_name']}"
40
+ end
41
+
42
+ private
43
+
44
+ # Downloads through a temp file in the *same* directory as
45
+ # `target_file` (so the final move is same-filesystem and atomic),
46
+ # named with a random suffix rather than anything derived from
47
+ # caller-controlled input, and removed on any failure. This means a
48
+ # failed or non-success download can never truncate or corrupt an
49
+ # already-good destination file from a previous successful import.
50
+ def download(file, target_file)
51
+ temp_file = "#{target_file}.#{SecureRandom.hex(8)}.part"
52
+ uri = Addressable::URI.parse(file['url'].to_s)
53
+ if uri.scheme == 'file'
54
+ download_file_source(uri, temp_file)
55
+ else
56
+ download_http_source(file, URI.parse(file['url'].to_s), temp_file)
57
+ end
58
+ FileUtils.mv(temp_file, target_file)
59
+ ensure
60
+ FileUtils.rm_f(temp_file) if temp_file && File.exist?(temp_file)
61
+ end
62
+
63
+ # The source was already confined to the configured filesystem
64
+ # provider's root by {Bulkrax::CloudSourceValidator}.
65
+ def download_file_source(uri, temp_file)
66
+ source_path = URI::DEFAULT_PARSER.unescape(uri.path.to_s)
67
+ IO.copy_stream(source_path, temp_file)
68
+ end
69
+
70
+ # Resolved, IP-pinned, redirect-free, and streamed straight to disk in
71
+ # bounded-size chunks — rather than handing the URL back to a transport
72
+ # (browse-everything's Retriever) that re-resolves the hostname with no
73
+ # SSRF protection at all. This is a *separate* connection from the one
74
+ # {Bulkrax::CloudSourceValidator}'s HEAD check made; each independently
75
+ # applies the same validation at its own connection time, so there is
76
+ # no single "pinned connection" shared between validation and download.
77
+ #
78
+ # ssrf_filter 1.0.8's public `get`/`head` API has no response-streaming
79
+ # hook: `fetch_once` calls `Net::HTTP#request` with no block, which
80
+ # reads the entire response body into memory before returning; the
81
+ # block `get`/`head` accept is for customizing the *outgoing* request,
82
+ # not consuming the response. So we can't use `SsrfFilter.get` here
83
+ # without buffering the whole remote file in memory first.
84
+ #
85
+ # Reuse ssrf_filter's resolver and IP policy, then pin the validated
86
+ # address with Net::HTTP#ipaddr=. Keeping the original hostname on
87
+ # the HTTP connection preserves native TLS SNI and certificate
88
+ # verification without relying on ssrf_filter's TLS monkeypatches.
89
+ def download_http_source(file, uri, temp_file)
90
+ headers = Bulkrax::CloudSourceValidator.request_headers(file)
91
+ stream_download(uri, headers) { |response| handle_response(file, response, temp_file) }
92
+ rescue SsrfFilter::Error
93
+ # The validator's own HEAD check already ran, but DNS can change
94
+ # between that check and this GET — same rationale as
95
+ # Bulkrax::CloudSourceValidator#validate_http_scheme!, and the same
96
+ # static, class-agnostic message for the same reason.
97
+ raise Bulkrax::UnsafeCloudSourceError, 'blocked unsafe remote source'
98
+ end
99
+
100
+ def handle_response(file, response, temp_file)
101
+ unless response.is_a?(Net::HTTPSuccess)
102
+ # Static on purpose — this becomes the importer's status message,
103
+ # not just an ops log; see Bulkrax::CloudSourceValidator for why
104
+ # status codes/response details aren't echoed into it. The status
105
+ # code itself is logged (not attacker-controlled, not a credential).
106
+ Rails.logger.error("Bulkrax: cloud file download failed for #{file['file_name']} with status #{response.code}")
107
+ raise Bulkrax::UnsafeCloudSourceError, 'download failed: non-success response'
108
+ end
109
+
110
+ File.open(temp_file, 'wb') { |io| response.read_body { |chunk| io.write(chunk) } }
111
+ end
112
+
113
+ # Resolves `uri`'s host, rejects it unless it has at least one public
114
+ # IP (reusing ssrf_filter's own resolver/blacklist — see above), and
115
+ # connects directly to that IP: with the environment proxy explicitly
116
+ # disabled (`Net::HTTP.new`'s `p_addr` defaults to `:ENV`, which would
117
+ # otherwise route through `http_proxy`/`https_proxy` and hand the
118
+ # pinned connection to a third party), and with the real hostname sent
119
+ # as the `Host` header and used for TLS SNI/certificate verification
120
+ # instead of the IP. Yields the live `Net::HTTPResponse` to the block
121
+ # *before* its body is read, so the caller can stream it.
122
+ def stream_download(uri, headers)
123
+ raise SsrfFilter::InvalidUriScheme, "URI scheme '#{uri.scheme}' not in whitelist" unless
124
+ SsrfFilter::DEFAULT_SCHEME_WHITELIST.include?(uri.scheme)
125
+
126
+ hostname = uri.hostname
127
+ ip_addresses = SsrfFilter::DEFAULT_RESOLVER.call(hostname)
128
+ raise SsrfFilter::UnresolvedHostname, "Could not resolve hostname '#{hostname}'" if ip_addresses.empty?
129
+
130
+ public_addresses = ip_addresses.reject { |ip| unsafe_ip_address?(ip) }
131
+ raise SsrfFilter::PrivateIPAddress, "Hostname '#{hostname}' has no public ip addresses" if public_addresses.empty?
132
+
133
+ request = Net::HTTP::Get.new(uri)
134
+ request['host'] = host_header(hostname, uri)
135
+ headers.each { |key, value| request[key] = value }
136
+
137
+ http = Net::HTTP.new(hostname, uri.port, nil)
138
+ http.ipaddr = public_addresses.sample.to_s
139
+ http.use_ssl = (uri.scheme == 'https')
140
+ http.start { |connection| connection.request(request) { |response| yield response } }
141
+ end
142
+
143
+ # Mirrors `SsrfFilter.unsafe_ip_address?`, but against its public
144
+ # `IPV4_BLACKLIST`/`IPV6_BLACKLIST` constants directly rather than
145
+ # calling that private method — we only ever check single resolved
146
+ # addresses here (never a CIDR range), so the masked-range guard it
147
+ # also carries doesn't apply.
148
+ def unsafe_ip_address?(ip_address)
149
+ return SsrfFilter::IPV4_BLACKLIST.any? { |range| range.include?(ip_address) } if ip_address.ipv4?
150
+ return SsrfFilter::IPV6_BLACKLIST.any? { |range| range.include?(ip_address) } if ip_address.ipv6?
151
+
152
+ true
153
+ end
154
+
155
+ # Matches `SsrfFilter.host_header`: the `Host` header should carry the
156
+ # real hostname (not the pinned IP we actually connect to), with the
157
+ # port suffixed only when it isn't the scheme's default.
158
+ def host_header(hostname, uri)
159
+ if (uri.port == 80 && uri.scheme == 'http') || (uri.port == 443 && uri.scheme == 'https')
160
+ hostname
161
+ else
162
+ "#{hostname}:#{uri.port}"
27
163
  end
28
- Rails.logger.info "Download complete: #{file['url']} to #{target_file}"
29
164
  end
30
165
  end
31
166
  end
@@ -10,8 +10,15 @@ module Bulkrax
10
10
  begin
11
11
  entry.build
12
12
  entry.save!
13
- ImporterRun.increment_counter(:processed_records, args[1])
14
- ImporterRun.increment_counter(:processed_collections, args[1])
13
+ # An entry that fails inside #build records the error on itself rather
14
+ # than raising, so its status is what says whether this run succeeded.
15
+ if entry.succeeded?
16
+ ImporterRun.increment_counter(:processed_records, args[1])
17
+ ImporterRun.increment_counter(:processed_collections, args[1])
18
+ else
19
+ ImporterRun.increment_counter(:failed_records, args[1])
20
+ ImporterRun.increment_counter(:failed_collections, args[1])
21
+ end
15
22
  ImporterRun.decrement_counter(:enqueued_records, args[1]) unless ImporterRun.find(args[1]).enqueued_records <= 0 # rubocop:disable Style/IdenticalConditionalBranches
16
23
  rescue => e
17
24
  ImporterRun.increment_counter(:failed_records, args[1])
@@ -67,10 +67,13 @@ module Bulkrax
67
67
  end
68
68
 
69
69
  def parse_subject(src)
70
- string = src.strip.downcase
70
+ string = src.strip
71
71
  return if string.blank?
72
72
 
73
- string.slice(0, 1).capitalize + string.slice(1..-1)
73
+ return string.sub(%r{\Ahttps?}i, &:downcase) if string.match?(%r{\Ahttps?://}i)
74
+
75
+ downcased = string.downcase
76
+ downcased.slice(0, 1).capitalize + downcased.slice(1..-1)
74
77
  end
75
78
 
76
79
  def parse_types(src)
@@ -141,7 +141,7 @@ module Bulkrax
141
141
 
142
142
  def add_metadata_for_model
143
143
  if factory_class.present? && factory_class == Bulkrax.collection_model_class
144
- add_collection_type_gid if defined?(::Hyrax)
144
+ add_collection_type_gid if Bulkrax.hyrax_loaded?
145
145
  # add any additional collection metadata methods here
146
146
  elsif factory_class == Bulkrax.file_model_class
147
147
  validate_presence_of_filename!
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bulkrax
4
+ class Current < ActiveSupport::CurrentAttributes
5
+ attribute :schema_cache
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bulkrax
4
+ # A minimal scope adapter that satisfies the Blacklight::SearchBuilder and
5
+ # Hydra::AccessControlsEnforcement interface for export permission filtering.
6
+ #
7
+ # Blacklight::SearchBuilder expects the scope to respond to +blacklight_config+
8
+ # and +current_ability+. In the Hyrax catalog this is the controller; for
9
+ # Bulkrax exports a lightweight PORO is sufficient.
10
+ class ExportScope
11
+ attr_reader :current_ability
12
+
13
+ def initialize(ability)
14
+ @current_ability = ability
15
+ end
16
+
17
+ def blacklight_config
18
+ @blacklight_config ||= Blacklight::Configuration.new
19
+ end
20
+ end
21
+ end
@@ -69,7 +69,7 @@ module Bulkrax
69
69
  end
70
70
 
71
71
  def workflow_status_list
72
- Sipity::WorkflowState.all.map { |s| [s.name&.titleize, s.name] }.uniq if defined?(::Hyrax)
72
+ Sipity::WorkflowState.all.map { |s| [s.name&.titleize, s.name] }.uniq if Bulkrax.hyrax_loaded?
73
73
  end
74
74
 
75
75
  # If field_mapping is empty, setup a default based on the export_properties
@@ -84,7 +84,7 @@ module Bulkrax
84
84
  end
85
85
 
86
86
  def export_from_list
87
- if defined?(::Hyrax)
87
+ if Bulkrax.hyrax_loaded?
88
88
  [
89
89
  [I18n.t('bulkrax.exporter.labels.importer'), 'importer'],
90
90
  [I18n.t('bulkrax.exporter.labels.collection'), 'collection'],
@@ -18,7 +18,7 @@ module Bulkrax
18
18
  has_many :entries, as: :importerexporter, dependent: :destroy
19
19
 
20
20
  validates :name, presence: true
21
- validates :admin_set_id, presence: true if defined?(::Hyrax)
21
+ validates :admin_set_id, presence: true if Bulkrax.hyrax_loaded?
22
22
  validates :parser_klass, presence: true
23
23
 
24
24
  delegate :create_parent_child_relationships, :valid_import?, :write_errored_entries_file, :visibility, to: :parser
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bulkrax
4
+ # Concern providing Bulkrax-specific ability methods.
5
+ #
6
+ # Include this in your application's Ability class to gain default
7
+ # implementations of all Bulkrax authorization methods. Override any
8
+ # method to customize authorization behavior for your application.
9
+ #
10
+ # ## Wiring CanCan rules
11
+ #
12
+ # Call +bulkrax_default_abilities+ from your Ability's +initialize+ (or add
13
+ # it to +self.ability_logic+) to register CanCan +can+ rules for all
14
+ # Bulkrax resources:
15
+ #
16
+ # class Ability
17
+ # include Hydra::Ability
18
+ # include Bulkrax::AbilityBehavior
19
+ # self.ability_logic += [:bulkrax_default_abilities]
20
+ # end
21
+ #
22
+ # The method uses the four predicate hooks below as guards, so you can
23
+ # control who gets which rules simply by overriding those predicates.
24
+ #
25
+ # Example:
26
+ #
27
+ # class Ability
28
+ # include Hydra::Ability
29
+ # include Bulkrax::AbilityBehavior
30
+ # self.ability_logic += [:bulkrax_default_abilities]
31
+ #
32
+ # # Grant import/export access to users who can create works
33
+ # def can_import_works?
34
+ # can_create_any_work?
35
+ # end
36
+ #
37
+ # def can_export_works?
38
+ # can_create_any_work?
39
+ # end
40
+ #
41
+ # # Grant admin-level importer access to site admins
42
+ # def can_admin_importers?
43
+ # current_user.admin?
44
+ # end
45
+ # end
46
+ module AbilityBehavior
47
+ extend ActiveSupport::Concern
48
+
49
+ # Returns true if the current user may use importer functionality at all.
50
+ # Override in your Ability class to customize.
51
+ def can_import_works?
52
+ false
53
+ end
54
+
55
+ # Returns true if the current user may use exporter functionality at all.
56
+ # Override in your Ability class to customize.
57
+ def can_export_works?
58
+ false
59
+ end
60
+
61
+ # Returns true if the current user may administer ALL importers —
62
+ # i.e., view, edit, update, or destroy any importer regardless of ownership.
63
+ # Defaults to false; override to grant admin access.
64
+ def can_admin_importers?
65
+ false
66
+ end
67
+
68
+ # Returns true if the current user may administer ALL exporters —
69
+ # i.e., view, edit, update, or destroy any exporter regardless of ownership.
70
+ # Defaults to false; override to grant admin access.
71
+ def can_admin_exporters?
72
+ false
73
+ end
74
+
75
+ # Registers CanCan +can+ rules for all Bulkrax resources.
76
+ #
77
+ # Call this from your Ability's +initialize+ or add it to
78
+ # +self.ability_logic+. The predicate hooks above control which rules are
79
+ # granted; override them to tailor access.
80
+ #
81
+ # Rules for Importer and Exporter use hash-form conditions (SQL-generatable)
82
+ # so they work with +Model.accessible_by(current_ability)+. Rules for Entry
83
+ # use block-form conditions (checked in Ruby) because authorization must
84
+ # traverse the parent association — do not call +Entry.accessible_by()+;
85
+ # scope entries through the parent instead (e.g. +@importer.entries+).
86
+ def bulkrax_default_abilities
87
+ # Skip all rules when there is no authenticated user; avoids nil-id bugs
88
+ # and prevents block rules from matching NULL user_id records.
89
+ return unless current_user&.id
90
+
91
+ # Map Bulkrax-specific non-CRUD actions to their closest CanCan equivalents
92
+ # so that load_and_authorize_resource can authorize them without requiring
93
+ # a dedicated `can` declaration for each custom action name.
94
+ alias_action :entry_table, :importer_table, :exporter_table,
95
+ :original_file, :export_errors, :upload_corrected_entries, to: :read
96
+ alias_action :continue, :upload_corrected_entries_file, to: :update
97
+
98
+ grant_importer_abilities
99
+ grant_exporter_abilities
100
+ end
101
+
102
+ def grant_importer_abilities
103
+ if can_admin_importers?
104
+ # Prefer a single unconditional rule for admins. Older CanCanCan/
105
+ # ActiveRecord combinations used by the Hyrax 4 appraisal can fail when
106
+ # accessible_by has to merge unconditional and hash-conditioned rules.
107
+ can :manage, Bulkrax::Importer
108
+ can [:read, :update, :destroy], Bulkrax::Entry do |entry|
109
+ entry.importerexporter.is_a?(Bulkrax::Importer)
110
+ end
111
+ elsif can_import_works?
112
+ can :create, Bulkrax::Importer
113
+ can [:read, :update, :destroy], Bulkrax::Importer, user_id: current_user.id
114
+ can [:read, :update, :destroy], Bulkrax::Entry do |entry|
115
+ entry.importerexporter.is_a?(Bulkrax::Importer) &&
116
+ entry.importerexporter.user_id == current_user.id
117
+ end
118
+ end
119
+ end
120
+
121
+ def grant_exporter_abilities
122
+ if can_admin_exporters?
123
+ # Prefer a single unconditional rule for admins. See importer rule
124
+ # above for the Hyrax 4 accessible_by compatibility note.
125
+ can :manage, Bulkrax::Exporter
126
+ can [:read, :update, :destroy], Bulkrax::Entry do |entry|
127
+ entry.importerexporter.is_a?(Bulkrax::Exporter)
128
+ end
129
+ elsif can_export_works?
130
+ can :create, Bulkrax::Exporter
131
+ can [:read, :update, :destroy], Bulkrax::Exporter, user_id: current_user.id
132
+ can [:read, :update, :destroy], Bulkrax::Entry do |entry|
133
+ entry.importerexporter.is_a?(Bulkrax::Exporter) &&
134
+ entry.importerexporter.user_id == current_user.id
135
+ end
136
+ end
137
+ end
138
+ end
139
+ end
@@ -230,7 +230,7 @@ module Bulkrax
230
230
  # @return [Array] hyrax fields
231
231
  def field_to(field)
232
232
  fields = mapping&.map do |key, value|
233
- return unless value
233
+ next unless value
234
234
 
235
235
  if value['from'].instance_of?(Array)
236
236
  key if value['from'].include?(field) || key == field
@@ -34,7 +34,7 @@ module Bulkrax
34
34
  # TODO: This guard clause is not necessary as we can handle it in the
35
35
  # underlying factory. However, to do that requires adjusting about 7
36
36
  # failing specs. So for now this refactor appears acceptable
37
- return unless defined?(::Hyrax)
37
+ return unless Bulkrax.hyrax_loaded?
38
38
  return unless self.class.to_s.include?("Collection")
39
39
  factory.add_user_to_collection_permissions(collection: @item, user: user)
40
40
  end
@@ -87,7 +87,7 @@ module Bulkrax
87
87
  end
88
88
 
89
89
  def add_admin_set_id
90
- return unless defined?(::Hyrax)
90
+ return unless Bulkrax.hyrax_loaded?
91
91
 
92
92
  self.parsed_metadata['admin_set_id'] = importerexporter.admin_set_id if self.parsed_metadata['admin_set_id'].blank?
93
93
  end
@@ -161,7 +161,7 @@ module Bulkrax
161
161
  # @param field [String] name of the controlled property
162
162
  # @return [Boolean] provided value is a present, active authority ID for the provided field
163
163
  def active_id_for_authority?(value, field)
164
- return false unless defined?(::Hyrax)
164
+ return false unless Bulkrax.hyrax_loaded?
165
165
  field_service = ('Hyrax::' + "#{field}_service".camelcase).constantize
166
166
  active_authority_ids = field_service.new.active_elements.map { |ae| ae['id'] }
167
167
 
@@ -472,27 +472,27 @@ module Bulkrax
472
472
  # Zip Slip preflight — reject entries whose names are obviously unsafe
473
473
  # (absolute paths, `..` segments) before we touch the filesystem.
474
474
  # {#safe_extract_path} is the final line of defense; this check just
475
- # fails fast with a clear message.
475
+ # fails fast with a clear message. Delegates to {Bulkrax::SafePath} so
476
+ # cloud-file destinations and sources share the same rules.
476
477
  #
477
478
  # @raise [Bulkrax::UnzipError] if the entry name is unsafe
478
- def reject_unsafe_entry!(name)
479
- return unless name.start_with?('/') || name.split('/').include?('..')
480
- raise Bulkrax::UnzipError, I18n.t('bulkrax.importer.unzip.errors.unsafe_entry', name: name)
479
+ def self.reject_unsafe_entry!(name)
480
+ Bulkrax::SafePath.reject_unsafe_entry!(name)
481
+ rescue Bulkrax::UnsafePathError
482
+ raise Bulkrax::UnzipError, I18n.t('bulkrax.importer.unzip.errors.unsafe_entry', name: name.dup.force_encoding(Encoding::UTF_8).scrub)
481
483
  end
482
484
 
483
485
  # Zip Slip chokepoint. Resolves `relative_dest` against `dest_dir` and
484
486
  # returns the absolute destination path — but only if it stays inside
485
- # `dest_dir`. Callers must use this value rather than building their
486
- # own path with `File.join`, so the path returned is always safe by
487
- # construction.
487
+ # `dest_dir`, including through any symlink. Callers must use this
488
+ # value rather than building their own path with `File.join`, so the
489
+ # path returned is always safe by construction.
488
490
  #
489
491
  # @return [String] absolute destination path, validated to be inside `dest_dir`
490
492
  # @raise [Bulkrax::UnzipError] if `relative_dest` escapes `dest_dir`
491
493
  def safe_extract_path(dest_dir, relative_dest)
492
- expanded_dest_dir = File.expand_path(dest_dir)
493
- dest_path = File.expand_path(relative_dest.to_s, expanded_dest_dir)
494
- return dest_path if dest_path == expanded_dest_dir
495
- return dest_path if dest_path.start_with?("#{expanded_dest_dir}#{File::SEPARATOR}")
494
+ Bulkrax::SafePath.confine!(dest_dir, relative_dest)
495
+ rescue Bulkrax::UnsafePathError
496
496
  raise Bulkrax::UnzipError, I18n.t('bulkrax.importer.unzip.errors.unsafe_entry', name: relative_dest)
497
497
  end
498
498