bulkrax 9.5.2 → 10.0.0

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 (50) 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 +43 -87
  8. data/app/controllers/concerns/bulkrax/datatables_behavior.rb +18 -12
  9. data/app/controllers/concerns/bulkrax/importer_file_handler.rb +9 -55
  10. data/app/factories/bulkrax/object_factory.rb +2 -2
  11. data/app/factories/bulkrax/valkyrie_object_factory.rb +29 -17
  12. data/app/helpers/bulkrax/validation_helper.rb +1 -1
  13. data/app/jobs/bulkrax/create_relationships_job.rb +1 -1
  14. data/app/jobs/bulkrax/download_cloud_file_job.rb +21 -156
  15. data/app/jobs/bulkrax/import_collection_job.rb +9 -2
  16. data/app/matchers/bulkrax/application_matcher.rb +5 -2
  17. data/app/models/bulkrax/csv_entry.rb +1 -1
  18. data/app/models/bulkrax/current.rb +7 -0
  19. data/app/models/bulkrax/export_scope.rb +21 -0
  20. data/app/models/bulkrax/exporter.rb +2 -2
  21. data/app/models/bulkrax/importer.rb +1 -1
  22. data/app/models/concerns/bulkrax/ability_behavior.rb +139 -0
  23. data/app/models/concerns/bulkrax/has_matchers.rb +1 -1
  24. data/app/models/concerns/bulkrax/import_behavior.rb +3 -3
  25. data/app/parsers/bulkrax/application_parser.rb +10 -10
  26. data/app/parsers/bulkrax/bagit_parser.rb +3 -10
  27. data/app/parsers/bulkrax/csv_parser.rb +28 -58
  28. data/app/parsers/bulkrax/oai_dc_parser.rb +1 -1
  29. data/app/parsers/bulkrax/parser_export_record_set.rb +26 -8
  30. data/app/parsers/concerns/bulkrax/csv_parser/csv_template_generation.rb +1 -1
  31. data/app/parsers/concerns/bulkrax/csv_parser/csv_validation.rb +1 -2
  32. data/app/parsers/concerns/bulkrax/csv_parser/csv_validation_helpers.rb +18 -1
  33. data/app/parsers/concerns/bulkrax/csv_parser/csv_validation_hierarchy.rb +2 -2
  34. data/app/search_builders/bulkrax/export_search_builder.rb +9 -0
  35. data/app/services/bulkrax/csv_template/column_builder.rb +2 -2
  36. data/app/services/bulkrax/csv_template/mapping_manager.rb +9 -7
  37. data/app/services/bulkrax/csv_template/schema_analyzer.rb +1 -5
  38. data/config/locales/bulkrax.en.yml +0 -2
  39. data/lib/bulkrax/engine.rb +1 -1
  40. data/lib/bulkrax/version.rb +1 -1
  41. data/lib/bulkrax.rb +76 -11
  42. data/lib/generators/bulkrax/install_generator.rb +22 -10
  43. data/lib/generators/bulkrax/templates/config/initializers/bulkrax.rb +1 -1
  44. metadata +32 -34
  45. data/app/errors/bulkrax/unsafe_cloud_source_error.rb +0 -11
  46. data/app/errors/bulkrax/unsafe_path_error.rb +0 -8
  47. data/app/parsers/bulkrax/application_parser.rb.orig +0 -545
  48. data/app/services/bulkrax/cloud_source_validator.rb +0 -123
  49. data/app/services/bulkrax/safe_path.rb +0 -129
  50. data/lib/wings/custom_queries/find_by_source_identifier.rb +0 -41
@@ -1,129 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Bulkrax
4
- # Shared path-confinement checks used anywhere Bulkrax resolves a
5
- # caller-supplied relative path against a trusted root directory: ZIP
6
- # extraction, cloud-file destinations, and filesystem cloud sources.
7
- #
8
- # Lexical checks alone (rejecting `..` and absolute paths) don't catch a
9
- # symlink planted inside the root that points back out of it, so
10
- # {.confine!} also resolves the longest existing ancestor of the
11
- # candidate path and checks *that* for escape.
12
- #
13
- # @raise [Bulkrax::UnsafePathError] from any public method, never a bare
14
- # StandardError, so callers can rescue one error class.
15
- class SafePath
16
- # Built via Integer#chr, not a literal escape, so this source
17
- # file never contains a raw NUL byte (which some tools,
18
- # including `git diff`, treat as a signal to classify the
19
- # whole file as binary).
20
- NUL = 0.chr.freeze
21
-
22
- # Preflight check for a raw, caller-supplied name — a ZIP entry name,
23
- # or a cloud-file's uploaded filename — before any join, space
24
- # normalization, or filesystem access. Callers must run this on the
25
- # *raw* name: joining it under a prefix first (e.g. `File.join('files',
26
- # name)`) can silently neutralize a leading `/`, which would defeat
27
- # this check if run afterward. {.confine!} runs it again on the
28
- # resolved candidate as the final authority.
29
- #
30
- # @param name [String] a raw entry/file name
31
- # @raise [Bulkrax::UnsafePathError] if the name is an absolute path, has
32
- # a `..` segment, or contains a NUL byte
33
- def self.reject_unsafe_entry!(name)
34
- name = name.to_s
35
- raise Bulkrax::UnsafePathError, 'path must not contain a NUL byte' if name.include?(NUL)
36
- raise Bulkrax::UnsafePathError, 'path must not contain a parent-directory reference' if name.split('/').include?('..')
37
- raise Bulkrax::UnsafePathError, 'path must not be an absolute path' if name.start_with?('/')
38
- end
39
-
40
- # Resolves `candidate` against `root` and returns the absolute result,
41
- # but only if it stays inside `root` once both symlinks and `..`
42
- # segments are resolved.
43
- #
44
- # By default `candidate` is held to the same lexical rule as
45
- # {.reject_unsafe_entry!}: no `..` segment and no leading `/`, even if
46
- # the fully-resolved result would happen to land inside `root` — a
47
- # destination filename that only *looks* safe after normalization is
48
- # still rejected outright, per the plan, rather than silently
49
- # corrected. The one legitimate exception is a `file:` *source* path
50
- # (always given as an absolute path by the filesystem provider);
51
- # pass `allow_absolute: true` for that case. `..` segments are never
52
- # allowed, even then.
53
- #
54
- # @param root [String] trusted directory that the result must stay inside
55
- # @param candidate [String] a relative (or, with `allow_absolute`, absolute) path to resolve
56
- # @param require_file [Boolean] when true, reject a result equal to `root` itself
57
- # @param allow_absolute [Boolean] when true, permit `candidate` to be an absolute path
58
- # @return [String] absolute path, validated to be inside `root`
59
- # @raise [Bulkrax::UnsafePathError] if `candidate` is blank, contains a
60
- # NUL byte, has a `..` segment, is an absolute path (unless
61
- # `allow_absolute`), escapes `root`, or escapes `root` via a symlink
62
- def self.confine!(root, candidate, require_file: false, allow_absolute: false)
63
- raise Bulkrax::UnsafePathError, 'path must not be blank' if candidate.blank?
64
-
65
- candidate = candidate.to_s
66
- if allow_absolute
67
- raise Bulkrax::UnsafePathError, 'path must not contain a NUL byte' if candidate.include?(NUL)
68
- raise Bulkrax::UnsafePathError, 'path must not contain a parent-directory reference' if candidate.split('/').include?('..')
69
- else
70
- reject_unsafe_entry!(candidate)
71
- end
72
-
73
- expanded_root = File.expand_path(root)
74
- expanded_path = File.expand_path(candidate, expanded_root)
75
-
76
- raise Bulkrax::UnsafePathError, 'path escapes the confined root' unless expanded_path == expanded_root || expanded_path.start_with?("#{expanded_root}#{File::SEPARATOR}")
77
- raise Bulkrax::UnsafePathError, 'a file path is required' if require_file && expanded_path == expanded_root
78
-
79
- reject_symlink_escape!(expanded_root, expanded_path)
80
- expanded_path
81
- end
82
-
83
- # True if a filesystem entry exists at `path` — including a dangling
84
- # symlink. File.exist? follows symlinks and reports `false` for one
85
- # whose target doesn't exist yet, which is exactly the entry a caller
86
- # is about to write through, so it can't be the only check here.
87
- def self.present?(path)
88
- File.exist?(path) || File.symlink?(path)
89
- end
90
- private_class_method :present?
91
-
92
- # Resolves `path` to its real, final location, tolerating a dangling
93
- # symlink (one whose target doesn't exist) by following the link
94
- # lexically instead of raising the way File.realpath would. Bounded
95
- # to 32 hops so a symlink cycle fails closed rather than hanging.
96
- def self.resolve_fully(path)
97
- current = path
98
- 32.times do
99
- return File.realpath(current) if File.exist?(current)
100
- return current unless File.symlink?(current)
101
- current = File.expand_path(File.readlink(current), File.dirname(current))
102
- end
103
- raise Bulkrax::UnsafePathError, 'path has too many levels of symbolic links'
104
- end
105
- private_class_method :resolve_fully
106
-
107
- # Resolves the longest existing-or-dangling-symlink ancestor of
108
- # `expanded_path` (which may be `expanded_path` itself) and checks it
109
- # for a symlink escape. Path components that aren't present at all
110
- # (not even as a symlink) can't be symlinks, so this is the final
111
- # line of defense without racing the filesystem. As the plan assumes,
112
- # this is not a guard against a *concurrent* write to the same path —
113
- # only against a symlink already planted in an application-controlled
114
- # staging directory.
115
- def self.reject_symlink_escape!(expanded_root, expanded_path)
116
- real_root = present?(expanded_root) ? resolve_fully(expanded_root) : expanded_root
117
-
118
- existing_ancestor = expanded_path
119
- existing_ancestor = File.dirname(existing_ancestor) until present?(existing_ancestor) || existing_ancestor == File.dirname(existing_ancestor)
120
- return unless present?(existing_ancestor)
121
-
122
- real_existing = resolve_fully(existing_ancestor)
123
- return if real_existing == real_root || real_existing.start_with?("#{real_root}#{File::SEPARATOR}")
124
-
125
- raise Bulkrax::UnsafePathError, 'path escapes the confined root via a symlink'
126
- end
127
- private_class_method :reject_symlink_escape!
128
- end
129
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Wings
4
- module CustomQueries
5
- class FindBySourceIdentifier
6
- # Custom query override specific to Wings
7
-
8
- def self.queries
9
- [:find_by_property_value]
10
- end
11
-
12
- attr_reader :query_service
13
- delegate :resource_factory, to: :query_service
14
-
15
- def initialize(query_service:)
16
- @query_service = query_service
17
- end
18
-
19
- # rubocop:disable Lint/UnusedMethodArgument
20
- def find_by_property_value(property:, value:, search_field:, use_valkyrie: Hyrax.config.use_valkyrie?)
21
- # rubocop:enable Lint/UnusedMethodArgument
22
- # NOTE: This is using the Bulkrax::ObjectFactory (e.g. the one envisioned for ActiveFedora).
23
- # In doing this, we avoid the situation where Bulkrax::ValkyrieObjectFactory calls this custom query.
24
-
25
- # This is doing a solr search first, so we have to use the search_field instead of the property for "field"
26
- # If it cannot find the object in Solr, it falls back to an ActiveFedora search, if the object is an ActiveFedora object
27
- af_object = Bulkrax::ObjectFactory.search_by_property(
28
- value: value,
29
- klass: ActiveFedora::Base,
30
- field: search_field,
31
- name_field: property
32
- )
33
-
34
- return if af_object.blank?
35
- return af_object unless use_valkyrie
36
-
37
- resource_factory.to_resource(object: af_object)
38
- end
39
- end
40
- end
41
- end