Skip to content

Sanitizing ports of -rpcconnect and -rpcport. - #27820

Closed
Brotcrunsher wants to merge 1 commit into
bitcoin:masterfrom
Brotcrunsher:PortSanitizing
Closed

Brotcrunsher wants to merge 1 commit into
bitcoin:masterfrom
Brotcrunsher:PortSanitizing

Conversation

@Brotcrunsher

Copy link
Copy Markdown
Contributor

Previously, if they contained malformed ports they were silently interpreted as value%0xffff. Illegal ports now lead to an error. Additionally, if rpcconnect has a port and rpcport is set, a useful warning is now printed.

…tained malformed ports they were silently interpreted as value%0xffff. Illegal ports now lead to an error. Additionally, if rpcconnect has a port and rpcport is set, a useful warning is now printed.
@DrahtBot

DrahtBot commented Jun 4, 2023 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept ACK luke-jr, S3RK

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

@luke-jr

luke-jr commented Jun 24, 2023

Copy link
Copy Markdown
Contributor

Concept ACK

@achow101
achow101 requested review from S3RK and achow101 September 20, 2023 16:53

@S3RK S3RK left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK.

Could you separate the PR into two commits? PR does two separate things, each of them could be placed in a separate commit:

  1. Handling of incorrect port values
  2. Warning in case both params have a port.

Also, could you add a functional test for the first? You can see similar examples in test/functional/interface_bitcoin_cli.py

Comment thread src/bitcoin-cli.cpp
{
bool containsPort;
if (!SplitHostPort(gArgs.GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host, &containsPort)) {
throw std::runtime_error(strprintf("Malformed Port in -rpcconnect: %s", gArgs.GetArg("-rpcconnect", "")));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@hebasto we can reuse InvalidPortErrMsg() here, but I don't see any other users of bilingual strings. What do you recommend for bitcoin-cli?

Comment thread src/bitcoin-cli.cpp
if (gArgs.IsArgSet("-rpcport")) {
int64_t desiredRpcPort = gArgs.GetIntArg("-rpcport", port);
if (desiredRpcPort <= 0 || desiredRpcPort > 0xFFFF) {
throw std::runtime_error(strprintf("Malformed Port in -rpcport: %lld", desiredRpcPort));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: better to read port as a string to show it back to the user exactly as it was written.

this is how src/init.cpp does it

const std::string port = args.GetArg(port_option, "");
uint16_t n;
if (!ParseUInt16(port, &n) || n == 0) {
    return InitError(InvalidPortErrMsg(port_option, port));
}

Comment thread src/util/strencodings.cpp
template std::optional<std::vector<uint8_t>> TryParseHex(std::string_view);

bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut, bool* cPortOut)

@S3RK S3RK Sep 25, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not a huge fan of this new parameter. The return value has almost exactly what we need, can we reuse it somehow?

@DrahtBot

Copy link
Copy Markdown
Contributor

Are you still working on this?

@fanquake

fanquake commented Jun 6, 2024

Copy link
Copy Markdown
Member

Picked up in #29521.

achow101 added a commit that referenced this pull request Jun 11, 2024
24bc46c cli: Add warning for duplicate port definition (tdb3)
e208fb5 cli: Sanitize ports in rpcconnect and rpcport (tdb3)

Pull request description:

  Adds invalid port detection to bitcoin-cli for -rpcconnect and -rpcport.

  In addition to detecting malformed/invalid ports (e.g. those outside of the 16-bit port range, not numbers, etc.), bitcoin-cli also now considers usage of port 0 to be invalid.  bitcoin-cli previously considered port 0 to be valid and attempted to use it to reach bitcoind.

  Functional tests were added for invalid port detection as well as port prioritization.
  Additionally, a warning is provided when a port is specified in both -rpcconnect and -rpcport.

  This PR is an alternate approach to PR #27820 (e.g. SplitHostPort is unmodified).

  Considered an alternative to 127.0.0.1 being specified in functional tests, but at first glance, this might need an update to test_framework/util.py (e.g.  rpc_url), which might be left to a future PR.

ACKs for top commit:
  S3RK:
    light code review ACK 24bc46c
  achow101:
    ACK 24bc46c
  cbergqvist:
    re ACK 24bc46c

Tree-SHA512: c83ab6a30a08dd1ac8b368a7dcc2b4f23170f0b61dd67ffcad7bcda05096d333bcb9821fba11018151f55b2929c0a333bfec15b8bb863d83f41fc1974c6efca5
@bitcoin bitcoin locked and limited conversation to collaborators Jun 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants