From b66ad06be4d9affba53c63cde50c79c60e734f40 Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Thu, 17 Jul 2025 14:24:46 +0200 Subject: [PATCH] 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. --- CHANGES.rst | 47 +++++++++++++++++++++++++++ etherlink/CHANGES_NODE.md | 5 +++ src/lib_stdlib_unix/lwt_utils_unix.ml | 17 +++++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index cfd152544c13..0e8e1ef16cf3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -28,24 +28,59 @@ General Node ---- +- **Breaking change** Enforced 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. (MR :gl:`!18745`) + Client ------ +- **Breaking change** Enforced stricter validation for the JSON files + manipulated by the client. 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. (MR :gl:`!18745`) + Signer ------ +- **Breaking change** Enforced stricter validation for the JSON files + manipulated by the signer. 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. (MR :gl:`!18745`) + Baker ----- +- **Breaking change** Enforced stricter validation for the JSON files + manipulated by the baker. 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. (MR :gl:`!18745`) + Agnostic Baker -------------- +- **Breaking change** Enforced stricter validation for the JSON files + manipulated by the agnostic baker. 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. (MR :gl:`!18745`) + Accuser ------- +- **Breaking change** Enforced stricter validation for the JSON files + manipulated by the accuser. 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. (MR :gl:`!18745`) + Agnostic Accuser ---------------- +- **Breaking change** Enforced stricter validation for the JSON files + manipulated by the agnostic accuser. 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. (MR :gl:`!18745`) + Proxy Server ------------ @@ -61,6 +96,12 @@ Docker Images Smart Rollup node ----------------- +- **Breaking change** Enforced 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. (MR :gl:`!18745`) + + Smart Rollup WASM Debugger -------------------------- @@ -70,5 +111,11 @@ Data Availability Layer (DAL) DAL node ~~~~~~~~ +- **Breaking change** Enforced 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. (MR :gl:`!18745`) + + Miscellaneous ------------- diff --git a/etherlink/CHANGES_NODE.md b/etherlink/CHANGES_NODE.md index 4a87863f766c..d71c16d02987 100644 --- a/etherlink/CHANGES_NODE.md +++ b/etherlink/CHANGES_NODE.md @@ -16,6 +16,11 @@ ### Execution changes +- 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. (!18745) + ### Storage changes ### Documentation changes 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