From 696ffb6568160562f86f6ac2c66cb23a2292ec74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 5 Oct 2022 08:32:22 +0100 Subject: [PATCH 1/2] Proto-env: add blanket fine-grained error handling to lazy-encoding --- .../environment_V5.ml | 2 +- .../environment_V6.ml | 2 +- .../tezos_protocol_environment_structs.ml | 2 + .../structs/v0_data_encoding.ml | 64 +++++++++++++ .../structs/v3_data_encoding.ml | 64 +++++++++++++ .../structs/v5_data_encoding.ml | 90 +++++++++++++++++++ 6 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 src/lib_protocol_environment/structs/v5_data_encoding.ml diff --git a/src/lib_protocol_environment/environment_V5.ml b/src/lib_protocol_environment/environment_V5.ml index 171f40886826..270111588459 100644 --- a/src/lib_protocol_environment/environment_V5.ml +++ b/src/lib_protocol_environment/environment_V5.ml @@ -250,7 +250,7 @@ struct module Lwt = Lwt module Data_encoding = struct - include Data_encoding + include Tezos_protocol_environment_structs.V5.Data_encoding type tag_size = [`Uint8 | `Uint16] diff --git a/src/lib_protocol_environment/environment_V6.ml b/src/lib_protocol_environment/environment_V6.ml index 6e55e2df035b..a2b54755f245 100644 --- a/src/lib_protocol_environment/environment_V6.ml +++ b/src/lib_protocol_environment/environment_V6.ml @@ -255,7 +255,7 @@ struct module Lwt = Lwt module Data_encoding = struct - include Data_encoding + include Tezos_protocol_environment_structs.V6.Data_encoding type tag_size = [`Uint8 | `Uint16] diff --git a/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml b/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml index 0a4c8a98291d..4bfdb7771837 100644 --- a/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml +++ b/src/lib_protocol_environment/structs/tezos_protocol_environment_structs.ml @@ -86,10 +86,12 @@ module V4 = struct end module V5 = struct + module Data_encoding = V5_data_encoding module Error_monad_infix_globals = V0_error_monad_infix_globals end module V6 = struct + module Data_encoding = V5_data_encoding module Error_monad_infix_globals = V0_error_monad_infix_globals module Plonk = V6_plonk end diff --git a/src/lib_protocol_environment/structs/v0_data_encoding.ml b/src/lib_protocol_environment/structs/v0_data_encoding.ml index add46e3eb570..14ebf055b8f0 100644 --- a/src/lib_protocol_environment/structs/v0_data_encoding.ml +++ b/src/lib_protocol_environment/structs/v0_data_encoding.ml @@ -25,6 +25,70 @@ include Data_encoding +module Encoding = struct + include Encoding + + let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + Repr.repr + (`O + [ + ( "unparsed-binary", + Repr.repr `Null ); + ]) + in + let read (type value) + (module Repr : Json_repr.Repr with type value = value) j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary +end + +(* We have to define this twice bc in data-encoding<0.7 the type equality for + [lazy_t] is not propagated. *) +let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + Repr.repr + (`O + [ + ( "unparsed-binary", + Repr.repr `Null ); + ]) + in + let read (type value) (module Repr : Json_repr.Repr with type value = value) + j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary + module Json = struct include Data_encoding.Json diff --git a/src/lib_protocol_environment/structs/v3_data_encoding.ml b/src/lib_protocol_environment/structs/v3_data_encoding.ml index 9f35ff48880c..995eec1e2416 100644 --- a/src/lib_protocol_environment/structs/v3_data_encoding.ml +++ b/src/lib_protocol_environment/structs/v3_data_encoding.ml @@ -25,6 +25,70 @@ include Data_encoding +module Encoding = struct + include Encoding + + let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + Repr.repr + (`O + [ + ( "unparsed-binary", + Repr.repr `Null ); + ]) + in + let read (type value) + (module Repr : Json_repr.Repr with type value = value) j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary +end + +(* We have to define this twice bc in data-encoding<0.7 the type equality for + [lazy_t] is not propagated. *) +let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + Repr.repr + (`O + [ + ( "unparsed-binary", + Repr.repr `Null ); + ]) + in + let read (type value) (module Repr : Json_repr.Repr with type value = value) + j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary + module Json = struct include Data_encoding.Json diff --git a/src/lib_protocol_environment/structs/v5_data_encoding.ml b/src/lib_protocol_environment/structs/v5_data_encoding.ml new file mode 100644 index 000000000000..2a5f2c3d9fc5 --- /dev/null +++ b/src/lib_protocol_environment/structs/v5_data_encoding.ml @@ -0,0 +1,90 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Nomadic Labs. *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +include Data_encoding + +module Encoding = struct + include Encoding + + let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + Repr.repr + (`O + [ + ( "unparsed-binary", + Repr.repr `Null ); + ]) + in + let read (type value) + (module Repr : Json_repr.Repr with type value = value) j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary +end + +(* We have to define this twice bc in data-encoding<0.7 the type equality for + [lazy_t] is not propagated. *) +let lazy_encoding encoding = + let binary = lazy_encoding encoding in + let json = + let open Json_encoding in + let write (type value) + (module Repr : Json_repr.Repr with type value = value) le = + match force_decode le with + | Some r -> + Json_repr.convert + (module Json_repr.Ezjsonm) + (module Repr) + (Json.construct encoding r) + | None -> + Repr.repr + (`O + [ + ( "unparsed-binary", + Repr.repr `Null ); + ]) + in + let read (type value) (module Repr : Json_repr.Repr with type value = value) + j = + let j = Json_repr.convert (module Repr) (module Json_repr.Ezjsonm) j in + make_lazy encoding (Json.destruct encoding j) + in + repr_agnostic_custom {write; read} ~schema:Json_schema.any + in + Data_encoding__Encoding.raw_splitted ~json ~binary -- GitLab From d704859c17f660e7987009c9332d785590f69607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Proust?= Date: Wed, 5 Oct 2022 09:09:56 +0100 Subject: [PATCH 2/2] Proto-env: exfiltrate invalid lazy bytes to JSON --- .../structs/v0_data_encoding.ml | 26 ++++++++++--------- .../structs/v3_data_encoding.ml | 26 ++++++++++--------- .../structs/v5_data_encoding.ml | 26 ++++++++++--------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/lib_protocol_environment/structs/v0_data_encoding.ml b/src/lib_protocol_environment/structs/v0_data_encoding.ml index 14ebf055b8f0..1988704c5d60 100644 --- a/src/lib_protocol_environment/structs/v0_data_encoding.ml +++ b/src/lib_protocol_environment/structs/v0_data_encoding.ml @@ -41,12 +41,13 @@ module Encoding = struct (module Repr) (Json.construct encoding r) | None -> - Repr.repr - (`O - [ - ( "unparsed-binary", - Repr.repr `Null ); - ]) + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le in let read (type value) (module Repr : Json_repr.Repr with type value = value) j = @@ -73,12 +74,13 @@ let lazy_encoding encoding = (module Repr) (Json.construct encoding r) | None -> - Repr.repr - (`O - [ - ( "unparsed-binary", - Repr.repr `Null ); - ]) + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le in let read (type value) (module Repr : Json_repr.Repr with type value = value) j = diff --git a/src/lib_protocol_environment/structs/v3_data_encoding.ml b/src/lib_protocol_environment/structs/v3_data_encoding.ml index 995eec1e2416..7b19599ddbca 100644 --- a/src/lib_protocol_environment/structs/v3_data_encoding.ml +++ b/src/lib_protocol_environment/structs/v3_data_encoding.ml @@ -41,12 +41,13 @@ module Encoding = struct (module Repr) (Json.construct encoding r) | None -> - Repr.repr - (`O - [ - ( "unparsed-binary", - Repr.repr `Null ); - ]) + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le in let read (type value) (module Repr : Json_repr.Repr with type value = value) j = @@ -73,12 +74,13 @@ let lazy_encoding encoding = (module Repr) (Json.construct encoding r) | None -> - Repr.repr - (`O - [ - ( "unparsed-binary", - Repr.repr `Null ); - ]) + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le in let read (type value) (module Repr : Json_repr.Repr with type value = value) j = diff --git a/src/lib_protocol_environment/structs/v5_data_encoding.ml b/src/lib_protocol_environment/structs/v5_data_encoding.ml index 2a5f2c3d9fc5..0dc7fdcb656e 100644 --- a/src/lib_protocol_environment/structs/v5_data_encoding.ml +++ b/src/lib_protocol_environment/structs/v5_data_encoding.ml @@ -41,12 +41,13 @@ module Encoding = struct (module Repr) (Json.construct encoding r) | None -> - Repr.repr - (`O - [ - ( "unparsed-binary", - Repr.repr `Null ); - ]) + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le in let read (type value) (module Repr : Json_repr.Repr with type value = value) j = @@ -73,12 +74,13 @@ let lazy_encoding encoding = (module Repr) (Json.construct encoding r) | None -> - Repr.repr - (`O - [ - ( "unparsed-binary", - Repr.repr `Null ); - ]) + apply_lazy + ~fun_value:(fun _ -> assert false) + ~fun_bytes:(fun b -> + let (`Hex h) = Hex.of_bytes b in + Repr.repr (`O [("unparsed-binary", Repr.repr (`String h))])) + ~fun_combine:(fun _ _ -> assert false) + le in let read (type value) (module Repr : Json_repr.Repr with type value = value) j = -- GitLab