Skip to content

fix(security): redact rpc_url from chain-RPC transport errors (#954) - #958

Merged
thiras merged 2 commits into
mainfrom
fix/954-redact-rpc-url-transport-errors
Jun 19, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/954-redact-rpc-url-transport-errors

Conversation

@thiras

@thiras thiras commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #954. Any failed chain-RPC call surfaced the full rpc_url — including the path/query where Infura/Alchemy API keys live — in operator-facing output. reqwest's Error Display appends for url (<url>), alloy's #[error("{0}")]-transparent transport error forwards it verbatim, and:

  • both binaries' main() returned anyhow::Result<()>, so the Termination impl Debug-prints the entire source chain → raw URL; and
  • the decdn-node daemon additionally logs chain errors mid-run via tracing warn!(%err).

This bypassed the established policy that config validate already follows (rpc_url: <redacted> (N chars)).

Approach

A shared sanitizer in decdn-common::redact, applied at every boundary where a chain-RPC error reaches output:

  • strip_urls — drops reqwest's for url (...) clause and, as a backstop, redacts any bare <scheme>:// token (keys off :// so it covers any scheme/case). Borrows through clean text.
  • sanitize_rpc_display (tracing sites) / sanitize_err_chain (main() boundaries).

Applied via:

  • CLI + daemon main() chokepoints (ExitCode, print the sanitized chain) — one edit each covers all propagated commands and fatal bring-up.
  • Daemon mid-run tracing sites across chain_staker_set, chain_origin_directory, node_address, reputation_indexer, payment_settlement, buyer_channel, and runtime watchers/bootstrap. Each %err rendering of a chain-RPC error is routed through the sanitizer; non-RPC store/IO/lock/validation renders are left untouched.
  • bundle pull sanitizes per-entry failures at the single EntryOutcome::failed construction site (they bypass main() via stderr and the --json report).
  • discovery parse-error path aligned to the <redacted> (N chars) form used by chain_ctx / setup.

The runtime RPC watchdog (probe_rpc) was already URL-safe (%err is plain Display = URL-free anyhow top context) but was sanitized anyway for robustness and its stale userinfo-only comment rewritten.

Tests

  • Unit cases for the sanitizer: reqwest tail, uppercase/any-scheme, query-only and userinfo secrets, URL-only message, multiple and unterminated for url (...) clauses, UTF-8 safety.
  • Integration test (crates/cli/tests/setup_redacts_rpc_error.rs) drives the real decdn binary (setup --dry-run against an unreachable ephemeral port with a fake key in the path) and asserts the key/host never reach stdout/stderr while the failure class survives.

Verification

  • cargo clippy --all-targets clean · cargo fmt -- --check clean
  • decdn-common + decdn-cli + decdn-node suites: 1517 tests pass

Review

This PR incorporates findings from a multi-agent review (code quality, silent failures, test coverage, comments): three additional missed daemon leak sites, the bundle pull stderr/JSON leak, the uppercase-scheme backstop gap, and the stale module/watchdog comments were all caught and fixed before opening.

🤖 Generated with Claude Code

Any failed chain-RPC call surfaced the full `rpc_url` — including the
path/query where Infura/Alchemy API keys live — in operator output.
reqwest's Display appends ` for url (<url>)` and alloy forwards it
transparently; both binaries' `main()` returned `anyhow::Result` (Debug
prints the whole source chain), and the daemon additionally logs chain
errors via `tracing` `warn!(%err)`.

Add a shared sanitizer in `decdn-common::redact` (`strip_urls`,
`sanitize_rpc_display`, `sanitize_err_chain`) and apply it at every
boundary where a chain-RPC error reaches output:

- CLI + daemon `main()` become `ExitCode` chokepoints that print the
  sanitized chain — covers all propagated commands and fatal bring-up.
- Daemon mid-run `tracing` sites (chain_staker_set, chain_origin_directory,
  node_address, reputation_indexer, payment_settlement, buyer_channel,
  runtime watchers/bootstrap) route the rendered error through the
  sanitizer; non-RPC store/IO/lock renders are left untouched.
- `bundle pull` sanitizes per-entry failures at construction (they bypass
  `main()` via stderr + the `--json` report).
- `discovery` parse-error path aligns to the `<redacted> (N chars)` form
  used by `chain_ctx` / `setup`.

`strip_urls` keys off the `://` separator so it scrubs any scheme/case as
a backstop, in addition to dropping reqwest's ` for url (...)` clause.

Tests: unit cases for the sanitizer (uppercase/any-scheme, query-only and
userinfo secrets, URL-only, multiple/unterminated clauses, UTF-8 safety)
plus an integration test driving the real `decdn` binary against an
unreachable RPC, asserting the key never reaches stdout/stderr.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 19, 2026 17:11

@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 addresses issue #954 by implementing robust credential and URL redaction across CLI commands and node services to prevent sensitive rpc_url secrets (such as Infura/Alchemy API keys in paths or queries) from leaking into logs, stderr, or JSON reports. It introduces utility functions in crates/common/src/redact.rs to strip entire URLs from error chains and transport failures, updates main entry points and logging sites to use these sanitizers, and adds a regression test. Feedback on the PR suggests an optimization in strip_urls to avoid an unnecessary string allocation when processing owned strings that do not contain bare URLs.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread crates/common/src/redact.rs Resolved

Copilot AI 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.

Pull request overview

This PR addresses issue #954 by preventing chain-RPC transport failures from leaking the configured rpc_url (including path/query API keys) into operator-facing output. It introduces shared URL-stripping sanitizers in decdn-common and applies them at the main error-print boundaries and at mid-run tracing log sites across the daemon and CLI.

Changes:

  • Added strip_urls + sanitize_rpc_display / sanitize_err_chain helpers in crates/common and unit tests for URL stripping behavior.
  • Switched both binaries’ main() to print a sanitized error chain and return ExitCode, avoiding anyhow’s default termination printing of the full source chain.
  • Routed daemon mid-run warn!/error! sites (and CLI bundle pull per-entry failures) through the sanitizer; added an end-to-end CLI integration regression test.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
crates/common/src/redact.rs Implements URL stripping/sanitizing helpers and adds unit tests.
crates/cli/src/main.rs Prints sanitized error chains at the CLI boundary and returns ExitCode.
crates/node/src/main.rs Prints sanitized error chains at the daemon boundary and returns ExitCode.
crates/node/src/runtime/mod.rs Sanitizes tracing output for chain-RPC failures (bootstrap + watchdog).
crates/node/src/reputation_indexer.rs Sanitizes watcher RPC error logs.
crates/node/src/payment_settlement.rs Sanitizes chain-RPC related logs in settlement paths.
crates/node/src/dht/node_address.rs Sanitizes node-address watcher RPC error logs.
crates/node/src/dht/chain_staker_set.rs Sanitizes staker-set watcher and resolve failure logs.
crates/node/src/dht/chain_origin_directory.rs Sanitizes origin-directory watcher/resolve failure logs.
crates/node/src/buyer_channel.rs Sanitizes buyer-channel chain-RPC error logs.
crates/cli/src/commands/bundle_pull.rs Sanitizes per-entry failures that bypass main() and can be serialized to JSON.
crates/cli/src/commands/discovery.rs Updates malformed-URL parse error messaging to fully redact rpc_url.
crates/cli/tests/setup_redacts_rpc_error.rs Adds end-to-end regression test to ensure rpc_url does not leak in CLI output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/common/src/redact.rs Resolved
Comment thread crates/common/src/redact.rs Resolved
Comment thread crates/cli/src/commands/discovery.rs Resolved
@github-actions

github-actions Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 83.79% (was 83.68%, ▲ +0.11%) — 39375/46991 lines

File Coverage Lines Δ
crates/cli/src/commands/bundle_pull.rs 28.94% 101/349 ▼ 0.42%
crates/cli/src/commands/discovery.rs 79.29% 111/140 ▼ 0.57%
crates/cli/src/main.rs 72.73% 24/33 ▲ +2.14%
crates/common/src/redact.rs 100.00% 175/175 ● 0.00%
crates/node/src/buyer_channel.rs 35.38% 231/653 ▼ 0.11%
crates/node/src/dht/chain_origin_directory.rs 71.25% 850/1193 ▼ 0.06%
crates/node/src/dht/chain_staker_set.rs 34.40% 118/343 ▼ 0.20%
crates/node/src/dht/node_address.rs 42.36% 86/203 ▼ 0.21%
crates/node/src/main.rs 0.00% 0/20 ● 0.00%
crates/node/src/payment_settlement.rs 50.40% 883/1752 ▼ 0.12%
crates/node/src/reputation_indexer.rs 49.32% 288/584 ▼ 0.08%
crates/node/src/runtime/mod.rs 48.90% 888/1816 ▼ 0.08%
History
Commit Total Δ vs base
76f5a5e 83.79% ▲ +0.10%
96cf520 (HEAD) 83.79% ▲ +0.11%

…assertion

Review feedback (gemini-code-assist, copilot):

- strip_urls: skip the redundant clone in the owned-no-URL fast path.
- remove_for_url_clauses / redact_bare_urls: a `)` (or other sub-delim) can
  appear unescaped in a URL path/query, and stopping the scan there would
  strand the secret-bearing tail. Scan the whitespace-free URL token to the
  next whitespace/end instead; over-consuming adjacent punctuation is safe,
  leaking a secret is not. Add a regression test for a `)`-bearing URL.
- redact_bare_urls: walk back over the full RFC-3986 scheme charset
  (alnum + `+` `-` `.`) so `git+ssh://…` etc. are covered, matching the
  "any scheme" doc.
- discovery.rs: reword the redaction comment to describe the policy rather
  than claim an exact format match.
- setup_redacts_rpc_error: the host:port assertion still checked the stale
  `127.0.0.1:1` after the switch to an ephemeral refused port; assert against
  the actual host:port and the full rpc_url instead.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit fe54032 into main Jun 19, 2026
20 checks passed
@thiras
thiras deleted the fix/954-redact-rpc-url-transport-errors branch June 19, 2026 19:31
thiras added a commit to decdn/sponsord that referenced this pull request Oct 5, 2026
* fix: keep the RPC URL out of sponsord errors and logs

reqwest names the URL in its transport errors (` for url (<url>)`) and
alloy forwards that text, so a failed chain call printed SPONSORD_RPC_URL,
often with a provider API key in its path, in the exit error and in the
keeper's and the HTTP API's warnings.

- ChainPool renders each chain-call error's cause chain with the URLs
  stripped (decdn_common::redact::strip_urls), so its errors keep the
  failure class (connection refused, timeout, DNS, TLS) and never carry
  the URL.
- The keeper and HTTP log sites go through sanitize_err_chain /
  sanitize_rpc_display as a backstop.
- main() prints its error through sanitize_err_chain instead of returning
  it to anyhow's Termination impl, as decdn-node does (decdn/decdn#958).

FakePool's failures now read like reqwest's error and name FAKE_RPC_URL,
and test_support gains CapturedLog, so the failure tests also assert that
the key never reaches a log line. A binary test checks the exit error.

Closes #38

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test: cover the keeper's remaining-read warning redaction

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix: close the remaining RPC URL leaks and keep decdn's top-up markers

Review follow-ups for #38:

- RUST_LOG=debug printed the URL on every chain call: alloy's
  `ReqwestTransport` debug span records it, and every event inside the
  span (reqwest's, hyper's) prints the span's fields. main() now builds
  the RUST_LOG filter itself with `alloy_transport_http=info` added,
  which only drops that span and alloy's own debug/trace events.
- `--help` printed SPONSORD_RPC_URL's value; it is now hidden like the
  token and password.
- `ChainConfig`'s Debug showed the URL; it now shows its length.
- Flattening `top_up` errors to text lost decdn's `TopUpUnconfirmed` and
  `AllowanceShortfall` markers, which callers downcast on (the former
  means the top-up may have mined). `redacted_top_up` re-attaches them.
- The http.rs log sites used `sanitize_rpc_display` on an anyhow::Error,
  which renders only the outermost message; they use
  `sanitize_err_chain` now.
- The listener is bound before the keeper starts, so a bind failure
  can no longer drop a top-up between the approve and the top-up.

Adds unit tests for `redacted()` and the marker handling, a closed-port
test of every ChainPool method, a `--help` test, and a binary test at
RUST_LOG=trace. Fixes the doc on main (the Termination impl is std's),
the changelog wording, and moves sponsord-core's Security section last.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

security: chain RPC transport errors leak the full rpc_url (path/query API keys) to operator output

2 participants