Skip to content

Check return value of yaml_*_event_initialize in the emitter - #823

Open
kou wants to merge 1 commit into
ruby:masterfrom
kou:emitter-check-event-initialize
Open

kou wants to merge 1 commit into
ruby:masterfrom
kou:emitter-check-event-initialize

Conversation

@kou

@kou kou commented Sep 21, 2026

Copy link
Copy Markdown
Member

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.

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.
@kou
kou force-pushed the emitter-check-event-initialize branch from 4bda41c to 769ddd9 Compare September 21, 2026 14:51
@joshuapinter

Copy link
Copy Markdown

Thanks for this. We hit exactly this in a Rails app. We got there through Psych.dump rather than Psych::Emitter, so here's that path in case it helps review.

Rack 3.2 tags a multipart part with the charset the part declares (Content-Type: text/plain; charset=windows-1252), and Rails only checks valid_encoding?. So a String like "\x81" tagged Windows-1252, which is valid there but has no UTF-8 equivalent, reached Psych.dump through serialized columns and Delayed Job handlers. rb_str_export_to_enc can't convert it, so it hands back the original bytes. yaml_scalar_event_initialize then refuses them, and the uninitialized event is emitted anyway.

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 Psych.dump might be worth adding next to test_invalid_event, since that's how an application ends up here. This one fails on master (on the message) and passes on this branch:

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants