common/node_id: runtime assertion override, not separate compile time for fuzzing.
What changed, and why it matters
This commit is a developer-only test refactor. It changes how a sanity check is disabled during fuzz testing so that the same code can run as a normal unit test without needing a special fuzzing compiler flag. There is no change to production behavior: the assertion that a node ID starts with a valid public-key prefix remains active in normal builds.
No action required. Treat as a normal refactoring commit. If reviewing further, verify that `dev_towire_allow_invalid_node_id` is not set outside test/fuzz code.
Security signals we found
No security-relevant behavior change in production code paths
Assertion guarding valid secp256k1 public-key prefix remains enabled by default
New global flag is explicitly named `dev_` and only toggled by the fuzz harness
Evidence from the diff
The patch replaces a compile-time guard (#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) around an assert(id->k[0] == 0x2 || id->k[0] == 0x3) in towire_node_id() with a runtime boolean dev_towire_allow_invalid_node_id. The fuzz harness in tests/fuzz/wire.h sets this boolean to true during initialization. In all other builds the boolean defaults to false, so the assertion still fires. This is purely a testing ergonomics change.
Changed components
common/node_id.ccommon/node_id.htests/fuzz/wire.hInspect captured patch +9 / −4
diff --git a/common/node_id.c b/common/node_id.c
index 4998323e..f98bb815 100644
--- a/common/node_id.c
+++ b/common/node_id.c
@@ -56,14 +56,16 @@ void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id)
fromwire(cursor, max, &id->k, sizeof(id->k));
}
+bool dev_towire_allow_invalid_node_id = false;
+
void towire_node_id(u8 **pptr, const struct node_id *id)
{
-#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
/* Cheap sanity check. For wire fuzzing, we only care about correct
* encoding of node IDs and not whether the IDs are valid, so we disable
* this check while fuzzing. */
- assert(id->k[0] == 0x2 || id->k[0] == 0x3);
-#endif
+ if (!dev_towire_allow_invalid_node_id)
+ assert(id->k[0] == 0x2 || id->k[0] == 0x3);
+
towire(pptr, id->k, sizeof(id->k));
}
diff --git a/common/node_id.h b/common/node_id.h
index 0f943a44..6b1087ed 100644
--- a/common/node_id.h
+++ b/common/node_id.h
@@ -48,6 +48,9 @@ static inline int node_id_idx(const struct node_id *id1,
void towire_node_id(u8 **pptr, const struct node_id *id);
void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id);
+/* Fuzzer creates these, so sets this flag to allow it */
+extern bool dev_towire_allow_invalid_node_id;
+
/* Hash table functions for node ids */
static inline const struct node_id *node_id_keyof(const struct node_id *id)
{
diff --git a/tests/fuzz/wire.h b/tests/fuzz/wire.h
index 28a52306..23d1ee1c 100644
--- a/tests/fuzz/wire.h
+++ b/tests/fuzz/wire.h
@@ -21,7 +21,7 @@ static u8 *prefix_arr(const u8 *data, size_t size, u16 prefix)
}
/* The init function used by all fuzz-wire-* targets. */
-void init(int *argc, char ***argv) { common_setup("fuzzer"); }
+void init(int *argc, char ***argv) { common_setup("fuzzer"); dev_towire_allow_invalid_node_id = true; }
/* Test that decoding arbitrary data does not crash. Then, if the data was
* successfully decoded, test that encoding and decoding the message does not
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.