ro-crate 0.7.1 → 0.7.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 125d04bfd22ce828caf9adb16e0dd4d5fcd70a67df4389e3d5444e23b8a69513
4
- data.tar.gz: be9cfce536b620833ba46a9595ae8bdca8d9ec5aab459827119f2f88e7cd3565
3
+ metadata.gz: 405a4d22f0d121f7bc29e40ebed415a91e296350fdb8a9f2b6824e0510d02ebf
4
+ data.tar.gz: bdf9ba7b6b2b8d86f571fec5191c7d84fc31a25a24f6ab8863b1f791db84dd00
5
5
  SHA512:
6
- metadata.gz: 3a44da2d8253ee5758cdaf9d87c152e1f8d263109004baec0983f9d348bde955c0bf9ba49ff6afa356d007a3e2b9e34ed3ca6b174764720c32e4400bf3cdf5ec
7
- data.tar.gz: d72d085135c8b6e39dd2389e79db97b645813dde486220fb79076841a8a5499ce182bd3a8e79a423602e943526bc4e718e58de674de10875f43fdda3af3e8a07
6
+ metadata.gz: 73f3d8a1628d48bd3b34a9f38f0d5c3ee3fc4b880a9b2a21ab61e8c16488843d7e197f9d0c79152777a4890a24314c436446806525a3dc1afa28f2ccceb8c9da
7
+ data.tar.gz: faea399dcfe59a0dff4400826d179f138355c7b401982380a3543c29dbd5d43227da576b9cc843c30c9d9b7a4fb97227e0a8eddc57a18d3aaac5d8d37e0a1e0c
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-3.4.9
1
+ ruby-3.3.14
@@ -92,11 +92,26 @@ module ROCrate
92
92
  # @param source_directory [String, Pathname, ::File,] The source directory that will be included in the crate.
93
93
  # @param create_entities [Boolean] Whether to create data entities for the added content, or just include them anonymously.
94
94
  # @param include_hidden [Boolean] Whether to include hidden files, i.e. those prefixed by a `.` (period).
95
+ # @param allow_external [Boolean] Whether to allow files that resolve to a location outside of `source_directory`,
96
+ # e.g. symlinks to files elsewhere on the disk. Symlinks to locations within `source_directory` are always allowed.
97
+ #
98
+ # @raise [ROCrate::Exception] Raised if `allow_external` is false and any files resolve to a location outside of
99
+ # `source_directory`. Nothing is added to the crate in this case.
95
100
  #
96
101
  # @return [Array<DataEntity>] Any entities that were created from the directory contents. Will be empty if `create_entities` was false.
97
- def add_all(source_directory, create_entities = true, include_hidden: false)
102
+ def add_all(source_directory, create_entities = true, include_hidden: false, allow_external: false)
98
103
  added = []
99
104
 
105
+ unless allow_external
106
+ external = list_all_files(source_directory, include_hidden: include_hidden).select do |rel_path|
107
+ path = ::File.join(source_directory, rel_path)
108
+ ::File.exist?(path) && !ROCrate::Reader.contained?(source_directory, path)
109
+ end
110
+ unless external.empty?
111
+ raise ROCrate::Exception, "Directory contains files that resolve to locations outside of it: #{external.join(', ')}"
112
+ end
113
+ end
114
+
100
115
  if create_entities
101
116
  list_all_files(source_directory, include_hidden: include_hidden).each do |rel_path|
102
117
  source_path = Pathname.new(::File.join(source_directory, rel_path)).expand_path
@@ -32,6 +32,8 @@ module ROCrate
32
32
  # @param crate_path [String, nil] The relative path within the RO-Crate where this data entity should be written.
33
33
  # Also used as the ID of the DataEntity. Will be taken from `properties` or generated if `crate_path` is nil.
34
34
  # @param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this DataEntity.
35
+ #
36
+ # @raise [ArgumentError] Raised if `crate_path` is an absolute path, or a relative path that leads outside the crate.
35
37
  def initialize(crate, source = nil, crate_path = nil, properties = {})
36
38
  if crate_path.is_a?(Hash) && properties.empty?
37
39
  properties = crate_path
@@ -43,6 +45,8 @@ module ROCrate
43
45
  if crate_path.nil?
44
46
  crate_path = @source.basename.to_s if @source.respond_to?(:basename)
45
47
  crate_path = @source.to_s if @source.is_a?(URI) && @source.absolute?
48
+ else
49
+ validate_crate_path(crate_path)
46
50
  end
47
51
 
48
52
  super(crate, crate_path, properties)
@@ -67,6 +71,26 @@ module ROCrate
67
71
 
68
72
  private
69
73
 
74
+ ##
75
+ # Ensure the given crate path is a relative path that stays within the crate's root, e.g. `a/../b` is allowed, but
76
+ # `../b` is not. Absolute URIs (for remote data entities) are not checked.
77
+ #
78
+ # @param crate_path [String, Pathname] The relative path within the RO-Crate.
79
+ # @raise [ArgumentError] Raised if the path is absolute, or leads outside the crate.
80
+ def validate_crate_path(crate_path)
81
+ crate_path = crate_path.to_s
82
+ if crate_path.start_with?('/')
83
+ raise ArgumentError, "crate_path must be relative to the crate root, not absolute: #{crate_path}"
84
+ end
85
+
86
+ uri = URI(crate_path) rescue nil
87
+ return if uri&.absolute?
88
+
89
+ if Pathname.new(crate_path).cleanpath.each_filename.first == '..'
90
+ raise ArgumentError, "crate_path must be within the crate root: #{crate_path}"
91
+ end
92
+ end
93
+
70
94
  ##
71
95
  # Do some normalization of the given source. Coverts `::File` and `String` (if relative path) to `Pathname`
72
96
  # and ensures they are expanded.
@@ -108,7 +108,11 @@ module ROCrate
108
108
  entry == ROCrate::Metadata::IDENTIFIER_1_0 }
109
109
 
110
110
  if metadata_file
111
- metadata_json = ::File.read(::File.join(source, metadata_file))
111
+ metadata_path = ::File.join(source, metadata_file)
112
+ unless contained?(source, metadata_path)
113
+ raise ROCrate::ReadException, "Metadata file is outside the crate: #{metadata_file}"
114
+ end
115
+ metadata_json = ::File.read(metadata_path)
112
116
  begin
113
117
  metadata = JSON.parse(metadata_json)
114
118
  rescue JSON::ParserError => e
@@ -173,9 +177,30 @@ module ROCrate
173
177
  crate.add_contextual_entity(entity)
174
178
  end
175
179
 
180
+ check_for_external_entries(crate, source)
181
+
176
182
  crate
177
183
  end
178
184
 
185
+ ##
186
+ # Check that none of the entries in the payload of the given crate (or its data entities) resolve to a location
187
+ # outside of the crate's root, e.g. symlinks to files elsewhere on the disk.
188
+ #
189
+ # @param crate [Crate] The RO-Crate being read.
190
+ # @param source [String, ::File, Pathname] The location of the RO-Crate being read.
191
+ #
192
+ # @raise [ROCrate::ReadException] Raised if any entries resolve to a location outside the crate.
193
+ def self.check_for_external_entries(crate, source)
194
+ external = crate.payload.select do |_, entry|
195
+ path = entry.path
196
+ path && ::File.exist?(path) && !contained?(source, path)
197
+ end.keys
198
+
199
+ return if external.empty?
200
+
201
+ raise ROCrate::ReadException, "Crate contains entries that resolve to locations outside the crate: #{external.join(', ')}"
202
+ end
203
+
179
204
  ##
180
205
  # Initialize a crate from the given set of entities.
181
206
  #
@@ -195,11 +220,12 @@ module ROCrate
195
220
  crate.metadata.context = context
196
221
  preview_properties = entity_hash.delete(ROCrate::Preview::IDENTIFIER)
197
222
  preview_path = ::File.join(source, ROCrate::Preview::IDENTIFIER)
198
- preview_path = ::File.exist?(preview_path) ? Pathname.new(preview_path) : nil
223
+ preview_path = (::File.exist?(preview_path) && contained?(source, preview_path)) ? Pathname.new(preview_path) : nil
199
224
  if preview_properties || preview_path
200
225
  crate.preview = ROCrate::Preview.new(crate, preview_path, preview_properties || {})
201
226
  end
202
- crate.add_all(source, false)
227
+ # External entries are checked for (and raise a ReadException) in `check_for_external_entries`.
228
+ crate.add_all(source, false, allow_external: true)
203
229
  end
204
230
  end
205
231
 
@@ -270,8 +296,8 @@ module ROCrate
270
296
  decoded_id = nil
271
297
  elsif !id.start_with?('#')
272
298
  [id, decoded_id].each do |i|
273
- fullpath = ::File.join(source, i)
274
- path = Pathname.new(fullpath) if ::File.exist?(fullpath)
299
+ fullpath = safe_local_path(source, i)
300
+ path = fullpath if fullpath.exist?
275
301
  end
276
302
  if path.nil?
277
303
  raise ROCrate::ReadException, "Local Data Entity not found in crate: #{id}"
@@ -281,6 +307,50 @@ module ROCrate
281
307
  entity_class.new(crate, path, decoded_id, entity_props)
282
308
  end
283
309
 
310
+ ##
311
+ # Resolve a local data entity's ID to a path within the crate's root directory, raising an exception if the
312
+ # resulting path (or the target of any symlinks along it) is outside of the crate's root.
313
+ #
314
+ # @param source [String, ::File, Pathname] The location of the RO-Crate being read.
315
+ # @param id [String] The local ID of the data entity.
316
+ #
317
+ # @raise [ROCrate::ReadException] Raised if the path resolves to a location outside the crate.
318
+ #
319
+ # @return [Pathname] The path to the data entity (which may not exist).
320
+ def self.safe_local_path(source, id)
321
+ raise ROCrate::ReadException, "Data Entity has absolute path: #{id}" if id.start_with?('/')
322
+ base = Pathname.new(::File.expand_path(source))
323
+ path = base.join(id).expand_path
324
+ unsafe = !within?(base, path) || (path.exist? && !contained?(base, path))
325
+ raise ROCrate::ReadException, "Data Entity path is outside the crate: #{id}" if unsafe
326
+
327
+ path
328
+ end
329
+
330
+ ##
331
+ # Does the given path, once any symlinks are resolved, point to a location within the given base directory
332
+ # (also with symlinks resolved)?
333
+ #
334
+ # @param base [String, Pathname] The path to a directory.
335
+ # @param path [String, Pathname] The path to check.
336
+ # @return [Boolean] true if the path is within the base directory, false if it is not or could not be resolved.
337
+ def self.contained?(base, path)
338
+ within?(Pathname.new(base).realpath, Pathname.new(path).realpath)
339
+ rescue SystemCallError
340
+ false
341
+ end
342
+
343
+ ##
344
+ # Is the given path the same as, or nested within, the given base directory?
345
+ #
346
+ # @param base [Pathname] An absolute path to a directory.
347
+ # @param path [Pathname] An absolute path to be tested.
348
+ # @return [Boolean]
349
+ def self.within?(base, path)
350
+ path.relative_path_from(base).each_filename.first != '..'
351
+ rescue ArgumentError # Handle unrelatable paths, e.g. on different drives.
352
+ false
353
+ end
284
354
 
285
355
  ##
286
356
  # Extract the metadata entity from the entity hash, according to the rules defined here:
data/ro_crate.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ro-crate'
3
- s.version = '0.7.1'
3
+ s.version = '0.7.2'
4
4
  s.summary = 'Create, manipulate, read RO-Crates.'
5
5
  s.authors = ['Finn Bacall']
6
6
  s.email = 'finn.bacall@manchester.ac.uk'
data/test/crate_test.rb CHANGED
@@ -406,4 +406,92 @@ class CrateTest < Test::Unit::TestCase
406
406
  $stderr = original_stderr
407
407
  end
408
408
  end
409
+
410
+ test 'refuses absolute crate_path for data entities' do
411
+ crate = ROCrate::Crate.new
412
+ source = fixture_file('info.txt').path
413
+
414
+ ['/etc/passwd', '//etc/passwd', Pathname.new('/etc/passwd')].each do |crate_path|
415
+ e = check_exception(ArgumentError) { crate.add_file(source, crate_path) }
416
+ assert_include e.message, 'crate_path must be relative'
417
+ e = check_exception(ArgumentError) { ROCrate::File.new(crate, source, crate_path) }
418
+ assert_include e.message, 'crate_path must be relative'
419
+ end
420
+ e = check_exception(ArgumentError) { crate.add_directory(fixture_file('directory').path, '/abs/dir') }
421
+ assert_include e.message, 'crate_path must be relative'
422
+
423
+ assert_equal 'info.txt', crate.add_file(source).id
424
+ assert_equal 'nested/info.txt', crate.add_file(source, 'nested/info.txt').id
425
+ assert_equal 'http://example.com/data.csv', crate.add_external_file('http://example.com/data.csv').id
426
+ end
427
+
428
+ test 'refuses crate_path that leads outside the crate' do
429
+ crate = ROCrate::Crate.new
430
+ source = fixture_file('info.txt').path
431
+
432
+ ['../escaped.txt', 'a/../../escaped.txt', 'a/./../b/../../escaped.txt', '..', Pathname.new('../x')].each do |crate_path|
433
+ e = check_exception(ArgumentError) { crate.add_file(source, crate_path) }
434
+ assert_include e.message, 'crate_path must be within the crate root'
435
+ end
436
+ e = check_exception(ArgumentError) { crate.add_directory(fixture_file('directory').path, '../dir') }
437
+ assert_include e.message, 'crate_path must be within the crate root'
438
+
439
+ # Paths that stay within the crate are fine
440
+ assert_equal 'a/../b.txt', crate.add_file(source, 'a/../b.txt').id
441
+ assert_equal 'x..y/..z.txt', crate.add_file(source, 'x..y/..z.txt').id
442
+ # Literal (not percent-decoded) file name, so this stays within the crate
443
+ assert_equal '%2E%2E/x.txt', crate.add_file(source, '%2E%2E/x.txt').filepath
444
+ # Remote entities are not checked
445
+ assert crate.add_file('http://example.com/a/../../b.txt', 'http://example.com/a/../../b.txt')
446
+
447
+ Dir.mktmpdir do |dir|
448
+ out = ::File.join(dir, 'out')
449
+ ROCrate::Writer.new(crate).write(out)
450
+ assert ::File.exist?(::File.join(out, 'b.txt'))
451
+ assert_empty Dir.children(dir) - ['out']
452
+ end
453
+ end
454
+
455
+ test 'add_all refuses files that resolve outside the source directory, unless allowed' do
456
+ Dir.mktmpdir do |dir|
457
+ ::File.write(::File.join(dir, 'secret.txt'), 'top secret')
458
+ ::FileUtils.mkdir_p(::File.join(dir, 'outside_dir'))
459
+ src = ::File.join(dir, 'src')
460
+ ::FileUtils.mkdir_p(::File.join(src, 'data'))
461
+ ::FileUtils.mkdir_p(::File.join(src, 'input'))
462
+ ::File.write(::File.join(src, 'data', 'big_file'), 'big')
463
+ # Internal symlinks
464
+ ::File.symlink('../data/big_file', ::File.join(src, 'input', 'a'))
465
+ ::File.symlink(::File.join(src, 'data', 'big_file'), ::File.join(src, 'input', 'b'))
466
+ ::File.symlink('data', ::File.join(src, 'data_link'))
467
+ # Dangling
468
+ ::File.symlink('../nope', ::File.join(src, 'input', 'dangling'))
469
+
470
+ [true, false].each do |create_entities|
471
+ crate = ROCrate::Crate.new
472
+ crate.add_all(src, create_entities)
473
+ assert_equal 'big', crate.payload['input/a'].read
474
+ assert_equal 'big', crate.payload['input/b'].read
475
+ assert crate.payload['input/a'].symlink?
476
+ end
477
+
478
+ ::File.symlink('../../secret.txt', ::File.join(src, 'input', 'evil'))
479
+ ::File.symlink(::File.join(dir, 'outside_dir'), ::File.join(src, 'evil_dir'))
480
+
481
+ [true, false].each do |create_entities|
482
+ crate = ROCrate::Crate.new
483
+ e = check_exception(ROCrate::Exception) { crate.add_all(src, create_entities) }
484
+ assert_include e.message, 'Directory contains files that resolve to locations outside of it:'
485
+ assert_include e.message, 'evil_dir'
486
+ assert_include e.message, 'input/evil'
487
+ assert_not_include e.message, 'input/a'
488
+ assert_empty crate.data_entities
489
+ refute crate.payload.key?('data/big_file')
490
+
491
+ crate = ROCrate::Crate.new
492
+ crate.add_all(src, create_entities, allow_external: true)
493
+ assert_equal 'top secret', crate.payload['input/evil'].read
494
+ end
495
+ end
496
+ end
409
497
  end
data/test/reader_test.rb CHANGED
@@ -523,4 +523,221 @@ class ReaderTest < Test::Unit::TestCase
523
523
  assert_equal 'https://w3id.org/ro/crate/1.1', crate.metadata.spec_url
524
524
  assert_equal 'https://w3id.org/ro/crate/1.1/context', crate.metadata.context_url
525
525
  end
526
+
527
+ test 'refuses data entity with path traversing outside crate in a zip' do
528
+ Dir.mktmpdir do |dir|
529
+ secret = ::File.join(dir, 'secret.txt')
530
+ ::File.write(secret, 'top secret')
531
+ zip_path = ::File.join(dir, 'evil.crate.zip')
532
+ evil_id = '../' * 20 + secret.delete_prefix('/')
533
+ Zip::File.open(zip_path, create: true) do |zip|
534
+ zip.get_output_stream('ro-crate-metadata.json') { |f| f.write(traversal_metadata(evil_id)) }
535
+ end
536
+
537
+ e = check_exception(ROCrate::ReadException) do
538
+ ROCrate::Reader.read(zip_path, target_dir: Dir.mktmpdir(nil, dir))
539
+ end
540
+ assert_include e.message, 'Data Entity path is outside the crate'
541
+ end
542
+ end
543
+
544
+ test 'refuses data entity with path traversing outside crate in a directory' do
545
+ Dir.mktmpdir do |dir|
546
+ ::File.write(::File.join(dir, 'secret.txt'), 'top secret')
547
+ crate_dir = ::File.join(dir, 'crate')
548
+ ::Dir.mkdir(crate_dir)
549
+ ['../secret.txt', '%2E%2E/secret.txt', '..%2Fsecret.txt'].each do |evil_id|
550
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata(evil_id))
551
+
552
+ e = check_exception(ROCrate::ReadException) do
553
+ ROCrate::Reader.read(crate_dir)
554
+ end
555
+ assert_include e.message, 'Data Entity path is outside the crate', "#{evil_id} was not refused"
556
+ end
557
+
558
+ ['/secret.txt', '//secret.txt', ::File.join(dir, 'secret.txt')].each do |evil_id|
559
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata(evil_id))
560
+
561
+ e = check_exception(ROCrate::ReadException) do
562
+ ROCrate::Reader.read(crate_dir)
563
+ end
564
+ assert_include e.message, 'Data Entity has absolute path', "#{evil_id} was not refused"
565
+ end
566
+ end
567
+ end
568
+
569
+ test 'refuses data entity that is a symlink to a file outside the crate' do
570
+ Dir.mktmpdir do |dir|
571
+ secret = ::File.join(dir, 'secret.txt')
572
+ ::File.write(secret, 'top secret')
573
+ crate_dir = ::File.join(dir, 'crate')
574
+ ::Dir.mkdir(crate_dir)
575
+ ::File.symlink(secret, ::File.join(crate_dir, 'diagram.svg'))
576
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata('diagram.svg'))
577
+
578
+ e = check_exception(ROCrate::ReadException) do
579
+ ROCrate::Reader.read(crate_dir)
580
+ end
581
+ assert_include e.message, 'Data Entity path is outside the crate: diagram.svg'
582
+ end
583
+ end
584
+
585
+ test 'refuses data entity within a symlinked directory outside the crate' do
586
+ Dir.mktmpdir do |dir|
587
+ outside = ::File.join(dir, 'outside')
588
+ ::Dir.mkdir(outside)
589
+ ::File.write(::File.join(outside, 'secret.txt'), 'top secret')
590
+ crate_dir = ::File.join(dir, 'crate')
591
+ ::Dir.mkdir(crate_dir)
592
+ ::File.symlink(outside, ::File.join(crate_dir, 'data'))
593
+ ['data/secret.txt', 'data/'].each do |evil_id|
594
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata(evil_id))
595
+
596
+ e = check_exception(ROCrate::ReadException) do
597
+ ROCrate::Reader.read(crate_dir)
598
+ end
599
+ assert_include e.message, 'Data Entity path is outside the crate', "#{evil_id} was not refused"
600
+ end
601
+ end
602
+ end
603
+
604
+ test 'allows data entity that is a symlink to a file inside the crate' do
605
+ Dir.mktmpdir do |dir|
606
+ crate_dir = ::File.join(dir, 'crate')
607
+ ::FileUtils.mkdir_p(::File.join(crate_dir, 'images'))
608
+ ::File.write(::File.join(crate_dir, 'diagram.svg'), '<svg/>')
609
+ ::File.symlink('../diagram.svg', ::File.join(crate_dir, 'images', 'workflow diagram.svg'))
610
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata('images/workflow%20diagram.svg'))
611
+
612
+ crate = ROCrate::Reader.read(crate_dir)
613
+ entity = crate.dereference('images/workflow%20diagram.svg')
614
+ assert_not_nil entity
615
+ assert_equal '<svg/>', entity.source.read
616
+ end
617
+ end
618
+
619
+ test 'allows data entity whose path has redundant but contained traversal' do
620
+ Dir.mktmpdir do |dir|
621
+ crate_dir = ::File.join(dir, 'crate')
622
+ ::FileUtils.mkdir_p(::File.join(crate_dir, 'a'))
623
+ ::File.write(::File.join(crate_dir, 'b.txt'), 'hello')
624
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata('a/../b.txt'))
625
+
626
+ crate = ROCrate::Reader.read(crate_dir)
627
+ entity = crate.data_entities.first
628
+ assert_not_nil entity
629
+ assert_equal 'hello', entity.source.read
630
+ end
631
+ end
632
+
633
+ test 'refuses crate with payload entries that are symlinks to locations outside the crate' do
634
+ Dir.mktmpdir do |dir|
635
+ secret = ::File.join(dir, 'secret.txt')
636
+ ::File.write(secret, 'top secret')
637
+
638
+ # Unlisted file in the crate root
639
+ crate_dir = ::File.join(dir, 'crate1')
640
+ ::Dir.mkdir(crate_dir)
641
+ ::File.symlink(secret, ::File.join(crate_dir, 'unlisted'))
642
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata('#nothing'))
643
+ e = check_exception(ROCrate::ReadException) { ROCrate::Reader.read(crate_dir) }
644
+ assert_equal 'Crate contains entries that resolve to locations outside the crate: unlisted', e.message
645
+
646
+ # File within a directory data entity
647
+ crate_dir = ::File.join(dir, 'crate2')
648
+ ::FileUtils.mkdir_p(::File.join(crate_dir, 'data'))
649
+ ::File.symlink('../../secret.txt', ::File.join(crate_dir, 'data', 'evil'))
650
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata('data/'))
651
+ e = check_exception(ROCrate::ReadException) { ROCrate::Reader.read(crate_dir) }
652
+ assert_equal 'Crate contains entries that resolve to locations outside the crate: data/evil', e.message
653
+ end
654
+ end
655
+
656
+ test 'allows crate with symlinks to locations inside the crate, and dangling symlinks' do
657
+ Dir.mktmpdir do |dir|
658
+ crate_dir = ::File.join(dir, 'crate')
659
+ ::FileUtils.mkdir_p(::File.join(crate_dir, 'data'))
660
+ ::File.write(::File.join(crate_dir, 'data', 'real.txt'), 'real')
661
+ ::File.symlink('real.txt', ::File.join(crate_dir, 'data', 'internal'))
662
+ ::File.symlink('data/real.txt', ::File.join(crate_dir, 'real-link'))
663
+ ::File.symlink(::File.join(dir, 'does-not-exist'), ::File.join(crate_dir, 'data', 'dangling'))
664
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata('data/'))
665
+
666
+ crate = ROCrate::Reader.read(crate_dir)
667
+
668
+ assert_equal 'real', crate.payload['data/real.txt'].read
669
+ assert_equal 'real', crate.payload['data/internal'].read
670
+ assert_equal 'real', crate.payload['real-link'].read
671
+ assert crate.payload['data/internal'].symlink?
672
+ assert crate.payload['data/dangling'].symlink?
673
+
674
+ out = ::File.join(dir, 'out')
675
+ ROCrate::Writer.new(crate).write(out)
676
+ assert ::File.symlink?(::File.join(out, 'data', 'internal'))
677
+ assert_equal 'real', ::File.read(::File.join(out, 'data', 'internal'))
678
+ end
679
+ end
680
+
681
+ test 'ignores preview that is a symlink to a location outside the crate' do
682
+ Dir.mktmpdir do |dir|
683
+ secret = ::File.join(dir, 'secret.txt')
684
+ ::File.write(secret, 'top secret')
685
+ crate_dir = ::File.join(dir, 'crate')
686
+ ::Dir.mkdir(crate_dir)
687
+ ::File.symlink(secret, ::File.join(crate_dir, 'ro-crate-preview.html'))
688
+ ::File.write(::File.join(crate_dir, 'ro-crate-metadata.json'), traversal_metadata('#nothing'))
689
+
690
+ crate = ROCrate::Reader.read(crate_dir)
691
+
692
+ assert crate.preview.source.source.is_a?(ROCrate::PreviewGenerator)
693
+ refute_includes crate.preview.source.read, 'top secret'
694
+ end
695
+ end
696
+
697
+ test 'refuses metadata file that is a symlink to a location outside the crate' do
698
+ Dir.mktmpdir do |dir|
699
+ secret = ::File.join(dir, 'secret.txt')
700
+ ::File.write(secret, 'top secret')
701
+ crate_dir = ::File.join(dir, 'crate')
702
+ ::Dir.mkdir(crate_dir)
703
+ ::File.symlink(secret, ::File.join(crate_dir, 'ro-crate-metadata.json'))
704
+
705
+ e = check_exception(ROCrate::ReadException) do
706
+ ROCrate::Reader.read(crate_dir)
707
+ end
708
+ assert_include e.message, 'Metadata file is outside the crate'
709
+ refute_includes e.message, 'top secret'
710
+ end
711
+ end
712
+
713
+ test 'within? treats unrelatable paths as not within' do
714
+ base = Pathname.new(fixture_dir).expand_path
715
+ assert ROCrate::Reader.within?(base, base.join('info.txt'))
716
+ refute ROCrate::Reader.within?(base, base.join('..', 'x').expand_path)
717
+ # An absolute path cannot be related to a relative one ("different prefix" ArgumentError), as with paths on
718
+ # different Windows drives.
719
+ refute ROCrate::Reader.within?(base, Pathname.new('relative/path'))
720
+ end
721
+
722
+ test 'contained? treats unresolvable paths as not contained' do
723
+ assert ROCrate::Reader.contained?(fixture_dir, ::File.join(fixture_dir, 'info.txt'))
724
+ assert ROCrate::Reader.contained?(fixture_dir, ::File.join(fixture_dir, 'symlink'))
725
+ # realpath raises Errno::ENOENT for a path that does not exist
726
+ refute ROCrate::Reader.contained?(fixture_dir, ::File.join(fixture_dir, 'does-not-exist'))
727
+ refute ROCrate::Reader.contained?(::File.join(fixture_dir, 'does-not-exist'), ::File.join(fixture_dir, 'info.txt'))
728
+ end
729
+
730
+ private
731
+
732
+ def traversal_metadata(id)
733
+ {
734
+ '@context' => 'https://w3id.org/ro/crate/1.1/context',
735
+ '@graph' => [
736
+ { '@id' => 'ro-crate-metadata.json', '@type' => 'CreativeWork',
737
+ 'about' => { '@id' => './' }, 'conformsTo' => { '@id' => 'https://w3id.org/ro/crate/1.1' } },
738
+ { '@id' => './', '@type' => 'Dataset', 'hasPart' => [{ '@id' => id }], 'mainEntity' => { '@id' => id } },
739
+ { '@id' => id, '@type' => id.end_with?('/') ? 'Dataset' : 'File' }
740
+ ]
741
+ }.to_json
742
+ end
526
743
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ro-crate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Finn Bacall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-24 00:00:00.000000000 Z
11
+ date: 2026-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable