Skip to content

Exit and show error if unrecognized command line args are present#742

Merged
hebasto merged 1 commit into
bitcoin-core:masterfrom
john-moffett:2023_06_ExitOnLooseArgument
Oct 25, 2023
Merged

Exit and show error if unrecognized command line args are present#742
hebasto merged 1 commit into
bitcoin-core:masterfrom
john-moffett:2023_06_ExitOnLooseArgument

Conversation

@john-moffett

@john-moffett john-moffett commented Jun 29, 2023

Copy link
Copy Markdown
Contributor

Fixes #741

Starting bitcoin-qt with non-hyphen ("-") arguments causes it to silently ignore any later valid options. For instance, invoking bitcoin-qt -server=1 foo -regtest on a fresh install will run mainnet instead of regtest.

This change makes the client exit with an error message if any such "loose" arguments are encountered. This mirrors how bitcoind handles it:

https://github.com/bitcoin/bitcoin/blob/c6287faae4c0e705a9258a340dfcf548906f12af/src/bitcoind.cpp#L127-L132

However, BIP-21 bitcoin: payment URIs are still allowed, but only if they're not followed by any additional options.

@DrahtBot

DrahtBot commented Jun 29, 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
ACK hernanmarino, pablomartin4btc, maflcko, hebasto

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

@maflcko

maflcko commented Jun 29, 2023

Copy link
Copy Markdown
Contributor

I always wondered why the code wasn't the same. Was this needed for payment requests, when they were linked to be opened by the gui application?

@john-moffett

Copy link
Copy Markdown
Contributor Author

I'm not entirely sure why they weren't the same. In the beginning, bitcoind would handle RPC commands. Then QT got special-cased so that you couldn't do that for bitcoin-qt -- though it wouldn't give any errors. It would just silently ignore the subsequent options.

Then the argument-handling code split entirely here. Now bitcoind and bitcoin-qt mostly did their own thing.

Subsequently, the ability to call RPC methods from bitcoind's command line was removed, and "loose" (non-hyphen) arguments would result in errors.

I can't see any good reason for "loose" arguments to remain in bitcoin-qt.

The "ignored options" issue was actually first raised here. The fix didn't address the specific issue exactly, as it just caught unrecognized options (ie - arguments with a hyphen). But by then, as I mentioned, the "loose" argument problem had already been fixed in bitcoind.

@achow101

achow101 commented Jun 29, 2023

Copy link
Copy Markdown
Member

I think this is needed to handle BIP 21 URIs (not to be confused with the now removed BIP 70 payment protocol). The "loose" argument is interpreted as the URI, although ignored if they could not be recognized as one. Anything that follows it is explicitly ignored to avoid argument injection, see https://achow101.com/2021/02/0.18-uri-vuln.

@john-moffett

Copy link
Copy Markdown
Contributor Author

Ah, thanks @achow101. I think this may still be salvageable by making an exception for just those arguments.

if (arg.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) // bitcoin: URI

Will revisit tomorrow.

@john-moffett
john-moffett force-pushed the 2023_06_ExitOnLooseArgument branch from 250b171 to 505c726 Compare June 30, 2023 19:59
@hebasto

hebasto commented Jul 3, 2023

Copy link
Copy Markdown
Member

Is it safer and simpler to just document that a payment URi must follow all command line options? In bitcoin-qt -help?

@john-moffett

Copy link
Copy Markdown
Contributor Author

I could modify this PR to enforce that behavior and add the documentation to bitcoin-qt -help. Right now, the PR only allows non-option arguments which must start with bitcoin: (case insensitive). It will still ignore any options after that.

I think leaving the current master behavior and just adding documentation to reflect it would be insufficient. My worry is that a user could be fooled by something like:

Scammer: "Can you send me some testnet coins? Start your client with bitcoin-qt select-chain -testnet. You should have the same number of testnet coins as you have on the real network. Just send me some, please."

It's also just an annoyance. I spent an embarrassingly long time trying to figure out why bitcoin-qt datadir=bitcoindatadir -regtest kept opening mainnet before I realized I omitted the hyphen on datadir.

Starting bitcoin-qt with non-dash ("-") arguments causes it to
silently ignore any later valid options. This change makes the
client exit with an error message if any such "loose" arguments
are encountered.

However, allow BIP-21 'bitcoin:' URIs only if no other options
follow.
@john-moffett
john-moffett force-pushed the 2023_06_ExitOnLooseArgument branch from 505c726 to 51e4dc4 Compare July 3, 2023 17:05
@john-moffett

Copy link
Copy Markdown
Contributor Author

Modified to follow @hebasto 's idea of only allowing bitcoin: URIs if no other options follow.

@hebasto
hebasto requested a review from achow101 July 4, 2023 15:58

@hernanmarino hernanmarino 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.

tested ACK 51e4dc4

@pablomartin4btc pablomartin4btc 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.

tACK 51e4dc4

I like both features the error shown when invalid args are passed and the error for options that are passed following a BIP21.

For the last one and regarding the -regtest being ignored in your original PR description, I detected a similar scenario in bitcoin-cli a few months ago (bitcoin/bitcoin#26990 - still in progress) where [options] passed after a command that receives arguments are not detected by the ArgsManager, they are being interpreted as another arg of the last command.

Last thing, not part of the scope of this PR, I think we could validate the BIP21 URI grammar before opening bitcoin-qt.

Comment thread src/qt/bitcoin.cpp
if (payment_server_token_seen && arg.startsWith("-")) {
InitError(Untranslated(strprintf("Options ('%s') cannot follow a BIP-21 payment URI", argv[i])));
QMessageBox::critical(nullptr, PACKAGE_NAME,
// message cannot be translated because translations have not been initialized

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we just initialize them in this codepath?

Comment thread src/qt/bitcoin.cpp
}
#endif
if (payment_server_token_seen && arg.startsWith("-")) {
InitError(Untranslated(strprintf("Options ('%s') cannot follow a BIP-21 payment URI", argv[i])));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
InitError(Untranslated(strprintf("Options ('%s') cannot follow a BIP-21 payment URI", argv[i])));
InitError(Untranslated(strprintf("Options (eg, '%s') cannot follow a URI", argv[i])));

@maflcko

maflcko commented Oct 20, 2023

Copy link
Copy Markdown
Contributor

lgtm ACK 51e4dc4

@maflcko

maflcko commented Oct 25, 2023

Copy link
Copy Markdown
Contributor

Fuzz CI failure can be ignored

@hebasto hebasto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 51e4dc4, I have reviewed the code and it looks OK.

@hebasto
hebasto merged commit afa081a into bitcoin-core:master Oct 25, 2023
hebasto added a commit to bitcoin/bitcoin that referenced this pull request Feb 11, 2024
…for BIP21 URIs

ede5014 Modify command line help to show support for BIP21 URIs (Hernan Marino)

Pull request description:

  While reviewing a different PR (see bitcoin-core/gui#742 ) **hebasto** suggested that the help for bitcoin-qt should be updated to reflect the fact that bitcoin-qt supports an optional BIP21 URI parameter.

  Since this reflects actual behaviour of bitcoin-qt and is independent of whether or not the other PR gets merged, I created this simple PR to fix the help message.

ACKs for top commit:
  kristapsk:
    utACK ede5014
  pablomartin4btc:
    lgtm, re ACK ede5014
  hebasto:
    ACK ede5014.

Tree-SHA512: c456297c486bc5cc65e0e092e7ba9d51b0bd7a584d4fabca7f7ca1f8e58cbcc66e96226539c689ed0f5e7f40da220bbc4ea30b90e31e1aeeb8867a385a90209c
@bitcoin-core bitcoin-core locked and limited conversation to collaborators Oct 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Command line options after any non-hyphen arguments are silently ignored

8 participants