test: Add coverage for Tor control HASHEDPASSWORD authentication #35292

Open
winterrdog wants to merge 1 commit from test/torcontrol-hashedpassword-auth into master AGit
winterrdog commented 2026-05-14 18:28:10 +00:00 (Migrated from github.com)

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.

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 commented 2026-05-14 18:28:18 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35292 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35292.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35292#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36497](https://github.com/bitcoin/bitcoin/pull/36497) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36497.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
sedited commented 2026-05-14 18:42:54 +00:00 (Migrated from github.com)

@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 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 commented 2026-05-14 18:59:41 +00:00 (Migrated from github.com)

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).

> 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 commented 2026-05-14 19:44:09 +00:00 (Migrated from github.com)

🚧 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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `lint`: https://github.com/bitcoin/bitcoin/actions/runs/25877900203/job/76052468485</sub> <sub>LLM reason (✨ experimental): CI failed due to Python lint errors from `ruff` (F541: extraneous `f`-string prefixes) in `test/functional/feature_torcontrol.py`.</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>
davidgumberg (Migrated from github.com) reviewed 2026-05-14 23:23:40 +00:00
davidgumberg (Migrated from github.com) commented 2026-05-14 23:23:40 +00:00

What is this testing separately from the correct password test?

What is this testing separately from the correct password test?
winterrdog (Migrated from github.com) reviewed 2026-05-15 20:28:47 +00:00
winterrdog (Migrated from github.com) commented 2026-05-15 20:28:47 +00:00

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 ?

this log check was an extra step i added to fully confirm that the error was actually noticed and [reported](https://github.com/bitcoin/bitcoin/blob/ddb94fd3e1061c465e425629d6c7d4b292301da5/src/torcontrol.cpp#L578) 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: ```md 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): ```md 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 ?
DrahtBot commented 2026-09-16 11:27:17 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
0xShadowX commented 2026-09-28 20:16:56 +00:00 (Migrated from github.com)

#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.

#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.
First-time contributor

🔵 Suggestion · New HASHEDPASSWORD cases miss embedded-quote escaping

Removing the existing quote-escaping step would leave all four new tests' assertions satisfied because none supplies a password containing a quote.

Location: test/functional/feature_torcontrol.py:115 (head).

Review coverage was incomplete.


Reviewed: 1d277d1c2316 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:df0443dc5cff1224f58cf2f0af2a814b52ac77c4fa18c64584a637f43b36d781:adfdaf6241fe97209d62717f4b34912a3f3c05551d5eb04e28ec078dd3f7e4f2 --> <!-- ralph-base:5bf3bc5e263407a252c14a17afe04fc3bf24a889 --> <!-- ralph-head:1d277d1c2316969c394d1502cbd33045901e2190 --> <!-- ralph-case:0babfdc9de70f6904af488a3e41813d9c4e67ba2aa0a87c29b82fd5ac3f39805 --> **🔵 Suggestion · New HASHEDPASSWORD cases miss embedded-quote escaping** Removing the existing quote-escaping step would leave all four new tests' assertions satisfied because none supplies a password containing a quote. Location: [`test/functional/feature_torcontrol.py:115`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/1d277d1c2316969c394d1502cbd33045901e2190/test/functional/feature_torcontrol.py#L115>) (head). Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`1d277d1c2316`](<https://git.fish.foo/bitcoin/bitcoin/commit/1d277d1c2316969c394d1502cbd33045901e2190>) · [Full review report](<https://review.fish.foo/traces/35292-1d277d1c2316969c394d1502cbd33045901e2190-2333-7.html>) · [Previous reports](<https://review.fish.foo/traces/35292/>) [permalink](<https://review.fish.foo/traces/35292-1d277d1c2316969c394d1502cbd33045901e2190-2333-7.html>)
winterrdog commented 2026-10-03 13:05:13 +00:00 (Migrated from github.com)

@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 #36374 on top of it

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

    • all tests that came before #36374
    • a test verifying the backslash fix
    • the backslash fix in src/torcontrol.cpp

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 ?

@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 #36374 on top of it 2. combine both PRs into one, with three commits: - all tests that came before #36374 - a test verifying the backslash fix - the backslash fix in `src/torcontrol.cpp` 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 commented 2026-10-03 14:42:34 +00:00 (Migrated from github.com)

pushed changes:

  • took Ralph's suggestion here: #35292 (comment) -- now, we wait for the mock's PROTOCOLINFO/AUTHENTICATE exchange inside the assert_debug_log context and give it a 10s timeout so that the "Authentication failed" check no longer races the 515 response (less likely to trigger in practice, but cheap to make robust)

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

pushed changes: - took Ralph's suggestion here: https://git.fish.foo/bitcoin/bitcoin/pulls/35292#issuecomment-607942 -- now, we wait for the mock's `PROTOCOLINFO`/`AUTHENTICATE` exchange inside the `assert_debug_log` context and give it a 10s timeout so that the `"Authentication failed"` check no longer races the `515` response (less likely to trigger in practice, but cheap to make robust) --- 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 commented 2026-10-03 14:56:00 +00:00 (Migrated from github.com)

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?

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 commented 2026-10-03 15:50:37 +00:00 (Migrated from github.com)

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

> 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 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 commented 2026-10-03 17:12:46 +00:00 (Migrated from github.com)

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

> 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 commented 2026-10-03 17:51:48 +00:00 (Migrated from github.com)

@0xShadowX

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

in https://github.com/bitcoin/bitcoin/pull/35292#issuecomment-5970759968

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

@0xShadowX after some thought, i think your PR can go in first. it is lighter and straightforward to review than this one in https://github.com/bitcoin/bitcoin/pull/35292#issuecomment-5970759968 > 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 commented 2026-10-05 14:22:42 +00:00 (Migrated from github.com)

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
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 commented 2026-11-05 11:00:14 +00:00 (Migrated from github.com)

Ping for rebase.

Ping for rebase.
DrahtBot commented 2026-11-05 12:19:21 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
winterrdog commented 2026-11-06 19:56:28 +00:00 (Migrated from github.com)

rebased

rebased
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35292
No description provided.