bulkrax 10.0.0 → 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.
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bulkrax
4
+ # Validates a cloud-file source (the Hash a user selected via
5
+ # browse-everything, or supplied directly in importer params) before
6
+ # Bulkrax downloads it. Destination confinement (see {Bulkrax::SafePath})
7
+ # stops a malicious filename from writing outside the importer's
8
+ # workspace; this validator stops a malicious `url` from reading a local
9
+ # file outside the configured filesystem-provider root, or from reaching
10
+ # a private/internal network over HTTP(S) (SSRF).
11
+ #
12
+ # Called twice by design: once before enqueueing, so an obviously unsafe
13
+ # batch never reaches a job queue, and again inside the download job,
14
+ # so a legacy or tampered job argument can't bypass validation.
15
+ class CloudSourceValidator
16
+ LOCAL_FILE_HOSTS = ['', 'localhost'].freeze
17
+
18
+ # Lightweight preflight for a single cloud-file selection: checks the
19
+ # raw file name and the source's safety, without resolving anything
20
+ # against an importer's workspace. Shared by CsvParser#plan_cloud_file,
21
+ # BagitParser#retrieve_cloud_files, and ImportersController's pre-save
22
+ # check, so a selection is judged unsafe the same way whether or not an
23
+ # importer has been persisted yet.
24
+ #
25
+ # @param file [Hash] a single cloud-file spec with at least `url` and `file_name` keys
26
+ # @raise [Bulkrax::UnsafePathError] if the raw file name is unsafe
27
+ # @raise [Bulkrax::UnsafeCloudSourceError] if the source isn't safe to download
28
+ # @return [true]
29
+ def self.preflight!(file)
30
+ name = file['file_name'] || file[:file_name]
31
+ raise Bulkrax::UnsafePathError, 'cloud file name must not be blank' if name.blank?
32
+ Bulkrax::SafePath.reject_unsafe_entry!(name)
33
+ validate!(file)
34
+ end
35
+
36
+ # @param file [Hash] a single cloud-file spec with at least a `url` key
37
+ # @raise [Bulkrax::UnsafeCloudSourceError] if the source isn't safe to download
38
+ # @return [true]
39
+ def self.validate!(file)
40
+ url = file['url'] || file[:url]
41
+ raise Bulkrax::UnsafeCloudSourceError, 'the cloud file is missing a url' if url.blank?
42
+
43
+ uri = parse_uri(url)
44
+ case uri.scheme
45
+ when 'file'
46
+ validate_file_scheme!(uri)
47
+ when 'http', 'https'
48
+ validate_http_scheme!(uri, file)
49
+ else
50
+ raise Bulkrax::UnsafeCloudSourceError, 'unsupported source scheme'
51
+ end
52
+ true
53
+ end
54
+
55
+ def self.parse_uri(url)
56
+ Addressable::URI.parse(url.to_s)
57
+ rescue Addressable::URI::InvalidURIError
58
+ raise Bulkrax::UnsafeCloudSourceError, 'the cloud file url is malformed'
59
+ end
60
+ private_class_method :parse_uri
61
+
62
+ def self.validate_file_scheme!(uri)
63
+ raise Bulkrax::UnsafeCloudSourceError, 'a file: source must not specify a remote host' unless LOCAL_FILE_HOSTS.include?(uri.host.to_s)
64
+
65
+ home = file_system_home
66
+ raise Bulkrax::UnsafeCloudSourceError, 'the filesystem provider is not configured' if home.blank?
67
+
68
+ decoded_path = URI::DEFAULT_PARSER.unescape(uri.path.to_s)
69
+ Bulkrax::SafePath.confine!(home, decoded_path, require_file: true, allow_absolute: true)
70
+ rescue Bulkrax::UnsafePathError => e
71
+ raise Bulkrax::UnsafeCloudSourceError, e.message
72
+ end
73
+ private_class_method :validate_file_scheme!
74
+
75
+ # Enforced at connection time, not by a DNS preflight: SsrfFilter
76
+ # resolves the hostname, rejects private/loopback/link-local/metadata
77
+ # addresses, and then connects directly to the validated IP (with TLS
78
+ # verified against the original hostname), closing the gap a
79
+ # resolve-then-connect check would leave open to DNS rebinding.
80
+ # Redirects are disabled outright (rather than re-validated per hop)
81
+ # so a header carrying provider credentials is never resent to a
82
+ # different host.
83
+ def self.validate_http_scheme!(uri, file)
84
+ raise Bulkrax::UnsafeCloudSourceError, 'no remote cloud provider is configured' unless remote_provider_configured?
85
+ raise Bulkrax::UnsafeCloudSourceError, 'the cloud file url must include a host' if uri.host.blank?
86
+ raise Bulkrax::UnsafeCloudSourceError, 'the cloud file url must not include credentials' if uri.userinfo.present?
87
+
88
+ SsrfFilter.head(uri.to_s, headers: request_headers(file), max_redirects: 0)
89
+ rescue SsrfFilter::Error, SocketError, SystemCallError, Timeout::Error, OpenSSL::SSL::SSLError
90
+ # Static and class-agnostic on purpose: SsrfFilter's own exception
91
+ # message can include the resolved IP or hostname it rejected, and
92
+ # this error is shown to the importing user / written to the
93
+ # importer's status, not just an ops log.
94
+ raise Bulkrax::UnsafeCloudSourceError, 'blocked unsafe remote source'
95
+ end
96
+ private_class_method :validate_http_scheme!
97
+
98
+ # Merges the caller-supplied headers (used for provider auth) but
99
+ # strips any transport-control header a malicious selection could use
100
+ # to smuggle a different Host past SsrfFilter's own Host header.
101
+ def self.request_headers(file)
102
+ headers = {}
103
+ headers.merge!(file['headers'] || file[:headers] || {})
104
+ headers.merge!(file['auth_header'] || file[:auth_header] || {})
105
+ headers.reject { |key, _| key.to_s.casecmp('host').zero? }
106
+ end
107
+
108
+ def self.file_system_home
109
+ config = provider_config('file_system')
110
+ config && (config['home'] || config[:home])
111
+ end
112
+
113
+ def self.remote_provider_configured?
114
+ BrowseEverything.config.any? { |key, value| key.to_s != 'file_system' && value.present? }
115
+ end
116
+
117
+ def self.provider_config(name)
118
+ config = BrowseEverything.config
119
+ config[name] || config[name.to_sym]
120
+ end
121
+ private_class_method :provider_config
122
+ end
123
+ end
@@ -0,0 +1,129 @@
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
@@ -91,6 +91,8 @@ en:
91
91
  public: Public
92
92
  browse_everything:
93
93
  add_cloud_files: Add Cloud Files
94
+ errors:
95
+ missing_destination_context: This cloud file download is missing required destination information and can no longer be safely processed. Please re-select the file and try the import again.
94
96
  csv:
95
97
  add_csv_to_import: 'Add CSV or ZIP File to Import:'
96
98
  add_files_hint: Choose files to upload. The filenames must be unique, and the filenames must be referenced in a column called 'file' in the accompanying CSV file.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bulkrax
4
- VERSION = '10.0.0'
4
+ VERSION = '10.0.1'
5
5
  end
data/lib/bulkrax.rb CHANGED
@@ -14,6 +14,7 @@ require 'language_list'
14
14
  require 'marcel'
15
15
  require 'nokogiri'
16
16
  require 'ostruct'
17
+ require 'ssrf_filter'
17
18
  require 'zip'
18
19
 
19
20
  def conditional_require(gem_name)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulkrax
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.0.0
4
+ version: 10.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Kaufman
@@ -257,6 +257,26 @@ dependencies:
257
257
  - - ">="
258
258
  - !ruby/object:Gem::Version
259
259
  version: '0'
260
+ - !ruby/object:Gem::Dependency
261
+ name: ssrf_filter
262
+ requirement: !ruby/object:Gem::Requirement
263
+ requirements:
264
+ - - ">="
265
+ - !ruby/object:Gem::Version
266
+ version: '1.0'
267
+ - - "<"
268
+ - !ruby/object:Gem::Version
269
+ version: '2.0'
270
+ type: :runtime
271
+ prerelease: false
272
+ version_requirements: !ruby/object:Gem::Requirement
273
+ requirements:
274
+ - - ">="
275
+ - !ruby/object:Gem::Version
276
+ version: '1.0'
277
+ - - "<"
278
+ - !ruby/object:Gem::Version
279
+ version: '2.0'
260
280
  - !ruby/object:Gem::Dependency
261
281
  name: dry-monads
262
282
  requirement: !ruby/object:Gem::Requirement
@@ -384,6 +404,8 @@ files:
384
404
  - app/controllers/concerns/bulkrax/download_behavior.rb
385
405
  - app/controllers/concerns/bulkrax/guided_import_demo_scenarios.rb
386
406
  - app/controllers/concerns/bulkrax/importer_file_handler.rb
407
+ - app/errors/bulkrax/unsafe_cloud_source_error.rb
408
+ - app/errors/bulkrax/unsafe_path_error.rb
387
409
  - app/errors/bulkrax/unzip_error.rb
388
410
  - app/factories/bulkrax/object_factory.rb
389
411
  - app/factories/bulkrax/object_factory_interface.rb
@@ -450,6 +472,7 @@ files:
450
472
  - app/models/concerns/bulkrax/importer_exporter_behavior.rb
451
473
  - app/models/concerns/bulkrax/status_info.rb
452
474
  - app/parsers/bulkrax/application_parser.rb
475
+ - app/parsers/bulkrax/application_parser.rb.orig
453
476
  - app/parsers/bulkrax/bagit_parser.rb
454
477
  - app/parsers/bulkrax/csv_parser.rb
455
478
  - app/parsers/bulkrax/oai_dc_parser.rb
@@ -461,6 +484,7 @@ files:
461
484
  - app/parsers/concerns/bulkrax/csv_parser/csv_validation_helpers.rb
462
485
  - app/parsers/concerns/bulkrax/csv_parser/csv_validation_hierarchy.rb
463
486
  - app/search_builders/bulkrax/export_search_builder.rb
487
+ - app/services/bulkrax/cloud_source_validator.rb
464
488
  - app/services/bulkrax/csv_template/column_builder.rb
465
489
  - app/services/bulkrax/csv_template/column_descriptor.rb
466
490
  - app/services/bulkrax/csv_template/csv_builder.rb
@@ -476,6 +500,7 @@ files:
476
500
  - app/services/bulkrax/csv_template/value_determiner.rb
477
501
  - app/services/bulkrax/factory_class_finder.rb
478
502
  - app/services/bulkrax/remove_relationships_for_importer.rb
503
+ - app/services/bulkrax/safe_path.rb
479
504
  - app/services/bulkrax/split_pattern_coercion.rb
480
505
  - app/services/bulkrax/stepper_response_formatter.rb
481
506
  - app/services/bulkrax/validation_error_csv_builder.rb
@@ -605,7 +630,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
605
630
  - !ruby/object:Gem::Version
606
631
  version: '0'
607
632
  requirements: []
608
- rubygems_version: 4.0.22
633
+ rubygems_version: 3.6.8
609
634
  specification_version: 4
610
635
  summary: Import and export tool for Hyrax and Hyku
611
636
  test_files: []