Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(evm): decode shortest-form empty CBOR text in Solidity metadata (#3226)

pablodiazjorgee195b8ba91c0
diff --git a/src/evm/SolidityMetadata.test.ts b/src/evm/SolidityMetadata.test.ts
index 1ca056f18..bf1eb5385 100644
--- a/src/evm/SolidityMetadata.test.ts
+++ b/src/evm/SolidityMetadata.test.ts
@@ -67,6 +67,46 @@ it("preserves a UTF-8 BOM inside a CBOR text value rather than stripping it", ()
).toBeNull();
});
+it.each([
+ [
+ "value",
+ "a1617860",
+ { kind: "text", value: "x" },
+ { kind: "text", value: "" },
+ ],
+ [
+ "key",
+ "a1606178",
+ { kind: "text", value: "" },
+ { kind: "text", value: "x" },
+ ],
+ [
+ "long-form value",
+ "a161787800",
+ { kind: "text", value: "x" },
+ { kind: "text", value: "" },
+ ],
+])(
+ "decodes an empty CBOR text %s like any other text",
+ (_name, hex, key, value) => {
+ expect(inspectSolidityMetadata(bytecode(hex))).toMatchObject({
+ status: "decoded",
+ entries: [{ key, value }],
+ });
+ },
+);
+
+it("keeps the compiler version beside a shortest-form empty text", () => {
+ const result = inspectSolidityMetadata(
+ bytecode("a264736f6c634300081e617860"),
+ );
+ expect(result).toMatchObject({
+ status: "decoded",
+ compiler_version: { representation: "release-bytes", value: "0.8.30" },
+ });
+ expect(result.entries).toHaveLength(2);
+});
+
it.each([
["invalid text", "a1617861ff"],
["truncated value", "a16178"],
diff --git a/src/evm/SolidityMetadata.ts b/src/evm/SolidityMetadata.ts
index 5b24ce245..8a82bceed 100644
--- a/src/evm/SolidityMetadata.ts
+++ b/src/evm/SolidityMetadata.ts
@@ -36,11 +36,14 @@ class BoundedMetadataTokenizer extends Tokenizer {
"CBOR tags and floating-point values are outside this metadata profile.",
);
if (Type.equals(token.type, Type.string)) {
- if (token.byteValue === undefined)
+ if (token.byteValue !== undefined)
+ token.value = utf8.decode(token.byteValue);
+ // cborg answers the shortest-form empty text (0x60) with a shared token that has no
+ // bytes; it can only mean "", which is already its value.
+ else if (token.value !== "" || token.encodedLength !== 1)
throw new Error(
"CBOR text bytes were unavailable for strict UTF-8 validation.",
);
- token.value = utf8.decode(token.byteValue);
}
const parent = this.remaining.length - 1;
if (parent >= 0) this.remaining[parent]!--;