Repository navigation
Conversation
libyaml's `yaml_*_event_initialize()` functions reject an anchor, tag, or value that is not valid UTF-8 (and can fail on allocation errors), returning 0 and leaving the passed `yaml_event_t` untouched. The emitter ignored that return value and emitted the event anyway. It may cause unexpected behavior. For example, `yaml_emitter_emit()` may touch an invalid address or double free an invalid event. We can avoid it by checking every `yaml_*_event_initialize()` call and raise instead of emitting a stale or uninitialized event.
4bda41c to
769ddd9
Compare
|
Thanks for this. We hit exactly this in a Rails app. We got there through Rack 3.2 tags a multipart part with the charset the part declares ( I built master (0e186fe) and this branch (769ddd9) on macOS arm64 with Ruby 3.4.12 and libyaml 0.2.5, and ran each case in a fresh process: s = "\x81".b.force_encoding(Encoding::Windows_1252)
Psych.dump(s)
# master: RuntimeError: expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS
# this branch: RuntimeError: failed to initialize scalar event
(Psych.dump({ "a" => s, "b" => "x" }) rescue nil)
Psych.dump({ "c" => "y" })
# master: the process aborted (SIGABRT, no message) or hung until killed, in every run
# this branch: the first dump raises as above, and the second returns "---\nc: \"y\"\n"psych 5.2.6 behaves like master. On another Mac (Ruby 3.4.13, psych 5.2.2, with jemalloc) the later dump spun at 100% CPU and ignored SIGTERM. This branch's suite passes here (660 tests). A test through def test_dump_string_without_utf8_mapping
omit 'libyaml backend only' unless libyaml?
string = "\x81".b.force_encoding(Encoding::Windows_1252) # Valid in its encoding, with no UTF-8 equivalent.
assert_raise_with_message(RuntimeError, /failed to initialize scalar event/) { Psych.dump(string) }
assert_equal "--- x\n", Psych.dump("x")
end |
libyaml's
yaml_*_event_initialize()functions reject an anchor, tag, or value that is not valid UTF-8 (and can fail on allocation errors), returning 0 and leaving the passedyaml_event_tuntouched.The emitter ignored that return value and emitted the event anyway. It may cause unexpected behavior. For example,
yaml_emitter_emit()may touch an invalid address or double free an invalid event.We can avoid it by checking every
yaml_*_event_initialize()call and raise instead of emitting a stale or uninitialized event.