Repository navigation
Escape string values written to the JSON output - #555
Conversation
json_handler::write_obj printed quoted string values with a raw vfprintf, so a string containing '"', '\' or a control character produced a document that json.load() rejects. Examples are a path with backslashes (such as a Windows --out-file or --json-out-file) and, on any platform, --key-prefix='we"ird\pre'. write_obj now escapes such values as \", \ and \u00XX (the encoding the Prometheus run-label helper already used) when the format is exactly a quoted %s. That helper no longer pre-escapes, otherwise its value would be escaped twice. Output for strings without those characters is unchanged, and all other formats still go through vfprintf as before. Adds a regression test that round-trips a quote/backslash key prefix and the out_file path through json.load().
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for splitting this out of #553, it's much easier to review on its own. I think the fix is right, and it's in the right place. Escaping once in A few small comments:
The core change looks solid to me. I'd just like the control-character case actually exercised by a test before this merges. (would file this as a comment, not a formal block) |
f4b95c1 to
48f7817
Compare
fcostaoliveira
left a comment
There was a problem hiding this comment.
Thanks — clean, well-tested fix. CI green.
Why
json_handler::write_objwrites"%s"values straight throughvfprintf, with no escaping. A configuration value containing",\or a control character therefore makesmb.jsoninvalid. For example, on Linux:Any Windows path (
C:\Users\...) hits the same thing, which is how I found it while porting memtier to Windows (#553). As suggested in the review of #553, I've split it out into this small, platform-neutral PR.What
write_objescapes the value when the format is exactly"\"%s\"":"→\",\→\\, and other control characters →\u00XX. That is the same encoding the Prometheus run-label helper already used. Every string call site uses that exact format today, and a comment at the gate notes that any other quoted format is not escaped.write_objnow does it. Otherwise labels would be escaped twice.test_v18_mb_json_round_tripcovers this.test_json_configuration_escapes_string_values, runs with a--key-prefixcontaining a quote, a backslash and control characters, and checks thatmb.jsonparses and the value round-trips.Output only changes for values that previously produced invalid JSON. Everything else is byte-identical.
Testing
test_json_output_integrity.pyandtest_cli_validation_prometheus.pypass (28/28).JSONDecodeError: Expecting ',' delimiter, so it discriminates.configuration.key_prefixdiffers, and only for values containing those characters. Before, master's file failedjson.load; with this change it parses and the value round-trips.Note
Low Risk
Output-format fix limited to JSON serialization; behavior changes only for values that previously produced unparsable JSON.
Overview
Fixes invalid
mb.jsonwhen configuration strings contain quotes, backslashes, or control characters by escaping those values at write time instead of emitting them verbatim.json_handler::write_objnow uses awrite_json_string_bodyhelper when the format is exactly"\"%s\"", producing standard JSON escapes (\",\\,\u00XX). Prometheus run labels no longer pre-escape inprometheus_run_labels_str, since centralizing escaping avoids double-encoding. A new integrity test runs with a hostile--key-prefixand asserts the configuration section parses and round-trips.Output is unchanged for values that were already valid JSON; only previously broken documents differ.
Reviewed by Cursor Bugbot for commit 48f7817. Bugbot is set up for automated code reviews on this repo. Configure here.