From 3f5396c1c47c0f89b1111379101a9aca7936f271 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Thu, 17 Jul 2025 14:24:46 +0200 Subject: [PATCH 1/2] Lwt_utils_unix: Fixes Json.from_file parsing * What The `Lwt_utils_unix.Json.from_file` now enforces stricter validation for the JSON configuration file. Previously, the parser would silently ignore any content that appeared after the first valid JSON object. Now, any extraneous data will cause the function to return an error. * Why This change prevents silent configuration errors. The former lenient parsing could mask issues like accidental file concatenation or stray characters, leading the node to run with an incomplete or unintended configuration. By failing, we ensure that users are immediately aware of malformed configuration files, instead of nodes only consuming part of a configuration file. * How The implementation of `Lwt_utils_unix.Json.from_file` was modified to replace the lenient `Ezjsonm.from_string` parser with the stricter `Data_encoding.Json.from_string`. This function ensures the entire file constitutes a single, valid JSON value. A new error, `Json_decoding_error`, was also introduced to provide more detailed diagnostics upon parsing failure. --- src/lib_stdlib_unix/lwt_utils_unix.ml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib_stdlib_unix/lwt_utils_unix.ml b/src/lib_stdlib_unix/lwt_utils_unix.ml index a729fd84d318..01bac84d8331 100644 --- a/src/lib_stdlib_unix/lwt_utils_unix.ml +++ b/src/lib_stdlib_unix/lwt_utils_unix.ml @@ -302,6 +302,19 @@ let getpass () = passwd module Json = struct + type error += Json_decoding_error of {input : string; error : string} + + let () = + register_error_kind + `Temporary + ~id:"json_decoding_error" + ~title:"Json decoding error" + ~description:"The input string is not a valid JSON value" + Data_encoding.(obj2 (req "input" string) (req "error" string)) + (function + | Json_decoding_error {input; error} -> Some (input, error) | _ -> None) + (fun (input, error) -> Json_decoding_error {input; error}) + let to_root = function | `O ctns -> `O ctns | `A ctns -> `A ctns @@ -322,7 +335,9 @@ module Json = struct Lwt_io.with_file ~mode:Input file (fun chan -> let open Lwt_result_syntax in let*! str = Lwt_io.read chan in - return (Ezjsonm.from_string str :> Data_encoding.json))) + match Data_encoding.Json.from_string str with + | Ok json -> return json + | Error error -> tzfail (Json_decoding_error {input = str; error}))) end (* This module is used by [safe_cancel_on_exit] to register and unregister -- GitLab From d1bc2acd36387992036c68277d65aed7fdae304e Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Sat, 19 Jul 2025 17:23:57 +0200 Subject: [PATCH 2/2] Tezt: Fix `Baking nonce format migration` ill-formated nonces file * What Tweak the tezt scenario to generate a well-formated file that can be parse in its entierety. Previously, the generated file would look like this. [ { "block": "BLibm5QJetZcbA427EWFp6C82AvaZ5cDfVrUyEmUiRrpdfkDJNb", "nonce": "1eb0fa2bcd291dc415c37bb10a3006656c0e3c87edfe6301e8b054639c74bc5a" }, { "block": "BLKZ45h2CHNhj1hpd5ztJXG6T9WEeBKXLKLzd55RJVtVXLmKg2R", "nonce": "2d73a1ccd11b9b08464f66866762f0db259cefc48a2e136eef4fde4a69065d87" } ][ { "block": "BMVwjFL5ZRdB1icmcJ4tjy2Uz7edz1jcZ3m872KCqmYnVEi6Hd1", "nonce": "4aea386a659f6a4f921bce12af466312e626407754e984550a22c47dac1432d1" } ] * Why Due to a buggy helper, the consumers of the nonce files stop trying to parse their contents once they have read a valid JSON object. Meaning that the new nonces were effectively ignore during the test. Generating a proper file ensure the binary also takes into account the new nonces. * How Parse the new and old nonces contents, in order to combine the two arrays into one, and delegate to `Data_encoding` the generation of a valid JSON string. --- tezt/tests/nonce_seed_revelation.ml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tezt/tests/nonce_seed_revelation.ml b/tezt/tests/nonce_seed_revelation.ml index dbef54d3dcc8..b5719a3d4cba 100644 --- a/tezt/tests/nonce_seed_revelation.ml +++ b/tezt/tests/nonce_seed_revelation.ml @@ -313,11 +313,16 @@ let test_baking_nonce_migration = Log.info "Concat old nonces contents with the new one" ; let new_nonces_contents = Base.read_file nonces_file in - let () = - Base.write_file - nonces_file - ~contents:(old_nonces_contents ^ new_nonces_contents) + let merged_nonces_contents = + match + ( Data_encoding.Json.from_string old_nonces_contents, + Data_encoding.Json.from_string new_nonces_contents ) + with + | Ok (`A old), Ok (`A new_) -> + Data_encoding.Json.to_string (`A (old @ new_)) + | _ -> Test.fail "Incorrectly formatted nonces files" in + let () = Base.write_file nonces_file ~contents:merged_nonces_contents in Log.info "Restart the baker and wait for ignore failed nonce migration event then \ -- GitLab