descriptor, rpc, wallet: do not echo key material in error messages #36648

pull fametrano wants to merge 1 commits into bitcoin:master from fametrano:descriptor-errors-no-keys changing 12 files +235 −64
  1. fametrano commented at 12:49 PM on October 24, 2026: none

    Some descriptor error messages quote the string the caller sent, and a descriptor can hold a private key. The error then carries the key, or part of it, into places the request may not go: an application's log, an error reporter, a screenshot shared for help.

    This PR stops the quoting. A failing key is named by its position, a failing entry of solving_data.descriptors by its index, and a function name is quoted only where the parser accepts it. The commit message lists each changed error.

    before after
    key '<key>' is not valid key is not valid
    Multi: key '<key>' is not valid Multi: key 3: key is not valid
    'wpkh(<key>)x' is not a valid descriptor function Unexpected text after the closing ')' of wpkh()
    '<start of a key>(<rest>' is not a valid descriptor function Not a valid descriptor expression
    Unable to parse descriptor '<descriptor>': Missing checksum Unable to parse descriptor at index 1: Missing checksum
    Could not add descriptor '<descriptor>': ... the same, quoting the public form of the descriptor
  2. DrahtBot commented at 12:49 PM on October 24, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Stale ACK jpk68

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36885 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36885.svg"></sub> (wallet, rpc: Add analyzedescriptor for multisig setups by pablomartin4btc)
    • #36444 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36444.svg"></sub> (wallet: Store information about created and imported multipath descriptors by achow101)
    • #36443 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36443.svg"></sub> (descriptor: Reconstruct a multipath descriptor from its expansion by achow101)
    • #36442 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36442.svg"></sub> (descriptor: Represent multipath descriptors in a single Descriptor object by achow101)
    • #36402 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36402.svg"></sub> (refactor: wallet_importdescriptors to separate run_test() contents into specific functions by nebula-21)
    • #36257 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36257.svg"></sub> (qa: assert_equals -> assert_true/assert_false by hodlinator)
    • #36122 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36122.svg"></sub> (BIP460: CISA for Taproot key path spends by fjahr)
    • #35918 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35918.svg"></sub> (descriptor: Expose key expression parsing by pseudoramdom)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. jpk68 commented at 8:35 PM on October 27, 2026: contributor

    ACK ad18016046b478f9d9adc807aab83328578ea603

  4. in src/script/descriptor.cpp:2831 in a3f26e8343
    2827 | +    const std::string_view expr_text{expr.begin(), expr.end()};
    2828 | +    const size_t paren{expr_text.find('(')};
    2829 | +    if (paren == std::string_view::npos || paren == 0 || paren > MAX_FUNCTION_NAME_CHARS) {
    2830 | +        error = "Not a valid descriptor function";
    2831 | +    } else {
    2832 | +        error = strprintf("'%s(...' is not a valid descriptor function", expr_text.substr(0, paren));
    


    uqlidi commented at 5:32 PM on October 28, 2026:

    For wpkh(KEY)x, the error is now 'wpkh(...' is not a valid descriptor function. wpkh is a valid function; the real problem is the trailing x. The old message echoed the full text, so the mistake was visible.

    you can make it skip the key inside the function print the chars that is connected to ).

    somethiing like that:

    Invalid descriptor expression 'wpkh(...)x'
    

    uqlidi commented at 5:38 PM on October 28, 2026:

    suggested diff:

    diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp
    index 3ae2da81d..29dbfa3a7 100644
    --- a/src/script/descriptor.cpp
    +++ b/src/script/descriptor.cpp
    @@ -2828,15 +2828,29 @@ std::vector<std::unique_ptr<DescriptorImpl>> ParseScript(uint32_t& key_exp_index
             error = "A function is needed within P2WSH";
             return {};
         }
    -    // Quote the function name only if it is 16 characters or fewer: every key is longer, and a bare key has no '('
    +    // Show the expression as "name(...)rest", where the arguments are never quoted, the name
    +    // only if it is 16 characters or fewer (every key is longer, and a bare key has no '('),
    +    // and the text after the matching ')' only if it is no longer than MAX_QUOTED_TOKEN_CHARS.
         constexpr size_t MAX_FUNCTION_NAME_CHARS{16};
         const std::string_view expr_text{expr.begin(), expr.end()};
    -    const size_t paren{expr_text.find('(')};
    -    if (paren == std::string_view::npos || paren == 0 || paren > MAX_FUNCTION_NAME_CHARS) {
    +    const size_t open{expr_text.find('(')};
    +    if (open == std::string_view::npos || open == 0 || open > MAX_FUNCTION_NAME_CHARS) {
             error = "Not a valid descriptor function";
    -    } else {
    -        error = strprintf("'%s(...' is not a valid descriptor function", expr_text.substr(0, paren));
    +        return {};
    +    }
    +    std::string shown{strprintf("%s(...", expr_text.substr(0, open))};
    +    int depth{0};
    +    for (size_t i{open}; i < expr_text.size(); ++i) {
    +        if (expr_text[i] == '(') {
    +            ++depth;
    +        } else if (expr_text[i] == ')' && --depth == 0) {
    +            const std::string_view rest{expr_text.substr(i + 1)};
    +            shown += ')';
    +            shown += rest.size() <= MAX_QUOTED_TOKEN_CHARS ? rest : std::string_view{"..."};
    +            break;
    +        }
         }
    +    error = strprintf("Invalid descriptor expression '%s'", shown);
         return {};
     }
    

    fametrano commented at 6:52 PM on November 1, 2026:

    Done: wpkh(KEY)x now gives Unexpected text after the closing ')' of wpkh(), which quotes the name only because it is a function the parser accepts, and never the text after the ), which can be the end of a key.

  5. in src/script/descriptor.cpp:2037 in a3f26e8343 outdated
    2033 | @@ -2021,7 +2034,7 @@ std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t& key_exp_
    2034 |                      return ret;
    2035 |                  }
    2036 |              }
    2037 | -            error = strprintf("Pubkey '%s' is invalid", str);
    2038 | +            error = "Pubkey is invalid";
    


    uqlidi commented at 5:49 PM on October 28, 2026:

    In a descriptor with many keys, the error doesn't say which one failed. For wsh(multi(2,KEY1,KEY2,KEY3)) with a typo in KEY3, the error is only:

    Multi: key is not valid
    

    With 15 keys, the user has to check each one by hand.

    You can print the key's position. key_exp_index already counts the keys parsed before the failing one, so no key text is needed.

    Something like that:

    Multi: Key [#3](/bitcoin-bitcoin/3/) is not valid
    

    Note: the count includes each musig() aggregate key, so a key after a musig() gets a number one higher than a user would count. The test expectations for these messages still need updating.


    uqlidi commented at 5:49 PM on October 28, 2026:

    In a descriptor with many keys, the error doesn't say which one failed. For wsh(multi(2,KEY1,KEY2,KEY3)) with a typo in KEY3, the error is only:

    Multi: key is not valid
    

    With 15 keys, the user has to check each one by hand.

    You can print the key's position. key_exp_index already counts the keys parsed before the failing one, so no key text is needed.

    Something like that:

    Multi: Key [#3](/bitcoin-bitcoin/3/) is not valid
    

    Note: the count includes each musig() aggregate key, so a key after a musig() gets a number one higher than a user would count. The test expectations for these messages still need updating.


    fametrano commented at 6:52 PM on November 1, 2026:

    Done: the error gives the key's position in its list, counted from 1, as in Multi: key 3: key is not valid, and a key after a musig() is not counted one higher.


    fametrano commented at 6:52 PM on November 1, 2026:

    Done, as in the other copy of this comment: Multi: key 3: key is not valid, counted within the list.

  6. in src/wallet/rpc/spend.cpp:632 in a3f26e8343
     628 | @@ -629,7 +629,7 @@ CreatedTransactionResult FundTransaction(CWallet& wallet, const CMutableTransact
     629 |                  std::vector<CScript> scripts_temp;
     630 |                  auto descs = Parse(desc_str, desc_out, error, true);
     631 |                  if (descs.empty()) {
     632 | -                    throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to parse descriptor '%s': %s", desc_str, error));
     633 | +                    throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to parse descriptor: %s", error));
    


    uqlidi commented at 5:56 PM on October 28, 2026:

    similar to the key index solving_data.descriptors is a list, now that the descriptor is no longer printed, the error doesn't say which one failed:

    Unable to parse descriptor: Missing checksum
    

    You can print its index in the list.

    should be:

    Unable to parse descriptor at index 1: Missing checksum
    

    we could do something like that instead:

    diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp
    index 383f752dc..0ab874793 100644
    --- a/src/wallet/rpc/spend.cpp
    +++ b/src/wallet/rpc/spend.cpp
    @@ -622,14 +622,15 @@ CreatedTransactionResult FundTransaction(CWallet& wallet, const CMutableTransact
             }
     
             if (solving_data.exists("descriptors")) {
    -            for (const UniValue& desc_univ : solving_data["descriptors"].get_array().getValues()) {
    -                const std::string& desc_str  = desc_univ.get_str();
    +            const auto& desc_values{solving_data["descriptors"].get_array().getValues()};
    +            for (size_t desc_index{0}; desc_index < desc_values.size(); ++desc_index) {
    +                const std::string& desc_str{desc_values[desc_index].get_str()};
                     FlatSigningProvider desc_out;
                     std::string error;
                     std::vector<CScript> scripts_temp;
                     auto descs = Parse(desc_str, desc_out, error, true);
                     if (descs.empty()) {
    -                    throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to parse descriptor: %s", error));
    +                    throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to parse descriptor at index %u: %s", desc_index, error));
                     }
                     for (auto& desc : descs) {
                         desc->Expand(0, desc_out, scripts_temp, desc_out);
    

    fametrano commented at 6:52 PM on November 1, 2026:

    Done with your loop: Unable to parse descriptor at index 1: Missing checksum.

  7. in src/util/bip32.cpp:34 in a3f26e8343
      29 | @@ -30,6 +30,8 @@ util::Expected<KeyPathElement, std::string> ParseKeyPathElement(std::span<const
      30 |  
      31 |      const auto number{ToIntegral<uint32_t>(std::string_view{elem.begin(), elem.end()})};
      32 |      if (!number) {
      33 | +        // A value longer than any uint32 may be a key written in the path: do not quote it
      34 | +        if (raw.size() > 11) return util::Unexpected{std::string{"Key path value is not a valid uint32"}};
    


    uqlidi commented at 5:59 PM on October 28, 2026:

    i see you are using the same value of MAX_QUOTED_CHARS in https://github.com/bitcoin/bitcoin/pull/36648/changes#diff-dcb25ae6b5207f65ae6a1cd364c9e62e5a9df69a1cd21b9ed79849cc99c2150bR1880

    you can define it here and reuse it inside descriptor.cpp


    fametrano commented at 6:52 PM on November 1, 2026:

    MAX_QUOTED_CHARS is gone: no error quotes text by length any more, so there is nothing left to share with bip32.cpp.

  8. fametrano commented at 6:57 PM on November 1, 2026: none

    @uqlidi Thanks, I addressed all four points; the replies are inline.

  9. DrahtBot added the label CI failed on Nov 1, 2026
  10. DrahtBot commented at 7:55 PM on November 1, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/41338732479/job/124943987697</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU/clang-format modified src/script/descriptor.cpp (adding <ranges>), so the lint check detected uncommitted formatting/include changes.</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  11. fametrano commented at 1:27 AM on November 2, 2026: none

    I pushed a fix for the CI failure, and one of the two suggestions in the Ralph review.

    The iwyu task wanted #include <ranges> in descriptor.cpp, and rpc_getdescriptoractivity.py still expected the old message for addrx(...). Both are fixed.

    An expression with miniscript wrappers that fails to parse, such as z:pk(K) or vv:pk(K), now gives "Not a valid miniscript expression" instead of "Not a valid pk() expression". The wrappers may be what is wrong, and pk(K) itself can be valid.

    The other suggestion adds the position of the failing tr() leaf and key path element, as in "tr(): leaf 2: pk(): key is not valid". It rewords errors master already has: 22 expectations change in descriptor_tests.cpp, 12 of them in master's own tests (+33 −27 in two files). I can add it here if a maintainer wants it.

  12. DrahtBot removed the label CI failed on Nov 2, 2026
  13. DrahtBot added the label Needs rebase on Nov 4, 2026
  14. descriptor, rpc, wallet: do not echo key material in error messages
    Several error messages quote text from the descriptor the caller sent,
    and that text can be a private key. Error text is often logged or
    shared where the request is not. Even the first characters of a key
    give away part of it.
    
    Parsing (src/script/descriptor.cpp, src/util/bip32.cpp):
    - "Key '%s' is invalid due to whitespace", "key '%s' is not valid" and
      "Pubkey '%s' is invalid" quoted the key. They quote nothing now.
    - A key that fails inside multi(), sortedmulti(), multi_a(),
      sortedmulti_a(), musig() or a miniscript is named by its position, as
      in "Multi: key 3: key is not valid" and "Miniscript: key 2: key is not
      valid".
    - "Multi threshold '%s' is not valid", "Key path value '%s' specifies
      multipath ..." and "Key path value '%s' is not a valid uint32" quote
      nothing now. A stray ',' or '/' inside a key can leave a short
      fragment of it as the threshold or a path value.
    - "Provided checksum '%s' does not match computed checksum '%s'" no
      longer quotes the provided checksum, "Key origin start '[ character
      expected but not found, got '%c' instead" no longer quotes the
      character, and "Fingerprint '%s' is not hex" no longer quotes the
      fingerprint. A stray '#', ']' or '[' inside a key puts part of it
      there.
    - "'%s' is not a valid descriptor function" no longer quotes the
      expression. A stray '(' inside a key leaves its first characters where
      a function name is expected, and a stray ')' leaves its last
      characters after the closing ')'. The message quotes the function name
      only where it is one of the descriptor and miniscript functions the
      parser accepts, as in "Not a valid pk() expression" and "Unexpected
      text after the closing ')' of wpkh()". Otherwise it reads "Not a valid
      descriptor expression", or "Unexpected text after the closing ')'".
      An expression with miniscript wrappers that fails to parse, as in
      z:pk(K), reads "Not a valid miniscript expression": the wrappers may
      be what is invalid.
    
    Callers (src/rpc/util.cpp, src/wallet/imports.cpp,
    src/wallet/rpc/spend.cpp):
    - "Cannot derive script without private keys" and "Could not add
      descriptor" quote desc->ToString(), the public form, instead of the
      string sent.
    - "Unable to parse descriptor" no longer quotes the string, and gives
      its index in solving_data.descriptors instead. No parsed descriptor
      exists there, and the parse error says what is wrong.
    
    Tests are updated for the new messages. New tests check that no part of
    a key is in the parse errors and the caller errors, including a key with
    a typo in its last character or a stray ',', '/', '(', ')', '#', '[' or
    ']' inside it. They also check the exact text of the caller errors.
    ad18016046
  15. DrahtBot removed the label Needs rebase on Nov 5, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-20 15:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me