Skip to content

Protocol+node: promote NodeId and Hash from type aliases to newtypes #653

Closed

Description

@thiras

Context

The type-design review on PR #643 flagged that `NodeId = [u8; 32]` is a type alias, not a newtype, and that this is the single biggest structural improvement available across the DHT cluster. Subsequent PRs (#646, #647) added a `Hash` type alias under the same constraint.

The routing-table-poisoning bug caught in PR #643 review (an authenticated peer A passing `req.requester = B` and having B inserted into A's routing table) would have been a compile error if `AuthenticatedNodeId` and `WireNodeId` (or even just `NodeId(#[repr(transparent)] [u8; 32])`) were distinct types: the handler would have had to perform an explicit `from_authenticated_quic(...)` conversion at exactly the boundary ADR 022 cares about.

Scope

Protocol crate

In `crates/protocol/src/lib.rs` (or a new `identity.rs`):

```rust
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[repr(transparent)]
#[serde(transparent)]
pub struct NodeId(pub [u8; 32]);

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[repr(transparent)]
#[serde(transparent)]
pub struct ContentHash(pub [u8; 32]);
```

`#[repr(transparent)]` + `#[serde(transparent)]` preserve the wire format — postcard sees the same 32-byte layout, so no discriminant tests move and no ALPN bump is required.

Use sites to migrate

  • `decdn_protocol::dht` — `FindValueRequest.hash`, `StoreRequest.hash`, `StoreRequest.holder`, `FindNodeRequest.target`, `FindNodeRequest.requester`, `FindNodeResponse.target`, `FindNodeResponse.closer_nodes`, `FindValueResponse.providers`, `FindValueResponse.closer_nodes`, `BatchStoreRequest.holder`.
  • `crates/node/src/dht/{routing,records,publish,bucket_refresh,bootstrap,client}.rs` — every place currently using `[u8; 32]` or the local type alias.
  • `crates/node/src/handlers/dht.rs` — `handle_store` becomes `fn handle_store(&self, peer: AuthenticatedNodeId, req: StoreRequest)` (where `AuthenticatedNodeId` is a separate newtype constructed only from `conn.remote_id()`).
  • `crates/common/src/config/resolved.rs` — `ResolvedDht.static_active_nodes` switches from `HashSet<[u8; 32]>` to `HashSet`.

Auxiliary newtypes

Consider also:

  • `AuthenticatedNodeId` — wraps `NodeId`, only constructible from an authenticated QUIC connection. The handler accepts this, the wire types accept `NodeId`. Forces the conversion at exactly the spot where the trust boundary is.
  • `CloserNodes` — wraps `Vec` with the `MAX_CLOSER_NODES` invariant enforced at construction time.

Compatibility

  • Wire: unchanged thanks to `#[repr(transparent)]` + `#[serde(transparent)]`.
  • ABI: unchanged (newtype around `[u8; 32]` lays out identically).
  • Source breakage: every use site needs to either wrap with `NodeId(...)` or `unwrap` with `.0`. The migration is mechanical but touches many call sites.

References

Splits out from #320 review feedback.

Activity

  1. self-assigned this
    on May 21, 2026
  2. added
    prio:high
    rust
    kind:security
    on May 21, 2026
  3. github-actions commented on May 21, 2026

    @github-actions

    🎯 Triage report

    This is a well-scoped refactoring task to promote NodeId and Hash from type aliases ([u8; 32]) to newtypes (NodeId(pub [u8; 32])), motivated directly by a routing-table-poisoning bug class identified in PR #643 review. The #[repr(transparent)] + #[serde(transparent)] approach ensures zero wire-format impact. Work spans crates/protocol, crates/node/src/dht/*, crates/node/src/handlers/dht.rs, and crates/common. Splits out from #320.

    📊 Assessment

    Dimension Value Reasoning
    Type Task Mechanical refactoring with clear scope; not a bug fix or feature addition per se
    Labels technical, rust, security, high Security-relevant (closes routing-table-poisoning bug class at compile time); Rust-specific; high priority given the authenticated-peer spoofing risk
    Coding agent Suitable Requirements are explicit and complete: exact struct definitions provided, all use sites enumerated, wire/ABI compatibility constraints stated, auxiliary newtypes (AuthenticatedNodeId, CloserNodes) specified

    🔗 Similar issues

    No similar issues found.

    💡 Notes and suggestions

    Security note: The core motivation is making the AuthenticatedNodeId ↔ WireNodeId confusion a compile error. The AuthenticatedNodeId newtype should be constructible only via conn.remote_id() — do not add impl From<NodeId> for AuthenticatedNodeId. This is what closes the routing-table-poisoning class.

    Suggested sub-tasks:

    • Define NodeId and ContentHash newtypes in crates/protocol/src/lib.rs (or a new identity.rs module)
    • Define AuthenticatedNodeId newtype in crates/protocol (or crates/node), constructible only from QUIC connection identity
    • Migrate decdn_protocol::dht message types (FindValueRequest, StoreRequest, FindNodeRequest, FindNodeResponse, FindValueResponse, BatchStoreRequest)
    • Migrate crates/node/src/dht/{routing,records,publish,bucket_refresh,bootstrap,client}.rs
    • Update handle_store and handle_find_node in crates/node/src/handlers/dht.rs to accept AuthenticatedNodeId
    • Update crates/common/src/config/resolved.rs (ResolvedDht.static_active_nodes)
    • Optionally add CloserNodes wrapper enforcing MAX_CLOSER_NODES at construction

    Implementation tips:

    • Add impl From<[u8; 32]> for NodeId + impl From<NodeId> for [u8; 32] for ergonomic migration
    • clippy will catch remaining [u8; 32] literals in DHT contexts once the types are in place — use it as a migration guide
    • Confirm with cargo nextest run -p decdn-protocol and cargo nextest run -p decdn-node that no tests fail
    • Wire compatibility: verify with a postcard round-trip test that NodeId([0u8; 32]) serializes to the same bytes as [0u8; 32]

    Generated by Agentic Triage for issue #653 · ● 2.1M · ◷

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/issue-triage.md@94ec5db57374c5b04a4b8eef8b4413f9af44d63f
    
  4. assigned and unassigned on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind:securityprio:highrust

Type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions