From d6a6378fa1dbb349843e4fd3bdcdc004d4c63d1f Mon Sep 17 00:00:00 2001 From: ilitteri Date: Fri, 4 Dec 2026 07:27:34 -0300 Subject: [PATCH] fix(rpc)!: reject keys outside the schema of an eth_getProof answer The AccountProof and StorageProof schemas of execution-apis set additionalProperties to false, so an answer or a slot holds no other property. The reader of an answer read the properties it knows and ignored any other, so verify_account_proof accepted such an answer. Both readers now check their keys first with onlyKeys, as the readers of block and account overrides do. onlyKeys moves next to fields in Serialization/JsonRpc.lean, with the lemmas onlyKeys_mkObj and onlyKeys_rejects. The round trip of the reader still holds, and accountProofOfJson_unknown_key and storageProofOfJson_unknown_key state the rejection. BREAKING CHANGE: execution.trie.verify_account_proof raises TrieError for an answer or a slot with a key outside its schema. In Lean, onlyKeys is EthLib.Serialization.JsonRpc.onlyKeys, where it was EthLib.Execution.Rpc.onlyKeys. --- CHANGELOG.md | 7 ++++ EthLib/Execution/Proofs/Rpc/Proof.lean | 32 +++++++++++++++++-- EthLib/Execution/Proofs/Rpc/Simulate.lean | 13 +------- EthLib/Execution/Rpc/Proof.lean | 11 +++++++ EthLib/Execution/Rpc/Simulate.lean | 7 ---- EthLib/Serialization/JsonRpc.lean | 9 ++++++ EthLib/Serialization/Proofs/JsonRpc.lean | 30 +++++++++++++++++ SOUNDNESS.md | 2 ++ .../python/tests/trie/test_account_proof.py | 4 +++ 9 files changed, 94 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 249de5dd22..2a1714a040 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ Not yet released. ### Proof additions +- In Lean, `accountProofOfJson_unknown_key` and `storageProofOfJson_unknown_key` (`Execution/Proofs/Rpc/Proof.lean`) state that the reader of an `eth_getProof` answer rejects an answer or a slot with a key outside its schema. + `onlyKeys_mkObj` and `onlyKeys_rejects` (`Serialization/Proofs/JsonRpc.lean`) state when an object holds only the given keys. + - In Python, `serialization.ssz` gains `verify_merkle_proof`, `compute_merkle_multiproof`, `verify_merkle_multiproof`, `value_of_json` and `json_of_value`, over the definitions of `Serialization/Ssz/Merkle/Verify.lean`, `Serialization/Ssz/Codec/Proof.lean` and `Serialization/Ssz/Codec/Json.lean`. The tagged form of a type spells the byte alias as `{"byte": None}`, which the JSON mapping writes as hex. The suite runs the 575 vectors of the ssz-specs release v0.2.0, in all six of its formats. @@ -2000,6 +2003,10 @@ Not yet released. ### Changes that break callers +- `execution.trie.verify_account_proof` rejects an answer or a slot with a key outside the `AccountProof` or `StorageProof` schema of execution-apis, which allow no other property. + It raises `TrieError` code 0 with `answer: unknown key` or `slot: unknown key`, then the key, where it ignored the key. + In Lean, `onlyKeys` moves from `Execution/Rpc/Simulate.lean` to `Serialization/JsonRpc.lean`, so its name is `EthLib.Serialization.JsonRpc.onlyKeys`. + - `serialization.ssz.get_generalized_index` names the SSZ fault of a path that it cannot read, with the code of `Err`, where it raised code 0 with "path: no field". A field name that a container does not declare, or a position given for a field, is `noSuchField`, code 45; a name on a basic value is `noParts`, code 40; a name on another shape is `notAPosition`, code 22. These are the names that the ssz-specs release v0.2.0 gives these paths. diff --git a/EthLib/Execution/Proofs/Rpc/Proof.lean b/EthLib/Execution/Proofs/Rpc/Proof.lean index a7a3b46c2d..a7f4d3d773 100644 --- a/EthLib/Execution/Proofs/Rpc/Proof.lean +++ b/EthLib/Execution/Proofs/Rpc/Proof.lean @@ -161,6 +161,11 @@ private theorem storageProofOfJson_storageProofJson {s : StorageProof} cases hb : s.key.asBytes <;> simp [Base.Toolchain.Json.isNull_eq, storageKeyJson, hb, quantityJson, proofNodesJson, listJson, dataJson]) + -- The writer writes the three properties of the schema, and no other. + have honly : onlyKeys "slot" storageProofKeys (Lean.Json.mkObj [ + ("key", storageKeyJson s.key), ("value", quantityJson s.value.toNat), + ("proof", proofNodesJson s.proof)]) = .ok () := + onlyKeys_mkObj "slot" (by simp) (by simp [storageProofKeys]) have f1 : field "slot" fs "key" = .ok (storageKeyJson s.key) := field_eq_ok_iff.mpr (hl "key" _ (by simp)) have f2 : field "slot" fs "value" = .ok (quantityJson s.value.toNat) := @@ -169,7 +174,7 @@ private theorem storageProofOfJson_storageProofJson {s : StorageProof} field_eq_ok_iff.mpr (hl "proof" _ (by simp)) unfold storageProofOfJson rw [storageProofJson, hfs] - simp only [bind, Except.bind, f1, f2, f3, answerKey_storageKeyJson _ hkey, + simp only [bind, Except.bind, honly, f1, f2, f3, answerKey_storageKeyJson _ hkey, quantity_toDigits _ (bits := 256) (by simpa using s.value.isLt), proofNodes_proofNodesJson] simp [pure, Except.pure] @@ -199,6 +204,13 @@ theorem accountProofOfJson_accountProofJson {p : AccountProof} (hnonce : p.nonce ("nonce", quantityJson p.nonce), ("storageHash", dataJson p.storageHash.toList), ("storageProof", listJson storageProofJson p.storageProof)]) (by simp) (by simp [Base.Toolchain.Json.isNull_eq, quantityJson, proofNodesJson, listJson, dataJson]) + -- The writer writes the seven properties of the schema, and no other. + have honly : onlyKeys "answer" accountProofKeys (Lean.Json.mkObj [ + ("address", dataJson p.address.toList), ("accountProof", proofNodesJson p.accountProof), + ("balance", quantityJson p.balance.toNat), ("codeHash", dataJson p.codeHash.toList), + ("nonce", quantityJson p.nonce), ("storageHash", dataJson p.storageHash.toList), + ("storageProof", listJson storageProofJson p.storageProof)]) = .ok () := + onlyKeys_mkObj "answer" (by simp) (by simp [accountProofKeys]) have f1 : field "answer" fs "address" = .ok (dataJson p.address.toList) := field_eq_ok_iff.mpr (hl "address" _ (by simp)) have f2 : field "answer" fs "accountProof" = .ok (proofNodesJson p.accountProof) := @@ -215,13 +227,29 @@ theorem accountProofOfJson_accountProofJson {p : AccountProof} (hnonce : p.nonce field_eq_ok_iff.mpr (hl "storageProof" _ (by simp)) unfold accountProofOfJson rw [accountProofJson, hfs] - simp only [bind, Except.bind, f1, f2, f3, f4, f5, f6, f7, proofNodes_proofNodesJson, + simp only [bind, Except.bind, honly, f1, f2, f3, f4, f5, f6, f7, proofNodes_proofNodesJson, array_storageProofOfJson _ hkeys, quantity_toDigits _ hnonce, quantity_toDigits _ (bits := 256) (by simpa using p.balance.isLt)] unfold dataJson simp only [fixed_hexString, address] simp [pure, Except.pure] +/-- A slot of an answer with a key outside the `StorageProof` schema is rejected. -/ +theorem storageProofOfJson_unknown_key {kvs : Std.TreeMap.Raw String Lean.Json compare} + {name : String} {value : Lean.Json} (hmem : (name, value) ∈ kvs.toList) + (hk : name ∉ storageProofKeys) (s : StorageProof) : storageProofOfJson (.obj kvs) ≠ .ok s := by + obtain ⟨e, he⟩ := onlyKeys_rejects "slot" hmem hk + intro hs + simp [storageProofOfJson, he, bind, Except.bind] at hs + +/-- An answer with a key outside the `AccountProof` schema is rejected. -/ +theorem accountProofOfJson_unknown_key {kvs : Std.TreeMap.Raw String Lean.Json compare} + {name : String} {value : Lean.Json} (hmem : (name, value) ∈ kvs.toList) + (hk : name ∉ accountProofKeys) (p : AccountProof) : accountProofOfJson (.obj kvs) ≠ .ok p := by + obtain ⟨e, he⟩ := onlyKeys_rejects "answer" hmem hk + intro hp + simp [accountProofOfJson, he, bind, Except.bind] at hp + /-- The word of the storage map that the state gives is the word that the proof reads. -/ private theorem getD_storage (state : State) (a : Address) (key : Bytes32) : (state.storage.getD a ∅).getD key 0 = state.getStorage a key := by diff --git a/EthLib/Execution/Proofs/Rpc/Simulate.lean b/EthLib/Execution/Proofs/Rpc/Simulate.lean index 5930397c8e..03f26ce691 100644 --- a/EthLib/Execution/Proofs/Rpc/Simulate.lean +++ b/EthLib/Execution/Proofs/Rpc/Simulate.lean @@ -1,6 +1,7 @@ module public import EthLib.Execution.Rpc.Simulate +import EthLib.Serialization.Proofs.JsonRpc /-! # Proofs: the request of `eth_simulateV1` @@ -30,18 +31,6 @@ open EthLib.Cryptography.Hash.Keccak (keccak256) /-! ## The reader -/ -private theorem onlyKeys_rejects {kvs : Std.TreeMap.Raw String Json compare} {name : String} - {value : Json} {keys : List String} (what : String) (hmem : (name, value) ∈ kvs.toList) - (hk : name ∉ keys) : ∃ e, onlyKeys what keys (.obj kvs) = .error e := by - unfold onlyKeys - cases h : kvs.toList.find? fun (name, _) => !keys.contains name with - | none => - have := List.find?_eq_none.1 h _ hmem - simp_all - | some p => - obtain ⟨n, v⟩ := p - exact ⟨_, by simp only [h]; rfl⟩ - /-- Block overrides with a key outside their schema are rejected. -/ theorem blockOverrides_unknown_key {kvs : Std.TreeMap.Raw String Json compare} {name : String} {value : Json} (hmem : (name, value) ∈ kvs.toList) (hk : name ∉ blockOverrideKeys) diff --git a/EthLib/Execution/Rpc/Proof.lean b/EthLib/Execution/Rpc/Proof.lean index 0b06cb2936..d5ce980e2e 100644 --- a/EthLib/Execution/Rpc/Proof.lean +++ b/EthLib/Execution/Rpc/Proof.lean @@ -56,8 +56,17 @@ def storageKeyJson (key : StorageKey) : Json := def proofNodes (what : String) (j : Json) : Read (List ByteArray) := array what (bytes what) j +/-- The properties of the `StorageProof` schema, which allows no other. -/ +def storageProofKeys : List String := ["key", "value", "proof"] + +/-- The properties of the `AccountProof` schema, which allows no other. -/ +def accountProofKeys : List String := + ["address", "accountProof", "balance", "codeHash", "nonce", "storageHash", "storageProof"] + /-- One slot of an answer: its key, its word and the nodes of its proof. -/ def storageProofOfJson (j : Json) : Read StorageProof := do + -- The `StorageProof` schema: `key`, `value` and `proof`, and no other property. + onlyKeys "slot" storageProofKeys j let fs ← fields "slot" j return ⟨← answerKey "key" (← field "slot" fs "key"), BitVec.ofNat 256 (← quantity "value" 256 (← field "slot" fs "value")), @@ -67,6 +76,8 @@ def storageProofOfJson (j : Json) : Read StorageProof := do Its fields are those of the `AccountProof` schema. -/ def accountProofOfJson (j : Json) : Read AccountProof := do + -- The `AccountProof` schema: its seven properties, and no other property. + onlyKeys "answer" accountProofKeys j let fs ← fields "answer" j let field := field "answer" fs -- The schema: an address of 20 bytes, a nonce below `2^64`, and a balance below `2^256`. diff --git a/EthLib/Execution/Rpc/Simulate.lean b/EthLib/Execution/Rpc/Simulate.lean index b47411b7c9..ff50997c9b 100644 --- a/EthLib/Execution/Rpc/Simulate.lean +++ b/EthLib/Execution/Rpc/Simulate.lean @@ -100,13 +100,6 @@ def blockOverrideKeys : List String := def accountOverrideKeys : List String := ["nonce", "balance", "code", "movePrecompileToAddress", "state", "stateDiff"] -/-- An object of the request that holds only the given keys, or why it does not. -/ -def onlyKeys (what : String) (keys : List String) (j : Json) : Read Unit := do - let .obj kvs := j | reject s!"{what}: not an object" - match kvs.toList.find? fun (name, _) => !keys.contains name with - | some (name, _) => reject s!"{what}: unknown key {name}" - | none => pure () - /-- Block overrides, with only the keys of their schema. -/ def blockOverrides (j : Json) : Read BlockOverrides := do onlyKeys "blockOverrides" blockOverrideKeys j diff --git a/EthLib/Serialization/JsonRpc.lean b/EthLib/Serialization/JsonRpc.lean index 875f2bc06f..24e3d52497 100644 --- a/EthLib/Serialization/JsonRpc.lean +++ b/EthLib/Serialization/JsonRpc.lean @@ -148,6 +148,15 @@ def fields (what : String) : Json → Read (List (String × Json)) | .obj kvs => pure (kvs.toList.filter fun (_, v) => !v.isNull) | _ => reject s!"{what}: not an object" +/-- An object that holds only the given keys, or why it does not. + +A key counts whatever its value, `null` included. -/ +def onlyKeys (what : String) (keys : List String) (j : Json) : Read Unit := do + let .obj kvs := j | reject s!"{what}: not an object" + match kvs.toList.find? fun (name, _) => !keys.contains name with + | some (name, _) => reject s!"{what}: unknown key {name}" + | none => pure () + /-- Whether a list of fields holds exactly the named fields. -/ def hasExactly (fs : List (String × Json)) (names : List String) : Bool := -- Step 1 of `engine_newPayloadV3`: the fields strictly match the expected ones. diff --git a/EthLib/Serialization/Proofs/JsonRpc.lean b/EthLib/Serialization/Proofs/JsonRpc.lean index b887ef61ec..c32e8f6f8f 100644 --- a/EthLib/Serialization/Proofs/JsonRpc.lean +++ b/EthLib/Serialization/Proofs/JsonRpc.lean @@ -1194,6 +1194,36 @@ theorem fields_mkObj (what : String) {pairs : List (String × Lean.Json)} fun l h => h.imp_of_mem (fun {a b} _ _ hne heq => hne (by rw [heq])) exact (List.perm_ext_iff_of_nodup (nodup _ hd) (nodup _ distinct)).mpr hmem +/-- An object with a key outside the given keys is rejected. -/ +theorem onlyKeys_rejects {kvs : Std.TreeMap.Raw String Lean.Json compare} {name : String} + {value : Lean.Json} {keys : List String} (what : String) (hmem : (name, value) ∈ kvs.toList) + (hk : name ∉ keys) : ∃ e, onlyKeys what keys (.obj kvs) = .error e := by + unfold onlyKeys + cases h : kvs.toList.find? fun (name, _) => !keys.contains name with + | none => + have := List.find?_eq_none.1 h _ hmem + simp_all + | some p => + obtain ⟨n, v⟩ := p + exact ⟨_, by simp only [h]; rfl⟩ + +/-- An object built from pairs with distinct names holds only the given keys, when each name is one. +-/ +theorem onlyKeys_mkObj (what : String) {keys : List String} {pairs : List (String × Lean.Json)} + (distinct : pairs.Pairwise fun a b => a.1 ≠ b.1) (named : ∀ p ∈ pairs, p.1 ∈ keys) : + onlyKeys what keys (Lean.Json.mkObj pairs) = .ok () := by + obtain ⟨kvs, hobj, -, hmem, -⟩ := toList_mkObj distinct + rw [hobj] + unfold onlyKeys + dsimp only + split + -- A pair that the search finds is a pair of the list, so its name is among the keys. + · rename_i name value hfind + have hkey := List.find?_some hfind + have hin := named _ ((hmem _).mp (List.mem_of_find?_eq_some hfind)) + simp_all + · rfl + /-- An object built from pairs with distinct names has the pairs that are not `null` as fields. They may come in another order. -/ diff --git a/SOUNDNESS.md b/SOUNDNESS.md index 79692ea9c5..1ea48a1b8a 100644 --- a/SOUNDNESS.md +++ b/SOUNDNESS.md @@ -3481,6 +3481,8 @@ An answer at the pending block or at a block off the canonical chain is not cove For an account in the state, it proves that the words are those of `eth_getStorageAt`. `accountProof_absent` proves that for an address with no account, the answer has zero hashes and each slot the word 0 with an empty proof. `accountProofOfJson_accountProofJson` proves that the reader reads back what the writer writes, for a nonce below `2^64` and for keys written as numbers below `16^63`. +The [`AccountProof` and `StorageProof` schemas](https://github.com/ethereum/execution-apis/blob/1ca0818569467cff85b7f41372f22e728d5daa14/src/schemas/state.yaml#L1-L63) allow no other property. +So the reader rejects an answer or a slot with another key, even of value `null` (`accountProofOfJson_unknown_key`, `storageProofOfJson_unknown_key`). ### The RLPx session diff --git a/bindings/python/tests/trie/test_account_proof.py b/bindings/python/tests/trie/test_account_proof.py index 2de6766717..176c73e45e 100644 --- a/bindings/python/tests/trie/test_account_proof.py +++ b/bindings/python/tests/trie/test_account_proof.py @@ -326,6 +326,10 @@ def test_field_pattern() -> None: ({"storageProof": [dict(_SLOT, key="0x" + "1" * 65)]}, "key: more than 64 hex digits"), ({"storageProof": [dict(_SLOT, key="0xA")]}, "key: not a hex digit"), ({"storageProof": [dict(_SLOT, key="00")]}, "key: no 0x marker"), + # The schemas AccountProof and StorageProof allow no other property, even of value null. + ({"extra": "0x0"}, "answer: unknown key extra"), + ({"extra": None}, "answer: unknown key extra"), + ({"storageProof": [dict(_SLOT, extra="0x0")]}, "slot: unknown key extra"), ], ) def test_field_out_of_its_pattern(change: dict[str, Any], message: str) -> None: