Skip to content

fix(config): reject bind_port == metrics_port; warn on well-known ports (#267) - #290

Merged
alpergundogdu merged 2 commits into
mainfrom
fix/267-port-validation
Apr 22, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
fix/267-port-validation

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Summary

Adds a cross-section port-layout check to resolve_config:

  • Hard error when network.bind_port == observability.metrics_port. QUIC (UDP) and the metrics server (TCP) wouldn't actually collide at the OS bind layer, but setting the two equal is almost certainly an operator typo — the check fails fast at config load with both field names in the message.
  • Stderr warning when either port is in the well-known range (<1024). Not a hard failure — operators legitimately run QUIC on 443 or use CAP_NET_BIND_SERVICE — but the warning surfaces the "ran as non-root, wondered why bind failed" case that's otherwise a cryptic startup error.

Lives next to ensure_region_when_publishing_global because it's inherently cross-section: inputs come from network and observability, so the rule belongs at the resolver boundary rather than inside either sub-resolver.

Closes #267.

Why eprintln! for the warning

Config resolution runs before tracing_subscriber is initialized (see commands::run — config first, then init_tracing). tracing::warn! at this point would be eaten by the default no-op subscriber. eprintln! matches the existing pattern already used at commands.rs for the "ignoring malformed RUST_LOG" warning.

Test plan

  • cargo clippy -p decdn-node --all-targets -- -D warnings → clean
  • cargo test -p decdn-node --lib config::tests::validate_port_layout → 3 new tests pass
    • validate_port_layout_rejects_bind_equal_metrics
    • validate_port_layout_accepts_distinct_ports
    • validate_port_layout_allows_well_known_port (pins the "warn, don't fail" contract for <1024)
  • cargo test -p decdn-node → full suite 57 passed
  • Pre-commit (fmt / clippy / deny) → all passed

🤖 Generated with Claude Code

…ts (#267)

A shared port number across network.bind_port (QUIC/UDP) and
observability.metrics_port (HTTP/TCP) wouldn't actually collide at bind
time because of the UDP/TCP split, but it's almost certainly an
operator typo — they'll file a bug when the two log streams get
confusing — so fail fast at config resolution instead. Ports below 1024
get a stderr warning (not an error): operators legitimately bind QUIC
on 443 or use CAP_NET_BIND_SERVICE, but flagging the well-known range
surfaces the common "ran as non-root, wondered why bind failed" case.

The new check lives next to `ensure_region_when_publishing_global` — it
is inherently cross-section (inputs come from `network` and
`observability`) so it belongs at the resolver boundary, not inside
either sub-resolver.

Closes #267.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a validation step for node port assignments to prevent collisions between the network bind port and the metrics port, while also providing warnings for ports in the well-known range (< 1024). The feedback suggests refining this logic to correctly handle port 0, which signifies ephemeral port allocation; specifically, the collision check should allow both ports to be 0, and the well-known port warning should be suppressed for port 0 to avoid misleading messages about elevated privileges.

Comment thread crates/node/src/config/mod.rs Outdated
Comment on lines +112 to +119
anyhow::ensure!(
network.bind_port != observability.metrics_port,
"network.bind_port ({}) must differ from observability.metrics_port ({}); \
QUIC (UDP) and metrics (TCP) would not collide at bind time, but sharing \
the same port number is almost certainly an operator typo",
network.bind_port,
observability.metrics_port,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The collision check currently rejects configurations where both ports are set to 0. In networking, port 0 is a special value that instructs the OS to allocate an ephemeral (random) port. If both bind_port and metrics_port are set to 0, the OS will allocate two distinct ports, so they won't actually collide. This check should be relaxed to allow port 0, which is common in testing or dynamic environments.

Suggested change
anyhow::ensure!(
network.bind_port != observability.metrics_port,
"network.bind_port ({}) must differ from observability.metrics_port ({}); \
QUIC (UDP) and metrics (TCP) would not collide at bind time, but sharing \
the same port number is almost certainly an operator typo",
network.bind_port,
observability.metrics_port,
);
anyhow::ensure!(
network.bind_port == 0 || observability.metrics_port == 0 || network.bind_port != observability.metrics_port,
"network.bind_port ({}) must differ from observability.metrics_port ({}); \
QUIC (UDP) and metrics (TCP) would not collide at bind time, but sharing \
the same port number is almost certainly an operator typo",
network.bind_port,
observability.metrics_port,
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — port 0 is "let the OS pick", so two zeros resolve to distinct ephemeral ports and don't collide. Fixed in 3e47f14: the equality check now short-circuits when either side is 0. New test validate_port_layout_allows_both_zero pins the contract.

I went with an if network.bind_port != 0 && observability.metrics_port != 0 guard around the ensure! rather than the inline disjunction — same semantics, but keeps the error message generator from reading "must differ from" predicates on an inherently-symmetric 0==0 case.

Comment thread crates/node/src/config/mod.rs Outdated
("network.bind_port", network.bind_port),
("observability.metrics_port", observability.metrics_port),
] {
if port < 1024 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The warning for well-known ports (< 1024) currently triggers for port 0. However, port 0 is not a well-known port and does not require elevated privileges to bind (it's a request for an ephemeral port). The warning message about requiring elevated privileges is misleading in this case. The check should be updated to exclude port 0.

Suggested change
if port < 1024 {
if port > 0 && port < 1024 {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — port 0 isn't well-known and needs no elevated privilege, so the warning text was misleading. Fixed in 3e47f14 using (1..1024).contains(&port) to exclude 0. Same behavior as the suggested port > 0 && port < 1024, just the idiomatic-Rust range form.

Port 0 is the sentinel for OS-assigned ephemeral ports: two zeros
resolve to distinct ports at bind time (no collision), and port 0
needs no elevated privilege (not a well-known port).

- Skip the bind==metrics equality check when either side is 0
- Skip the <1024 well-known warning for port 0 (the warning's
  "requires elevated privilege" text was misleading for it)

Adds two regression tests: both-zero accepted, zero-with-nonzero
accepted.

Addresses review comments on PR #290.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@alpergundogdu
alpergundogdu merged commit f1c4276 into main Apr 22, 2026
9 checks passed
@alpergundogdu
alpergundogdu deleted the fix/267-port-validation branch April 22, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

config: bind_port is not validated to be outside the well-known port range (< 1024) nor checked for conflicts with metrics_port before startup

1 participant