Skip to content

test: Add coverage for Tor control HASHEDPASSWORD authentication - #35292

Open
winterrdog wants to merge 1 commit into
bitcoin:masterfrom
winterrdog:test/torcontrol-hashedpassword-auth
Open

winterrdog wants to merge 1 commit into
bitcoin:masterfrom
winterrdog:test/torcontrol-hashedpassword-auth

Conversation

@winterrdog

@winterrdog winterrdog commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

this is a tests-only PR aimed at adding functional test coverage for
Tor control HASHEDPASSWORD authentication.

currently, the functional test suite does not explicitly explore this
authentication path, which means regressions in tor authentication
handling could go unnoticed. for instance incorrectly formatted
AUTHENTICATE commands, broken fallback behavior, or authentication
attempts being made when no password is configured.

The 4 tests herein extend the existing mock tor control server to
simulate METHODS=HASHEDPASSWORD responses and cover successful
authentication with the correct password, failing with an incorrect
password, behavior when -torpassword is not set, and cases where the
server does not choose to advertise HASHEDPASSWORD as a way of
authenticating.

@DrahtBot DrahtBot added the Tests label May 14, 2026
@DrahtBot

DrahtBot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36497 (test: Add coverage for TorControl SAFECOOKIE authentication by winterrdog)

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.

@sedited

sedited commented May 14, 2026

Copy link
Copy Markdown
Contributor

@winterrdog can you re-write the description in your own words without following this LLM-formulaic format? This would give reviewers more confidence that you understand the change.

@winterrdog

Copy link
Copy Markdown
Contributor Author

re-write the description in your own words without following this LLM-formulaic format?

thanks! done edition (seems like i followed the wrong examples online).

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/25877900203/job/76052468485
LLM reason (✨ experimental): CI failed due to Python lint errors from ruff (F541: extraneous f-string prefixes) in test/functional/feature_torcontrol.py.

Hints

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.

@winterrdog
winterrdog force-pushed the test/torcontrol-hashedpassword-auth branch 3 times, most recently from 8b2af28 to f886aa2 Compare May 14, 2026 20:34
Comment thread test/functional/feature_torcontrol.py Outdated
assert_equal(mock_tor.received_commands[1], 'AUTHENTICATE "wrong_password"')
# After auth failure, no further commands should be sent and authentication failure should be logged
ensure_for(duration=2, f=lambda: len(mock_tor.received_commands) == 2)
self.wait_until(lambda: any("Authentication failed" in line for line in self.nodes[0].debug_log_path.read_text().splitlines()))

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.

What is this testing separately from the correct password test?

@winterrdog winterrdog May 15, 2026 •

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.

this log check was an extra step i added to fully confirm that the error was actually noticed and reported instead of being silently swallowed. it was not needed in the correct password test since there we already verify that Bitcoin Core continues past authentication by checking that more than 2 commands (AUTHENTICATE and PROTOCOLINFO - the initial commands) are sent.

here comes the long story. let's assume the correct password is the happy path and the wrong password is not.

so in the happy path (correct password), this is what happens in Bitcoin Core:

correct password sent
    -> tor daemon returns `250 OK`
        -> `TorController::auth_cb` hits the `if` branch & continues on to `GETINFO` and `ADD_ONION`
            -> connection stays active

now for the sad path (wrong password):

incorrect password sent
    -> tor daemon returns `515 Bad authentication`
        -> `TorController::auth_cb` hits the `else` branch
            -> `"tor: Authentication failed"` is logged
                -> no further commands are sent
                    -> connection effectively stops

the key difference here is that the wrong password test verifies Bitcoin Core logs and stops trying to do anything else when authentication fails, which is not the case with a correct password as you saw above.

did you have something different in mind ?

@0xShadowX

0xShadowX commented Sep 28, 2026 •

Copy link
Copy Markdown

#36374 fixes -torpassword values containing a backslash, which aren't escaped in AUTHENTICATE. It adds a small HASHEDPASSWORD mock to feature_torcontrol.py that overlaps with yours. Would you like to add an escaping case (e.g. pa\ss"word\) here, and I'll drop the test from #36374 and keep just the fix? Or I can rebase onto this once it's merged.

@winterrdog

winterrdog commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@0xShadowX

that is a valid catch you got going on in #36374. backslashes are not escaped on present master. i verified that this is also required by the tor control spec:

In QuotedStrings, backslashes and quotes must be escaped; other characters need not be escaped.

from https://spec.torproject.org/control-spec/message-format.html#description-format.


anyway, i see 2 ways we can handle this:

  1. wait for this PR to be merged into mainline, then rebase torcontrol: escape backslashes in -torpassword #36374 on top of it

  2. combine both PRs into one, with three commits:

for the second & third commit, i can add you as a co-author. so #36374 effectively becomes part of this PR's shadow. essentially, it comes down to whether we want to keep the PRs separate and rebase later, or combine them now


my take: option 1

what do you propose ?

@winterrdog
winterrdog force-pushed the test/torcontrol-hashedpassword-auth branch from b4f0141 to 35683fc Compare October 3, 2026 14:41
@winterrdog

winterrdog commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

pushed changes:


P.S: looks like the Ralph bot edited the original message. so, you might have to look for the edit just below the latest edition it made on the comment

@0xShadowX

Copy link
Copy Markdown

Thanks for checking it against the spec. Option 1 sounds good to me, keeping them separate makes each one easier to review.

One small adjustment: since #36374 carries its own minimal mock test, it doesn't strictly need to wait for this one. Whichever gets merged first, I'll rebase the other on top and fold the overlapping mock code into yours, so the escaping case ends up as a test in your HASHEDPASSWORD coverage. Does that work for you?

@winterrdog

winterrdog commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll rebase the other on top and fold the overlapping mock code into yours, so the escaping case ends up as a test in your HASHEDPASSWORD coverage. Does that work for you?

alright!

then, your PR becomes the C++ fix plus one subcase function called test_hashedpassword_escaping. currently, the hashedpassword mock tor server checks if command != f'AUTHENTICATE "{test_password}"' by default, so i would have to pass in 'AUTHENTICATE "pa\\\\ss\\"word\\\\"' as expected_auth's argument in HashedPasswordServer's constructor and then your subcase can just become this:

diff --git a/test/functional/feature_torcontrol.py b/test/functional/feature_torcontrol.py
index d404467f4e..0bb6047c0c 100755
--- a/test/functional/feature_torcontrol.py
+++ b/test/functional/feature_torcontrol.py
@@ -367,6 +367,24 @@ class TorControlTest(BitcoinTestFramework):

         mock_tor.stop()

+    def test_hashedpassword_escaping(self):
+        self.log.info("Test that -torpassword is sent as a correctly escaped quoted string")
+
+        expected_auth = 'AUTHENTICATE "pa\\\\ss\\"word\\\\"'
+        mock_tor = HashedPasswordServer(self.next_port(), expected_auth)
+        mock_tor.start()
+        self.restart_node(0, extra_args=[
+            f"-torcontrol=127.0.0.1:{mock_tor.port}",
+            "-listenonion=1",
+            '-torpassword=pa\\ss"word\\',
+        ])
+        self.wait_until(lambda: len(mock_tor.received_commands) >= 2, timeout=10)
+
+        # Backslashes and quotes must both be escaped, otherwise Tor decodes a different password
+        assert_equal(mock_tor.received_commands[1], expected_auth)
+
+        mock_tor.stop()
+
     def run_test(self):
         self.test_basic()
         self.test_partial_data()
@@ -376,6 +394,7 @@ class TorControlTest(BitcoinTestFramework):
         self.test_reconnect_backoff()
         self.test_hashedpassword_auth_success()
         self.test_hashedpassword_auth_wrong_password()
+        self.test_hashedpassword_escaping()
         self.test_hashedpassword_auth_no_password()
         self.test_hashedpassword_auth_unsupported()

but, in the event that yours goes first, i will rebase, fold your mock into mine, and move your escaping test into test_hashedpassword_escaping as a subcase. whichever way it goes, i am fine with it

@winterrdog
winterrdog force-pushed the test/torcontrol-hashedpassword-auth branch from 35683fc to 9be411d Compare October 3, 2026 17:12
@winterrdog

Copy link
Copy Markdown
Contributor Author

so i would have to make that configurable (by adding a member like self.expected_auth in HashedPasswordServer, defaulting to the test_password)

done in the latest push

@winterrdog

Copy link
Copy Markdown
Contributor Author

@0xShadowX

after some thought, i think your PR can go in first. it is lighter and straightforward to review than this one

in #35292 (comment)

but, in the event that yours goes first, i will rebase, fold your mock into mine, and move your escaping test into test_hashedpassword_auth as a subcase.

with the PR's current state, i think making this change in order to incorporate your new test case into this PR is trivial to do

@winterrdog

Copy link
Copy Markdown
Contributor Author

pushed changes:

  • split test_hashedpassword_auth into 4 per-scenario tests (_success, _wrong_password, _no_password, _unsupported) for easier review
  • moved HashedPasswordServer to module level and the password into a TEST_PASSWORD constant so that all test case functions can share it
  • zero changes to what is tested

@sedited

sedited commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Ping for rebase.

add functional test coverage for `HASHEDPASSWORD` authentication,
including a correctly quoted password, an incorrect password, an unset
`-torpassword`, and a TOR server that does not advertise
`HASHEDPASSWORD`
@winterrdog
winterrdog force-pushed the test/torcontrol-hashedpassword-auth branch from 5ba52ae to 1d277d1 Compare November 6, 2026 19:14
@winterrdog

Copy link
Copy Markdown
Contributor Author

rebased

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants