psych 5.4.0-java → 5.5.1-java

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: e62755a6f6b0b7f7eebe2651fbb2b1c81538e2bcbc1d010fe58574b7ede21b84
4
- data.tar.gz: 39bc8e482405345038582fb548d6164bdcea116424786c508be42f25753567e8
3
+ metadata.gz: 74ed1114e0b28695a5895c24a53b117485d5d379f8798742c0e201a3a11c481e
4
+ data.tar.gz: 779ab19a63e585c75d58b9b6d426e6466f3812a780b549714a80b1b10952fe50
5
5
  SHA512:
6
- metadata.gz: d2fcda255f004e0fa53379d5192acf1e77b0cb5f8af431495acda6291fd818a2089569d4e840aca9920ad3a70d92d2eb4af2fe4e98e957410a08f4c3b94ab990
7
- data.tar.gz: dc0f517aa6da6eaf7fe614229069d01c34edc3b29da6056e613a66f7cd33034599e1b3d761d4c1ea879d1c1e8ad92df5f5489ef7b242f6fff56723d6b6cc3259
6
+ metadata.gz: f5009e5bb754489b0a6783e8c769875f6f15a8d05414185929d776c875f09bebdcd0143f83378ee2f5ce1d384a5c04f93b610d2be726a01136274ecf3edac3b9
7
+ data.tar.gz: dc22703c2c2a7c6ad0d362133abd1894266e44742f20ae92907df2dabbc343fded431358d405dc3624ff77669563c03a8ce0fb0a0e1f9bae76ff3e09629a8e09
data/README.md CHANGED
@@ -23,6 +23,7 @@ Psych.dump("foo") # => "--- foo\n...\n"
23
23
  ## Dependencies
24
24
 
25
25
  * libyaml
26
+ * libfyaml (optional, only for the experimental `--enable-libfyaml` backend)
26
27
 
27
28
  ## Installation
28
29
 
@@ -57,6 +58,58 @@ gem 'psych'
57
58
 
58
59
  JRuby ships with a pure Java implementation of Psych.
59
60
 
61
+ ## Experimental libfyaml backend
62
+
63
+ Psych ships an experimental, opt-in backend built on
64
+ [libfyaml](https://github.com/pantoniou/libfyaml), a fully YAML 1.2 compliant
65
+ parser and emitter. It is compiled only when you explicitly pass
66
+ `--enable-libfyaml` at build time. Without the flag the default libyaml
67
+ backend is used and nothing changes.
68
+
69
+ ```bash
70
+ # libfyaml and pkg-config must be installed first, for example:
71
+ # apt-get install libfyaml-dev # Debian/Ubuntu
72
+ # brew install libfyaml # macOS
73
+ gem install psych -- --enable-libfyaml
74
+ ```
75
+
76
+ This backend is not supported on Windows.
77
+
78
+ Because libfyaml follows YAML 1.2, the YAML 1.1 booleans `yes`, `no`, `on`, and
79
+ `off` load as plain strings instead of `true`/`false` (only `true`/`false` are
80
+ booleans). This resolves the so-called "Norway problem", where the country
81
+ code `no` was parsed as `false`:
82
+
83
+ ```ruby
84
+ Psych.load("country: no") # => {"country" => "no"}
85
+ ```
86
+
87
+ You can check which backend is active:
88
+
89
+ ```ruby
90
+ Psych::BACKEND # => "libfyaml" (or "libyaml")
91
+ Psych.libfyaml_version # => "0.9.6"
92
+ ```
93
+
94
+ The backend is experimental. Its output is valid YAML but is formatted
95
+ differently from libyaml in places, and a few emitter edge cases are not yet
96
+ matched. The default libyaml backend remains the supported choice.
97
+
98
+ Two more differences are worth knowing. Scalars emitted with the default
99
+ (`ANY`) style may be quoted or laid out differently from libyaml, so
100
+ byte-for-byte output is not guaranteed to match. On a parse error,
101
+ `Psych::SyntaxError#problem` carries libfyaml's full diagnostic message and
102
+ `Psych::SyntaxError#context` is always `nil`, whereas libyaml splits the
103
+ description across `#problem` and `#context`.
104
+
105
+ This backend targets YAML 1.2 compliance, not speed. In a rough
106
+ single-machine benchmark that loads and dumps in-memory documents, parsing
107
+ was roughly on par with libyaml (sometimes faster on string-heavy input),
108
+ while emitting was about 1.7x to 1.9x slower. Your numbers will vary, but the
109
+ shape holds: libfyaml is competitive at parsing and slower at emitting. Use
110
+ this backend when you need YAML 1.2 semantics. If throughput is your priority,
111
+ keep using the default libyaml backend.
112
+
60
113
  ## Release
61
114
 
62
115
  We used the trusted publisher and [rubygems/release-gem](https://github.com/rubygems/release-gem) workflow.
data/ext/psych/depend CHANGED
@@ -12,6 +12,17 @@ distclean-so::
12
12
  -cd libyaml && $(MAKE) distclean
13
13
  -$(Q)$(RMDIRS) libyaml/* libyaml
14
14
 
15
- $(OBJS): $(HDRS) $(ruby_headers) \
16
- $(hdrdir)/ruby/encoding.h \
17
- $(hdrdir)/ruby/oniguruma.h
15
+ # AUTOGENERATED DEPENDENCIES START
16
+ psych.o psych_emitter.o psych_emitter_fy.o psych_parser.o psych_parser_fy.o psych_to_ruby.o psych_yaml_tree.o: psych.h
17
+ psych.o psych_emitter.o psych_emitter_fy.o psych_parser.o psych_parser_fy.o psych_to_ruby.o psych_yaml_tree.o: psych_emitter.h
18
+ psych.o psych_emitter.o psych_emitter_fy.o psych_parser.o psych_parser_fy.o psych_to_ruby.o psych_yaml_tree.o: psych_parser.h
19
+ psych.o psych_emitter.o psych_emitter_fy.o psych_parser.o psych_parser_fy.o psych_to_ruby.o psych_yaml_tree.o: psych_to_ruby.h
20
+ psych.o psych_emitter.o psych_emitter_fy.o psych_parser.o psych_parser_fy.o psych_to_ruby.o psych_yaml_tree.o: psych_yaml_tree.h
21
+ psych.o: psych.c
22
+ psych_emitter.o: psych_emitter.c
23
+ psych_emitter_fy.o: psych_emitter_fy.c
24
+ psych_parser.o: psych_parser.c
25
+ psych_parser_fy.o: psych_parser_fy.c
26
+ psych_to_ruby.o: psych_to_ruby.c
27
+ psych_yaml_tree.o: psych_yaml_tree.c
28
+ # AUTOGENERATED DEPENDENCIES END
data/ext/psych/extconf.rb CHANGED
@@ -2,6 +2,28 @@
2
2
  # frozen_string_literal: true
3
3
  require 'mkmf'
4
4
 
5
+ # Experimental, opt-in libfyaml backend. Only used when psych is built with
6
+ # --enable-libfyaml. Without the flag nothing below changes and the default
7
+ # libyaml backend is built exactly as before.
8
+ if enable_config("libfyaml", false)
9
+ if $mswin or $mingw or $cygwin
10
+ abort "The libfyaml backend (--enable-libfyaml) is not supported on Windows"
11
+ end
12
+ unless pkg_config('libfyaml')
13
+ abort "libfyaml was requested with --enable-libfyaml but was not found via pkg-config"
14
+ end
15
+ # libfyaml 0.8 and earlier crash psych's emitter, so require a known-good
16
+ # version rather than building something that segfaults at runtime.
17
+ pkgconfig = ENV["PKG_CONFIG"] || "pkg-config"
18
+ unless system(pkgconfig, "--atleast-version=0.9", "libfyaml")
19
+ abort "The libfyaml backend requires libfyaml 0.9 or newer"
20
+ end
21
+ $defs << "-DPSYCH_USE_LIBFYAML"
22
+
23
+ create_makefile 'psych'
24
+ return
25
+ end
26
+
5
27
  if $mswin or $mingw or $cygwin
6
28
  $CPPFLAGS << " -DYAML_DECLARE_STATIC"
7
29
  end
data/ext/psych/psych.c CHANGED
@@ -2,14 +2,25 @@
2
2
 
3
3
  /* call-seq: Psych.libyaml_version
4
4
  *
5
- * Returns the version of libyaml being used
5
+ * Returns the version of the underlying YAML library as a three-element
6
+ * array. This is libyaml by default. On the experimental libfyaml backend,
7
+ * where libyaml is not linked, it reports the libfyaml version instead.
6
8
  */
7
9
  static VALUE libyaml_version(VALUE module)
8
10
  {
9
11
  int major, minor, patch;
10
12
  VALUE list[3];
11
13
 
14
+ #ifdef PSYCH_USE_LIBFYAML
15
+ /* Experimental libfyaml backend: there is no libyaml linked in. Report
16
+ * the libfyaml version so callers still receive a 3-element version. */
17
+ const struct fy_version *v = fy_version_default();
18
+ major = v ? v->major : 0;
19
+ minor = v ? v->minor : 0;
20
+ patch = 0;
21
+ #else
12
22
  yaml_get_version(&major, &minor, &patch);
23
+ #endif
13
24
 
14
25
  list[0] = INT2NUM(major);
15
26
  list[1] = INT2NUM(minor);
@@ -18,6 +29,20 @@ static VALUE libyaml_version(VALUE module)
18
29
  return rb_ary_new4((long)3, list);
19
30
  }
20
31
 
32
+ #ifdef PSYCH_USE_LIBFYAML
33
+ /* call-seq: Psych.libfyaml_version
34
+ *
35
+ * Returns the libfyaml version string. This method is only defined when
36
+ * psych was built with the experimental libfyaml backend
37
+ * (+--enable-libfyaml+).
38
+ */
39
+ static VALUE libfyaml_version(VALUE module)
40
+ {
41
+ const char *v = fy_library_version();
42
+ return v ? rb_usascii_str_new2(v) : Qnil;
43
+ }
44
+ #endif
45
+
21
46
  VALUE mPsych;
22
47
 
23
48
  void Init_psych(void)
@@ -29,6 +54,13 @@ void Init_psych(void)
29
54
 
30
55
  rb_define_singleton_method(mPsych, "libyaml_version", libyaml_version, 0);
31
56
 
57
+ #ifdef PSYCH_USE_LIBFYAML
58
+ rb_define_singleton_method(mPsych, "libfyaml_version", libfyaml_version, 0);
59
+ rb_define_const(mPsych, "BACKEND", rb_usascii_str_new2("libfyaml"));
60
+ #else
61
+ rb_define_const(mPsych, "BACKEND", rb_usascii_str_new2("libyaml"));
62
+ #endif
63
+
32
64
  Init_psych_parser();
33
65
  Init_psych_emitter();
34
66
  Init_psych_to_ruby();
data/ext/psych/psych.h CHANGED
@@ -4,7 +4,15 @@
4
4
  #include <ruby.h>
5
5
  #include <ruby/encoding.h>
6
6
 
7
+ #ifndef HAVE_RB_LEN_T
8
+ typedef long rb_len_t;
9
+ #endif
10
+
11
+ #ifdef PSYCH_USE_LIBFYAML
12
+ #include <libfyaml.h>
13
+ #else
7
14
  #include <yaml.h>
15
+ #endif
8
16
 
9
17
  #include <psych_parser.h>
10
18
  #include <psych_emitter.h>
@@ -1,5 +1,7 @@
1
1
  #include <psych.h>
2
2
 
3
+ #ifndef PSYCH_USE_LIBFYAML
4
+
3
5
  #if !defined(RARRAY_CONST_PTR)
4
6
  #define RARRAY_CONST_PTR(s) (const VALUE *)RARRAY_PTR(s)
5
7
  #endif
@@ -23,7 +25,7 @@ static void emit(yaml_emitter_t * emitter, yaml_event_t * event)
23
25
  static int writer(void *ctx, unsigned char *buffer, size_t size)
24
26
  {
25
27
  VALUE self = (VALUE)ctx, io = rb_attr_get(self, id_io);
26
- VALUE str = rb_enc_str_new((const char *)buffer, (long)size, rb_utf8_encoding());
28
+ VALUE str = rb_enc_str_new((const char *)buffer, (rb_len_t)size, rb_utf8_encoding());
27
29
  VALUE wrote = rb_funcall(io, id_write, 1, str);
28
30
  return (int)NUM2INT(wrote);
29
31
  }
@@ -170,8 +172,8 @@ static VALUE start_document_try(VALUE d)
170
172
  }
171
173
 
172
174
  if(RTEST(tags)) {
173
- long i = 0;
174
- long len;
175
+ rb_len_t i = 0;
176
+ rb_len_t len;
175
177
  rb_encoding * encoding = rb_utf8_encoding();
176
178
 
177
179
  Check_Type(tags, T_ARRAY);
@@ -587,3 +589,5 @@ void Init_psych_emitter(void)
587
589
  id_indentation = rb_intern("indentation");
588
590
  id_canonical = rb_intern("canonical");
589
591
  }
592
+
593
+ #endif /* PSYCH_USE_LIBFYAML */
@@ -1,9 +1,10 @@
1
1
  #include <psych.h>
2
2
 
3
+ #ifndef PSYCH_USE_LIBFYAML
4
+
3
5
  VALUE cPsychParser;
4
6
 
5
7
  static ID id_read;
6
- static ID id_path;
7
8
  static ID id_empty;
8
9
  static ID id_start_stream;
9
10
  static ID id_end_stream;
@@ -51,19 +52,28 @@ static int io_reader(void * data, unsigned char *buf, size_t size, size_t *read)
51
52
  return 1;
52
53
  }
53
54
 
55
+ /* The parser calls back into Ruby for every event, so a handler can call
56
+ * Psych::Parser#parse again on the same object. parse() reinitialises the
57
+ * parser it is handed, which would pull the input out from under the loop
58
+ * still driving it, so keep a flag to reject a reentrant call. */
59
+ typedef struct {
60
+ yaml_parser_t yaml_parser;
61
+ int parsing;
62
+ } psych_parser_t;
63
+
54
64
  static void dealloc(void * ptr)
55
65
  {
56
- yaml_parser_t * parser;
66
+ psych_parser_t * parser;
57
67
 
58
- parser = (yaml_parser_t *)ptr;
59
- yaml_parser_delete(parser);
68
+ parser = (psych_parser_t *)ptr;
69
+ yaml_parser_delete(&parser->yaml_parser);
60
70
  xfree(parser);
61
71
  }
62
72
 
63
73
  #if 0
64
74
  static size_t memsize(const void *ptr)
65
75
  {
66
- const yaml_parser_t *parser = ptr;
76
+ const psych_parser_t *parser = ptr;
67
77
  /* TODO: calculate parser's size */
68
78
  return 0;
69
79
  }
@@ -80,10 +90,10 @@ static const rb_data_type_t psych_parser_type = {
80
90
 
81
91
  static VALUE allocate(VALUE klass)
82
92
  {
83
- yaml_parser_t * parser;
84
- VALUE obj = TypedData_Make_Struct(klass, yaml_parser_t, &psych_parser_type, parser);
93
+ psych_parser_t * parser;
94
+ VALUE obj = TypedData_Make_Struct(klass, psych_parser_t, &psych_parser_type, parser);
85
95
 
86
- yaml_parser_initialize(parser);
96
+ yaml_parser_initialize(&parser->yaml_parser);
87
97
 
88
98
  return obj;
89
99
  }
@@ -256,9 +266,22 @@ static VALUE protected_event_location(VALUE pointer)
256
266
  return rb_funcall3(args[0], id_event_location, 4, args + 1);
257
267
  }
258
268
 
259
- static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
269
+ struct parse_args {
270
+ psych_parser_t * psych_parser;
271
+ VALUE self;
272
+ VALUE handler;
273
+ VALUE yaml;
274
+ VALUE path;
275
+ };
276
+
277
+ static VALUE parse_body(VALUE ptr)
260
278
  {
261
- yaml_parser_t * parser;
279
+ struct parse_args * pargs = (struct parse_args *)ptr;
280
+ yaml_parser_t * parser = &pargs->psych_parser->yaml_parser;
281
+ VALUE self = pargs->self;
282
+ VALUE handler = pargs->handler;
283
+ VALUE yaml = pargs->yaml;
284
+ VALUE path = pargs->path;
262
285
  yaml_event_t event;
263
286
  int done = 0;
264
287
  int state = 0;
@@ -266,8 +289,6 @@ static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
266
289
  int encoding = rb_utf8_encindex();
267
290
  rb_encoding * internal_enc = rb_default_internal_encoding();
268
291
 
269
- TypedData_Get_Struct(self, yaml_parser_t, &psych_parser_type, parser);
270
-
271
292
  yaml_parser_delete(parser);
272
293
  yaml_parser_initialize(parser);
273
294
 
@@ -311,6 +332,10 @@ static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
311
332
  event_args[3] = end_line;
312
333
  event_args[4] = end_column;
313
334
  rb_protect(protected_event_location, (VALUE)event_args, &state);
335
+ if (state) {
336
+ yaml_event_delete(&event);
337
+ rb_jump_tag(state);
338
+ }
314
339
 
315
340
  switch(event.type) {
316
341
  case YAML_STREAM_START_EVENT:
@@ -394,7 +419,7 @@ static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
394
419
  VALUE plain_implicit, quoted_implicit, style;
395
420
  VALUE val = rb_str_new(
396
421
  (const char *)event.data.scalar.value,
397
- (long)event.data.scalar.length
422
+ (rb_len_t)event.data.scalar.length
398
423
  );
399
424
 
400
425
  PSYCH_TRANSCODE(val, encoding, internal_enc);
@@ -495,7 +520,11 @@ static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
495
520
  rb_protect(protected_end_mapping, handler, &state);
496
521
  break;
497
522
  case YAML_NO_EVENT:
523
+ /* Once libyaml has produced the stream end, every later call
524
+ * succeeds with a zeroed event and YAML_STREAM_END_EVENT can no
525
+ * longer be reached. Stop rather than loop forever. */
498
526
  rb_protect(protected_empty, handler, &state);
527
+ done = 1;
499
528
  break;
500
529
  case YAML_STREAM_END_EVENT:
501
530
  rb_protect(protected_end_stream, handler, &state);
@@ -509,6 +538,37 @@ static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
509
538
  return self;
510
539
  }
511
540
 
541
+ static VALUE parse_ensure(VALUE ptr)
542
+ {
543
+ psych_parser_t * parser = (psych_parser_t *)ptr;
544
+
545
+ parser->parsing = 0;
546
+
547
+ return Qnil;
548
+ }
549
+
550
+ static VALUE parse(VALUE self, VALUE handler, VALUE yaml, VALUE path)
551
+ {
552
+ psych_parser_t * parser;
553
+ struct parse_args pargs;
554
+
555
+ TypedData_Get_Struct(self, psych_parser_t, &psych_parser_type, parser);
556
+
557
+ if (parser->parsing) {
558
+ rb_raise(rb_const_get(mPsych, rb_intern("Exception")),
559
+ "parser is already parsing, it cannot be reused from a handler callback");
560
+ }
561
+ parser->parsing = 1;
562
+
563
+ pargs.psych_parser = parser;
564
+ pargs.self = self;
565
+ pargs.handler = handler;
566
+ pargs.yaml = yaml;
567
+ pargs.path = path;
568
+
569
+ return rb_ensure(parse_body, (VALUE)&pargs, parse_ensure, (VALUE)parser);
570
+ }
571
+
512
572
  /*
513
573
  * call-seq:
514
574
  * parser.mark # => #<Psych::Parser::Mark>
@@ -520,13 +580,13 @@ static VALUE mark(VALUE self)
520
580
  {
521
581
  VALUE mark_klass;
522
582
  VALUE args[3];
523
- yaml_parser_t * parser;
583
+ psych_parser_t * parser;
524
584
 
525
- TypedData_Get_Struct(self, yaml_parser_t, &psych_parser_type, parser);
585
+ TypedData_Get_Struct(self, psych_parser_t, &psych_parser_type, parser);
526
586
  mark_klass = rb_const_get_at(cPsychParser, rb_intern("Mark"));
527
- args[0] = SIZET2NUM(parser->mark.index);
528
- args[1] = SIZET2NUM(parser->mark.line);
529
- args[2] = SIZET2NUM(parser->mark.column);
587
+ args[0] = SIZET2NUM(parser->yaml_parser.mark.index);
588
+ args[1] = SIZET2NUM(parser->yaml_parser.mark.line);
589
+ args[2] = SIZET2NUM(parser->yaml_parser.mark.column);
530
590
 
531
591
  return rb_class_new_instance(3, args, mark_klass);
532
592
  }
@@ -559,7 +619,6 @@ void Init_psych_parser(void)
559
619
  rb_define_method(cPsychParser, "mark", mark, 0);
560
620
 
561
621
  id_read = rb_intern("read");
562
- id_path = rb_intern("path");
563
622
  id_empty = rb_intern("empty");
564
623
  id_start_stream = rb_intern("start_stream");
565
624
  id_end_stream = rb_intern("end_stream");
@@ -573,3 +632,5 @@ void Init_psych_parser(void)
573
632
  id_end_mapping = rb_intern("end_mapping");
574
633
  id_event_location = rb_intern("event_location");
575
634
  }
635
+
636
+ #endif /* PSYCH_USE_LIBFYAML */
@@ -9,6 +9,7 @@ module Psych
9
9
  DATA = 'Data' unless RUBY_VERSION < "3.2"
10
10
  DATE = 'Date'
11
11
  DATE_TIME = 'DateTime'
12
+ ENCODING = 'Encoding'
12
13
  EXCEPTION = 'Exception'
13
14
  OBJECT = 'Object'
14
15
  PSYCH_OMAP = 'Psych::Omap'
@@ -34,6 +35,11 @@ module Psych
34
35
  sym.to_sym
35
36
  end
36
37
 
38
+ # Raises if instance variables may not be restored on +obj+. The
39
+ # unrestricted loader allows instance variables on any object.
40
+ def check_ivars obj
41
+ end
42
+
37
43
  constants.each do |const|
38
44
  konst = const_get const
39
45
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
@@ -75,6 +81,12 @@ module Psych
75
81
  }.compact].freeze
76
82
 
77
83
  class Restricted < ClassLoader
84
+ # Core classes whose instances must not have instance variables
85
+ # restored on them unless the class is explicitly permitted. Exact
86
+ # class match only, so permitted subclasses (for example a String
87
+ # subclass with its own ivars) still load.
88
+ BUILTIN_CLASSES = [::String, ::Hash].freeze
89
+
78
90
  def initialize classes, symbols
79
91
  @classes = classes
80
92
  @symbols = symbols
@@ -91,6 +103,13 @@ module Psych
91
103
  end
92
104
  end
93
105
 
106
+ def check_ivars obj
107
+ klass = obj.class
108
+ if BUILTIN_CLASSES.include?(klass) && !@classes.include?(klass.name)
109
+ raise DisallowedInstanceVariables.new(klass.name)
110
+ end
111
+ end
112
+
94
113
  private
95
114
 
96
115
  def find klassname
@@ -25,4 +25,10 @@ module Psych
25
25
  super "Tried to #{action} unspecified class: #{klass_name}"
26
26
  end
27
27
  end
28
+
29
+ class DisallowedInstanceVariables < Exception
30
+ def initialize klass_name
31
+ super "Tried to set instance variables on unspecified class: #{klass_name}"
32
+ end
33
+ end
28
34
  end
data/lib/psych/parser.rb CHANGED
@@ -59,7 +59,46 @@ module Psych
59
59
  # See Psych::Parser and Psych::Parser#handler
60
60
 
61
61
  def parse yaml, path = yaml.respond_to?(:path) ? yaml.path : "<unknown>"
62
- _native_parse @handler, yaml, path
62
+ _native_parse @handler, strip_bom(yaml), path
63
+ end
64
+
65
+ private
66
+
67
+ BOM = {
68
+ Encoding::UTF_8 => "\u{FEFF}".freeze,
69
+ Encoding::UTF_16LE => "\u{FEFF}".encode(Encoding::UTF_16LE).freeze,
70
+ Encoding::UTF_16BE => "\u{FEFF}".encode(Encoding::UTF_16BE).freeze,
71
+ Encoding::UTF_32LE => "\u{FEFF}".encode(Encoding::UTF_32LE).freeze,
72
+ Encoding::UTF_32BE => "\u{FEFF}".encode(Encoding::UTF_32BE).freeze,
73
+ }.freeze
74
+ private_constant :BOM
75
+
76
+ # libyaml only skips a leading byte order mark when it detects the stream
77
+ # encoding by itself. Psych passes the encoding explicitly whenever it is
78
+ # known, and on that path libyaml counts the BOM as a first-line character,
79
+ # which shifts the column of every token on the first line and silently
80
+ # terminates a block mapping at the second line [Bug #13615].
81
+ def strip_bom yaml
82
+ if String === yaml
83
+ bom = BOM[yaml.encoding]
84
+ # delete_prefix copies even when there is no prefix, so keep the guard.
85
+ return yaml.delete_prefix(bom) if bom && yaml.start_with?(bom)
86
+ elsif yaml.respond_to?(:read) && yaml.respond_to?(:external_encoding) &&
87
+ yaml.respond_to?(:pos) && yaml.respond_to?(:seek)
88
+ bom = BOM[yaml.external_encoding]
89
+ skip_io_bom yaml, bom.b if bom
90
+ end
91
+ yaml
92
+ end
93
+
94
+ def skip_io_bom io, bom
95
+ begin
96
+ pos = io.pos
97
+ rescue SystemCallError, IOError
98
+ return # Not seekable; nothing has been consumed yet.
99
+ end
100
+ head = io.read(bom.bytesize)
101
+ io.seek(pos, IO::SEEK_SET) if head && head.b != bom
63
102
  end
64
103
  end
65
104
  end
@@ -24,6 +24,18 @@ module Psych
24
24
  |[-+]?(?:0|[1-9](?:[0-9]|,[0-9]|_[0-9])*) (?# base 10)
25
25
  |[-+]?0x[_,]*[0-9a-fA-F][0-9a-fA-F_,]* (?# base 16))$/x
26
26
 
27
+ # YAML 1.1 treats yes/no/on/off as booleans in addition to true/false,
28
+ # while YAML 1.2's core schema only recognizes true/false. The default
29
+ # libyaml backend keeps the 1.1 set for backward compatibility; the
30
+ # experimental libfyaml backend follows 1.2.
31
+ if defined?(Psych::BACKEND) && Psych::BACKEND == 'libfyaml'
32
+ BOOLEAN_TRUE = /^true$/i
33
+ BOOLEAN_FALSE = /^false$/i
34
+ else
35
+ BOOLEAN_TRUE = /^(yes|true|on)$/i
36
+ BOOLEAN_FALSE = /^(no|false|off)$/i
37
+ end
38
+
27
39
  attr_reader :class_loader
28
40
 
29
41
  # Create a new scanner
@@ -48,9 +60,9 @@ module Psych
48
60
  string
49
61
  elsif string == '~' || string.match?(/^null$/i)
50
62
  nil
51
- elsif string.match?(/^(yes|true|on)$/i)
63
+ elsif string.match?(BOOLEAN_TRUE)
52
64
  true
53
- elsif string.match?(/^(no|false|off)$/i)
65
+ elsif string.match?(BOOLEAN_FALSE)
54
66
  false
55
67
  else
56
68
  string
@@ -115,7 +127,7 @@ module Psych
115
127
  def parse_time string
116
128
  klass = class_loader.load 'Time'
117
129
 
118
- date, time = *(string.split(/[ tT]/, 2))
130
+ date, time = *(string.split(/[Tt]|\s+/, 2))
119
131
  (yy, m, dd) = date.match(/^(-?\d{4})-(\d{1,2})-(\d{1,2})/).captures.map { |x| x.to_i }
120
132
  md = time.match(/(\d+:\d+:\d+)(?:\.(\d*))?\s*(Z|[-+]\d+(:\d\d)?)?/)
121
133
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Psych
4
4
  # The version of Psych you are using
5
- VERSION = '5.4.0'
5
+ VERSION = '5.5.1'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
8
  DEFAULT_SNAKEYAML_VERSION = '2.10'.freeze
@@ -48,7 +48,9 @@ module Psych
48
48
  end
49
49
  end
50
50
 
51
- result = deduplicate(result).freeze if @freeze
51
+ # Classes and modules named by !ruby/class and !ruby/module are shared
52
+ # by the whole process, not created by this load, so leave them alone.
53
+ result = deduplicate(result).freeze if @freeze && !(Module === result)
52
54
  result
53
55
  end
54
56
 
@@ -72,7 +74,7 @@ module Psych
72
74
  when '!binary', 'tag:yaml.org,2002:binary'
73
75
  o.value.unpack('m').first
74
76
  when /^!(?:str|ruby\/string)(?::(.*))?$/, 'tag:yaml.org,2002:str'
75
- klass = resolve_class($1)
77
+ klass = resolve_subclass($1, String)
76
78
  if klass
77
79
  klass.allocate.replace o.value
78
80
  else
@@ -87,6 +89,7 @@ module Psych
87
89
  DateTime.civil(*t.to_a[0, 6].reverse, Rational(t.utc_offset, 86400)) +
88
90
  (t.subsec/86400)
89
91
  when '!ruby/encoding'
92
+ class_loader.encoding
90
93
  ::Encoding.find o.value
91
94
  when "!ruby/object:Complex"
92
95
  class_loader.complex
@@ -156,7 +159,7 @@ module Psych
156
159
  }
157
160
  map
158
161
  when /^!(?:seq|ruby\/array):(.*)$/
159
- klass = resolve_class($1)
162
+ klass = resolve_subclass($1, Array)
160
163
  list = register(o, klass.allocate)
161
164
  o.children.each { |c| list.push accept c }
162
165
  list
@@ -246,7 +249,7 @@ module Psych
246
249
  end
247
250
 
248
251
  when /^!(?:str|ruby\/string)(?::(.*))?$/, 'tag:yaml.org,2002:str'
249
- klass = resolve_class($1)
252
+ klass = resolve_subclass($1, String)
250
253
  members = {}
251
254
  string = nil
252
255
 
@@ -258,6 +261,12 @@ module Psych
258
261
  if klass
259
262
  string = klass.allocate.replace value
260
263
  else
264
+ # The value node can carry any tag, for example !ruby/class.
265
+ # Without this check the instance variables below would be
266
+ # set on whatever shared object that tag resolved to.
267
+ unless value.is_a?(String)
268
+ raise TypeError, "Invalid tag: expected a String for str, got #{value.class}"
269
+ end
261
270
  string = value
262
271
  end
263
272
  register(o, string)
@@ -267,13 +276,15 @@ module Psych
267
276
  end
268
277
  init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
269
278
  when /^!ruby\/array:(.*)$/
270
- klass = resolve_class($1)
279
+ klass = resolve_subclass($1, Array)
271
280
  list = register(o, klass.allocate)
272
281
 
273
282
  members = Hash[o.children.map { |c| accept c }.each_slice(2).to_a]
274
283
  list.replace members['internal']
275
284
 
276
- members['ivars'].each do |ivar, v|
285
+ ivars = members['ivars']
286
+ class_loader.check_ivars(list) unless ivars.empty?
287
+ ivars.each do |ivar, v|
277
288
  list.instance_variable_set ivar, v
278
289
  end
279
290
  list
@@ -301,13 +312,14 @@ module Psych
301
312
  set
302
313
 
303
314
  when /^!ruby\/hash-with-ivars(?::(.*))?$/
304
- hash = $1 ? resolve_class($1).allocate : {}
315
+ hash = $1 ? resolve_subclass($1, Hash).allocate : {}
305
316
  register o, hash
306
317
  o.children.each_slice(2) do |key, value|
307
318
  case key.value
308
319
  when 'elements'
309
320
  revive_hash hash, value
310
321
  when 'ivars'
322
+ class_loader.check_ivars(hash) unless value.children.empty?
311
323
  value.children.each_slice(2) do |k,v|
312
324
  hash.instance_variable_set accept(k), accept(v)
313
325
  end
@@ -316,7 +328,7 @@ module Psych
316
328
  hash
317
329
 
318
330
  when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/
319
- revive_hash register(o, resolve_class($1).allocate), o
331
+ revive_hash register(o, resolve_subclass($1, Hash).allocate), o
320
332
 
321
333
  when '!omap', 'tag:yaml.org,2002:omap'
322
334
  map = register(o, class_loader.psych_omap.new)
@@ -444,9 +456,6 @@ module Psych
444
456
  end
445
457
  end
446
458
 
447
- def merge_key hash, key, val
448
- end
449
-
450
459
  def revive klass, node
451
460
  s = register(node, klass.allocate)
452
461
  init_with(s, revive_hash({}, node, true), node)
@@ -459,6 +468,7 @@ module Psych
459
468
  if o.respond_to?(:init_with)
460
469
  o.init_with c
461
470
  else
471
+ class_loader.check_ivars(o) unless h.empty?
462
472
  h.each { |k,v| o.instance_variable_set(:"@#{k}", v) }
463
473
  end
464
474
  o
@@ -468,6 +478,19 @@ module Psych
468
478
  def resolve_class klassname
469
479
  class_loader.load klassname
470
480
  end
481
+
482
+ # Resolve +klassname+ and ensure it is +parent+ or one of its
483
+ # subclasses. Tags such as !ruby/hash-with-ivars are only ever emitted
484
+ # for subclasses of a specific core class; without this check a crafted
485
+ # document could name an unrelated (but permitted) class and have its
486
+ # state populated directly, bypassing the class's own init_with.
487
+ def resolve_subclass klassname, parent
488
+ klass = resolve_class(klassname)
489
+ if klass && !(klass <= parent)
490
+ raise ArgumentError, "Invalid tag: expected a subclass of #{parent}, got #{klass}"
491
+ end
492
+ klass
493
+ end
471
494
  end
472
495
 
473
496
  class NoAliasRuby < ToRuby
@@ -496,9 +496,6 @@ module Psych
496
496
  end
497
497
  end
498
498
 
499
- def dump_list o
500
- end
501
-
502
499
  def dump_exception o, msg
503
500
  tag = ['!ruby/exception', o.class.name].join ':'
504
501
 
data/lib/psych.jar CHANGED
Binary file
data/lib/psych.rb CHANGED
@@ -296,6 +296,17 @@ module Psych
296
296
  #
297
297
  # Now the Date class can be loaded in addition to the classes listed above.
298
298
  #
299
+ # Instance variables are not restored on String or Hash objects unless
300
+ # String or Hash is listed in +permitted_classes+; otherwise a
301
+ # Psych::DisallowedInstanceVariables exception is raised.
302
+ #
303
+ # Permitting a class lets the YAML set arbitrary instance variables on
304
+ # instances of that class. Each new instance variable name creates a
305
+ # symbol that is never garbage collected, so untrusted input loaded with
306
+ # +permitted_classes+ can grow memory without bound. Validate untrusted
307
+ # YAML against a schema before loading it, for example with the
308
+ # yaml-schema gem (https://github.com/tenderlove/yaml-schema).
309
+ #
299
310
  # Aliases can be explicitly allowed by changing the +aliases+ keyword argument.
300
311
  # For example:
301
312
  #
metadata CHANGED
@@ -1,139 +1,145 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psych
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.0
4
+ version: 5.5.1
5
5
  platform: java
6
6
  authors:
7
- - Aaron Patterson
8
- - SHIBATA Hiroshi
9
- - Charles Oliver Nutter
7
+ - Aaron Patterson
8
+ - SHIBATA Hiroshi
9
+ - Charles Oliver Nutter
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 1980-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: jar-dependencies
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ">="
19
- - !ruby/object:Gem::Version
20
- version: 0.1.7
21
- type: :runtime
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- version: 0.1.7
28
- - !ruby/object:Gem::Dependency
29
- name: date
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :runtime
36
- prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '0'
14
+ - !ruby/object:Gem::Dependency
15
+ name: jar-dependencies
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ -
19
+ - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ -
27
+ - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.1.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: date
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ -
35
+ - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ -
43
+ - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
42
46
  description: |
43
47
  Psych is a YAML parser and emitter. Psych leverages libyaml[https://pyyaml.org/wiki/LibYAML]
44
48
  for its YAML parsing and emitting capabilities. In addition to wrapping libyaml,
45
49
  Psych also knows how to serialize and de-serialize most Ruby objects to and from the YAML format.
46
50
  email:
47
- - aaron@tenderlovemaking.com
48
- - hsbt@ruby-lang.org
49
- - headius@headius.com
51
+ - aaron@tenderlovemaking.com
52
+ - hsbt@ruby-lang.org
53
+ - headius@headius.com
50
54
  executables: []
51
55
  extensions: []
52
56
  extra_rdoc_files:
53
- - README.md
57
+ - README.md
54
58
  files:
55
- - CONTRIBUTING.md
56
- - LICENSE
57
- - README.md
58
- - ext/java/org/jruby/ext/psych/PsychEmitter.java
59
- - ext/java/org/jruby/ext/psych/PsychLibrary.java
60
- - ext/java/org/jruby/ext/psych/PsychParser.java
61
- - ext/java/org/jruby/ext/psych/PsychToRuby.java
62
- - ext/psych/depend
63
- - ext/psych/extconf.rb
64
- - ext/psych/psych.c
65
- - ext/psych/psych.h
66
- - ext/psych/psych_emitter.c
67
- - ext/psych/psych_emitter.h
68
- - ext/psych/psych_parser.c
69
- - ext/psych/psych_parser.h
70
- - ext/psych/psych_to_ruby.c
71
- - ext/psych/psych_to_ruby.h
72
- - ext/psych/psych_yaml_tree.c
73
- - ext/psych/psych_yaml_tree.h
74
- - lib/psych.jar
75
- - lib/psych.rb
76
- - lib/psych/class_loader.rb
77
- - lib/psych/coder.rb
78
- - lib/psych/core_ext.rb
79
- - lib/psych/exception.rb
80
- - lib/psych/handler.rb
81
- - lib/psych/handlers/document_stream.rb
82
- - lib/psych/handlers/recorder.rb
83
- - lib/psych/json/ruby_events.rb
84
- - lib/psych/json/stream.rb
85
- - lib/psych/json/tree_builder.rb
86
- - lib/psych/json/yaml_events.rb
87
- - lib/psych/nodes.rb
88
- - lib/psych/nodes/alias.rb
89
- - lib/psych/nodes/document.rb
90
- - lib/psych/nodes/mapping.rb
91
- - lib/psych/nodes/node.rb
92
- - lib/psych/nodes/scalar.rb
93
- - lib/psych/nodes/sequence.rb
94
- - lib/psych/nodes/stream.rb
95
- - lib/psych/omap.rb
96
- - lib/psych/parser.rb
97
- - lib/psych/scalar_scanner.rb
98
- - lib/psych/set.rb
99
- - lib/psych/stream.rb
100
- - lib/psych/streaming.rb
101
- - lib/psych/syntax_error.rb
102
- - lib/psych/tree_builder.rb
103
- - lib/psych/versions.rb
104
- - lib/psych/visitors.rb
105
- - lib/psych/visitors/depth_first.rb
106
- - lib/psych/visitors/emitter.rb
107
- - lib/psych/visitors/json_tree.rb
108
- - lib/psych/visitors/to_ruby.rb
109
- - lib/psych/visitors/visitor.rb
110
- - lib/psych/visitors/yaml_tree.rb
111
- - lib/psych/y.rb
112
- - lib/psych_jars.rb
113
- homepage: https://github.com/ruby/psych
59
+ - CONTRIBUTING.md
60
+ - LICENSE
61
+ - README.md
62
+ - ext/java/org/jruby/ext/psych/PsychEmitter.java
63
+ - ext/java/org/jruby/ext/psych/PsychLibrary.java
64
+ - ext/java/org/jruby/ext/psych/PsychParser.java
65
+ - ext/java/org/jruby/ext/psych/PsychToRuby.java
66
+ - ext/psych/depend
67
+ - ext/psych/extconf.rb
68
+ - ext/psych/psych.c
69
+ - ext/psych/psych.h
70
+ - ext/psych/psych_emitter.c
71
+ - ext/psych/psych_emitter.h
72
+ - ext/psych/psych_parser.c
73
+ - ext/psych/psych_parser.h
74
+ - ext/psych/psych_to_ruby.c
75
+ - ext/psych/psych_to_ruby.h
76
+ - ext/psych/psych_yaml_tree.c
77
+ - ext/psych/psych_yaml_tree.h
78
+ - lib/psych.jar
79
+ - lib/psych.rb
80
+ - lib/psych/class_loader.rb
81
+ - lib/psych/coder.rb
82
+ - lib/psych/core_ext.rb
83
+ - lib/psych/exception.rb
84
+ - lib/psych/handler.rb
85
+ - lib/psych/handlers/document_stream.rb
86
+ - lib/psych/handlers/recorder.rb
87
+ - lib/psych/json/ruby_events.rb
88
+ - lib/psych/json/stream.rb
89
+ - lib/psych/json/tree_builder.rb
90
+ - lib/psych/json/yaml_events.rb
91
+ - lib/psych/nodes.rb
92
+ - lib/psych/nodes/alias.rb
93
+ - lib/psych/nodes/document.rb
94
+ - lib/psych/nodes/mapping.rb
95
+ - lib/psych/nodes/node.rb
96
+ - lib/psych/nodes/scalar.rb
97
+ - lib/psych/nodes/sequence.rb
98
+ - lib/psych/nodes/stream.rb
99
+ - lib/psych/omap.rb
100
+ - lib/psych/parser.rb
101
+ - lib/psych/scalar_scanner.rb
102
+ - lib/psych/set.rb
103
+ - lib/psych/stream.rb
104
+ - lib/psych/streaming.rb
105
+ - lib/psych/syntax_error.rb
106
+ - lib/psych/tree_builder.rb
107
+ - lib/psych/versions.rb
108
+ - lib/psych/visitors.rb
109
+ - lib/psych/visitors/depth_first.rb
110
+ - lib/psych/visitors/emitter.rb
111
+ - lib/psych/visitors/json_tree.rb
112
+ - lib/psych/visitors/to_ruby.rb
113
+ - lib/psych/visitors/visitor.rb
114
+ - lib/psych/visitors/yaml_tree.rb
115
+ - lib/psych/y.rb
116
+ - lib/psych_jars.rb
117
+ homepage: "https://github.com/ruby/psych"
114
118
  licenses:
115
- - MIT
119
+ - MIT
116
120
  metadata:
117
121
  msys2_mingw_dependencies: libyaml
118
- changelog_uri: https://github.com/ruby/psych/releases
122
+ changelog_uri: "https://github.com/ruby/psych/releases"
119
123
  rdoc_options:
120
- - "--main"
121
- - README.md
124
+ - "--main"
125
+ - README.md
122
126
  require_paths:
123
- - lib
127
+ - lib
124
128
  required_ruby_version: !ruby/object:Gem::Requirement
125
129
  requirements:
126
- - - ">="
127
- - !ruby/object:Gem::Version
128
- version: 2.5.0
130
+ -
131
+ - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: 2.5.0
129
134
  required_rubygems_version: !ruby/object:Gem::Requirement
130
135
  requirements:
131
- - - ">="
132
- - !ruby/object:Gem::Version
133
- version: '0'
136
+ -
137
+ - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: "0"
134
140
  requirements:
135
- - jar org.snakeyaml:snakeyaml-engine, 2.10
136
- rubygems_version: 4.0.3
141
+ - "jar org.snakeyaml:snakeyaml-engine, 2.10"
142
+ rubygems_version: 4.1.1
137
143
  specification_version: 4
138
144
  summary: Psych is a YAML parser and emitter
139
145
  test_files: []