Skip to content

Commit 3c3dfe3

Browse files
committed
Fix a bug that Gem::YAMLSerializer.load ignores quotation
`"a: b"` must be processed as a string value (`a: b`) not a map value (`{"a" => "b"}`).
1 parent 5c535b0 commit 3c3dfe3

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lib/rubygems/yaml_serializer.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ def parse_node(base_indent)
9595

9696
if stripped.start_with?("- ") || stripped == "-"
9797
parse_sequence(indent, anchor)
98+
elsif stripped.start_with?("\"") && stripped.end_with?("\"")
99+
# We don't need to care about the following case here:
100+
# 1. "value with comment" # ...
101+
# 2. "key": "value"
102+
#
103+
# 1. must not happen because YAMLSerializer doesn't emit any
104+
# comment. YAMLSerializer parses only YAML that is generated
105+
# by YAMLSerializer.
106+
#
107+
# 2. must not happen because #parse_node isn't used non
108+
# top-level mapping. Non top-level mapping always uses
109+
# #parse_mapping. Top-level mapping never use the '"key":
110+
# "value"' form because all top-level keys
111+
# ("!ruby/object:Gem::Specification"'s keys) are known and
112+
# #emit_specification doesn't quote anything.
113+
parse_plain_scalar(indent, anchor)
114+
elsif stripped.start_with?("'") && stripped.end_with?("'")
115+
# See also the above note for double quotation.
116+
parse_plain_scalar(indent, anchor)
98117
elsif stripped =~ MAPPING_KEY_RE && !stripped.start_with?("!ruby/object:")
99118
parse_mapping(indent, anchor)
100119
elsif stripped.start_with?("!ruby/object:")

test/rubygems/test_gem_safe_yaml.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ def test_requirements_hash_converted_to_array
318318
assert_kind_of Hash, reqs
319319
end
320320

321+
def test_requirement_quote
322+
yaml = <<~YAML
323+
requirements:
324+
- "system: arrow-glib>=25.0.0: amazon_linux: arrow-glib-devel"
325+
- 'system: arrow-glib>=25.0.0: fedora: libarrow-glib-devel'
326+
YAML
327+
328+
expected = [
329+
"system: arrow-glib>=25.0.0: amazon_linux: arrow-glib-devel",
330+
"system: arrow-glib>=25.0.0: fedora: libarrow-glib-devel",
331+
]
332+
assert_equal expected, yaml_load(yaml)["requirements"]
333+
end
334+
321335
def test_rdoc_options_hash_converted_to_array
322336
# Some gemspecs incorrectly have rdoc_options: {} instead of rdoc_options: []
323337
yaml = <<~YAML

0 commit comments

Comments
 (0)